]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/blobdiff - wrpylib/mwmarkup.py
Add webcam to Buttonleiste.
[philipp/winterrodeln/wrpylib.git] / wrpylib / mwmarkup.py
index 2eb89c0d709a6223d98a12dc2321c7f60474360e..c14e96256e924ed5855a3a38a47ce40241207e92 100644 (file)
@@ -1,7 +1,3 @@
-#!/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.
 
@@ -10,14 +6,29 @@ Other Python MediaWiki parsers:
 * 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}}
@@ -27,17 +38,40 @@ def format_template_table(template, keylen=None):
     | 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('_', ' ')