earwig / mwparserfromhell

A Python parser for MediaWiki wikicode
https://mwparserfromhell.readthedocs.io/
MIT License
741 stars 74 forks source link

template with a leading space before its name on the wiki isn't matched #316

Closed RheingoldRiver closed 6 months ago

RheingoldRiver commented 6 months ago

With the following code:

        for template in wikitext.filter_templates(recursive=self.recursive):
            if template.name.matches(self.template_name):
                # do something

If the wiki code is {{ TemplateName (i.e. there is a leading space between the braces & the text on the wiki) then TemplateName will not match it for this template. Right now I'm thinking the best workaround without side effects is to clone the template, replace the name with its str(name).strip() but I think matches shouldn't fail here.

lahwaacz commented 6 months ago

Works fine for me:

import mwparserfromhell
wikitext = mwparserfromhell.parse("{{ foo | bar }}")
for template in wikitext.filter_templates():
    if template.name.matches("foo"):
        print("found foo")
RheingoldRiver commented 6 months ago

hmmm I'm a bit confused what's going on now because it was definitely not saving until I did a find-replace of all of the {{ i to {{i but I can't repro this now, so maybe there was some other problem. I'll close this, sorry for the trouble!