]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - wrpylib/mwmarkup.py
Create "Buttonleiste" with key_value_template.
[philipp/winterrodeln/wrpylib.git] / wrpylib / mwmarkup.py
index 41b9b3999cc18aef86333deb5e9812690cd5f014..c14e96256e924ed5855a3a38a47ce40241207e92 100644 (file)
@@ -1,12 +1,77 @@
-#!/usr/bin/python2.6
-# -*- coding: iso-8859-15 -*-
-# $Id$
-# $HeadURL$
-"""This module contains general functions that help parsing the mediawiki markup.
-I looked for an already existing MediaWiki parser in Python but I didn't find anything 
-that convinced me. However, here are the links:
+"""For parsing MediaWiki text, we rely on the package mwparserfromhell (https://github.com/earwig/mwparserfromhell).
+This module just contains a few additional useful functions.
 
+Other Python MediaWiki parsers:
 * py-wikimarkup https://github.com/dcramer/py-wikimarkup
 * mwlib http://code.pediapress.com/wiki/wiki
+* https://www.mediawiki.org/wiki/Alternative_parsers
 """
+from typing import Optional, Dict, List
 
+from mwparserfromhell.nodes import Template
+
+
+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}}
+    >>> format_template_table(template)
+    {{foo
+    | bar
+    | 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 = 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 = 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('_', ' ')