Package: mediawiki-parser
PresentationThis is a parser for MediaWiki’s (MW) syntax. It’s goal is to transform wikitext into an abstract syntax tree (AST) and then render this AST into various formats such as plain text and HTML.It is an original work by Peter Potrowl and his mentor Erik Rose achieved during the Google Summer of Code 2011.RequirementsThis parser relies on Pijnu. You must install the latest version of Pijnu, available at:https://github.com/peter17/pijnuDo not use the version fromhttp://spir.wikidot.com, which is outdated.For basic and simple installation, just try:pip install mediawiki-parserHow it worksTwo files, preprocessor.pijnu and mediawiki.pijnu describe the MW syntax using patterns that form a grammar. Another Python tool called Pijnu will interpret those grammars and use them to match the wikitext content and build the AST.Then, specific Python functions will render the leaves of the AST into the wanted format.The reason why we use two grammars is that we will first substitute the templates in the wikitext with a preprocessor before actually parsing the content of the page.Building the parsersThe preprocessor and mediwiki parsers must be built from the Pijnu
grammars before you can use mediawiki-parser. You can build them through
setup.py, possibly setting the PYTHONPATH to point at pijnu:cd /PATH/TO/mediawiki-parser/
env PYTHONPATH=/PATH/TO/pijnu python setup.py build_parsersHow to testThe current simplest way to test the tool is to put wikitext inside the wikitext.txt file. Then, run:python parser.pyand the wikitext will be rendered as HTML in the article.htm file.Other ways might be implemented in the future.Unit testsInstall nose and run:cd /PATH/TO/mediawiki-parser/
env PYTHONPATH=/PATH/TO/pijnu/ nosetests testsHow to use in a programExample for HTMLIn order to use this tool to render wikitext into HTML in a Python program, you can use the following lines:templates = {}
allowed_tags = []
allowed_self_closing_tags = []
allowed_attributes = []
interwiki = {}
namespaces = {}

from mediawiki_parser.preprocessor import make_parser
preprocessor = make_parser(templates)

from mediawiki_parser.html import make_parser
parser = make_parser(allowed_tags, allowed_self_closing_tags, allowed_attributes, interwiki, namespaces)

preprocessed_text = preprocessor.parse(source)
output = parser.parse(preprocessed_text.leaves())Theoutputstring will contain the rendered HTML. You should describe the behavior you expect by filling the variables of the first lines:if the wikitext calls foreign templates, put their names and content in thetemplatesdict (e.g.:{‘my template’: ‘my template content’})if some HTML tags are allowed on your wiki, list them in theallowed_tagslist (e.g.:[‘center’, ‘big’, ‘small’, ‘span’]; avoid‘script’and some others, for security reasons)if some self-closing HTML tags are allowed on your wiki, list them in theallowed_self_closing_tagslist (e.g.:[‘br’, ‘hr’]; avoid‘script’and some others, for security reasons)if some HTML tags are allowed on your wiki, list the attributes they can use theallowed_attributeslist (e.g.:[‘style’, ‘class’]; avoid‘onclick’and some others, for security reasons)if you want to be able to use interwiki links, list the foreign wikis in theinterwikidict (e.g.:{‘fr’: ‘http://fr.wikipedia.org/wiki/’})if you want to be able to distinguish between standard links, file inclusions or categories, list the namespaces of your wiki in thenamespacesdict (e.g.:{‘Template’: 10, ‘Category’: 14, ‘File’: 6}where the numbers are the namespace codes used in MW)Example for textIn order to use this tool to render wikitext into text in a Python program, you can use the following lines:templates = {}

from mediawiki_parser.preprocessor import make_parser
preprocessor = make_parser(templates)

from mediawiki_parser.text import make_parser
parser = make_parser()

preprocessed_text = preprocessor.parse(source)
output = parser.parse(preprocessed_text.leaves())Theoutputstring will contain the rendered text.
If the wikitext calls foreign templates, put their names and content in thetemplatesdict (e.g.:{‘my template’: ‘my template content’})Example for templates substitutionIf you just want to replace the templates in a given wikitext, you can just call the preprocessor and no rendering postprocessor:templates = {}

from mediawiki_parser.preprocessor import make_parser
preprocessor = make_parser(templates)

output = preprocessor.parse(source)Theoutputstring will contain the rendered wikitext.
Put the templates names and content in thetemplatesdict (e.g.:{‘my template’: ‘my template content’})PostprocessorsThe parser produces an AST. In order to provide human readable output, three postprocessors are provided:html.py, for HTML outputtext.py, for text outputraw.py, for raw outputFor now, we mainly focused on HTML postprocessor. The text output might not be as cleaned as expected.You can adapt them according to your needs.Known bugsThis tool should be able to render any wikitext page into text or HTML.However, it does not intent to be bug-for-bug compatible with MW. For instance, using HTML entities in template calls (e.g.:‘{{temp&copy;late}}’) is currently not supported.Please don’t hesitate to report bugs that you may find when using this tool.Special thanksTo Nicholas Burlett for his directory restructure, performance improvements and other fixes