I wouldnt worry about dealing with nodes. I would use something like:

  wikicode = mwparserfromhell.parse(page_text)
  templates = wikicode.filter_templates(recursive=True)
  for template in templates:
    if re.search(ur'mytemplate',unicode(template.name),re.I):
      template_text = unicode(template)
      template_text+="<!-- comment -->"
      if re.search(re.escape(template_text),page_text,re.I):
        Do whatever you want to do


On Sun, Jan 1, 2023 at 10:19 AM Roy Smith <roy@panix.com> wrote:
Let's say I've got the following wikitext:

Some stuff
More stuff
{{MyTemplate}}<!-- comment -->
Blah
Blah

I want to find {{MyTemplate}} but only if it's immediately followed by <!-- comment -->, and insert something after the comment.  Is there anything that does that kind of pattern matching?  I can use mwparserfromhell to find the template:

for node in wikicode.nodes:
if isinstance(node, mwp.nodes.Template) and node.name.matches("MyTemplate"):

but I don't see any way to match on two consecutive nodes without building my own little state machine in the loop to keep track of what the previous node was.

_______________________________________________
pywikibot mailing list -- pywikibot@lists.wikimedia.org
To unsubscribe send an email to pywikibot-leave@lists.wikimedia.org