-
Notifications
You must be signed in to change notification settings - Fork 5
/
beautifier.py
43 lines (36 loc) · 1.39 KB
/
beautifier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import re, os, sys
doc_dollar_parity = 0
problems = False
def red(s):
return '\033[31m' + s + '\033[39m'
with open (sys.argv[1], 'r') as f:
for i, line in enumerate(f):
line_dollar_parity = line.count('$') % 2
if (doc_dollar_parity == 0 and line_dollar_parity == 1):
problems = True
head, tail = line.rsplit('$', 1)
print('%04d: %s%s' % (i+1, head, red('$' + tail)), end="")
if doc_dollar_parity == 1 and line_dollar_parity == 0:
problems = True
if line.count('$') == 0:
print('%04d: %s' % (i+1,red(line)), end="")
else:
head, tail = line.split('$', 1)
mid, tail = tail.rsplit('$', 1)
print('%04d: %s%s%s' % (i+1, red(head + '$'), mid, red('$' + tail)), end="")
if doc_dollar_parity == 1 and line_dollar_parity == 1:
problems = True
head, tail = line.split('$', 1)
print('%04d: %s%s' % (i+1, head, tail), end="")
doc_dollar_parity += line_dollar_parity
doc_dollar_parity %= 2
if problems:
sys.exit("Unmatched $'s found.")
good_delims = lambda x: re.sub(r"\$(.*?)\$", r"\\(\1\\)", x, flags = re.M)
with open (sys.argv[1], 'r+' ) as f:
content = f.read()
content = good_delims(content)
f.seek(0)
f.write(content)
f.truncate()
os.system("latexindent -w " + sys.argv[1])