-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.py
187 lines (114 loc) · 3.95 KB
/
compile.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from argparse import ArgumentParser
from collections import deque
import json
import html
from pathlib import Path
import re
import shutil
from subprocess import run
from sys import stderr
from textwrap import dedent
from util import valid_slug
parser = ArgumentParser()
parser.add_argument('contest')
args = parser.parse_args()
print(f"CONTEST IS {args.contest}")
if not args.contest:
raise RuntimeError("invalid contest")
contest_path = Path(args.contest)
if not contest_path.is_dir():
raise RuntimeError(f"{contest_path} must be a directory")
print("\nLOADING CONFIG")
with (contest_path / 'config.json').open() as f:
config = json.load(f)
print(f"Contest name is {config['contest_name']}")
root = contest_path / Path('compiled')
print(f"\nCLEARING {root = !s}")
if root.is_dir(): shutil.rmtree(root)
root.mkdir(parents=True)
(root / 'raw').mkdir(parents=True)
problem_data = {}
slugs = []
for problem in config['problems']:
slug = problem['slug']
print(f"\nPROBLEM {slug}")
label = problem['label']
print(f"{label = !s}")
title = problem['title']
print(f"{title = !s}")
etitle = html.escape(title)
print(f"{etitle = !s}")
if not valid_slug(slug):
raise RuntimeError(f"Invalid slug: {slug!r}")
slugs.append(slug)
with (contest_path / 'raw' / f'{slug}.md').open() as f:
statement = f.read()
if slug in problem_data:
raise RuntimeError(f"Duplicate problem {slug!r}")
problem_data[slug] = {
'label': label,
'title': title,
'etitle': etitle,
'statement': statement,
}
with (root / 'raw' / f'{slug}.md').open('w') as f:
print(f"<!-- TITLE: {etitle} -->\n\n", file=f)
f.write(statement)
combined_md = root / 'combined' / 'combined.md'
print(f"\nCOMBINING TO {combined_md}")
breakstr = '<br>'
(root / 'combined').mkdir(parents=True)
with combined_md.open('w') as f:
for slug in slugs:
problem = problem_data[slug]
print(f"WRITING {problem['label']}: {slug}: ({problem['title']})", file=stderr)
print(dedent(f"""\
<!-- NEW PROBLEM -->
<!-- {problem['label']} - {problem['etitle']} -->
# {problem['label']} – {problem['etitle']} {{.problem-title}}
<div class="problem-contents">
"""), file=f)
for lineno, line in enumerate(problem['statement'].splitlines(), 1):
lft = deque()
rgt = deque()
i, j = 0, len(line)
while (k := line.find(breakstr, i, j)) != -1:
K = k + len(breakstr)
assert line[k: K] == breakstr
if line[i: k].strip(): break
lft.append(line[i: k])
lft.append('\n\n')
i = K
while (k := line.rfind(breakstr, i, j)) != -1:
K = k + len(breakstr)
assert line[k: K] == breakstr
if line[K: j].strip(): break
rgt.appendleft(line[K: j])
rgt.appendleft('\n\n')
j = k
nline = ''.join((*lft, line[i: j], *rgt))
if line != nline:
print(f"REPLACING {breakstr} in line {lineno:>4}: {line!r}: {nline!r}")
line = nline
print(line, file=f)
print(dedent(f"""\
</div>
<!-- <div class="problem-end"></div> -->
<!-- END PROBLEM -->
"""), file=f)
template = Path('template.html')
combined_html = root / 'combined' / 'combined.html'
print(f"\nCONVERTING TO HTML {combined_html} WITH TEMPLATE {template}")
run([
'pandoc',
'-s', combined_md,
'-o', combined_html,
'--template', template,
'--metadata',
f"title={config['contest_name']}"
])
combined_pdf = root / f'{args.contest}.pdf'
print(f"\nCONVERTING TO PDF {combined_pdf}")
from weasyprint import HTML
HTML(combined_html).write_pdf(combined_pdf)
print("\nDONE")