Commit 085f6617 authored by Jakob Lerch's avatar Jakob Lerch
Browse files

do not ignore leading space

parent d9e4a2b1
......@@ -11,11 +11,13 @@ import utils
from pad import Etherpad, HedgedocNote, Pad
from translation import DeepL, LibreTranslate, Translation
# TODO: implement initial test of config file (if values are ok and stuff), provide fallback values, error msgs
# TODO: scheint nur bei fem-instanz zu funktionieren. bei libretranslate.de kackt das ab
# TODO: document config properly
# TODO: proper typing
# TODO: update setup.py
# TODO: tab in quelldatei führt zu 8space in destination
# TODO: übersetzung parallelisieren mit echten threads
# TODO: proper typing
# TODO: error msgs werden nicht in systemd krams geschrieben
# TODO: ordentliche setup anleitung
def main():
......@@ -124,20 +126,39 @@ def main():
def translate_with_leading_whitespace(lines: List[str]) -> List[str]:
""" translate list of strings with trailing whitespace lines:
* strip leading lines consisting of whitespace,
* add them when writing to dst_pad
* strip leading space string from each line
* add both when writing to dst_pad
This has to be done, because libretranslate ignores leading whitespace,
leading to incorrect order of the translated lines.
"""
leading_space_lines = list(
# filter leading space lines
space_lines = list(
takewhile(lambda line: line.isspace() or not line, lines))
text_to_translate = '\n'.join(
lines[len(leading_space_lines):])
# filter leading space strings of each line
lstrings: List[str] = []
lines_to_translate: List[str] = []
for line in lines[len(space_lines):]:
# compute leading strings
lchars = list(
takewhile(lambda character: character.isspace(), line))
lstring = ''.join(lchars)
lstrings.append(lstring)
# create lines where leading space strings are stripped
lines_to_translate.append(line[len(lchars):])
# translate it
text_to_translate = '\n'.join(lines_to_translate)
translated_lines = t.translate(text_to_translate, src_lang,
dst_lang).splitlines()
return leading_space_lines + translated_lines
# append leading strings per line
translated_lines_lspace: List[str] = []
for space, line in zip(lstrings, translated_lines):
translated_lines_lspace.append(space + line)
return space_lines + translated_lines_lspace
dst_pad.write("initializing...")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment