from typing import Tuple, Optional, List, Callable, Union, TypeVar, Dict, NamedTuple
import mwparserfromhell # https://github.com/earwig/mwparserfromhell
+from mwparserfromhell.nodes import Template
from wrpylib.mwmarkup import format_template_table
pass
-def wikibox_from_template(template, converter_dict):
+def wikibox_from_template(template: Template, converter_dict: dict) -> dict:
"""Returns an ordered dict."""
result = OrderedDict()
exceptions_dict = OrderedDict()
return result
-def wikibox_to_template(value, name, converter_dict) -> mwparserfromhell.nodes.template.Template:
- template = mwparserfromhell.nodes.template.Template(name)
+def wikibox_to_template(value: dict, name: str, converter_dict: dict) -> Template:
+ template = Template(name)
for key, converter in converter_dict.items():
template.add(key, converter.to_str(value[key]))
return template
-def template_from_str(value, name):
+def template_from_str(value: str, name: str) -> Template:
wikicode = mwparserfromhell.parse(value)
template_list = wikicode.filter_templates(recursive=False, matches=lambda t: t.name.strip() == name)
if len(template_list) == 0:
return template_list[0]
-def wikibox_from_str(value, name, converter_dict):
+def wikibox_from_str(value: str, name: str, converter_dict: dict) -> dict:
template = template_from_str(value, name)
return wikibox_from_template(template, converter_dict)
-def wikibox_to_str(value, name, converter_dict):
+def wikibox_to_str(value: dict, name: str, converter_dict: dict) -> str:
return str(wikibox_to_template(value, name, converter_dict))
])
-def rodelbahnbox_from_template(template):
+def rodelbahnbox_from_template(template: Template) -> dict:
"""Returns an ordered dict."""
return wikibox_from_template(template, RODELBAHNBOX_DICT)
-def rodelbahnbox_to_template(value):
+def rodelbahnbox_to_template(value: dict) -> Template:
return wikibox_to_template(value, RODELBAHNBOX_TEMPLATE_NAME, RODELBAHNBOX_DICT)
('Rodelbahnen', opt_wikipage_enum_converter)])
-def gasthausbox_from_template(template):
+def gasthausbox_from_template(template: Template) -> dict:
"""Returns an ordered dict."""
return wikibox_from_template(template, GASTHAUSBOX_DICT)
-def gasthausbox_to_template(value):
+def gasthausbox_to_template(value: dict) -> Template:
return wikibox_to_template(value, GASTHAUSBOX_TEMPLATE_NAME, GASTHAUSBOX_DICT)
-def gasthausbox_from_str(value):
+def gasthausbox_from_str(value: str) -> dict:
"""Returns an ordered dict."""
return wikibox_from_str(value, GASTHAUSBOX_TEMPLATE_NAME, GASTHAUSBOX_DICT)
-def gasthausbox_to_str(value):
+def gasthausbox_to_str(value: dict) -> str:
template = gasthausbox_to_template(value)
format_template_table(template, 17)
return str(template)
# Helper function to make page title pretty
# -----------------------------------------
-def sledrun_page_title_to_pretty_url(page_title):
+def sledrun_page_title_to_pretty_url(page_title: str) -> str:
"""Converts a page_title from the page_title column of wrsledruncache to name_url.
name_url is not used by MediaWiki but by new applications like wrweb."""
return page_title.lower().replace(' ', '-').replace('_', '-').replace('(', '').replace(')', '')