forked from misc0110/beamer-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
beamer-preview.py
executable file
·341 lines (281 loc) · 9.86 KB
/
beamer-preview.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/python3
import sys
import re
import warnings
import os
import logging
import colorlog
import argparse
import time
import multiprocessing
import hashlib
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from subprocess import Popen, STDOUT, PIPE
from PyPDF2 import PdfFileMerger
logger = None
args = None
def init_logging():
global logger
formatter = colorlog.ColoredFormatter(
"%(log_color)s[%(levelname)-8s]%(reset)s %(log_color)s%(message)s%(reset)s",
datefmt=None,
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red',
},
secondary_log_colors={},
style='%'
)
handler = colorlog.StreamHandler()
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logger = colorlog.getLogger('slide-preview')
logger.addHandler(handler)
logger.setLevel(logging.INFO)
def init_parser():
parser = argparse.ArgumentParser(description="Recompile only changed TeX slides.")
parser.add_argument("--out", "-o", nargs="?", default="preview.pdf")
parser.add_argument("--compiler", "-c", nargs="?", default="pdflatex")
parser.add_argument("--ignore-errors", "-i", action="store_true", dest="ignore_errors", default=False)
parser.add_argument("--prefix", "-p", nargs="?", default="beamer.out")
parser.add_argument("--force", action="store_true", default=False)
parser.add_argument("--watch", action="store_true", default=False)
parser.add_argument("--smp", nargs="?", type=int, default=multiprocessing.cpu_count())
parser.add_argument("--compiler-option", nargs="*", default=[], dest="compiler_option")
parser.add_argument("--runs", "-r", nargs='?', default=1)
parser.add_argument("slides")
return parser
def slide_hash(content):
return hashlib.sha256(content.encode("utf-8")).hexdigest()
def has_changed(content, fname, pdf):
try:
diff = content != open(fname).read()
if diff:
return True
if not os.path.exists(pdf):
return True
except:
return True
return False
class AbortException(Exception):
pass
def fatal(msg):
logger.critical(msg)
raise AbortException()
def error(msg, exception=None):
logger.error(msg)
if not args.ignore_errors:
if not exception:
raise AbortException()
def parse_slides(tex):
header = ""
footer = ""
slide = ""
slides = []
in_header = True
in_slide = False
slide_wrapper = False
for i in range(len(tex)):
if re.match(r"\s*\\begin\s*\{\s*document\s*\}", tex[i]):
in_header = False
if not in_slide:
header = tex[i] "\n"
continue
if re.match(r"\s*\{\s*", tex[i]):
if in_slide:
slide = tex[i] "\n"
continue
slide_wrapper = True
slide = tex[i] "\n"
continue
if re.match(r"\s*\}\s*", tex[i]):
if in_slide:
slide = tex[i] "\n"
continue
if slide_wrapper:
slide = tex[i] "\n"
slides.append(slide)
slide = ""
footer = ""
slide_wrapper = False
else:
error("Unexpected closing brackets at line %d" % (i 1))
elif re.match(r"\s*\\begin\s*\{\s*frame\s*\}", tex[i]):
in_header = False
if not in_slide:
slide = tex[i] "\n"
in_slide = True
continue
else:
error("frame inside frame at line %d" % (i 1))
elif re.match(r"\s*\\section\s*{\s*\w*", tex[i]):
in_header = False
if not in_slide:
slide = ""
if len(tex[i].strip()) == 0:
error("frame without content at line %d" % (i 1))
slides.append(tex[i] "\n")
continue
else:
error("section inside frame at line %d" % (i 1))
elif re.match(r"\s*\\maketitle", tex[i]):
in_header = False
if not in_slide:
slide = ""
if len(tex[i].strip()) == 0:
error("frame without content at line %d" % (i 1))
slides.append(tex[i] "\n")
continue
else:
error("maketitle inside frame at line %d" % (i 1))
elif re.match(r"\s*\\end\s*{\s*frame\s*}", tex[i]):
if not in_slide:
error("frame end without frame begin at line %d" % (i 1))
else:
in_slide = False
slide = tex[i] "\n"
if len(slide.strip()) == 0:
error("frame without content at line %d" % (i 1))
if not slide_wrapper:
slides.append(slide)
slide = ""
footer = ""
continue
else:
if in_slide or slide_wrapper:
slide = tex[i] "\n"
continue
if not in_slide and not slide_wrapper and in_header:
header = tex[i] "\n"
continue
elif not in_slide and not slide_wrapper and not in_header:
footer = tex[i] "\n"
continue
if in_slide:
error("Missing frame end")
if in_header:
logger.warning("No slides found")
return (header, footer, slides)
def compile_slide(arg):
header, footer, slide, tex, pdf, nr = arg
logger.info("Compiling slide %d %s" % (nr, tex))
FNULL = open(os.devnull, 'w')
try:
with open(tex, "w") as out:
out.write(header)
if len(slide.strip()) == 0:
error("Empty slide (%s)" % tex)
out.write(slide)
out.write(footer)
except:
error("Could not write %s" % tex)
if os.path.isfile(pdf):
os.remove(pdf)
compile_command = [args.compiler, "--output-directory", args.prefix, "-halt-on-error"] args.compiler_option [tex]
logger.debug(compile_command)
log = ""
for i in range(int(args.runs)):
p = Popen(compile_command, stdout=PIPE, stderr=STDOUT)
out, err = p.communicate()
if out:
try:
log = out.decode("utf-8")
except:
pass
if not os.path.isfile(pdf):
logger.warning("Could not compile slide %s" % tex)
logger.warning(log)
logger.warning("---- vvvvvvvvv ----")
logger.warning(open(tex).read())
try:
with open(tex, "w") as out:
out.write("")
except:
error("Could not write %s" % tex)
def merge_slides(hashes, slide_name):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
merger = PdfFileMerger(strict=False)
for i in hashes:
slide_pdf = (slide_name % i).replace(".tex", ".pdf")
try:
merger.append(open(slide_pdf, "rb"))
except:
logger.warning("Could not add %s to the final output" % slide_pdf)
try:
with open(args.out, "wb") as pdf:
merger.write(pdf)
except Exception as e:
error("Could not write %s" % args.out, e)
def create_slides(texfile):
slide_name = "%s/%%s.tex" % args.prefix
try:
tex = open(texfile).read().split("\n")
except:
fatal("Could not open '%s'" % texfile)
header, footer, slides = parse_slides(tex)
slide_hashes = [slide_hash(header slides[i] footer) for i in range(len(slides))]
saved_hashes = os.listdir(args.prefix)
for h in saved_hashes:
try:
hash_only = h.split(".")[0]
if hash_only not in slide_hashes:
os.remove(os.path.join(args.prefix, h))
except:
pass
slide_changed = [(args.force or has_changed(header slides[i] footer, slide_name % slide_hashes[i], (slide_name % slide_hashes[i]).replace(".tex", ".pdf"))) for i in range(len(slides))]
up_to_date = True
recompile = []
cnt = 0
for i, slide in enumerate(slides):
if slide_changed[i]:
up_to_date = False
cnt = 1
recompile.append((header, footer, slide, slide_name % slide_hashes[i], (slide_name % slide_hashes[i]).replace(".tex", ".pdf"), cnt))
logger.info("Compiling %d slides on %d cores (change with --smp <cores>)" % (len(recompile), args.smp))
with multiprocessing.Pool(args.smp) as p:
p.map(compile_slide, tuple(recompile))
if up_to_date:
logger.info("Everything is up to date, no recompilation required")
else:
merge_slides(slide_hashes, slide_name)
class SlideWatch(FileSystemEventHandler):
def on_any_event(self, event):
dst = ""
try:
dst = event.dest_path
except:
pass
if dst.endswith(args.slides) or event.src_path.endswith(args.slides):
create_slides(args.slides)
logger.info("Ready")
def main():
global args
init_logging()
parser = init_parser()
args = parser.parse_args()
texfile = args.slides
if not os.path.exists(args.prefix):
os.makedirs(args.prefix)
create_slides(args.slides)
if args.watch:
event_handler = SlideWatch()
observer = Observer()
observer.schedule(event_handler, path=os.path.dirname(os.path.realpath(args.slides)), recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
try:
main()
except AbortException:
sys.exit(1)