Skip to content

Commit

Permalink
feat: Made the unaligned translation lines adhere to the position set…
Browse files Browse the repository at this point in the history
…ting. #208
  • Loading branch information
bookfere committed Mar 4, 2024
1 parent 6bf8a7d commit 941e3b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
30 changes: 20 additions & 10 deletions lib/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ def get_content(self, placeholder):

def add_translation(self, translation, placeholder, position=None,
lang=None, color=None):
if position == 'only':
self.element[2] = translation
elif position == 'after':
self.element[2] += '\n%s' % translation
else:
self.element[2] = '%s\n%s' % (translation, self.element[2])
if translation is not None:
if position == 'only':
self.element[2] = translation
elif position == 'after':
self.element[2] += '\n%s' % translation
else:
self.element[2] = '%s\n%s' % (translation, self.element[2])
return self.element


Expand All @@ -89,9 +90,10 @@ def get_content(self, placeholder):

def add_translation(self, translation, placeholder, position=None,
lang=None, color=None):
items = [self.element.title, translation]
self.element.title = items[-1] if position == 'only' else \
' '.join(reversed(items) if position == 'before' else items)
if translation is not None:
items = [self.element.title, translation]
self.element.title = items[-1] if position == 'only' else \
' '.join(reversed(items) if position == 'before' else items)
return self.element


Expand Down Expand Up @@ -146,6 +148,10 @@ def _polish_translation(self, translation):

def add_translation(self, translation, placeholder, position=None,
lang=None, color=None):
if translation is None:
if position == 'only':
self.delete()
return self.element
# Escape the markups (<m id=1 />) to replace escaped markups.
translation = xml_escape(translation)
for rid, reserve in enumerate(self.reserve_elements):
Expand Down Expand Up @@ -398,7 +404,11 @@ def align_paragraph(self, paragraph):
translations = translation.strip().split(self.separator)
offset = len(originals) - len(translations)
if offset > 0:
translations += ['-'] * offset
addition = [None] * offset
if self.position == 'after':
translations = addition + translations
else:
translations += addition
elif offset < 0:
translations = translations[:offset]
for original in originals:
Expand Down
47 changes: 32 additions & 15 deletions tests/test_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def test_get_text(self):
def test_get_content(self):
self.assertEqual('a', self.element.get_content(Base.placeholder))

def test_add_translation_none(self):
element = self.element.add_translation(None, Base.placeholder, 'after')
self.assertEqual('a', element[2])

def test_add_translation_after(self):
element = self.element.add_translation('A', Base.placeholder, 'after')
self.assertEqual('a\nA', element[2])
Expand All @@ -85,6 +89,10 @@ def test_get_text(self):
def test_get_content(self):
self.assertEqual('a', self.element.get_content(Base.placeholder))

def test_add_translation_none(self):
element = self.element.add_translation(None, Base.placeholder, 'after')
self.assertEqual('a', element.title)

def test_add_translation_after(self):
element = self.element.add_translation('A', Base.placeholder, 'after')
self.assertEqual('a A', element.title)
Expand Down Expand Up @@ -156,7 +164,12 @@ def test_get_content(self):
def test_get_attributes(self):
self.assertEqual('{"class": "abc"}', self.element.get_attributes())

def test_add_translation(self):
def test_add_translation_none(self):
self.assertIs(
self.element.add_translation(None, Base.placeholder),
self.element.element)

def test_add_translation_with_placeholder(self):
self.element.get_content(Base.placeholder)
translation = ('{{id_00000}} Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa '
'{{id_00001}} Bbbbbbbbbbbbbbb C {{id_00002}} D E '
Expand Down Expand Up @@ -573,7 +586,7 @@ def test_align_paragraph(self):
'a {{id_0}} b {{id_1}} c {{id_3}}', False, None, None,
'A {{id_0}} B {{id_1}}', 'ENGINE', 'LANG')
self.assertEqual(
['A', 'B', '-'], self.handler.align_paragraph(paragraph))
['A', 'B', None], self.handler.align_paragraph(paragraph))

paragraph = Paragraph(
0, 'm1', '<p id="a">a</p><p id="b">b</p><p id="c">c</p>',
Expand All @@ -589,12 +602,23 @@ def test_align_paragraph(self):
self.assertEqual(
['A', 'B', 'C'], self.handler.align_paragraph(paragraph))

paragraph = Paragraph(
0, 'm1', '<p id="a">a</p><p id="b">b</p><p id="c">c</p>',
'a\n\nb\n\nx\n\n', False, None, None, 'A\n\nB\n\n',
'ENGINE', 'LANG')
self.assertEqual(
['A', 'B'], self.handler.align_paragraph(paragraph))

paragraph = Paragraph(
0, 'm1', '<p id="a">a</p><p id="b">b</p><p id="c">c</p>',
'a\n\nb\n\nc\n\n', False, None, None, 'A\n\nB\n\n',
'ENGINE', 'LANG')
self.assertEqual(
['A', 'B', '-'], self.handler.align_paragraph(paragraph))
['A', 'B', None], self.handler.align_paragraph(paragraph))

self.handler.position = 'after'
self.assertEqual(
[None, 'A', 'B'], self.handler.align_paragraph(paragraph))

@patch('calibre_plugins.ebook_translator.lib.element.uid')
def test_prepare_original_merge_separator(self, mock_uid):
Expand Down Expand Up @@ -665,13 +689,12 @@ def test_add_translations_merge_separator(self):

elements = self.xhtml.findall('./x:body/*', namespaces=ns)

self.assertEqual(8, len(elements))
self.assertEqual(7, len(elements))
self.assertEqual('a', elements[0].text)
self.assertEqual('A B', elements[1].text)
self.assertEqual('b', elements[2].text)
self.assertEqual('C', elements[3].text)
self.assertEqual('c', elements[5].text)
self.assertEqual('-', elements[6].text)

def test_add_translations_merge_separator_multiple(self):
self.handler.merge_length = 2
Expand Down Expand Up @@ -706,14 +729,13 @@ def test_add_translations_merge_placeholder_missing_id(self):

elements = self.xhtml.findall('./x:body/*', namespaces=ns)

self.assertEqual(8, len(elements))
self.assertEqual(7, len(elements))
self.assertEqual('a', elements[0].text)
self.assertEqual('A B', elements[1].text)
self.assertEqual('b', elements[2].text)
self.assertEqual('C', elements[3].text)

self.assertEqual('c', elements[5].text)
self.assertEqual('-', elements[6].text)

def test_add_translations_merge_placeholder_missing_newline(self):
self.handler.separator = Base.separator
Expand All @@ -725,14 +747,13 @@ def test_add_translations_merge_placeholder_missing_newline(self):

elements = self.xhtml.findall('./x:body/*', namespaces=ns)

self.assertEqual(8, len(elements))
self.assertEqual(7, len(elements))
self.assertEqual('a', elements[0].text)
self.assertEqual('A B', elements[1].text)
self.assertEqual('b', elements[2].text)
self.assertEqual('C', elements[3].text)

self.assertEqual('c', elements[5].text)
self.assertEqual('-', elements[6].text)

def test_add_translations_merge_palceholder_only(self):
self.handler.position = 'only'
Expand Down Expand Up @@ -778,12 +799,10 @@ def test_add_translations_merge_placeholder_only_missing_id(self):
'A B {{id_1}} C {{id_3}}', 'ENGINE', 'LANG')])

elements = self.xhtml.findall('./x:body/*', namespaces=ns)
self.assertEqual(5, len(elements))
self.assertEqual(4, len(elements))
self.assertEqual('A B', elements[0].text)
self.assertEqual('C', elements[1].text)

self.assertEqual('-', elements[3].text)

def test_add_translations_merge_separator_only_missing_id(self):
self.handler.position = 'only'
self.handler.separator = Base.separator
Expand All @@ -796,8 +815,6 @@ def test_add_translations_merge_separator_only_missing_id(self):

elements = self.xhtml.findall('./x:body/*', namespaces=ns)

self.assertEqual(5, len(elements))
self.assertEqual(4, len(elements))
self.assertEqual('A B', elements[0].text)
self.assertEqual('C', elements[1].text)

self.assertEqual('-', elements[3].text)

0 comments on commit 941e3b6

Please sign in to comment.