From 3954ae2ead9ba0c38d26c2b6a92dae1efbda36fc Mon Sep 17 00:00:00 2001 From: Philipp Spitzer Date: Mon, 6 Jun 2022 22:15:55 +0200 Subject: [PATCH] Move some functions to lib_sledrun_wikitext_to_json.py --- bots/sledrun_wikitext_to_json.py | 27 ++----------------------- wrpylib/lib_sledrun_wikitext_to_json.py | 26 +++++++++++++++++++++++- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/bots/sledrun_wikitext_to_json.py b/bots/sledrun_wikitext_to_json.py index 60bcb21..72880cd 100644 --- a/bots/sledrun_wikitext_to_json.py +++ b/bots/sledrun_wikitext_to_json.py @@ -38,35 +38,12 @@ from wrpylib.wrmwmarkup import create_sledrun_wiki, lonlat_to_json, lonlat_ele_t from wrpylib.wrvalidators import rodelbahnbox_from_template, tristate_german_to_str, difficulty_german_to_str, \ avalanches_german_to_str, public_transport_german_to_str, opt_lonlat_from_str, \ opt_uint_from_str -from wrpylib.lib_sledrun_wikitext_to_json import optional_set, get_sledrun_description +from wrpylib.lib_sledrun_wikitext_to_json import optional_set, get_sledrun_description, wikilink_to_json, \ + template_to_json, external_link_to_json docuReplacements = {'¶ms;': pagegenerators.parameterHelp} -def template_to_json(value: Template) -> dict: - parameter = [] - for p in value.params: - parameter.append({'value': str(p)}) - return { - 'name': str(value.name), - 'parameter': parameter - } - - -def wikilink_to_json(value: Wikilink) -> dict: - wl = {'title': str(value.title)} - if value.text is not None: - wl['text'] = str(value.text) - return wl - - -def external_link_to_json(value: ExternalLink) -> dict: - link = {'url': str(value.url)} - if value.title is not None: - link['text'] = str(value.title) - return link - - class SledrunWikiTextToJsonBot( SingleSiteBot, ConfigParserBot, diff --git a/wrpylib/lib_sledrun_wikitext_to_json.py b/wrpylib/lib_sledrun_wikitext_to_json.py index 4fffb55..11ad3ad 100644 --- a/wrpylib/lib_sledrun_wikitext_to_json.py +++ b/wrpylib/lib_sledrun_wikitext_to_json.py @@ -1,7 +1,7 @@ from itertools import takewhile, dropwhile from typing import Optional, Any -from mwparserfromhell.nodes import Template, Text, Tag +from mwparserfromhell.nodes import Template, Text, Tag, Wikilink, ExternalLink from mwparserfromhell.wikicode import Wikicode @@ -11,6 +11,30 @@ def optional_set(haystack: dict, key: str, value: Optional[Any]): haystack[key] = value +def wikilink_to_json(value: Wikilink) -> dict: + wl = {'title': str(value.title)} + if value.text is not None: + wl['text'] = str(value.text) + return wl + + +def external_link_to_json(value: ExternalLink) -> dict: + link = {'url': str(value.url)} + if value.title is not None: + link['text'] = str(value.title) + return link + + +def template_to_json(value: Template) -> dict: + parameter = [] + for p in value.params: + parameter.append({'value': str(p)}) + return { + 'name': str(value.name), + 'parameter': parameter + } + + def get_sledrun_description(sledrun: Wikicode) -> Optional[str]: """Get description from sledrun""" for v in sledrun.get_sections(levels=[2], matches='Allgemeines', include_headings=False): -- 2.39.5