]> ToastFreeware Gitweb - philipp/winterrodeln/wrpylib.git/commitdiff
Create function strip_eol().
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Fri, 18 Mar 2022 14:04:58 +0000 (15:04 +0100)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Fri, 18 Mar 2022 14:04:58 +0000 (15:04 +0100)
tests/test_lib_sledrun_json_to_wikitext.py [new file with mode: 0644]
wrpylib/lib_sledrun_json_to_wikitext.py [new file with mode: 0644]

diff --git a/tests/test_lib_sledrun_json_to_wikitext.py b/tests/test_lib_sledrun_json_to_wikitext.py
new file mode 100644 (file)
index 0000000..02c9cf9
--- /dev/null
@@ -0,0 +1,13 @@
+import unittest
+
+from wrpylib.lib_sledrun_json_to_wikitext import strip_eol
+
+
+class TestTrimEol(unittest.TestCase):
+    def test_trim_eol(self):
+        self.assertEqual('', strip_eol(''))
+        self.assertEqual(' abc\n', strip_eol(' abc '))
+        self.assertEqual('  abc\n  def\ngh\n', strip_eol('  abc\n  def   \ngh '))
+        self.assertEqual('', strip_eol('\n\n'))
+        self.assertEqual('abc\n', strip_eol('abc     \n'))
+        self.assertEqual('\na\n', strip_eol('\na\n\n'))
diff --git a/wrpylib/lib_sledrun_json_to_wikitext.py b/wrpylib/lib_sledrun_json_to_wikitext.py
new file mode 100644 (file)
index 0000000..86c5055
--- /dev/null
@@ -0,0 +1,7 @@
+def strip_eol(text: str) -> str:
+    """Remove each whitespace from end of lines of string and also remove trailing empty lines."""
+    result = '\n'.join(line.rstrip() for line in text.splitlines(False))
+    result = result.rstrip()
+    if len(result) > 0:
+        return result + '\n'
+    return ''