From b00d8d0cb22131436295d39d70c5b91cd95d9100 Mon Sep 17 00:00:00 2001 From: fla-t Date: Fri, 28 Jun 2024 11:17:10 +0500 Subject: [PATCH 1/4] feat: Update dotstrings parser to handle multi-line entries The dotstrings parser has been updated to handle multi-line entries in the format "key = value;". This allows for more flexibility in writing and parsing dotstrings files. --- dotstrings/parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotstrings/parser.py b/dotstrings/parser.py index 5716ba1..998062a 100755 --- a/dotstrings/parser.py +++ b/dotstrings/parser.py @@ -14,7 +14,7 @@ class Patterns: comment = re.compile(r"(\'(?:[^\'\\]|\\[\s\S])*\')|//.*|/\*(?:[^*]|\*(?!/))*\*/", re.MULTILINE) whitespace = re.compile(r"\s*", re.MULTILINE) - entry = re.compile(r'"(.*)"\s*=\s*"(.*)";') + entry = re.compile(r'"([^"]*?)"\s*=\s*"((?:[^";]|"(?!\s*;))*?)";', re.DOTALL) class Scanner: From 94cef1ca5a2ca0b92e81f587a5796fc2f03cdd08 Mon Sep 17 00:00:00 2001 From: fla-t Date: Fri, 28 Jun 2024 11:17:41 +0500 Subject: [PATCH 2/4] tests: Add tests to make sure that everything is working fine --- .../string_with_multiline_value.strings | 13 +++++++ tests/test_simple.py | 36 +++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 tests/strings_files/string_with_multiline_value.strings diff --git a/tests/strings_files/string_with_multiline_value.strings b/tests/strings_files/string_with_multiline_value.strings new file mode 100644 index 0000000..6589fb1 --- /dev/null +++ b/tests/strings_files/string_with_multiline_value.strings @@ -0,0 +1,13 @@ +"key-case1" = "value +that +has +newlines"; + +"key-case2" = "Major text in one line and just the closing in next line +"; + +"key-case3" = "A paragraph like text structure + +that goes like this and goes on and on + +and ends here"; \ No newline at end of file diff --git a/tests/test_simple.py b/tests/test_simple.py index 9a7dac2..4a75090 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -2,12 +2,12 @@ # pylint: disable=line-too-long +import dotstrings import os import sys import unittest sys.path.insert(0, os.path.abspath(os.path.join(os.path.abspath(__file__), "..", ".."))) -import dotstrings class SimpleTests(unittest.TestCase): @@ -86,9 +86,10 @@ def test_strings_format(self): entries = dotstrings.load(strings_file_path) self.assertEqual(entries[0].strings_format(), '/* This is a\n multiline comment */\n"carry" = "breathe";') - self.assertEqual(entries[1].strings_format(), '/* This is another\n multiline comment, but for a dupe */\n"condition" = "outgoing";') + self.assertEqual(entries[1].strings_format( + ), '/* This is another\n multiline comment, but for a dupe */\n"condition" = "outgoing";') self.assertEqual(entries[2].strings_format(), '/* precious */\n"condition" = "outgoing";') - + def test_string_with_uneven_whitespace(self): """Test that string with uneven whitespaces work""" string_with_uneven_whitespace_path = os.path.join(self.strings_path, "string_with_uneven_whitespace.strings") @@ -103,3 +104,32 @@ def test_string_with_uneven_whitespace(self): self.assertEqual(entries[2].key, "This is again a key") self.assertEqual(entries[2].value, "This is again a value") + + def test_string_with_multiline_value(self) -> None: + """Test the strings that have values spanning multiple lines""" + + multiline_value_path = os.path.join(self.strings_path, "string_with_multiline_value.strings") + entries = dotstrings.load(multiline_value_path) + self.assertEqual(len(entries), 3) + + self.assertEqual(entries[0].key, "key-case1") + self.assertEqual(entries[0].value, """value +that +has +newlines""") + self.assertEqual(entries[0].value, "value\nthat\nhas\nnewlines") + + self.assertEqual(entries[1].key, "key-case2") + self.assertEqual(entries[1].value, """Major text in one line and just the closing in next line +""") + self.assertEqual(entries[1].value, "Major text in one line and just the closing in next line\n") + + self.assertEqual(entries[2].key, "key-case3") + self.assertEqual(entries[2].value, """A paragraph like text structure + +that goes like this and goes on and on + +and ends here""") + + self.assertEqual( + entries[2].value, "A paragraph like text structure\n\nthat goes like this and goes on and on\n\nand ends here") From 507b8de5cbebd752b6718610edf0b007fac4cac9 Mon Sep 17 00:00:00 2001 From: fla-t Date: Fri, 28 Jun 2024 15:02:33 +0500 Subject: [PATCH 3/4] suggestion: revert formatting change --- tests/test_simple.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_simple.py b/tests/test_simple.py index 4a75090..1dd116a 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -86,8 +86,7 @@ def test_strings_format(self): entries = dotstrings.load(strings_file_path) self.assertEqual(entries[0].strings_format(), '/* This is a\n multiline comment */\n"carry" = "breathe";') - self.assertEqual(entries[1].strings_format( - ), '/* This is another\n multiline comment, but for a dupe */\n"condition" = "outgoing";') + self.assertEqual(entries[1].strings_format(), '/* This is another\n multiline comment, but for a dupe */\n"condition" = "outgoing";') self.assertEqual(entries[2].strings_format(), '/* precious */\n"condition" = "outgoing";') def test_string_with_uneven_whitespace(self): From 84ff6d3d079e37d09b56548ce754d11d48adfe21 Mon Sep 17 00:00:00 2001 From: fla-t Date: Fri, 28 Jun 2024 15:04:28 +0500 Subject: [PATCH 4/4] revert: keep the dotstring import as is --- tests/test_simple.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_simple.py b/tests/test_simple.py index 1dd116a..58709c2 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -2,12 +2,12 @@ # pylint: disable=line-too-long -import dotstrings import os import sys import unittest sys.path.insert(0, os.path.abspath(os.path.join(os.path.abspath(__file__), "..", ".."))) +import dotstrings class SimpleTests(unittest.TestCase):