From 544e1456bff9e89df6fab44a591a9fc64268c89d Mon Sep 17 00:00:00 2001 From: Sofia Margariti Date: Mon, 22 Jan 2018 18:26:27 +0200 Subject: [PATCH] Change compile --- openformats/formats/indesign.py | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/openformats/formats/indesign.py b/openformats/formats/indesign.py index d56f60d4..95356170 100644 --- a/openformats/formats/indesign.py +++ b/openformats/formats/indesign.py @@ -117,7 +117,6 @@ def _find_and_replace(self, story_xml): the input string with all translatable content replaced by the md5 hash of the string. """ - template = self.CONTENT_REGEX.sub(self._replace, story_xml) return template @@ -143,9 +142,7 @@ def compile(self, template, stringset, **kwargs): # The content is a binary IDML file idml = UCF(io.BytesIO(template)) - translations_dict = {s.template_replacement: s for s in stringset} - hash_regex = re.compile('[a-z,0-9]{32}_tr') - + pending_string = False # Iterate over the contents of the IDML file for key in self._get_ordered_stories(idml): try: @@ -153,17 +150,28 @@ def compile(self, template, stringset, **kwargs): except KeyError: continue - for match in hash_regex.finditer(story_content): - string_hash = match.group() - story_content = story_content.replace( - string_hash, - translations_dict.get(string_hash).string.encode('utf-8') - ) + story_content = idml[key] + transcriber = Transcriber(story_content) + found = True + while found: + try: + if not pending_string: + string = next(stringset) + hash_position = story_content.index(string.template_replacement) + pending_string = False + transcriber.copy_until(hash_position) + transcriber.add(string.string.encode('utf-8')) + transcriber.skip(len(string.template_replacement)) + except ValueError: + found = False + pending_string = True + except StopIteration: + break # Update the XML file to contain the template strings - idml[key] = story_content + transcriber.copy_until(len(story_content)) + idml[key] = transcriber.get_destination() out = io.BytesIO() idml.save(out) - return out.getvalue()