commit 7a7e199f91a2edc148874727dcf0bcbda661661e Author: Maximilian Keßler Date: Wed Feb 16 14:47:29 2022 +0100 first version of legacy converter diff --git a/constants.py b/constants.py new file mode 100644 index 0000000..e0c710d --- /dev/null +++ b/constants.py @@ -0,0 +1,22 @@ +ENVIRONMENTS = { + 'prop': 'proposition', + 'satz': 'theorem', + 'kor': 'corollary', + 'bsp': 'example', + 'bem': 'remark', +} + +ALPHABETS = { + 'b': 'mathbb', + 'c': 'mathcal', + 'f': 'mathfrak', + '': 'mathbb', +} + +DEPRECATED = { + r'\eps': r'\varepsilon', + r'\inv': r'^{-1}', + r'\se': r'\subseteq', + r'\sm': r'\setminus', + r'\st': r'^{\ast}', +} diff --git a/latex_legacy_converter.py b/latex_legacy_converter.py new file mode 100644 index 0000000..d1c6e14 --- /dev/null +++ b/latex_legacy_converter.py @@ -0,0 +1,69 @@ +from constants import ENVIRONMENTS, ALPHABETS, DEPRECATED +import re +import pathlib +from pathlib import Path + + +def _migrate_lazy_math_alphabet(line: str, letters: str, short: str, alph: str): + for letter in letters: + while True: + m = re.search(r'\\%s%s(?=![a-zA-Z@])' % (short, letter), line) + if m is None: + break + else: + line = line[:m.start()] + r'\%s{%s}' % (alph, letter) + line[m.end():] + return line + + +def migrate_lazy_math_alphabets(line: str) -> str: + for short, alph in ALPHABETS: + if short == '': + line = _migrate_lazy_math_alphabet(line, 'NZQRCFK', '', alph) + else: + line = _migrate_lazy_math_alphabet(line, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', short, alph) + if short == 'f': + line = _migrate_lazy_math_alphabet(line, 'klmspqabc', short, alph) + return line + + +def migrate_deprecated(line: str) -> str: + for old, new in DEPRECATED.items(): + while True: + old = old.replace('\\', r'\\') + m = re.search(old + '(?=![a-zA-Z@])', line) + if m is None: + break + else: + line = line[:m.start()] + new + line[m.end():] + return line + + +def migrate_environments(line: str) -> str: + for old_env, new_env in ENVIRONMENTS.items(): + line = line.replace(r'\begin{%s}' % old_env, r'\begin{%s}' % new_env) + line = line.replace(r'\end{%s}' % old_env, r'\end{%s}' % new_env) + return line + + +def optimize_line(line: str) -> str: + line = migrate_environments(line) + line = migrate_lazy_math_alphabets(line) + line = migrate_deprecated(line) + return line + + +def convert(path: Path): + with path.open('r') as f: + lines = f.readlines() + newlines = [] + for line in lines: + newlines.append(optimize_line(line)) + + with path.open('w') as f: + path.write_text(''.join(newlines)) + + +if __name__ == "__main__": + here = Path('.') + for file in here.rglob('*.tex'): + convert(Path(file))