-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompile.py
More file actions
executable file
·121 lines (117 loc) · 4.14 KB
/
Copy pathcompile.py
File metadata and controls
executable file
·121 lines (117 loc) · 4.14 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import subprocess
import sys
import os
import shutil
import wand.image
import re
import math
import json
import argparse
def compile(entry):
entry_file = os.path.basename(entry)
entry_directory = os.path.dirname(entry)
directory = os.path.join(entry_directory, 'tmp')
os.makedirs(directory, exist_ok=True)
retcode = subprocess.Popen(['pdflatex', '-interaction=nonstopmode', '-8bit', '-output-directory={}'.format(os.path.abspath(directory)), '-jobname=output', entry_file], stdout=None, stderr=None, cwd=entry_directory).wait()
if retcode!=0:
raise subprocess.CalledProcessError(retcode, 'pdflatex')
with open(os.path.join(directory, 'output.aux')) as f:
aux = f.read()
points = [(x, float(y)) for (x, y) in re.findall(r'\\zref\@newlabel\{([^\}]+)\}\{\\posy\{(\d+)\}\}', aux)]
image = wand.image.Image(resolution=300, filename=os.path.join(directory, 'output.pdf'))
y0 = points[0][1]
y1 = points[-1][1]
points = [(x, int(math.ceil((image.size[1])*float(y0-y)/float(y0-y1)))) for x, y in points]
config = [x[len('config='):] for x, _ in points if x.startswith('config=')]
config = dict([(x.split('=')[0], '='.join(x.split('=')[1:])) for x in config])
points = [(x, y) for x, y in points if not x.startswith('config=')]
dname = entry_file
if dname.rindex('.')!=-1:
dname = dname[:dname.rindex('.')]
dname = 'sets/{}'.format(dname)
obj = {}
if os.path.exists(dname):
shutil.rmtree(dname)
os.makedirs(dname, exist_ok=True)
obj['name'] = config.get('name', entry_file)
obj['contents'] = []
points = [(x, y0, y1) for ((x, y0), (_, y1)) in zip(points[:-1], points[1:])]
part = {}
def push():
if part.get('type') is not None:
obj['contents'].append(part.copy())
part.clear()
part['imgs'] = []
push()
all_imgs = []
for x, y0, y1 in points:
def append(is_hint=False, is_solution=False):
if y1!=y0:
all_imgs.append('{}/{}.png'.format(dname, len(all_imgs)))
img = image[:, y0:y1]
img.format='png'
img.save(filename=all_imgs[-1])
if is_hint:
part['hint'] = all_imgs[-1]
elif is_solution:
part['solution'] = all_imgs[-1]
else:
part['imgs'].append(all_imgs[-1])
def hint():
append(is_hint=True)
def solution():
append(is_solution=True)
if x=='begin_document':
push()
part['type']='static'
append()
elif x=='begin_choices':
push()
part['type']='single'
hint()
elif x=='begin_multi':
push()
part['type']='multi'
part['answer'] = []
hint()
elif x=='begin_freeform':
push()
part['type']='freeform'
append()
elif x in ['end_choices', 'end_multi', 'end_freeform']:
push()
part['type']='static'
append()
elif x.startswith('ans_equals@'):
append()
part['answer'] = x[len('ans_equals@'):]
elif x.startswith('solution'):
solution()
elif x=='true_choice':
if part['type']=='single':
part['answer']=len(part['imgs'])
elif part['type']=='multi':
part['answer'].append(len(part['imgs']))
append()
elif x=='false_choice':
append()
elif x=='hint':
hint()
push()
with open(os.path.join(dname, 'meta.js'), 'w') as f:
f.write('sets.push({})'.format(json.dumps(obj)))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--sets", type=str, default='')
args = parser.parse_args()
if args.sets:
sets = args.sets.split(' ')
else:
sets = os.listdir('latex/sets')
for s in sets:
try:
int(s)
path = os.path.abspath(os.path.join('latex/sets', s, 'set{}.tex'.format(s)))
compile(path)
except ValueError:
pass