├── .gitignore ├── .travis.yml ├── COPYING ├── MANIFEST.in ├── README.rst ├── TODO.rst ├── article.htm ├── article.txt ├── config.py ├── mediawiki.pijnu ├── mediawiki_parser ├── __init__.py ├── apostrophes.py ├── constants.py ├── html.py ├── preprocessor.py ├── raw.py └── text.py ├── parserExample.py ├── parsers.rst ├── performance_test ├── __init__.py ├── bigfile.html ├── bigfile.py └── bigfile.wiki ├── preprocessor.pijnu ├── setup.py ├── tests ├── __init__.py ├── test_comments.py ├── test_html_postprocessor.py ├── test_links.py ├── test_lists.py ├── test_nowiki.py ├── test_paragraphs.py ├── test_preformatted_paragraphs.py ├── test_rules.py ├── test_special_chars.py ├── test_tables.py ├── test_tags.py ├── test_templates.py ├── test_text_postprocessor.py └── test_titles.py └── wikitext.txt /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | testit.py 3 | build 4 | mediawiki_parser/preprocessorParser.py 5 | mediawiki_parser/wikitextParser.py 6 | performance_test/results -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: xenial 2 | language: python 3 | python: 4 | - "2.7" 5 | - "3.4" 6 | - "3.5" 7 | - "3.6" 8 | - "3.7" 9 | - "3.8" 10 | # command to install dependencies 11 | install: 12 | - pip install pijnu 13 | - python setup.py build_parsers 14 | # command to run tests 15 | script: nosetests tests 16 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.rst 2 | include mediawiki.pijnu 3 | include preprocessor.pijnu -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | .. image:: https://travis-ci.org/peter17/mediawiki-parser.svg?branch=master 2 | :alt: Build Status 3 | :target: https://travis-ci.org/peter17/mediawiki-parser 4 | 5 | Presentation 6 | ============ 7 | 8 | This 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. 9 | 10 | It is an original work by Peter Potrowl and his mentor Erik Rose achieved during the Google Summer of Code 2011. 11 | 12 | 13 | Requirements 14 | ============ 15 | 16 | This parser relies on Pijnu. You must install the latest version of Pijnu, available at: https://github.com/peter17/pijnu 17 | 18 | Do not use the version from http://spir.wikidot.com, which is outdated. 19 | 20 | For basic and simple installation, just try: 21 | 22 | :: 23 | 24 | pip install mediawiki-parser 25 | 26 | How it works 27 | ============ 28 | 29 | Two 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. 30 | 31 | Then, specific Python functions will render the leaves of the AST into the wanted format. 32 | 33 | 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. 34 | 35 | Building the parsers 36 | ==================== 37 | 38 | The preprocessor and mediwiki parsers must be built from the Pijnu 39 | grammars before you can use mediawiki-parser. You can build them through 40 | setup.py, possibly setting the PYTHONPATH to point at pijnu: 41 | 42 | :: 43 | 44 | cd /PATH/TO/mediawiki-parser/ 45 | env PYTHONPATH=/PATH/TO/pijnu python setup.py build_parsers 46 | 47 | How to test 48 | =========== 49 | 50 | The current simplest way to test the tool is to put wikitext inside the wikitext.txt file. Then, run: 51 | 52 | :: 53 | 54 | python parser.py 55 | 56 | and the wikitext will be rendered as HTML in the article.htm file. 57 | 58 | Other ways might be implemented in the future. 59 | 60 | Unit tests 61 | ---------- 62 | 63 | Install nose and run: 64 | 65 | :: 66 | 67 | cd /PATH/TO/mediawiki-parser/ 68 | env PYTHONPATH=/PATH/TO/pijnu/ nosetests tests 69 | 70 | How to use in a program 71 | ======================= 72 | 73 | Example for HTML 74 | ---------------- 75 | In order to use this tool to render wikitext into HTML in a Python program, you can use the following lines: 76 | 77 | :: 78 | 79 | templates = {} 80 | allowed_tags = [] 81 | allowed_self_closing_tags = [] 82 | allowed_attributes = [] 83 | interwiki = {} 84 | namespaces = {} 85 | 86 | from mediawiki_parser.preprocessor import make_parser 87 | preprocessor = make_parser(templates) 88 | 89 | from mediawiki_parser.html import make_parser 90 | parser = make_parser(allowed_tags, allowed_self_closing_tags, allowed_attributes, interwiki, namespaces) 91 | 92 | preprocessed_text = preprocessor.parse(source) 93 | output = parser.parse(preprocessed_text.leaves()) 94 | 95 | The `output` string will contain the rendered HTML. You should describe the behavior you expect by filling the variables of the first lines: 96 | * if the wikitext calls foreign templates, put their names and content in the `templates` dict (e.g.: `{'my template': 'my template content'}`) 97 | * if some HTML tags are allowed on your wiki, list them in the `allowed_tags` list (e.g.: `['center', 'big', 'small', 'span']`; avoid `'script'` and some others, for security reasons) 98 | * if some self-closing HTML tags are allowed on your wiki, list them in the `allowed_self_closing_tags` list (e.g.: `['br', 'hr']`; avoid `'script'` and some others, for security reasons) 99 | * if some HTML tags are allowed on your wiki, list the attributes they can use the `allowed_attributes` list (e.g.: `['style', 'class']`; avoid `'onclick'` and some others, for security reasons) 100 | * if you want to be able to use interwiki links, list the foreign wikis in the `interwiki` dict (e.g.: `{'fr': 'http://fr.wikipedia.org/wiki/'}`) 101 | * if you want to be able to distinguish between standard links, file inclusions or categories, list the namespaces of your wiki in the `namespaces` dict (e.g.: `{'Template': 10, 'Category': 14, 'File': 6}` where the numbers are the namespace codes used in MW) 102 | 103 | Example for text 104 | ---------------- 105 | In order to use this tool to render wikitext into text in a Python program, you can use the following lines: 106 | 107 | :: 108 | 109 | templates = {} 110 | 111 | from mediawiki_parser.preprocessor import make_parser 112 | preprocessor = make_parser(templates) 113 | 114 | from mediawiki_parser.text import make_parser 115 | parser = make_parser() 116 | 117 | preprocessed_text = preprocessor.parse(source) 118 | output = parser.parse(preprocessed_text.leaves()) 119 | 120 | The `output` string will contain the rendered text. 121 | If the wikitext calls foreign templates, put their names and content in the `templates` dict (e.g.: `{'my template': 'my template content'}`) 122 | 123 | Example for templates substitution 124 | ---------------------------------- 125 | If you just want to replace the templates in a given wikitext, you can just call the preprocessor and no rendering postprocessor: 126 | 127 | :: 128 | 129 | templates = {} 130 | 131 | from mediawiki_parser.preprocessor import make_parser 132 | preprocessor = make_parser(templates) 133 | 134 | output = preprocessor.parse(source) 135 | 136 | The `output` string will contain the rendered wikitext. 137 | Put the templates names and content in the `templates` dict (e.g.: `{'my template': 'my template content'}`) 138 | 139 | Postprocessors 140 | -------------- 141 | 142 | The parser produces an AST. In order to provide human readable output, three postprocessors are provided: 143 | * html.py, for HTML output 144 | * text.py, for text output 145 | * raw.py, for raw output 146 | 147 | For now, we mainly focused on HTML postprocessor. The text output might not be as cleaned as expected. 148 | 149 | You can adapt them according to your needs. 150 | 151 | Known bugs 152 | ========== 153 | 154 | This tool should be able to render any wikitext page into text or HTML. 155 | 156 | However, it does not intent to be bug-for-bug compatible with MW. For instance, using HTML entities in template calls (e.g.: `'{{temp©late}}`') is currently not supported. 157 | 158 | Please don't hesitate to report bugs that you may find when using this tool. 159 | 160 | Special thanks 161 | ============== 162 | * To Nicholas Burlett for his directory restructure, performance improvements and other fixes 163 | -------------------------------------------------------------------------------- /TODO.rst: -------------------------------------------------------------------------------- 1 | Known bugs 2 | ========== 3 | 4 | * ,
, etc. are not correcly recognized; we should implement a case-insensitive mode
 5 | * IPv6 addresses are not correcly parsed
 6 | * "=== test 
essai1
test ===" is not correcly parsed (correct result: "

test

essai1

test

") 7 | * "=== test\ntest ===" is not correcly parsed (correct result: "

=== test test ===

") 8 | 9 | 10 | =================================================================== ============== ======================================================= 11 | Syntax type (example) Implementation Remark 12 | =================================================================== ============== ======================================================= 13 | Titles, levels 1 to 6 (==Title==) ¤¤¤ 14 | Paragraphs (correctly combine inline text) ¤¤¤ 15 | Bold and italic (, , "''", "'''") ¤¤¤ 16 | Internal links ([[Namespace:Title#section|text]]) ¤¤¤ This will include files, categories and interwiki links 17 | External links ([http://www.mozilla.org title]) ¤¤¤ 18 | Inline URLs ¤¤¤ 19 | Safe HTML tags (, , ...) ¤¤¤ 20 | HTML entities (invalid &xxxx; -> &xxxx; "<" -> <) ¤¤¤ 21 | Unsafe HTML tags (