mwparserfromhell
mwparserfromhell copied to clipboard
Suggestion: Offer a setting to offset MW param numbers to be 0-indexed
A source of constant mild annoyance is that Python's 0-indexing disagrees with MW params' 1-indexing - I know you mentioned not wanting to add settings to parse, but what about the possibility to treat params as being 0-indexed, and internally doing all of the +1 translations as needed? So e.g. if I did for i in range(n): template.add(i, template_data[i])
then it would add the correct parameters in order, and if the template had |3=cat
and i asked for template.get(2).value
it would give me cat
, etc.
What about a case where template params are reordered, like {{foo|3=a|2=b|1=c}}
? Would you expect template.get(2).value
to yield a
?
When you work with sys.argv, the program arguments are actually 1-indexed (sys.argv[0]
is reserved for the program name). Is it annoying? Maybe, but much more important is Python's consistence with the behaviour of other low-level languages and operating systems. Likewise, mwparserfromhell is consistent with MediaWiki itself.
Anyway, there are Python tools which provide switching between 0- and 1-based indexing of certain things in their API. For example jinja2 has loop.index
and loop.index0
etc.
What about a case where template params are reordered, like {{foo|3=a|2=b|1=c}}? Would you expect template.get(2).value to yield a?
Yes. Though perhaps with a warning?
Likewise, mwparserfromhell is consistent with MediaWiki itself. Well, part of the reason this came up is that there's discussions of adding JS support to Scribunto, and I've always been opposed to that because I think the convenience of Lua's 1-indexing matching MediaWiki's is extremely important. Everyone I've mentioned this to has immediately said "well just reindex params when you access them inside a Scribunto script" - in this case I think you run into documentation confusion where what developers are writing isn't the same as what users will be using, but I do think it would be the best solution.
I haven't tried using any tools to help with this (mostly didn't think to look to see if any existed) but I still think this option would be a very convenient way to avoid adding a lot of extra complexity.