-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.py
executable file
·411 lines (307 loc) · 10.7 KB
/
slides.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
# Requirements:
# System:
# - wkhtmltopdf
# - pdftk
# Python 3:
# - Python Markdown
# - Beautiful Soup 4
import os
import sys
import re
import json
import copy
import pdfkit
import shutil
import bs4
import markdown
import markdown.extensions.codehilite
BS4_PARSER = "lxml"
ENABLE_DEBUG_INFO = False
def log_info(message):
print("(info) %s" % message)
def log_warning(message):
print("(warn) %s" % message)
def log_error(message):
print("(error) %s" % message)
def DEBUG(message):
global ENABLE_DEBUG_INFO
if ENABLE_DEBUG_INFO:
print("(DEBUG) %s" % message)
def merge_map(dest, src):
for key, value in src.items():
dest[key] = value
# Mathjax Extension
class MathJaxPattern(markdown.inlinepatterns.Pattern):
def __init__(self):
markdown.inlinepatterns.Pattern.__init__(
self,
r'(?<!\\)(\$\$?)(.+?)\2'
)
def handleMatch(self, m):
node = markdown.util.etree.Element('mathjax')
node.text = markdown.util.AtomicString(
m.group(2) + m.group(3) + m.group(2))
return node
class MathJaxExtension(markdown.Extension):
def extendMarkdown(self, md, md_globals):
# Needs to come before escape matching because \ is pretty important in
# LaTeX
md.inlinePatterns.add('mathjax', MathJaxPattern(), '<escape')
def latex_friendly(configs=[]):
return MathJaxExtension(configs)
# Markdown parser
class MarkdownParser(object):
"""Parse markdown content to HTML content"""
def __init__(self, extensions, config):
super(MarkdownParser, self).__init__()
self.parser = markdown.Markdown(
extensions = extensions,
extension_configs = config
)
def from_string(self, content):
result = self.parser.convert(content)
return (result, self.parser.Meta)
def from_fileobj(self, reader):
result = self.parser.convert(reader.read())
return (result, self.parser.Meta)
def from_file(self, filepath):
with open(filepath) as reader:
result = self.parser.convert(reader.read())
return (result, self.parser.Meta)
def get_markdown(extensions, config):
extensions += [
"markdown.extensions.meta",
latex_friendly()
]
return MarkdownParser(extensions, config)
# HTML parser
class HTMLParser(object):
"""Parse HTML code to pdf document"""
def __init__(self, options):
super(HTMLParser, self).__init__()
self.options = options
def from_string(self, content, output):
pdfkit.from_string(
content, output,
options = self.options
)
def from_fileobj(self, reader, output):
pdfkit.from_string(
reader.read(), output,
options = self.options
)
def from_file(self, filepath, output):
pdfkit.from_file(
filepath, output,
options = self.options
)
def get_html(options):
_options = {
"page-size": "A4",
"orientation": "Landscape",
"margin-top": "0.0px",
"margin-right": "0.0px",
"margin-bottom": "0.0px",
"margin-left": "0.0px",
"encoding": "UTF-8"
}
merge_map(_options, options)
return HTMLParser(_options)
# Markdown spliter
class MarkdownSpliter(object):
"""Split markdown source into seperate pages"""
def __init__(self, work_folder):
super(MarkdownSpliter, self).__init__()
self.work_folder = work_folder
def _title_matcher(self, content):
content = content.lstrip()
return content.startswith("#") and not content.startswith("##")
def _seperator_matcher(self, content):
content = content.strip()
return len(content) >= 3 and content.strip("-=") == ""
def _codeblock_matcher(self, content):
return content.lstrip().startswith("```")
def from_string(self, content):
lines = content.split("\n")
DEBUG(lines)
meta = []
result = []
pos = 0
# Skip meta info
while pos < len(lines):
line = lines[pos]
pos += 1
if self._title_matcher(line):
DEBUG("At #%s matched title." % pos)
result.append([line])
break
else:
meta.append(line)
# Seperate markdown source
quiet = False
while pos < len(lines):
line = lines[pos]
pos += 1
if self._codeblock_matcher(line):
DEBUG("At #%s matched codeblock." % pos)
quiet = not quiet
result[-1].append(line)
elif quiet:
result[-1].append(line)
continue
elif self._title_matcher(line):
DEBUG("At #%s matched title." % pos)
result.append([line])
elif self._seperator_matcher(line):
DEBUG("At #%s matched seperator." % pos)
result.append(copy.copy(result[-1]))
# Skip next emtpy line
if pos < len(lines) and lines[pos].strip() == "":
pos += 1
else:
result[-1].append(line)
return (
"%s" % "\n".join(meta),
["\n".join(x) for x in result]
)
def from_file(self, filepath):
base = os.path.basename(filepath)
with open(filepath) as reader:
meta, contents = self.from_string(reader.read())
idx = 0
for content in contents:
with open(os.path.join(self.work_folder, "%s.%s" % (base, idx)), "w") as writer:
writer.write(meta)
writer.write("\n")
writer.write(content)
idx += 1
return (base, idx)
# Template manager
class TemplateManager(object):
"""Manage the required templates"""
def __init__(self, work_folder, content_folder):
super(TemplateManager, self).__init__()
self.work_folder = work_folder
self.content_folder = content_folder
self.templates = {}
self.stylesheets = set()
def load(self, template):
if template in self.templates:
return
with open(os.path.join(self.content_folder, template)) as reader:
self.templates[template] = reader.read()
document = bs4.BeautifulSoup(self.templates[template], BS4_PARSER)
for x in document.find_all("link", attrs = {"rel": "stylesheet"}):
css = x["href"]
if not css in self.stylesheets:
self.stylesheets.add(css)
shutil.copy(
os.path.join(self.content_folder, css),
os.path.join(self.work_folder, css)
)
def get(self, template):
return self.templates[template]
def main():
global ENABLE_DEBUG_INFO
if "--debug" in sys.argv:
log_warning("Debug mode enabled.")
ENABLE_DEBUG_INFO = True
sys.argv.remove("--debug")
if len(sys.argv) < 2:
print("Usage: %s [SLIDES FOLDER] [--debug]" % (sys.argv[0]))
exit(-1)
name = sys.argv[1]
log_info("Processing %s..." % name)
log_info("Reading configuration...")
if not os.path.exists("{0}/{0}.json".format(name)):
log_error("No configuration file: %s" % "{0}/{0}.json".format(name))
exit(-1)
config = None
with open("{0}/{0}.json".format(name)) as reader:
config = json.load(reader)
temporary = "%s/.build/" % name
log_info("Set temporary folder to %s" % temporary)
if not os.path.exists("%s" % temporary):
log_info("Folder not created. Create a new one.")
os.mkdir("%s" % temporary)
log_info("Spliting files...")
spliter = MarkdownSpliter(temporary)
worklist = []
for file in config["input"]:
log_info("Spliting '%s'..." % file)
data = spliter.from_file("%s/%s" % (name, file))
worklist.append(data)
log_info("Parsing pages...")
markdown_parser = get_markdown(
config["markdown_extensions"],
config["markdown_config"]
)
htmllist = []
for filename, count in worklist:
for idx in range(0, count):
target = os.path.join(temporary, filename) + "." + str(idx)
log_info("Parsing %s..." % target)
data = markdown_parser.from_file(target)
htmllist.append(data)
DEBUG(htmllist)
log_info("Preparing files and folders...")
for file in config["prepare_files"]:
src = os.path.join(name, file)
dest = os.path.join(temporary, file)
if os.path.exists(dest):
os.remove(dest)
shutil.copy(src, dest)
for folder in config["prepare_folders"]:
src = os.path.join(name, folder)
dest = os.path.join(temporary, folder)
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.copytree(src, dest)
log_info("Printing pages...")
manager = TemplateManager(temporary, name)
pagelist = []
idx = 0
for content, metainfo in htmllist:
temp_config = copy.copy(config["options"])
# Because mathjax is too slow...
if config["no_mathjax_auto_delay"] > 0 and not "<mathjax>" in content:
log_info("'no_mathjax_auto_delay' enabled.")
temp_config["javascript-delay"] = config["no_mathjax_auto_delay"]
html_parser = get_html(temp_config)
log_info("Printing... [%s / %s]" % (idx + 1, len(htmllist)))
# Extract HTML info
document = bs4.BeautifulSoup(content, BS4_PARSER)
title = document.h1.text # Each slide has only one h1 header
# Each slide must have a template
if not "template" in metainfo:
log_error("Each slide must have a template")
exit(-1)
template = metainfo["template"][0]
manager.load(template) # Only select the first one
content = manager.get(template).format(
title = title,
content = content
)
target_html = os.path.join(temporary, "%s.html" % idx)
with open(target_html, "w") as writer:
writer.write(content)
target = os.path.join(temporary, "%s.pdf" % idx)
html_parser.from_file(target_html, target)
pagelist.append(target)
idx += 1
log_info("Connecting printed pages...")
os.system(
"pdftk %s cat output %s" % (
" ".join(pagelist),
os.path.join(temporary, config["output"])
)
)
log_info("Copy the result to the work folder.")
shutil.copy(
os.path.join(temporary, config["output"]),
os.path.join(name, config["output"])
)
log_info("slides.py exited.")
if __name__ == "__main__":
main()