-
Notifications
You must be signed in to change notification settings - Fork 2
/
render
executable file
·315 lines (269 loc) · 10.1 KB
/
render
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
import argparse
import math
import logging
import os
from pathlib import Path
import re
import shlex
import shutil
import subprocess
import sys
from tempfile import TemporaryDirectory
import yaml
import zipfile
# non standard libraries
from jinja2 import Environment, FileSystemLoader
options = {
"block_start_string": "<+++",
"block_end_string": "+++>",
"variable_start_string": "<++",
"variable_end_string": "++>",
"comment_start_string": "{=",
"comment_end_string": "=}",
}
section_re = re.compile("\\\section\{")
infile_re = re.compile(r"(\\input)|(\\include)")
extract_re = re.compile(r"(?<=\{).+?(?=\})")
PATH = os.path.split(os.path.realpath(__file__))[0]
jinja_env = Environment(loader=FileSystemLoader(PATH), **options)
def edition_from_tag():
"""
sets the edition from the latests tag
this assumes that the tag is the set per release
"""
cmd = "git tag -l | tail -1"
return subprocess.getoutput(cmd)
def screen_master(masterdoc, tempdir):
prefix = os.path.join(tempdir, "slides")
fnames = list()
with open(os.path.join(prefix, masterdoc)) as infile:
for line in infile:
if infile_re.match(line):
fnames.extend(extract_re.findall(line))
return fnames
def count_matching_lines(fp):
count = 0
for line in fp:
if section_re.match(line):
count += 1
return count
def count_sections(file_list, tempdir):
"""
counting '\section(*)' lines by calling
`count_matching_lines` - the function will
operate in the temporary directory, to ensure
sections introduced after parsing the configuration
are counted.
input:
- `file_list` - the list of files need to typeset
the master TeX document
- `tempdir` - the temporary directory
ouput:
- the total number of sections.
"""
prefix = os.path.join(tempdir, "slides")
count = 0
for fname in file_list:
to_count = os.path.join(prefix, fname) + ".tex"
try:
with open(to_count) as fp:
count += count_matching_lines(fp)
except:
print(f"Error treating counting sections in: '{fname}'")
return count
def define_boundaries(section_count):
fhe = math.floor(section_count / 2)
return {
"lower": 1,
"upper": section_count,
"first_half_end": fhe,
"second_half_start": fhe + 1,
}
def render_tex(env, template):
template = template
tpl = jinja_env.get_template(template)
x = tpl.render(env)
return x
def to_file(tex, fname, tempdir, path=""):
with open(os.path.join(tempdir, path, fname), "w") as f:
f.write(tex)
def get_pdf_name(filename):
path_object = Path(filename)
new_filename = path_object.with_suffix("." + "pdf")
return new_filename
def run_pdflatex(fname="out.tex", path=".", handout=False):
outdir = os.path.realpath(os.path.join(os.getcwd(), "slides"))
cmd = "pdflatex -synctex=1 -interaction=nonstopmode"
if handout:
cmd += r' "\def\ishandout{1} \input{' + fname + r'}"'
else:
cmd += f" {fname}"
cmd = shlex.split(cmd)
retcode = subprocess.call(cmd, cwd=path)
pdfname = get_pdf_name(fname)
shutil.copy(os.path.join(path, pdfname), os.path.join(outdir, pdfname))
return retcode
def parse_config(fname):
"""
will parse the configuration file in YAML format
an invalid yaml file, will cause the script to crash
"""
with open(fname) as configfile:
data = yaml.safe_load(configfile)
return data
def find_and_replace_sections(boundaries, section_estimate, fname):
# tex files aren't big, read all content in memory
try:
with open(fname) as infile:
lines = infile.readlines()
except:
print(f"Error reading: '{fname}'")
return
# get the matching 'currentsection' lines
first_done, second_done, minitocline = False, False, False
for lcount, line in enumerate(lines):
if not first_done:
if "currentsection" in line and not "hideothersubsections" in line:
first_done = lcount
break
if "hideothersubsections" in line:
minitocline = lcount
break
if not minitocline: # we need to count the line for the 2nd toc column
for lcount, line in enumerate(lines[first_done + 1 :]):
if not second_done:
if "currentsection" in line:
second_done = lcount + first_done + 1
break
if not minitocline and not (first_done and second_done):
logger.debug("no outline found for %s -> skipping" % fname)
return
# first render minitoc
if minitocline:
lower = max((section_estimate - 2), 1) # lower boundary not lower than 1
upper = min(
(section_estimate + 3), boundaries["upper"]
) # upper boundary not higher then upper boundary ;-)
sections = f"{lower}-{upper}"
lines[minitocline] = (
" \\tableofcontents[currentsection, sections={"
+ sections
+ "}, hideothersubsections]"
)
# adapt the two lines with our new boundaries:
elif first_done and second_done:
lines[first_done] = (
" \\tableofcontents[sections={%d-%d},currentsection]\n"
% (
boundaries["lower"],
boundaries["first_half_end"],
)
)
lines[second_done] = (
" \\tableofcontents[sections={%d-%d},currentsection]\n"
% (
boundaries["second_half_start"],
boundaries["upper"],
)
)
else:
logger.error(f"TOC expectations for '{fname}' not met")
with open(fname, "w") as outfile:
for line in lines:
outfile.write(line)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--master-tex", required=True, help="indicate the Master TeX document"
)
parser.add_argument(
"-c",
"--configfile",
default="config/config.yaml",
help="indicate the configuration YAML file - default: 'config/config.yaml'",
)
parser.add_argument(
"--handout", default=False, action=argparse.BooleanOptionalAction
)
parser.add_argument(
"-n", "--no-rerun", default=False, action=argparse.BooleanOptionalAction
)
parser.add_argument(
"--sample-directory",
help="needed with `--handout` - should indicate directory with script file (cloze and solution)",
)
args = parser.parse_args()
if args.handout and not args.sample_directory:
print(
"ERROR: must indicate --sample-directory when writing handouts",
file=sys.stderr,
)
# the master tex needs to be without the 'slides' path, because
# on the tempfs, the relative path work without
args.master_tex = os.path.basename(args.master_tex)
logger = logging.getLogger(__name__)
data = parse_config(args.configfile)
# setting the edition automatically:
data["course"]["edition"] = edition_from_tag()
with TemporaryDirectory() as tempdir:
shutil.copytree("images", os.path.join(tempdir, "images"))
# creating 'slides' directory (does not exist, yet):
Path(os.path.join(tempdir, "slides", "common")).mkdir(parents=True)
Path(os.path.join(tempdir, "slides", "users")).mkdir()
Path(os.path.join(tempdir, "slides", "creators")).mkdir()
Path(os.path.join(tempdir, "slides", "admin")).mkdir()
for root, dirs, files in os.walk("slides"):
for fname in files:
if not fname.endswith(".tex"):
continue
logger.info(f"Attempt to render {fname}")
tex = render_tex(data, os.path.join(root, fname))
to_file(tex, fname, tempdir, root)
# we might need to adapt the chapter settings.
file_list = screen_master(args.master_tex, tempdir)
count = count_sections(file_list, tempdir)
logger.info("Found %d sections." % count)
boundaries = define_boundaries(count)
# adapting chapter section display in toc slides
# NOTE: section estimate is the assumption, that we have
# one section per file. This assumption will NOT always hold
# but is good enough for our purpose of setting boundaries in minitocs.
for section_estimate, fname in enumerate(file_list):
to_adapt = os.path.join(tempdir, "slides", fname) + ".tex"
find_and_replace_sections(boundaries, section_estimate, to_adapt)
# now the typesetting needs to be triggered
run_pdflatex(
fname=args.master_tex,
path=os.path.join(tempdir, "slides"),
handout=args.handout,
)
# re-run might be required
if args.no_rerun:
print("no rerun as requested")
sys.exit()
run_pdflatex(fname=args.master_tex, path=os.path.join(tempdir, "slides"))
# only to produce handouts (together with script bundles)
if args.handout:
handout_version = (
os.path.join("slides", os.path.splitext(args.master_tex)[0]) + ".pdf"
)
final_place = os.path.basename(handout_version)
file_list = [
(handout_version, final_place),
("handout/README.txt", "README.txt"),
]
opj = os.path.join
for root, dirs, files in os.walk(args.sample_directory):
for fname in files:
# restrict to sample files, no helper files
if "copy" in fname or "README" in fname:
continue
file_list.append(
(opj(root, fname), opj(*root.split("/")[1:], fname))
)
buildzipfname = "snakemake_intro_%s.zip" % edition_from_tag()
z = zipfile.ZipFile(buildzipfname, "w", compression=zipfile.ZIP_DEFLATED)
for item in file_list:
z.write(item[0], item[1])
z.close()