Skip to content

Instantly share code, notes, and snippets.

@jwg4
Created October 19, 2020 21:37
Show Gist options
  • Save jwg4/a9ca3bba32f973be43048dabf39e7a7d to your computer and use it in GitHub Desktop.
Save jwg4/a9ca3bba32f973be43048dabf39e7a7d to your computer and use it in GitHub Desktop.
Transforming plaintext to latex
import logging
import re
import sys
REPLACEMENTS = {
"’": "'",
"—": "--",
"“": "``",
"”": "''",
"…": "{\\ldots}",
"‘": "`",
"ä": "\\={a}",
"&": "\\&",
"$": "\\$",
}
TRANSFORMATIONS = {
r"^Chapter ([0-9]+)[ ]*$": r"\chapter*{\1}",
r"^PART ([A-Z]+)[ ]*$": r"\part*{\1}",
}
def prepare(line):
for s in REPLACEMENTS:
line.replace(s, REPLACEMENTS[s])
line = line.strip()
for pattern in TRANSFORMATIONS:
line = re.sub(pattern, TRANSFORMATIONS[pattern], line)
return line + '\n'
def warn_on_ascii(line):
for c in line:
try:
x = c.encode('ascii')
except UnicodeEncodeError:
print('line = line.replace("%s", "???")' % (c, ))
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
with open(sys.argv[1], 'r', encoding='utf-8') as f:
with open(sys.argv[2], 'w', encoding='utf-8') as ff:
for line in f.readlines():
output = prepare(line)
logging.debug(repr(line), repr(output))
ff.write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment