Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,23 @@ def process_small_tag(text):
wrapped_content = _wrap_in_translate(content)
return f"{prefix}{wrapped_content}{suffix}"

def process_existing_translate(text):
"""
Processes existing <translate> tags in the wikitext.
It removes the existing tags and processes the content through the converter,
which will add new translate tags as needed.
"""
assert(text.startswith('<translate>') and text.endswith('</translate>')), "Invalid translate tag"
start_tag_end = text.find('>') + 1
end_tag_start = text.rfind('<')
if start_tag_end >= end_tag_start:
return ""
content = text[start_tag_end:end_tag_start]
if not content.strip():
return content # Return just whitespace without tags
# Process the content through the converter (it will add translate tags as needed)
return convert_to_translatable_wikitext(content)

def process_nowiki(text):
"""
Processes <nowiki> tags in the wikitext.
Expand Down Expand Up @@ -539,6 +556,38 @@ def convert_to_translatable_wikitext(wikitext):
curr = end_pos
last = curr
continue

# Process content inside existing <translate> tags
pattern = '<translate>'
if wikitext.startswith(pattern, curr):
end_pattern = wikitext.find('</translate>', curr) + len('</translate>')
if last < curr:
parts.append((wikitext[last:curr], _wrap_in_translate))
parts.append((wikitext[curr:end_pattern], process_existing_translate))
curr = end_pattern
last = curr
continue

pattern = '<languages/>'
if wikitext.startswith(pattern, curr):
end_pattern = curr + len(pattern)
if last < curr:
parts.append((wikitext[last:curr], _wrap_in_translate))
parts.append((wikitext[curr:end_pattern], lambda x: x))
curr = end_pattern
last = curr
continue

pattern = '<language>'
if wikitext.startswith(pattern, curr):
end_pattern = curr + len('<language>')
if last < curr:
parts.append((wikitext[last:curr], _wrap_in_translate))
parts.append((wikitext[curr:end_pattern], lambda x: x))
curr = end_pattern
last = curr
continue

# Table block
pattern = '{|'
if wikitext.startswith(pattern, curr):
Expand Down
12 changes: 10 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_template_with_parameters(self):
def test_template_nested_in_text(self):
self.assertEqual(
convert_to_translatable_wikitext('Some text with {{a template here}} and more text.'),
'<translate>Some text with</translate> {{A template here}} <translate>and more text.</translate>'
'<translate>Some text with</translate> {{a template here}} <translate>and more text.</translate>'
)

def test_nowiki_tag(self):
Expand All @@ -110,11 +110,13 @@ def test_poem_tag(self):
)

def test_code_tag_with_tvar(self):

# Assuming process_code_tag assigns tvar names sequentially starting from 0
self.assertEqual(
convert_to_translatable_wikitext("Here is <code>some code</code> for you."),
"<translate>Here is <code><tvar name=code0>some code</tvar></code> for you.</translate>"
"<translate>Here is <tvar name=code0><code>some code</code></tvar> for you.</translate>"
)


def test_div_tag(self):
self.assertEqual(
Expand Down Expand Up @@ -187,6 +189,12 @@ def test_definition_list(self):
convert_to_translatable_wikitext(";Term\n:Definition\n:Description"),
"; <translate>Term</translate>\n: <translate>Definition</translate>\n: <translate>Description</translate>\n"
)

def test_existing_translate_tags(self):
self.assertEqual(
convert_to_translatable_wikitext("<translate>This is already translated.</translate>"),
"<translate>This is already translated.</translate>"
)

if __name__ == '__main__':
unittest.main(exit=False, failfast=True)