Skip to content

Instantly share code, notes, and snippets.

@chtnnh
Forked from halfak/example.py
Last active March 12, 2020 14:03
Show Gist options
  • Save chtnnh/601437d7ff324b5c7484c1c5ba9266b5 to your computer and use it in GitHub Desktop.
Save chtnnh/601437d7ff324b5c7484c1c5ba9266b5 to your computer and use it in GitHub Desktop.
import mwparsefromhell
example = """
{{foo bar baz}}
{{I am a random template}}
{{Marca de projeto|3|Biografias|4|Políticos|4|Brasil|3|WP Offline|2|bot=4/20111127|rev=20170714}}"""
templates = list(mwparserfromhell.parse(example_text).filter_templates())
def from_template(template):
"Returns an iterator of projects and classes from a template if applicable"
WP_PREFIX = re.compile("\{\{marca de projeto\|[0-9*]\|([a-z]*\|[0-9*])+\}\}", re.I)
return wP_PREFIX.sub('', template.name.lower().replace("_", " ")).strip()
for template in templates:
print(list(from_template(template)))
[]
[]
[("Biographias", "4"), ("Políticos", "4"), ("Brasil", "3"), ("WP Offline", "2")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment