1 """For parsing MediaWiki text, we rely on the package mwparserfromhell (https://github.com/earwig/mwparserfromhell).
2 This module just contains a few additional useful functions.
4 Other Python MediaWiki parsers:
5 * py-wikimarkup https://github.com/dcramer/py-wikimarkup
6 * mwlib http://code.pediapress.com/wiki/wiki
7 * https://www.mediawiki.org/wiki/Alternative_parsers
11 class ParseError(RuntimeError):
12 """Exception used by some of the functions"""
16 def format_template_table(template, keylen=None):
17 """Reformat the given template to be tabular.
21 >>> format_template_table(template)
27 :param keylen: length of the keys or None for automatic determination
30 shown_keys = [len(param.name.strip()) for param in template.params if param.showkey]
31 keylen = max(shown_keys) if shown_keys else 0
32 template.name = '{}\n'.format(template.name.strip())
33 for param in template.params:
35 param.name = ' {{:{}}} '.format(keylen).format(param.name.strip())
36 value = param.value.strip()
38 param.value = ' {}\n'.format(value)
43 def format_template_oneline(template):
44 """Formats a template like this: {{template_name|param| }}
45 (whitespace is stripped and empty parameters are replaced with one space)."""
46 template.name = template.name.strip()
47 for param in template.params:
49 param.name = param.name.strip()
50 value = param.value.strip()
56 def dbkey_to_title(value):
57 """Converts a article database key to a article title. Private function secureAndSplit() of the Title class
58 on line 3316 of includes/Title.php says:
59 $this->mTextform = str_replace( '_', ' ', $this->mDbkeyform );
60 No check for None because a missing title is an error."""
61 return value.replace('_', ' ')