From c8c4bc42d81b74badf007f715c8c4cc5a1fc9352 Mon Sep 17 00:00:00 2001 From: Philipp Spitzer Date: Fri, 18 Mar 2022 15:04:58 +0100 Subject: [PATCH] Create function strip_eol(). --- tests/test_lib_sledrun_json_to_wikitext.py | 13 +++++++++++++ wrpylib/lib_sledrun_json_to_wikitext.py | 7 +++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/test_lib_sledrun_json_to_wikitext.py create mode 100644 wrpylib/lib_sledrun_json_to_wikitext.py diff --git a/tests/test_lib_sledrun_json_to_wikitext.py b/tests/test_lib_sledrun_json_to_wikitext.py new file mode 100644 index 0000000..02c9cf9 --- /dev/null +++ b/tests/test_lib_sledrun_json_to_wikitext.py @@ -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 index 0000000..86c5055 --- /dev/null +++ b/wrpylib/lib_sledrun_json_to_wikitext.py @@ -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 '' -- 2.39.5