Skip to content

Commit

Permalink
Create private method to handle compiling of a single story
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofia Margariti committed Jan 24, 2018
1 parent 0a93002 commit a04c819
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 24 deletions.
60 changes: 36 additions & 24 deletions openformats/formats/indesign.py
Expand Up @@ -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):
Expand All @@ -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
101 changes: 101 additions & 0 deletions openformats/tests/formats/indesign/test_indesign.py
Expand Up @@ -2,6 +2,7 @@
import unittest

from openformats.formats.indesign import InDesignHandler
from openformats.strings import OpenString


class InDesignTestCase(unittest.TestCase):
Expand Down Expand Up @@ -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 = """
<Story>
<Content>9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr</Content>
<Content>3afcdbfeb6ecfbdd0ba628696e3cc163_tr</Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
simple_compiled_story = """
<Story>
<Content>Some string 1</Content>
<Content>Some string 2</Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
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 = """
<Story>
<Content>9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr</Content>
<Content>3afcdbfeb6ecfbdd0ba628696e3cc163_tr</Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
simple_compiled_story = """
<Story>
<Content>Some string 1</Content>
<Content></Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
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 = """
<Story>
<Content>9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr</Content>
<Content>3afcdbfeb6ecfbdd0ba628696e3cc163_tr</Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
second_story_template = """
<Story>
<Content>9a1c7ee2c7ce38d4bbbaf29ab9f2ac1e_tr</Content>
<Content>cdee9bf40a070d58d14dfa3bb61e0032_tr</Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
expected_first_compiled_story = """
<Story>
<Content>Some string 1</Content>
<Content></Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
expected_second_compiled_story = """
<Story>
<Content></Content>
<Content>Some string 2</Content>
<Metadata></Metadata>
<Content> <?ACE 7?></Content>
</Story>
"""
# 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)

0 comments on commit a04c819

Please sign in to comment.