Modernise revealjs build.py (still not good)
This commit is contained in:
parent
425640e2b5
commit
97a2c2a822
|
@ -9,75 +9,85 @@ from commonmark import commonmark
|
||||||
|
|
||||||
# "template macros"
|
# "template macros"
|
||||||
template_replace = [
|
template_replace = [
|
||||||
('<notes>', '<aside class="notes">\n'),
|
("<notes>", '<aside class="notes">\n'),
|
||||||
('</notes>', '\n</aside>'),
|
("</notes>", "\n</aside>"),
|
||||||
]
|
]
|
||||||
|
|
||||||
try:
|
directory = os.path.dirname(sys.argv[1])
|
||||||
directory = os.path.dirname(sys.argv[1])
|
|
||||||
print(f'Processing {directory}…')
|
|
||||||
except IndexError:
|
|
||||||
print(f'USAGE: {sys.argv[0]} path/to/index.html', file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
template_env = Environment(loader=FileSystemLoader(os.path.join(directory, '../_templates')))
|
template_env = Environment(
|
||||||
|
loader=FileSystemLoader(os.path.join(directory, "../_templates"))
|
||||||
|
)
|
||||||
|
|
||||||
if not os.path.isfile(os.path.join(directory, 'index.md')):
|
if not os.path.isfile(os.path.join(directory, "index.md")):
|
||||||
print(f'{directory} is not valid: missing index.md')
|
raise Exception("{directory} is missing index.md")
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
files = ['index.md']
|
files = ["index.md"]
|
||||||
files.extend(sorted(filter(lambda x: os.path.isfile(os.path.join(directory, x)) and re.match('.*\.md$', x) and x != 'index.md', os.listdir(directory))))
|
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"
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
'theme': '../_assets/reveal.js/css/theme/black.css',
|
"theme": "../_assets/reveal.js/css/theme/black.css",
|
||||||
'mathjax': False,
|
"mathjax": False,
|
||||||
'controls': True,
|
"controls": True,
|
||||||
'progress': True,
|
"progress": True,
|
||||||
'history': True,
|
"history": True,
|
||||||
'center': True,
|
"center": True,
|
||||||
'show_notes': False, # on print
|
"show_notes": False, # on print
|
||||||
'width': 1280,
|
"width": 1280,
|
||||||
'height': 800,
|
"height": 800,
|
||||||
'transition': 'none', # none, fade, slide, convex, concave, zoom
|
"transition": "none", # none, fade, slide, convex, concave, zoom
|
||||||
'background_transition': 'none'
|
"background_transition": "none",
|
||||||
}
|
}
|
||||||
content = ""
|
content = ""
|
||||||
|
|
||||||
for idx, file in enumerate(files):
|
for idx, file in enumerate(files):
|
||||||
with open(os.path.join(directory, file), 'r') as f:
|
with open(os.path.join(directory, file), "r") as f:
|
||||||
parts = f.read().split('---\n')
|
parts = f.read().split("---\n")
|
||||||
|
|
||||||
if idx == 0:
|
if idx == 0:
|
||||||
config = {**config, **yaml.safe_load(parts[0])}
|
config = {**config, **yaml.safe_load(parts[0])}
|
||||||
parts = parts[1:]
|
parts = parts[1:]
|
||||||
|
|
||||||
section_content = '<section>'
|
section_content = "<section>"
|
||||||
|
|
||||||
for slide in parts:
|
for slide in parts:
|
||||||
for r in template_replace:
|
for r in template_replace:
|
||||||
slide = slide.replace(r[0], r[1])
|
slide = slide.replace(r[0], r[1])
|
||||||
|
|
||||||
# parse config
|
# parse config
|
||||||
slide_config = slide.split('\n')[0]
|
slide_config = slide.split("\n")[0]
|
||||||
slide = '\n'.join(slide.split('\n')[1:])
|
slide = "\n".join(slide.split("\n")[1:])
|
||||||
if slide_config != '':
|
if slide_config != "":
|
||||||
slide_config = re.split(' (?=(?:[^"]|"[^"]*")*$)', slide_config)
|
slide_config = re.split(' (?=(?:[^"]|"[^"]*")*$)', slide_config)
|
||||||
slide_config = list(map(lambda x: x if re.match('^style=.*', x) else f'data-{x}', slide_config)) # data-
|
slide_config = list(
|
||||||
slide_config = ' ' + ' '.join(slide_config)
|
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}>'
|
slide_content = f"<section{slide_config}>"
|
||||||
slide_content += commonmark(slide)
|
slide_content += commonmark(slide)
|
||||||
slide_content += '</section>'
|
slide_content += "</section>"
|
||||||
|
|
||||||
if slide_content != '<section></section>':
|
if slide_content != "<section></section>":
|
||||||
section_content += slide_content
|
section_content += slide_content
|
||||||
|
|
||||||
section_content += '</section>'
|
section_content += "</section>"
|
||||||
|
|
||||||
if section_content != '<section></section>':
|
if section_content != "<section></section>":
|
||||||
content += section_content
|
content += section_content
|
||||||
|
|
||||||
with open(os.path.join(directory, 'index.html'), 'w') as f:
|
with open(os.path.join(directory, "index.html"), "w") as f:
|
||||||
f.write(template_env.get_template('index.html.j2').render(config, content=content))
|
f.write(template_env.get_template("index.html.j2").render(config, content=content))
|
||||||
print(f'done')
|
|
||||||
|
|
Reference in a new issue