legacy-code-converter/latex_legacy_converter.py

81 lines
2.3 KiB
Python
Raw Normal View History

2022-02-16 17:23:29 +01:00
from constants import *
2022-02-16 14:47:29 +01:00
import re
from pathlib import Path
def _migrate_lazy_math_alphabet(line: str, letters: str, short: str, alph: str):
for letter in letters:
while True:
2022-02-16 16:57:13 +01:00
if r'\fS' in line:
pass
m = re.search(r'\\%s%s(?![a-zA-Z@])' % (short, letter), line)
2022-02-16 14:47:29 +01:00
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:
2022-02-16 16:57:13 +01:00
for short, alph in ALPHABETS.items():
2022-02-16 14:47:29 +01:00
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:
2022-02-16 17:23:29 +01:00
if '' in line:
foo = 2
2022-02-16 14:47:29 +01:00
for old, new in DEPRECATED.items():
while True:
old = old.replace('\\', r'\\')
2022-02-16 16:57:13 +01:00
m = re.search(old + '(?![a-zA-Z@])', line)
2022-02-16 14:47:29 +01:00
if m is None:
break
else:
line = line[:m.start()] + new + line[m.end():]
return line
2022-02-16 17:23:29 +01:00
def replace_unicode(line: str) -> str:
for old, new in UNICODE.items():
line = line.replace(old, r'\ensuremath{%s}' % new)
return line
2022-02-16 14:47:29 +01:00
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)
2022-02-16 17:23:29 +01:00
line = replace_unicode(line)
2022-02-16 14:47:29 +01:00
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__":
2022-02-16 16:57:13 +01:00
here = Path('.').resolve()
files = here.rglob('*.tex')
for file in files:
2022-02-16 14:47:29 +01:00
convert(Path(file))