-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinter.py
75 lines (52 loc) · 2.28 KB
/
linter.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import re
import time
import shutil
import sys, os
# for making tex *** fancier ***
DEFAULT_OUTPUT_DIRECTORY = "./conspects/"
if len(sys.argv) <= 1:
print('Enter source filename as command argument')
exit(1)
show_warnings = False
if '--warning' in sys.argv or '-w' in sys.argv:
show_warnings = True
linted_output = None
if '--linted-output' in sys.argv or '-l' in sys.argv:
ind = sys.argv.index('-l') if '-l' in sys.argv else sys.argv.index('--linted-output')
if ind + 1 < len(sys.argv):
linted_output = sys.argv[ind + 1]
output_directory = None
if '--output-directory' in sys.argv or '-o' in sys.argv:
ind = sys.argv.index('-o') if '-o' in sys.argv else sys.argv.index('--output-directory')
if ind + 1 < len(sys.argv):
output_directory = sys.argv[ind + 1]
filename = sys.argv[1]
folder = os.path.join(*os.path.split(filename)[:-1])
file = open(filename, encoding='utf8').read()
file = "%% THIS FILE IS GENERATED AUTOMATICALLY BY linter.py, ALL CHANGES WILL BE LOST\n\n\n" + file
to_display = True
for i in list(re.finditer(r'(\$.+?\$)|(%nodisplay)|(%yesdisplay)', file, re.MULTILINE | re.DOTALL)):
if '%nodisplay' in i.group(0):
to_display = False
if '%yesdisplay' in i.group(0):
to_display = True
if not to_display:
continue
if r'\displaystyle' in i.group(0):
continue
if any(map(lambda x: x in i.group(0), [r'\frac', r'\sum', r'\int', r'\iint', r'\iiint', '^', '_', r'\lim'])):
file = file.replace(i.group(0), r'$\displaystyle ' + i.group(0)[1:])
if not os.path.exists('linted'):
os.mkdir('linted')
if linted_output is None:
linted_output = os.path.join('linted', os.path.split(filename)[-1])
open(linted_output, 'w', encoding='utf8').write(file)
interaction = 'nonstopmode' if show_warnings else 'batchmode'
print(f'\n\nRendering {filename}...\n')
start_time = time.time()
os.system(f"pdflatex -file-line-error -interaction={interaction} -synctex=1 -output-format=pdf "
f"-output-directory=\"{os.path.join(DEFAULT_OUTPUT_DIRECTORY, folder) if output_directory is None else output_directory}\" "
f"-aux-directory=./auxil/ "
f"{linted_output} "
f"-file-line-error")
print(f'\nRender of {filename} completed in {round(time.time() - start_time, 2)} s!')