hinamizawa-downporter/src/orig.py

100 lines
3.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2024-02-18 11:44:16 +00:00
import os
from unidecode import unidecode
2024-02-18 11:44:16 +00:00
import config
import parser
2024-02-18 16:11:31 +00:00
import flow
japanese_ranges = [
(0x4E00, 0x9FFF), # Kanji
(0x3040, 0x309F), # Hiragana
(0x30A0, 0x30FF), # Katakana
(0xFF00, 0xFFEF), # Full-width Roman characters and symbols
(0x3000, 0x303F), # CJK symbols and punctuation (including 「」)
]
2024-02-18 16:11:31 +00:00
def process_sections():
2024-02-18 11:44:16 +00:00
output_filepath = os.path.join(config.get('output_path'), 'out.txt')
outfile = open(output_filepath, 'w', encoding='shift_jisx0213')
2024-02-18 16:11:31 +00:00
origfile = open(config.get('original_path'), 'r', encoding='shift_jisx0213')
sections = dict()
for line in origfile:
if line.startswith('*'):
section_name = line[1:].split(' ', 1)[0].replace('\n','')
outfile.write(line)
if section_name in flow.onik:
print("entering", section_name)
write_translated(
outfile,
origfile,
flow.onik[section_name],
)
else:
outfile.write(line)
outfile.close()
origfile.close()
# Given a set of translation files, the original file and the output file
# replace the japanese lines with the translated ones in a given section.
def write_translated(outfile, origfile, translation_file_paths):
for transfilepath in translation_file_paths:
print(f'- reading "{transfilepath}"')
structure = parser.parse_to_structure(transfilepath)
2024-02-18 16:11:31 +00:00
for i, line in enumerate(origfile):
found = False
for start, end in japanese_ranges:
if start <= ord(line[0]) <= end:
found = True
2024-02-18 16:11:31 +00:00
if found:
# The amount of lines may not coincide because the original
# might have one line for what we have multiple lines. Count
# the number of appearances of the end-of-command symbols
# (@ and ¥) to determine how many of the translated lines is
# equivalent to the given original line.
amount = line.count("@") + line.count("¥") + line.count('/')
outfile.write('`')
2024-02-18 16:11:31 +00:00
_printed_line = ""
while True:
if amount <= 0:
break
if structure[0][3] == 'Line_ContinueAfterTyping':
amount += 1
_printed_line += structure[0][2]
outfile.write(
2024-02-18 16:11:31 +00:00
unidecode(structure.pop(0)[1]).replace("\\", "¥")
)
amount -= 1
outfile.write('\n')
print("\n-", transfilepath)
print(">", _printed_line)
print("<", line, end='')
# Used up all of the structures, this chapter has ended.
# Got to the next one
if len(structure) <= 0:
break
else:
outfile.write(line)