]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Create function get_sledrun_description.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 15 Mar 2022 21:09:58 +0000 (22:09 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Tue, 15 Mar 2022 21:09:58 +0000 (22:09 +0100)
tests/test_lib_sledrun_wikitext_to_json.py [new file with mode: 0644]
wrpylib/lib_sledrun_wikitext_to_json.py [new file with mode: 0644]

diff --git a/tests/test_lib_sledrun_wikitext_to_json.py b/tests/test_lib_sledrun_wikitext_to_json.py
new file mode 100644 (file)
index 0000000..afbc25d
--- /dev/null
@@ -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 (file)
index 0000000..541551e
--- /dev/null
@@ -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