diff --git a/openformats/formats/json.py b/openformats/formats/json.py index 0eff452d..c7f0dea5 100644 --- a/openformats/formats/json.py +++ b/openformats/formats/json.py @@ -9,12 +9,12 @@ import six -from ..exceptions import ParseError -from ..handlers import Handler -from ..strings import OpenString -from ..transcribers import Transcriber -from ..utils.icu import ICUCompiler, ICUParser -from ..utils.json import DumbJson, escape, unescape +from openformats.exceptions import ParseError +from openformats.handlers import Handler +from openformats.strings import OpenString +from openformats.transcribers import Transcriber +from openformats.utils.icu import ICUCompiler, ICUParser +from openformats.utils.json import DumbJson, escape, unescape try: from StringIO import StringIO @@ -509,7 +509,9 @@ def compile(self, template, translations, **kwargs): self.transcriber.copy_to_end() return self.transcriber.get_destination() - def _compile_value(self, value, template_value, value_position): + def _compile_value(self, value, template_value, value_position, skip=False): + value = template_value if skip else value + if value is not None: if value == '' and template_value is None: self.transcriber.add(u"null") @@ -520,6 +522,7 @@ def _compile_value(self, value, template_value, value_position): self.transcriber.add(u"{}".format(value)) else: self.transcriber.add(u"null") + self.transcriber.skip(len(u"{}".format(template_value))) self.transcriber.copy_until(value_position + len(u"{}".format(template_value)) + @@ -539,7 +542,16 @@ def _compile_recursively(self, current_part): self.transcriber.copy_until(value_position) self._compile_recursively(value) else: - translation = next(self.translations, None) + (value, _) = current_part.find_children(self.STRING_KEY)[0] + if not value.strip(): + translation = OpenString( + "", value + ) + skip=True + else: + translation = next(self.translations, None) + skip=False + context_added = False character_limit_added = False developer_comments_added = False @@ -561,19 +573,22 @@ def _compile_recursively(self, current_part): context = translation.context self._compile_value(self.escape(context), value, - value_position) + value_position, + skip=skip) context_added = True elif key == self.DEVELOPER_COMMENT_KEY and translation: developer_comment = translation.developer_comment self._compile_value(self.escape(developer_comment), value, - value_position) + value_position, + skip=skip) developer_comments_added = True elif key == self.CHARACTER_LIMIT_KEY and translation: character_limit = translation.character_limit self._compile_value(character_limit, value, - value_position) + value_position, + skip=skip) character_limit_added = True elif key == self.STRING_KEY and translation: if translation.pluralized: diff --git a/openformats/tests/formats/structuredkeyvaluejson/test_keyvaluejson.py b/openformats/tests/formats/structuredkeyvaluejson/test_keyvaluejson.py index 8b47b40f..64f9e33c 100644 --- a/openformats/tests/formats/structuredkeyvaluejson/test_keyvaluejson.py +++ b/openformats/tests/formats/structuredkeyvaluejson/test_keyvaluejson.py @@ -29,6 +29,16 @@ def setUp(self): self.random_string, order=0) self.random_hash = self.random_openstring.template_replacement + def test_broken(self): + stringset = [ + OpenString("b", "foo_tr", order=0) + ] + string_hash = stringset[0].template_replacement + template, stringset = self.handler.parse('{"a": {"string":" ", "character_limit": 1, "developer_comment": "A"}, "b": {"string": "foo"}}') # noqa + compiled = self.handler.compile(template, stringset) + self.assertEqual(template, '{"a": {"string":" ", "character_limit": 1, "developer_comment": "A"}, "b": {"string": "%s"}}' % string_hash) # noqa + self.assertEqual(compiled, '{"a": {"string":" ", "character_limit": 1, "developer_comment": "A"}, "b": {"string": "foo"}}') # noqa + def test_simple(self): template, stringset = self.handler.parse('{"a": {"string":"%s"}}' % self.random_string) @@ -40,12 +50,12 @@ def test_simple(self): self.random_openstring.__dict__) self.assertEqual(compiled, '{"a": {"string":"%s"}}' % self.random_string) - + def test_dots_in_key(self): first_level_key = "a.b" source = '{"%s": {"c": {"string": "%s"}}}' % (first_level_key, self.random_string) openstring = OpenString( - "{}.c".format(self.handler._escape_key(first_level_key)), + "{}.c".format(self.handler._escape_key(first_level_key)), self.random_string, order=0 ) random_hash = openstring.template_replacement @@ -63,7 +73,7 @@ def test_escaped_character_in_key(self): first_level_key = "a\/b" source = '{"%s": {"c": {"string": "%s"}}}' % (first_level_key, self.random_string) openstring = OpenString( - "{}.c".format(self.handler._escape_key(first_level_key)), + "{}.c".format(self.handler._escape_key(first_level_key)), self.random_string, order=0 ) random_hash = openstring.template_replacement @@ -460,7 +470,7 @@ def test_template_with_existing_values(self): source = """ { "a": { - "character_limit":150, + "character_limit":150, "string":"%s", "developer_comment": "i am a developer", "context": "contexttt" @@ -471,7 +481,7 @@ def test_template_with_existing_values(self): expected_compilation = """ { "a": { - "character_limit":49, + "character_limit":49, "string":"%s", "developer_comment": "comment_changed", "context": "context_changed"