This repository has been archived on 2022-03-27. You can view files and clone it, but cannot push or open issues/pull-requests.
presis/revealjs/build.py

78 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python3
import os
import re
import sys
import yaml
from jinja2 import Environment, FileSystemLoader
from CommonMark import commonmark
# "template macros"
template_replace = [
('<notes>', '<aside class="notes">\n'),
('</notes>', '\n</aside>'),
]
template_env = Environment(loader=FileSystemLoader('_templates'))
directories = filter(lambda x: os.path.isdir(x) and not re.match('^_', x), os.listdir('.'))
for directory in directories:
if not os.path.isfile(os.path.join(directory, 'index.md')):
print(f'{directory} is not valid: missing index.md')
continue
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))))
config = {
'theme': '../_assets/reveal.js/css/theme/black.css',
'controls': True,
'progress': True,
'history': True,
'center': True,
'show_notes': False, # on print
'width': 1280,
'height': 800,
'transition': 'slide', # none, fade, slide, convex, concave, zoom
}
content = ""
for idx, file in enumerate(files):
with open(os.path.join(directory, file), 'r') as f:
parts = f.read().split('---\n')
if idx == 0:
config = {**config, **yaml.load(parts[0])}
parts = parts[1:]
section_content = '<section>'
for slide in parts:
for r in template_replace:
slide = slide.replace(r[0], r[1])
# parse config
slide_config = slide.split('\n')[0]
slide = '\n'.join(slide.split('\n')[1:])
if 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 = ' ' + ' '.join(slide_config)
slide_content = f'<section{slide_config}>'
slide_content += commonmark(slide)
slide_content += '</section>'
if slide_content != '<section></section>':
section_content += slide_content
section_content += '</section>'
if section_content != '<section></section>':
content += section_content
with open(os.path.join(directory, 'index.html'), 'w') as f:
f.write(template_env.get_template('index.html.j2').render(config, content=content))
print(f'Processed {directory}')