2018-08-29 19:55:10 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2018-09-24 23:17:02 +02:00
|
|
|
from commonmark import commonmark
|
2018-08-29 19:55:10 +02:00
|
|
|
|
|
|
|
# "template macros"
|
|
|
|
template_replace = [
|
2020-03-07 18:06:33 +01:00
|
|
|
("<notes>", '<aside class="notes">\n'),
|
|
|
|
("</notes>", "\n</aside>"),
|
2018-08-29 19:55:10 +02:00
|
|
|
]
|
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
directory = os.path.dirname(sys.argv[1])
|
2018-08-30 14:48:18 +02:00
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
template_env = Environment(
|
|
|
|
loader=FileSystemLoader(os.path.join(directory, "../_templates"))
|
|
|
|
)
|
2018-08-30 14:48:18 +02:00
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
if not os.path.isfile(os.path.join(directory, "index.md")):
|
|
|
|
raise Exception("{directory} is missing index.md")
|
2018-08-30 14:48:18 +02:00
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
files = ["index.md"]
|
|
|
|
files.extend(
|
|
|
|
sorted(
|
|
|
|
[
|
|
|
|
file
|
|
|
|
for file in os.listdir(directory)
|
|
|
|
if os.path.isfile(os.path.join(directory, file))
|
|
|
|
and re.match(".*\.md$", file)
|
|
|
|
and file != "index.md"
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2018-08-30 14:48:18 +02:00
|
|
|
|
|
|
|
config = {
|
2020-03-07 18:06:33 +01:00
|
|
|
"theme": "../_assets/reveal.js/css/theme/black.css",
|
|
|
|
"mathjax": False,
|
|
|
|
"controls": True,
|
|
|
|
"progress": True,
|
|
|
|
"history": True,
|
|
|
|
"center": True,
|
|
|
|
"show_notes": False, # on print
|
|
|
|
"width": 1280,
|
|
|
|
"height": 800,
|
|
|
|
"transition": "none", # none, fade, slide, convex, concave, zoom
|
|
|
|
"background_transition": "none",
|
2018-08-30 14:48:18 +02:00
|
|
|
}
|
|
|
|
content = ""
|
|
|
|
|
|
|
|
for idx, file in enumerate(files):
|
2020-03-07 18:06:33 +01:00
|
|
|
with open(os.path.join(directory, file), "r") as f:
|
|
|
|
parts = f.read().split("---\n")
|
2018-08-30 14:48:18 +02:00
|
|
|
|
|
|
|
if idx == 0:
|
2019-03-23 21:41:46 +01:00
|
|
|
config = {**config, **yaml.safe_load(parts[0])}
|
2018-08-30 14:48:18 +02:00
|
|
|
parts = parts[1:]
|
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
section_content = "<section>"
|
2018-08-30 14:48:18 +02:00
|
|
|
|
|
|
|
for slide in parts:
|
|
|
|
for r in template_replace:
|
|
|
|
slide = slide.replace(r[0], r[1])
|
|
|
|
|
|
|
|
# parse config
|
2020-03-07 18:06:33 +01:00
|
|
|
slide_config = slide.split("\n")[0]
|
|
|
|
slide = "\n".join(slide.split("\n")[1:])
|
|
|
|
if slide_config != "":
|
2018-08-30 14:48:18 +02:00
|
|
|
slide_config = re.split(' (?=(?:[^"]|"[^"]*")*$)', slide_config)
|
2020-03-07 18:06:33 +01:00
|
|
|
slide_config = list(
|
|
|
|
map(
|
|
|
|
lambda x: x if re.match("^style=.*", x) else f"data-{x}",
|
|
|
|
slide_config,
|
|
|
|
)
|
|
|
|
) # data-
|
|
|
|
slide_config = " " + " ".join(slide_config)
|
|
|
|
|
|
|
|
slide_content = f"<section{slide_config}>"
|
2018-08-30 14:48:18 +02:00
|
|
|
slide_content += commonmark(slide)
|
2020-03-07 18:06:33 +01:00
|
|
|
slide_content += "</section>"
|
2018-08-30 14:48:18 +02:00
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
if slide_content != "<section></section>":
|
2018-08-30 14:48:18 +02:00
|
|
|
section_content += slide_content
|
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
section_content += "</section>"
|
2018-08-30 14:48:18 +02:00
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
if section_content != "<section></section>":
|
2018-08-30 14:48:18 +02:00
|
|
|
content += section_content
|
|
|
|
|
2020-03-07 18:06:33 +01:00
|
|
|
with open(os.path.join(directory, "index.html"), "w") as f:
|
|
|
|
f.write(template_env.get_template("index.html.j2").render(config, content=content))
|