diff --git a/app.py b/app.py index 744f7ab..b98b898 100644 --- a/app.py +++ b/app.py @@ -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 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('') and text.endswith('')), "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 tags in the wikitext. @@ -539,6 +556,38 @@ def convert_to_translatable_wikitext(wikitext): curr = end_pos last = curr continue + + # Process content inside existing tags + pattern = '' + if wikitext.startswith(pattern, curr): + end_pattern = wikitext.find('', curr) + len('') + 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 = '' + 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 = '' + if wikitext.startswith(pattern, curr): + end_pattern = curr + len('') + 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): diff --git a/tests.py b/tests.py index 2112349..207f384 100644 --- a/tests.py +++ b/tests.py @@ -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.'), - 'Some text with {{A template here}} and more text.' + 'Some text with {{a template here}} and more text.' ) def test_nowiki_tag(self): @@ -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 some code for you."), - "Here is some code for you." + "Here is some code for you." ) + def test_div_tag(self): self.assertEqual( @@ -187,6 +189,12 @@ def test_definition_list(self): convert_to_translatable_wikitext(";Term\n:Definition\n:Description"), "; Term\n: Definition\n: Description\n" ) + + def test_existing_translate_tags(self): + self.assertEqual( + convert_to_translatable_wikitext("This is already translated."), + "This is already translated." + ) if __name__ == '__main__': unittest.main(exit=False, failfast=True)