Skip to content

Commit 1a16c40

Browse files
feat:add header tags translate tags (#21)
1 parent 9e3fcbb commit 1a16c40

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

app.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,21 @@ def process_template(text):
512512

513513
return str(code)
514514

515+
516+
# --- Section Heading Handler ---
517+
def process_section_heading(text):
518+
"""
519+
Processes section headings like ==Title== and wraps the heading text in <translate> tags.
520+
"""
521+
# Match ==Title==, ===Subsection===, etc.
522+
match = re.match(r'^(=+)([^=]+)(=+)$', text.strip())
523+
if not match:
524+
return text
525+
level = match.group(1)
526+
heading_text = match.group(2).strip()
527+
# Reconstruct with same number of = on both sides
528+
return f'{level}<translate>{heading_text}</translate>{level}'
529+
515530
def process_raw_url(text):
516531
"""
517532
Processes raw URLs in the wikitext.
@@ -546,6 +561,19 @@ def convert_to_translatable_wikitext(wikitext):
546561

547562
while curr < text_length :
548563
found = None
564+
if wikitext[curr] == '=':
565+
# Find the end of the line
566+
end_line = wikitext.find('\n', curr)
567+
if end_line == -1:
568+
end_line = text_length
569+
line = wikitext[curr:end_line]
570+
if re.match(r'^(=+)[^=]+(=+)$', line.strip()):
571+
if last < curr:
572+
parts.append((wikitext[last:curr], _wrap_in_translate))
573+
parts.append((line, process_section_heading))
574+
curr = end_line
575+
last = curr
576+
continue
549577
# Syntax highlight block
550578
pattern = '<syntaxhighlight'
551579
if wikitext.startswith(pattern, curr):

0 commit comments

Comments
 (0)