Add auto wrap line support

This commit is contained in:
2026-04-28 21:32:28 +08:00
parent a423cae411
commit 661d34eb65

View File

@@ -198,6 +198,49 @@ def patch_dict(script_path: str, dict_path: str, output_path: str):
f.writelines(origin_lines[end_line + 1:])
def wrap_lines(input: List[ParsedLine], max_width: int = 30) -> List[ParsedLine]:
result: List[ParsedLine] = []
current_line: ParsedLine = []
current_len = 0
for parsed_line in input:
for node in parsed_line:
if isinstance(node, TagNode):
current_line.append(node)
elif isinstance(node, TextNode):
text = node.text
while text:
available = max_width - current_len
if available <= 0:
if current_line:
result.append(current_line)
current_line = []
current_len = 0
available = max_width
take = min(len(text), available)
if take > 0:
current_line.append(TextNode(text[:take]))
current_len += take
text = text[take:]
if current_len >= max_width:
result.append(current_line)
current_line = []
current_len = 0
# End of original line — flush current line to preserve \n breaks
if current_line:
result.append(current_line)
current_line = []
current_len = 0
if current_line:
result.append(current_line)
return result
def patch_script(script_path: str, m3t_path: str, output_path: str, names, term):
with open(script_path, "r", encoding="utf-8") as f:
script_text = f.read()
@@ -242,6 +285,7 @@ def patch_script(script_path: str, m3t_path: str, output_path: str, names, term)
raise ValueError('unknown wd target', target)
node.attributes['s'] = overrides[target]
node.attributes['s'] = terms[node.attributes['s']]['translation']
nws = wrap_lines(nws)
new_script.extend(nws)
elif line.name.startswith("") and line.name.endswith(""):
name = line.name[1:-1]