onpoint/chapters.py

63 lines
2.1 KiB
Python
Executable File

import os
import cache
import helper
import slides as slides_helper
root = ""
lang = ""
# 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(root_directory, language, force_recompile=False):
global root
root = root_directory
global lang
lang = language
structure = helper.read_yaml(root, 'slides.yml')
no_cache = cache.is_recompile_obligatory(root, lang) or force_recompile
chapters = ""
for chapter in structure:
chapters += '<section>' + os.linesep + compile_chapter(chapter, no_cache) + os.linesep + '</section>'
cache.save_compile_timestamp(root, lang)
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, no_cache):
global root
if type(chapter) != str:
raise Exception('Chapters with attributes are not supported yet.\n' +
str(chapter))
chapter_file_name = 'slides/' + chapter + '.' + lang + '.md'
chapter_path = os.path.join(root, chapter_file_name)
can_use_cache = not (
no_cache or
cache.not_cached_before(root, lang, chapter) or
cache.file_touched_since_last_compile(root, lang, chapter_file_name))
if can_use_cache:
print("Using cached version of chapter " + chapter + " (" + lang + ").")
return cache.get_cached_chapter(root, lang, chapter)
print("Recompiling chapter " + chapter + " (" + lang + ").")
chapter_file = open(chapter_path, 'r', encoding='utf-8')
delimiter = '\n@slide'
chapter_input = ('\n' + chapter_file.read()).split(delimiter)
slides = [delimiter + slide for slide in chapter_input][1:]
chapter_content = ''
for slide in slides:
chapter_content +=\
'<article>\n' +\
slides_helper.compile_slide(slide, root) + '\n' +\
'</article>\n'
cache.store_cached_chapter(root, lang, chapter, chapter_content)
return chapter_content