Skip to content

Commit

Permalink
Change compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofia Margariti committed Jan 22, 2018
1 parent 3d8e047 commit 544e145
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions openformats/formats/indesign.py
Expand Up @@ -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

Expand All @@ -143,27 +142,36 @@ 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:
story_content = idml[key]
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()

0 comments on commit 544e145

Please sign in to comment.