-#!/usr/bin/python3.4
-# coding=utf-8
-# $Id$
-# $HeadURL$
"""For parsing MediaWiki text, we rely on the package mwparserfromhell (https://github.com/earwig/mwparserfromhell).
This module just contains a few additional useful functions.
* mwlib http://code.pediapress.com/wiki/wiki
* https://www.mediawiki.org/wiki/Alternative_parsers
"""
+from typing import Optional, Dict, List
-class ParseError(RuntimeError):
- """Exception used by some of the functions"""
- pass
+from mwparserfromhell.nodes import Template
-def format_template_table(template, keylen=None):
- """Reformat the given template to be tabular.
+def create_template(name: str, args: List[str], kwargs: Optional[Dict[str, str]] = None) -> Template:
+ """Creates a mwparserfromhell template with from a dictionary (string: string)
+
+ :param name: Name of the template
+ :param args: list of unnamed parameters
+ :param kwargs: named parameters
+ """
+ template = Template(name)
+ for i, value in enumerate(args, 1):
+ template.add(str(i), value, False)
+ if kwargs is not None:
+ for key, value in kwargs.items():
+ template.add(key, value, True)
+ return template
+
+
+def format_template_table(template: Template, keylen: Optional[int] = None):
+ """Reformat the given template to be tabular. The template is modified in-place
>>> template
{{foo|bar|bazz=7}}
| bazz = 7
}}
+ :param template: MediaWiki template to be formatted
:param keylen: length of the keys or None for automatic determination
"""
if keylen is None:
shown_keys = [len(param.name.strip()) for param in template.params if param.showkey]
keylen = max(shown_keys) if shown_keys else 0
- template.name = '{}\n'.format(template.name.strip())
+ template.name = f'{template.name.strip()}\n'
for param in template.params:
if param.showkey:
param.name = ' {{:{}}} '.format(keylen).format(param.name.strip())
value = param.value.strip()
if len(value) > 0:
- param.value = ' {}\n'.format(value)
+ param.value = f' {value}\n'
else:
param.value = '\n'
+
+
+def format_template_oneline(template: Template):
+ """Formats a template like this: {{template_name|param| }}
+ (whitespace is stripped and empty parameters are replaced with one space).
+ The template is modified in-place."""
+ template.name = template.name.strip()
+ for param in template.params:
+ if param.showkey:
+ param.name = param.name.strip()
+ value = param.value.strip()
+ if value == '':
+ value = ' '
+ param.value = value
+
+
+def dbkey_to_title(value: str) -> str:
+ """Converts a article database key to a article title. Private function secureAndSplit() of the Title class
+ on line 3316 of includes/Title.php says:
+ $this->mTextform = str_replace( '_', ' ', $this->mDbkeyform );
+ No check for None because a missing title is an error."""
+ return value.replace('_', ' ')