From c42d72278f717134c0bac53bb2d32f09f5025949 Mon Sep 17 00:00:00 2001 From: Philipp Spitzer Date: Tue, 15 Mar 2022 22:09:58 +0100 Subject: [PATCH] Create function get_sledrun_description. --- tests/test_lib_sledrun_wikitext_to_json.py | 15 +++++++++++++++ wrpylib/lib_sledrun_wikitext_to_json.py | 15 +++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/test_lib_sledrun_wikitext_to_json.py create mode 100644 wrpylib/lib_sledrun_wikitext_to_json.py 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 -- 2.39.5