From: Philipp Spitzer Date: Tue, 15 Mar 2022 21:09:58 +0000 (+0100) Subject: Create function get_sledrun_description. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wrpylib.git/commitdiff_plain/c42d72278f717134c0bac53bb2d32f09f5025949 Create function get_sledrun_description. --- diff --git a/tests/test_lib_sledrun_wikitext_to_json.py b/tests/test_lib_sledrun_wikitext_to_json.py new file mode 100644 index 0000000..afbc25d --- /dev/null +++ b/tests/test_lib_sledrun_wikitext_to_json.py @@ -0,0 +1,15 @@ +import unittest + +from mwparserfromhell import parse + +from wrpylib.lib_sledrun_wikitext_to_json import get_sledrun_description + + +class TestGetSledrunDescription(unittest.TestCase): + def test_get_sledrun_description(self): + wikitext = "== Allgemeines ==\n{{Rodelbahnbox}}\nDas ist ''die'' Beschreibung.\n" + \ + "Weitere Details bei https://example.com.\n\n* Position\n" + wikicode = parse(wikitext) + expected = "Das ist ''die'' Beschreibung.\nWeitere Details bei https://example.com." + actual = get_sledrun_description(wikicode) + self.assertEqual(expected, actual) diff --git a/wrpylib/lib_sledrun_wikitext_to_json.py b/wrpylib/lib_sledrun_wikitext_to_json.py new file mode 100644 index 0000000..541551e --- /dev/null +++ b/wrpylib/lib_sledrun_wikitext_to_json.py @@ -0,0 +1,15 @@ +from itertools import takewhile, dropwhile +from typing import Optional, Any + +from mwparserfromhell.nodes import Template, Text, Tag +from mwparserfromhell.wikicode import Wikicode + + +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): + dw = dropwhile(lambda n: isinstance(n, Template) or (isinstance(n, Text) and not str(n).strip()), v.nodes) + tw = takewhile(lambda n: not (isinstance(n, Tag) and str(n) == '*'), dw) + result = str(Wikicode(tw)).strip() + if result: + return result