MWParserFromHell is a parser for MediaWiki wikicode.
Project description
mwparserfromhell (the MediaWiki Parser from Hell) is a Python package that provides an easy-to-use and outrageously powerful parser for MediaWiki wikicode. It supports Python 3.5+.
Developed by Earwig with contributions from Σ, Legoktm, and others. Full documentation is available on ReadTheDocs. Development occurs on GitHub.
Installation
The easiest way to install the parser is through the Python Package Index; you can install the latest release with pip install mwparserfromhell (get pip). Make sure your pip is up-to-date first, especially on Windows.
Alternatively, get the latest development version:
git clone https://github.com/earwig/mwparserfromhell.git cd mwparserfromhell python setup.py install
You can run the comprehensive unit testing suite with python -m unittest discover.
Usage
Normal usage is rather straightforward (where text is page text):
>>> import mwparserfromhell >>> wikicode = mwparserfromhell.parse(text)
wikicode is a mwparserfromhell.Wikicode object, which acts like an ordinary str object with some extra methods. For example:
>>> text = "I has a template! {{foo|bar|baz|eggs=spam}} See it?" >>> wikicode = mwparserfromhell.parse(text) >>> print(wikicode) I has a template! {{foo|bar|baz|eggs=spam}} See it? >>> templates = wikicode.filter_templates() >>> print(templates) ['{{foo|bar|baz|eggs=spam}}'] >>> template = templates[0] >>> print(template.name) foo >>> print(template.params) ['bar', 'baz', 'eggs=spam'] >>> print(template.get(1).value) bar >>> print(template.get("eggs").value) spam
Since nodes can contain other nodes, getting nested templates is trivial:
>>> text = "{{foo|{{bar}}={{baz|{{spam}}}}}}" >>> mwparserfromhell.parse(text).filter_templates() ['{{foo|{{bar}}={{baz|{{spam}}}}}}', '{{bar}}', '{{baz|{{spam}}}}', '{{spam}}']
You can also pass recursive=False to filter_templates() and explore templates manually. This is possible because nodes can contain additional Wikicode objects:
>>> code = mwparserfromhell.parse("{{foo|this {{includes a|template}}}}") >>> print(code.filter_templates(recursive=False)) ['{{foo|this {{includes a|template}}}}'] >>> foo = code.filter_templates(recursive=False)[0] >>> print(foo.get(1).value) this {{includes a|template}} >>> print(foo.get(1).value.filter_templates()[0]) {{includes a|template}} >>> print(foo.get(1).value.filter_templates()[0].get(1).value) template
Templates can be easily modified to add, remove, or alter params. Wikicode objects can be treated like lists, with append(), insert(), remove(), replace(), and more. They also have a matches() method for comparing page or template names, which takes care of capitalization and whitespace:
>>> 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) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{uncategorized}} >>> code.replace("{{uncategorized}}", "{{bar-stub}}") >>> print(code) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} >>> print(code.filter_templates()) ['{{cleanup|date=July 2012}}', '{{bar-stub}}']
You can then convert code back into a regular str object (for saving the page!) by calling str() on it:
>>> text = str(code) >>> print(text) {{cleanup|date=July 2012}} '''Foo''' is a [[bar]]. {{bar-stub}} >>> text == code True
Limitations
While the MediaWiki parser generates HTML and has access to the contents of templates, among other things, mwparserfromhell acts as a direct interface to the source code only. This has several implications:
Syntax elements produced by a template transclusion cannot be detected. For example, imagine a hypothetical page "Template:End-bold" that contained the text </b>. While MediaWiki would correctly understand that <b>foobar{{end-bold}} translates to <b>foobar</b>, mwparserfromhell has no way of examining the contents of {{end-bold}}. Instead, it would treat the bold tag as unfinished, possibly extending further down the page.
Templates adjacent to external links, as in http://example.com{{foo}}, are considered part of the link. In reality, this would depend on the contents of the template.
When different syntax elements cross over each other, as in {{echo|''Hello}}, world!'', the parser gets confused because this cannot be represented by an ordinary syntax tree. Instead, the parser will treat the first syntax construct as plain text. In this case, only the italic tag would be properly parsed.
Workaround: Since this commonly occurs with text formatting and text formatting is often not of interest to users, you may pass skip_style_tags=True to mwparserfromhell.parse(). This treats '' and ''' as plain text.
A future version of mwparserfromhell may include multiple parsing modes to get around this restriction more sensibly.
Additionally, the parser lacks awareness of certain wiki-specific settings:
Word-ending links are not supported, since the linktrail rules are language-specific.
Localized namespace names aren’t recognized, so file links (such as [[File:...]]) are treated as regular wikilinks.
Anything that looks like an XML tag is treated as a tag, even if it is not a recognized tag name, since the list of valid tags depends on loaded MediaWiki extensions.
Integration
mwparserfromhell is used by and originally developed for EarwigBot; Page objects have a parse method that essentially calls mwparserfromhell.parse() on page.get().
If you’re using Pywikibot, your code might look like this:
import mwparserfromhell
import pywikibot
def parse(title):
site = pywikibot.Site()
page = pywikibot.Page(site, title)
text = page.get()
return mwparserfromhell.parse(text)
If you’re not using a library, you can parse any page with the following Python 3 code (using the API and the requests library):
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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for mwparserfromhell-0.6-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3a761dd6b18f671dc6a5057097d16e8b4c8b556ad8ed58860d18b577690707a |
|
MD5 | c851392744e2517358c0cafb60dec048 |
|
BLAKE2b-256 | 2d41ef01f21ab1dd1f6933c6450d1522fba329960be452fb51fcf8d17ae2d38b |
Hashes for mwparserfromhell-0.6-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3329caa591c9589deda52eae7f5b39760fadd5ee6512b52e99a36e281386f416 |
|
MD5 | 8bfbb10539529c60d5b329d012e4b89d |
|
BLAKE2b-256 | 10b6dbfa382c86ba0fb4525b6495b39c11b4d6534b17523fb009678a89b5a6e2 |
Hashes for mwparserfromhell-0.6-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4fa5d6976b750fade6b076d7972f71e09adcd9408384cb957f4e7ac9d22b46b0 |
|
MD5 | 887c99d376c7f1db5a129f831994e223 |
|
BLAKE2b-256 | 306f2c12fef8d8931847c1d5b82fd087e1f21b26fb878ac69d572cae65d12840 |
Hashes for mwparserfromhell-0.6-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99529d8073df6e1e629b79d184a016b102e2bf94032fe9917022f28b7b8e09dc |
|
MD5 | 2b20358e19e38d407af08bed39e770a5 |
|
BLAKE2b-256 | 7c834e00d02c13e3846a766522bdee6246bd12b06e78d4daf6c23ca842890384 |
Hashes for mwparserfromhell-0.6-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfa0707c5b51dea52ec6723e0d2e44791788de6cecfaed88c377fd163bfa6e19 |
|
MD5 | 0930d9644008bcdc62818ad3a3f85b99 |
|
BLAKE2b-256 | aea5eb8230168de876d36a38c20bb11bce97cc2acf3d91ec6ac2a6cc4231dbda |
Hashes for mwparserfromhell-0.6-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b01a4f75c13eb2d26a2f2b87952e6f63389b3539519249f7de054643683a398a |
|
MD5 | e881aa2d6db7bd9b161284d29c290803 |
|
BLAKE2b-256 | 51654dc57b5e1829a5623d7f850aa1b4f79f0f161ed4f4faa5d2180e88f29d04 |
Hashes for mwparserfromhell-0.6-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50502838077997c4707617f69723e01f9e03f4e231dbaac24ed81962b92e790a |
|
MD5 | a270602daa8d11b5d8b48a91bb4097f2 |
|
BLAKE2b-256 | 8d44553355a2a9bdbb086de59f3a54386c78001193dda615e3aa721e0a4cd22d |
Hashes for mwparserfromhell-0.6-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12b4f75842b86aabc17e87e79e8f5dd289e44cf08f199ad0350851e189661d6d |
|
MD5 | 08b383b46877865e66db3d6532edd5f6 |
|
BLAKE2b-256 | f0299700e7faf4e5d338cc29bb2dfdd25c4594d62b69efc75f8225ce55eb5c91 |
Hashes for mwparserfromhell-0.6-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 418912aa0e7f5073ad20dc192ade81b25c4c9c57cafae7eb7525d612daf63d5e |
|
MD5 | 5fbb99db2faa59f73e2d525a24d73269 |
|
BLAKE2b-256 | 8bf19fffb4f62818afbff70f5cfa38535bf36270c169b707505be907b50c7334 |
Hashes for mwparserfromhell-0.6-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcc271dd9a20894640b8cfb14ff0146d3fe54a27e01cfd48bebd0c9770964d61 |
|
MD5 | 70d65155341fc4e2e83b3fa1ade83b7a |
|
BLAKE2b-256 | 1e14dbdcc24a525f04bac9e2a722266507d9d223589de3340be90bb29e8a3a94 |
Hashes for mwparserfromhell-0.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6908487f663c826a3f73f289719821ec2da94ed28450e727c0b33f0dc4850ca2 |
|
MD5 | 97f376690911469cad57437d8727a2ff |
|
BLAKE2b-256 | 2bd1086f064c37c282e00315a4d1565d0fde41d7d9d81f2c18c95106641cc956 |
Hashes for mwparserfromhell-0.6-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f1a5588a066f171d9d9a7de1dff0b8aeab3e91f65fe9dbea1b4fbf6e84abc39 |
|
MD5 | fbb3e3bfcfd326f116c4488a57cfccd1 |
|
BLAKE2b-256 | f466f5dc43e76d585fd86662ca175c07612629b677ef922d78846327383a2126 |
Hashes for mwparserfromhell-0.6-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00c33680cae89c88a5d5aa5492b016329cb4cb162bfd66e3dad1d6551732b56c |
|
MD5 | 83881b12e03008c4af77eacd844e2e21 |
|
BLAKE2b-256 | 31c1ecbb652c72853f00428966a8cdd17a9c8723b31c2d2822091ffbec471542 |
Hashes for mwparserfromhell-0.6-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb47d8cb09fc6fcd343204d575cb1a82b3664fc5797c8054f1d7286ba96fadd9 |
|
MD5 | 6e32ddc8b16688b8777bc3b055032c3f |
|
BLAKE2b-256 | b365eeb5221d4a967ddea7c3ac9a41922a9642f49cb99faf9731fc4dae2d1ff0 |
Hashes for mwparserfromhell-0.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a73d6c04333bbc4f0f402a509dc7711dd62cacfe487c41a53b044c72e23f723 |
|
MD5 | 3a5ff6c56437a34db9debe40d6ccad32 |
|
BLAKE2b-256 | c60003ccc2676e592f73ce455fd0343eb38d3779878332ba01ef4c0281a7d2a9 |
Hashes for mwparserfromhell-0.6-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40b431318f6b353dee0907d82b06fe270ad96b92c31241cca0cac7612547f0c8 |
|
MD5 | 07a008c3cee269416ab5422c7fc0bd4c |
|
BLAKE2b-256 | bd65b1cae1256e9c32ba2c78717127b78ee114595feb9a775255f91cec82b000 |
Hashes for mwparserfromhell-0.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 720390238e282050cc43999b870aa97167b45367c6ad8314244a0aa553b926f3 |
|
MD5 | 7bf46056a7365f740a873aba8404aee1 |
|
BLAKE2b-256 | 0c8ce0b1ec73f6eb0dfd6e9aedaf0805b523a959efd421a2953db1cc2cb1fa08 |
Hashes for mwparserfromhell-0.6-cp35-cp35m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 116f7a60bf304ae503d0f566f274ff72e51b945587348b761e9fdf0653b7224c |
|
MD5 | 8d40843fd956ec61b6883291e35ebef3 |
|
BLAKE2b-256 | e001e183907fa5ad01c35bce74def66260bbee277f315fd8823e2b224396a2d8 |