From 661d34eb65e0ffd12cadf394e97554e948fbfcec Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 28 Apr 2026 21:32:28 +0800 Subject: [PATCH] Add auto wrap line support --- iinkai.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/iinkai.py b/iinkai.py index 544a5b7..e651212 100644 --- a/iinkai.py +++ b/iinkai.py @@ -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]