diff --git a/openformats/formats/json.py b/openformats/formats/json.py index 984b60a0..c1d4ea61 100644 --- a/openformats/formats/json.py +++ b/openformats/formats/json.py @@ -258,6 +258,9 @@ def _replace_translations(self, template, stringset, is_real_stringset): self.stringset = stringset self.stringset_index = 0 + self.depth = 0 + self.metadata_blocks = [] + parsed = DumbJson(template) self._insert(parsed, is_real_stringset) @@ -270,7 +273,8 @@ def _insert(self, parsed, is_real_stringset): elif parsed.type == list: return self._insert_from_list(parsed, is_real_stringset) - def _insert_item(self, value, value_position, is_real_stringset): + def _insert_item(self, value, value_position, is_real_stringset, key_position=0): + at_least_one = False if isinstance(value, (six.binary_type, six.text_type)): @@ -318,10 +322,14 @@ def _insert_item(self, value, value_position, is_real_stringset): ) elif isinstance(value, DumbJson): + self.depth += 1 + if self.depth == 1: + self.metadata_blocks.append((key_position - 1, value.end + 1)) items_still_left = self._insert(value, is_real_stringset) if not items_still_left: self._copy_until_and_remove_section(value.end + 1) + self.depth -= 1 else: at_least_one = True @@ -336,12 +344,14 @@ def _insert_from_dict(self, parsed, is_real_stringset): at_least_one = not bool(list(parsed)) for key, key_position, value, value_position in parsed: + if key.startswith("@") and key != ("@@locale") and isinstance(value, (six.binary_type, six.text_type)) : + self.metadata_blocks.append((key_position - 1, value_position + len(value) + 1)) self.transcriber.copy_until(key_position - 1) self.transcriber.mark_section_start() tmp_at_least_one = self._insert_item( - value, value_position, is_real_stringset + value, value_position, is_real_stringset, key_position ) if tmp_at_least_one: @@ -604,7 +614,24 @@ def compile(self, template, stringset, language_info=None, **kwargs): new_template = self._replace_translations( template, fake_stringset, False ) + num_of_mdb = len(self.metadata_blocks) + while num_of_mdb > 0: + idx = num_of_mdb - 1 + new_template = u"{}{}".format( + new_template[:self.metadata_blocks[idx][0]], + new_template[self.metadata_blocks[idx][1]:] + ) + num_of_mdb -= 1 + + # Remember whether the root JSON ends with "}" on a separate line + closed_on_new_line = (new_template[new_template.rfind("}") - 1] == "\n") + new_template = self._clean_empties(new_template) + end_of_root_json = new_template.rfind("}") + if closed_on_new_line and new_template[end_of_root_json - 1] != "\n": + new_template = u"{}{}{}".format( + new_template[:end_of_root_json], "\n", new_template[end_of_root_json:] + ) if language_info is not None: match = re.search(r'(\"@@locale\"\s*:\s*\")([A-Z_a-z]*)\"', new_template) diff --git a/openformats/tests/formats/arb/files/1_el.arb b/openformats/tests/formats/arb/files/1_el.arb index 59ee823d..4589e572 100644 --- a/openformats/tests/formats/arb/files/1_el.arb +++ b/openformats/tests/formats/arb/files/1_el.arb @@ -1,16 +1,9 @@ { "@@locale": "en_US", - "@@x-template": "path/to/template.arb", - "@@context": "HomePage", "MSG_OK": "el:Everything works fine.", "title_bar": "el:My Cool Home", - "@title_bar": { - "type": "text", - "context": "HomePage", - "description": "Page title." - }, "total_files": "{ item_count, plural, one {el:You have {file_count} file.} other {el:You have {file_count} files.} }", "special_chars": "{ cnt, plural, one {el:This is Sam's book.} other {el:These are Sam's books.} }", @@ -18,26 +11,11 @@ "custom_plural_value": "{number, plural, one {el:1 New} two {el:# New}}", "logo@src": "images/001.jpg", - "@logo@src": { - "context": "arb_editor", - "type": "image", - "description": "logo image, 128x128" - }, "font_style": "#title {font-family: Verdana, Geneva, sans-serif; font-style: oblique; font-size: 36px}", - "@font_style": { - "context": "arb_editor", - "type": "css", - "description": "font specific css" - }, "input_test1@placeholder": "el:localized placeholder text", "input_test2@value": "el:localized input value", - "logo": "el:ARB", - "@logo": { - "type": "text", - "screen": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBhQGBRUIBwgKFQkKDRYODRYMFhYfHhoWHRweHB8cHh4cJzIqIyUkHB4cITssLycpLCwsFSExPjAtNSgrLEABCQoKDQsNGQ4OGTUkHiQ1LDU1NS4sLCo1NTYpNTYpLCw1NS40NCw0LikuKSkpLCwpLCw0KTQpKSwsNCkpNCkpLP/AABEIADIAMgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAABQIEBgcBA//EADQQAAEDAgIIAwUJAAAAAAAAAAEAAgMEEQUhBhITMUFRYXEUIjIjgbHB0QcWNEJTkaGi4f/EABgBAAMBAQAAAAAAAAAAAAAAAAABBAUC/8QAIBEAAgEEAgMBAAAAAAAAAAAAAAECAxExQQQhEiKhFP/aAAwDAQACEQMRAD8A7c30+4LN6WSubVwxxyPbYufdhIzyA3dyvtJjTqHFjHKWvpZbOjLbXFsiLjI2cDlvzVPSmobII6mJwLQ19rcxY2PLsmVUINVFddFj7xeGwNz5zeoiadU88sj9f9SfB8VqKOQvmqHSMkiHllPpflmCM7b7hIJ6uSplAkn9kDrFthY24Gwum9LXtqachoIeBmD8QeKWDS/PCEX0nf4OsG0hkmxjYVjmmOa4ZqgDVIBPcg9ei1Nlh8FDY8VbNM6zIgXn9rD+Sr2JaXPjkEVBTN13uDI9oTck7hYfVMhrUPKfotGrQq8DX+HbtpGbXVGvqjK9s7e9epEVjn+k8UmB4trAu8FUv2sd9weR5gOR49R2VeWsGI0oEbgJb2IPG+S6NV0TK+jNPVRNdE8WcHLn+O6DzUFUZMHa99M4ekWLmnkQSLjrv+KT6NCjyY+KUumtmbnqNhUGF5Ie02IKvUkxEJlYPLexKli2D1FDRNrcSpYtj6Ha4zHImxNhwGe9JjWSOpCwgiFxvEQ02yyIHPuk6mmiyNeMkOZMW1W2a4342TvQzAXYhiLcYqmkQwkmC/5iQRcdBc58Sk+iNBTmU1OkNXFZpAhidffzNt/AAZ9eS6w1oa2zQAAMrLq60S8nkNJwis7JoQhBmEW+kdgpWUW+kdgpIAyv2iR+I0dbSkfiayCP+4PyT+sw2PEaXYVdOx0Rys4fDkkOlz9pjNDSfq120PZgv81qBuSWzlN3Yjw/Q6mwup29NS+1YbtL3OdY8wHGwPVPEL1M7cnLLBCEIERb6R2CkhCAKlTA2Sdkj42F8TzsyQLt8h3HgrQQhAlk9QhCBghCEAf/2Q==", - "video": "http://www.youtube.com/user_interaction" - } + "logo": "el:ARB" } \ No newline at end of file diff --git a/openformats/tests/formats/arb/files/1_en_exported.arb b/openformats/tests/formats/arb/files/1_en_exported.arb index 6673e571..b0921f01 100644 --- a/openformats/tests/formats/arb/files/1_en_exported.arb +++ b/openformats/tests/formats/arb/files/1_en_exported.arb @@ -1,16 +1,9 @@ { "@@locale": "en_US", - "@@x-template": "path/to/template.arb", - "@@context": "HomePage", "MSG_OK": "Everything works fine.", "title_bar": "My Cool Home", - "@title_bar": { - "type": "text", - "context": "HomePage", - "description": "Page title." - }, "total_files": "{ item_count, plural, one {You have {file_count} file.} other {You have {file_count} files.} }", "special_chars": "{ cnt, plural, one {This is Sam's book.} other {These are Sam's books.} }", @@ -18,26 +11,11 @@ "custom_plural_value": "{number, plural, one {1 New} two {# New}}", "logo@src": "images/001.jpg", - "@logo@src": { - "context": "arb_editor", - "type": "image", - "description": "logo image, 128x128" - }, "font_style": "#title {font-family: Verdana, Geneva, sans-serif; font-style: oblique; font-size: 36px}", - "@font_style": { - "context": "arb_editor", - "type": "css", - "description": "font specific css" - }, "input_test1@placeholder": "localized placeholder text", "input_test2@value": "localized input value", - "logo": "ARB", - "@logo": { - "type": "text", - "screen": "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBhQGBRUIBwgKFQkKDRYODRYMFhYfHhoWHRweHB8cHh4cJzIqIyUkHB4cITssLycpLCwsFSExPjAtNSgrLEABCQoKDQsNGQ4OGTUkHiQ1LDU1NS4sLCo1NTYpNTYpLCw1NS40NCw0LikuKSkpLCwpLCw0KTQpKSwsNCkpNCkpLP/AABEIADIAMgMBIgACEQEDEQH/xAAbAAACAwEBAQAAAAAAAAAAAAAABQIEBgcBA//EADQQAAEDAgIIAwUJAAAAAAAAAAEAAgMEEQUhBhITMUFRYXEUIjIjgbHB0QcWNEJTkaGi4f/EABgBAAMBAQAAAAAAAAAAAAAAAAABBAUC/8QAIBEAAgEEAgMBAAAAAAAAAAAAAAECAxExQQQhEiKhFP/aAAwDAQACEQMRAD8A7c30+4LN6WSubVwxxyPbYufdhIzyA3dyvtJjTqHFjHKWvpZbOjLbXFsiLjI2cDlvzVPSmobII6mJwLQ19rcxY2PLsmVUINVFddFj7xeGwNz5zeoiadU88sj9f9SfB8VqKOQvmqHSMkiHllPpflmCM7b7hIJ6uSplAkn9kDrFthY24Gwum9LXtqachoIeBmD8QeKWDS/PCEX0nf4OsG0hkmxjYVjmmOa4ZqgDVIBPcg9ei1Nlh8FDY8VbNM6zIgXn9rD+Sr2JaXPjkEVBTN13uDI9oTck7hYfVMhrUPKfotGrQq8DX+HbtpGbXVGvqjK9s7e9epEVjn+k8UmB4trAu8FUv2sd9weR5gOR49R2VeWsGI0oEbgJb2IPG+S6NV0TK+jNPVRNdE8WcHLn+O6DzUFUZMHa99M4ekWLmnkQSLjrv+KT6NCjyY+KUumtmbnqNhUGF5Ie02IKvUkxEJlYPLexKli2D1FDRNrcSpYtj6Ha4zHImxNhwGe9JjWSOpCwgiFxvEQ02yyIHPuk6mmiyNeMkOZMW1W2a4342TvQzAXYhiLcYqmkQwkmC/5iQRcdBc58Sk+iNBTmU1OkNXFZpAhidffzNt/AAZ9eS6w1oa2zQAAMrLq60S8nkNJwis7JoQhBmEW+kdgpWUW+kdgpIAyv2iR+I0dbSkfiayCP+4PyT+sw2PEaXYVdOx0Rys4fDkkOlz9pjNDSfq120PZgv81qBuSWzlN3Yjw/Q6mwup29NS+1YbtL3OdY8wHGwPVPEL1M7cnLLBCEIERb6R2CkhCAKlTA2Sdkj42F8TzsyQLt8h3HgrQQhAlk9QhCBghCEAf/2Q==", - "video": "http://www.youtube.com/user_interaction" - } + "logo": "ARB" } \ No newline at end of file