Commenting, formatting and slight refactoring.

This commit is contained in:
Kremitzl 2019-12-17 17:23:02 +01:00
parent f624aebd18
commit 67b8c2301a

View File

@ -1,9 +1,11 @@
# Our python script goes here. #!/usr/bin/python3
import yaml import yaml
import pypandoc import pypandoc
import re import re
# Compiles a presentation in the given language from the given directory and
# stores it in a corresponding slides.lang.html file inside the same directory.
def compile(directory, language='en'): def compile(directory, language='en'):
global root global root
root = directory root = directory
@ -16,6 +18,8 @@ def compile(directory, language='en'):
output.write(wrapper) output.write(wrapper)
print('done') print('done')
# Replaces the placeholders in the given content with corresponding values in
# the given language from the meta.yml and returns the filled content.
def insert_metadata(content, lang=None): def insert_metadata(content, lang=None):
metadata = read_yaml('meta.yml') metadata = read_yaml('meta.yml')
for key, value in metadata.items(): for key, value in metadata.items():
@ -26,6 +30,8 @@ def insert_metadata(content, lang=None):
content = content.replace(placeholder, filler) content = content.replace(placeholder, filler)
return content return content
# Compiles a presentation's chapters (the content without the wrapper)
# according to slides.yml and returns them as one string of <section>s.
def compile_chapters(): def compile_chapters():
structure = read_yaml('slides.yml') structure = read_yaml('slides.yml')
chapters = "" chapters = ""
@ -33,19 +39,24 @@ def compile_chapters():
chapters += '<section>\n' + compile_chapter(chapter) + '\n</section>' chapters += '<section>\n' + compile_chapter(chapter) + '\n</section>'
return chapters # string return chapters # string
# Compiles a presentation's chapter (given its name) by splitting it into
# slides, compiling those, and returning them as a string of <article>s.
def compile_chapter(chapter): def compile_chapter(chapter):
if type(chapter) == str: # plain chapter names if type(chapter) != str:
chapter_input = '\n' + open(root + 'slides/' + chapter + '.' + lang + '.md', 'r').read() raise Exception('Chapters with attributes are not supported yet.\n' +
delimiter = '\n@slide' str(chapter))
slides = [delimiter + slide for slide in chapter_input.split(delimiter)][1:] chapter_file = open(root + 'slides/' + chapter + '.' + lang + '.md', 'r')
print(slides) delimiter = '\n@slide'
chapter = '' chapter_input = ('\n' + chapter_file.read()).split(delimiter)
for slide in slides: slides = [delimiter + slide for slide in chapter_input][1:]
chapter += '<article>\n' + compile_slide(slide) + '\n</article>\n' chapter = ''
return chapter # string for slide in slides:
else: chapter += '<article>\n' + compile_slide(slide) + '\n</article>\n'
raise Exception('Chapters with attributes are not supported yet.\n' + str(chapter)) return chapter
# Compiles a single slide by splitting it up into single metadata and data
# attributes, loading them into the corresponding template, and returning the
# result.
def compile_slide(slide): def compile_slide(slide):
slide = slide.strip() slide = slide.strip()
slide_metadata = get_slide_metadata(slide) slide_metadata = get_slide_metadata(slide)
@ -57,8 +68,11 @@ def compile_slide(slide):
print('replace', placeholder, 'with', filler) print('replace', placeholder, 'with', filler)
if '@' + key in slide: if '@' + key in slide:
slide = slide.replace(placeholder, filler) slide = slide.replace(placeholder, filler)
return slide # string return slide
# Parses the metadata passage of a given slide and returns the metadata as a
# dictionary. For some items, default values are used if the slide does not
# contain them.
def get_slide_metadata(slide): def get_slide_metadata(slide):
metadata = { 'layout': 'default' } metadata = { 'layout': 'default' }
metadata_attributes = re.search('^@slide\((.+)\)', slide.splitlines()[0]) metadata_attributes = re.search('^@slide\((.+)\)', slide.splitlines()[0])
@ -66,24 +80,32 @@ def get_slide_metadata(slide):
metadata_attributes = metadata_attributes.group(1).split(' ') metadata_attributes = metadata_attributes.group(1).split(' ')
for attribute in metadata_attributes: for attribute in metadata_attributes:
metadata[attribute.split('=')[0]] = attribute.split('=')[1] metadata[attribute.split('=')[0]] = attribute.split('=')[1]
print("\nMETADATA:\n"+str(metadata)+"\n\n")
return metadata return metadata
# Parses the data part of a given slide and returns dictionary of field names
# and corresponding raw values.
def get_slide_data(slide): def get_slide_data(slide):
if slide.startswith('@slide'): if slide.startswith('@slide'):
slide = slide[slide.find('\n')+1:] # cut off @slide declaration slide = slide[slide.find('\n')+1:] # cut off @slide declaration
slide_data = { data.split()[0].strip('@') : "\n".join(data.splitlines()[1:]) for data in slide.strip().split('\n\n@') } slide_data = slide.strip().split('\n\n@')
slide_data = { item.split()[0].strip('@') : "\n".join(item.splitlines()[1:]) for item in slide_data }
return slide_data return slide_data
# Reads the layout file for a given layout name and returns its content as a
# string.
def get_slide_layout(layout_name): def get_slide_layout(layout_name):
try: try:
return open(root + 'layouts/' + layout_name + '.html', 'r').read() return open(root + 'layouts/' + layout_name + '.html', 'r').read()
except FileNotFoundError: except FileNotFoundError:
raise Exception('Layout ' + layout_name + ' not found!') raise Exception('Layout ' + layout_name + ' not found!')
# Calls the pandoc converter to convert a given Markdown string to HTML.
# Returns an HTML string.
def convert_slide_content(content): def convert_slide_content(content):
return pypandoc.convert_text(content, 'html', format='md') return pypandoc.convert_text(content, 'html', format='md')
# Calls a YAML parser for the given relativ path inside the presentation root
# directory. Returns a dictionary with the YAML content.
def read_yaml(path): def read_yaml(path):
with open(root + path, 'r') as stream: with open(root + path, 'r') as stream:
try: try:
@ -93,3 +115,4 @@ def read_yaml(path):
compile('../test/', language='de') compile('../test/', language='de')
compile('../test/', language='en') compile('../test/', language='en')