Add auto wrap line support
This commit is contained in:
44
iinkai.py
44
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:])
|
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):
|
def patch_script(script_path: str, m3t_path: str, output_path: str, names, term):
|
||||||
with open(script_path, "r", encoding="utf-8") as f:
|
with open(script_path, "r", encoding="utf-8") as f:
|
||||||
script_text = f.read()
|
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)
|
raise ValueError('unknown wd target', target)
|
||||||
node.attributes['s'] = overrides[target]
|
node.attributes['s'] = overrides[target]
|
||||||
node.attributes['s'] = terms[node.attributes['s']]['translation']
|
node.attributes['s'] = terms[node.attributes['s']]['translation']
|
||||||
|
nws = wrap_lines(nws)
|
||||||
new_script.extend(nws)
|
new_script.extend(nws)
|
||||||
elif line.name.startswith("【") and line.name.endswith("】"):
|
elif line.name.startswith("【") and line.name.endswith("】"):
|
||||||
name = line.name[1:-1]
|
name = line.name[1:-1]
|
||||||
|
|||||||
Reference in New Issue
Block a user