diff --git a/openformats/formats/indesign.py b/openformats/formats/indesign.py
index cb697066..86f4e8a5 100644
--- a/openformats/formats/indesign.py
+++ b/openformats/formats/indesign.py
@@ -141,9 +141,8 @@ def _replace(self, match):
def compile(self, template, stringset, **kwargs):
# The content is a binary IDML file
idml = UCF(io.BytesIO(template))
- pending_string = False
- stringset = iter(stringset)
+ self.stringset = list(stringset)
# Iterate over the contents of the IDML file
for key in self._get_ordered_stories(idml):
@@ -153,29 +152,42 @@ def compile(self, template, stringset, **kwargs):
continue
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
- transcriber.copy_until(len(story_content))
- idml[key] = transcriber.get_destination()
+ idml[key] = self._compile_story(story_content)
out = io.BytesIO()
idml.save(out)
return out.getvalue()
+
+ def _compile_story(self, story_content):
+ """ Handles the compilation of a single story
+ args:
+ story_content: the xml content of the story
+ returns:
+ compiled_story: the compiled story content
+ """
+ transcriber = Transcriber(story_content)
+ hash_regex = re.compile('[a-z,0-9]{32}_tr')
+ found = True
+ while found:
+ try:
+ current_string = self.stringset.pop(0)
+ hash_position = story_content.index(
+ current_string.template_replacement
+ )
+ except ValueError:
+ found = False
+ self.stringset.insert(0, current_string)
+ except IndexError:
+ break
+ else:
+ transcriber.copy_until(hash_position)
+ transcriber.add(current_string.string.encode('utf-8'))
+ transcriber.skip(len(current_string.template_replacement))
+
+ # Update the XML file to contain the template strings
+ transcriber.copy_until(len(story_content))
+ compiled_story = transcriber.get_destination()
+ # in case there are any hashes that have not been replaced, replace
+ # them with an empty string
+ compiled_story = hash_regex.sub('', compiled_story)
+ return compiled_story
diff --git a/openformats/tests/formats/indesign/test_indesign.py b/openformats/tests/formats/indesign/test_indesign.py
index aad40b37..90923f7a 100644
--- a/openformats/tests/formats/indesign/test_indesign.py
+++ b/openformats/tests/formats/indesign/test_indesign.py
@@ -2,6 +2,7 @@
import unittest
from openformats.formats.indesign import InDesignHandler
+from openformats.strings import OpenString
class InDesignTestCase(unittest.TestCase):
@@ -81,3 +82,103 @@ def test_find_and_replace_simple_story(self):
out = handler._find_and_replace(simple_input)
self.assertEqual(out, simple_output)
self.assertEqual(len(handler.stringset), 1)
+
+ def test_compile_story(self):
+ simple_story_template = """
+
+ 9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr
+ 3afcdbfeb6ecfbdd0ba628696e3cc163_tr
+
+
+
+ """
+ simple_compiled_story = """
+
+ Some string 1
+ Some string 2
+
+
+
+ """
+ handler = self.HANDLER_CLASS()
+ handler.stringset = [
+ OpenString(str(0), "Some string 1", order=0),
+ OpenString(str(1), "Some string 2", order=1),
+ ]
+
+ compiled_story = handler._compile_story(simple_story_template)
+ self.assertEqual(compiled_story, simple_compiled_story)
+
+ def test_compile_story_missing_strings(self):
+ simple_story_template = """
+
+ 9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr
+ 3afcdbfeb6ecfbdd0ba628696e3cc163_tr
+
+
+
+ """
+ simple_compiled_story = """
+
+ Some string 1
+
+
+
+
+ """
+ handler = self.HANDLER_CLASS()
+ handler.stringset = [
+ OpenString(str(0), "Some string 1", order=0),
+ ]
+
+ compiled_story = handler._compile_story(simple_story_template)
+ self.assertEqual(compiled_story, simple_compiled_story)
+
+ def test_compile_two_stories_with_strings(self):
+ first_story_template = """
+
+ 9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr
+ 3afcdbfeb6ecfbdd0ba628696e3cc163_tr
+
+
+
+ """
+ second_story_template = """
+
+ 9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr
+ cdee9bf40a070d58d14dfa3bb61e0032_tr
+
+
+
+ """
+ expected_first_compiled_story = """
+
+ Some string 1
+
+
+
+
+ """
+ expected_second_compiled_story = """
+
+
+ Some string 2
+
+
+
+ """
+ # strings #1 and #2 are missing from the stringset
+ handler = self.HANDLER_CLASS()
+ handler.stringset = [
+ OpenString(str(0), "Some string 1", order=0),
+ OpenString(str(3), "Some string 2", order=3),
+ ]
+
+ first_compiled_story = handler._compile_story(
+ first_story_template
+ )
+ second_compiled_story = handler._compile_story(
+ second_story_template
+ )
+ self.assertEqual(first_compiled_story, expected_first_compiled_story)
+ self.assertEqual(second_compiled_story, expected_second_compiled_story)