### Install Latest Development Version of MWParserFromHell Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/index.md Clone the repository, navigate to the directory, sync dependencies with uv, and then run a Python command to check the installed version. This method is for obtaining the latest development build. ```bash git clone https://github.com/earwig/mwparserfromhell.git cd mwparserfromhell uv sync uv run python -c 'import mwparserfromhell; print(mwparserfromhell.__version__)' ``` -------------------------------- ### Parse MediaWiki Page with Pywikibot Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/integration.md Use this snippet when integrating with Pywikibot to fetch and parse a MediaWiki page. Ensure Pywikibot is installed. ```python import mwparserfromhell import pywikibot def parse(title): site = pywikibot.Site() page = pywikibot.Page(site, title) text = page.get() return mwparserfromhell.parse(text) ``` -------------------------------- ### Extract and inspect templates Source: https://github.com/earwig/mwparserfromhell/blob/main/README.rst Parse wikicode and use the filter_templates() method to get a list of all templates. Access template names, parameters, and specific parameter values by index or name. ```python text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" wikicode = mwparserfromhell.parse(text) templates = wikicode.filter_templates() template = templates[0] print(template.name) print(template.params) print(template.get(1).value) print(template.get("eggs").value) ``` -------------------------------- ### Explore Templates Manually (Recursive=False) Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/usage.md Use `filter_templates(recursive=False)` to get only top-level templates and then manually explore nested `Wikicode` objects. This is useful for fine-grained control over template parsing. ```python code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}") print(code.filter_templates(recursive=False)) foo = code.filter_templates(recursive=False)[0] print(foo.get(1).value) print(foo.get(1).value.filter_templates()[0]) print(foo.get(1).value.filter_templates()[0].get(1).value) ``` -------------------------------- ### Run MWParserFromHell Tests with uv Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/index.md Execute the comprehensive test suite using pytest. If using uv, include the --reinstall-package flag to ensure proper testing of updates to the extension module. ```bash uv run --reinstall-package mwparserfromhell pytest ``` -------------------------------- ### Parse MediaWiki Page with Requests and API Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/integration.md This snippet shows how to fetch MediaWiki page content directly from the API using the requests library and then parse it with mwparserfromhell. Requires the requests library and a User-Agent header. ```python import mwparserfromhell import requests API_URL = "https://en.wikipedia.org/w/api.php" def parse(title): params = { "action": "query", "prop": "revisions", "rvprop": "content", "rvslots": "main", "rvlimit": 1, "titles": title, "format": "json", "formatversion": "2", } headers = {"User-Agent": "My-Bot-Name/1.0"} req = requests.get(API_URL, headers=headers, params=params) res = req.json() revision = res["query"]["pages"][0]["revisions"][0] text = revision["slots"]["main"]["content"] return mwparserfromhell.parse(text) ``` -------------------------------- ### Parse Wikitext Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/usage.md Import the library and parse a string of wikitext into a Wikicode object. ```python import mwparserfromhell wikicode = mwparserfromhell.parse(text) ``` -------------------------------- ### Modify templates: add, replace, and match names Source: https://github.com/earwig/mwparserfromhell/blob/main/README.rst Wikicode objects can be treated like lists for template modification. Use add(), replace(), and matches() to manage templates and their parameters. The matches() method handles case-insensitivity and whitespace. ```python text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" code = mwparserfromhell.parse(text) for template in code.filter_templates(): if template.name.matches("Cleanup") and not template.has("date"): template.add("date", "July 2012") code.replace("{{uncategorized}}", "{{bar-stub}}") print(code) print(code.filter_templates()) ``` -------------------------------- ### Modify Templates: Add, Replace, and Filter Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/usage.md Shows how to modify wikicode by adding parameters to templates using `add()`, replacing entire template nodes using `replace()`, and re-filtering to see the changes. The `matches()` method is useful for case-insensitive and whitespace-insensitive template name comparisons. ```python text = "{{cleanup}} '''Foo''' is a [[bar]]. {{uncategorized}}" code = mwparserfromhell.parse(text) for template in code.filter_templates(): if template.name.matches("Cleanup") and not template.has("date"): template.add("date", "July 2012") print(code) code.replace("{{uncategorized}}", "{{bar-stub}}") print(code) print(code.filter_templates()) ``` -------------------------------- ### Parse Wiki Text with Style Tag Skipping Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/limitations.md Use the `skip_style_tags=True` argument in `mwparserfromhell.parse()` to treat wiki-style emphasis markers (like `''` and `'''`) as plain text. This is useful when style formatting is not of interest. ```python import mwparserfromhell wikitext = "''Hello'' world" parsed = mwparserfromhell.parse(wikitext, skip_style_tags=True) print(parsed) ``` -------------------------------- ### Convert Wikicode back to String Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/usage.md Convert a modified `Wikicode` object back into a regular string using `str()`. This is the final step before saving changes to a page. ```python text = str(code) print(text) text == code ``` -------------------------------- ### Extract and Inspect Templates Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/usage.md Parse wikitext, filter for templates, and access their names and parameters. The `filter_templates()` method returns a list of all templates found in the wikicode. ```python text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" wikicode = mwparserfromhell.parse(text) print(wikicode) templates = wikicode.filter_templates() print(templates) template = templates[0] print(template.name) print(template.params) print(template.get(1).value) print(template.get("eggs").value) ``` -------------------------------- ### Extract Nested Templates Source: https://github.com/earwig/mwparserfromhell/blob/main/docs/usage.md Demonstrates how `filter_templates()` recursively finds all templates, including those nested within other templates. ```python text = "{{foo|{{bar}}={{baz|{{spam}}᱔}}" mwparserfromhell.parse(text).filter_templates() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.