From f624aebd18080f227b8d69906c960b4b49b9f37a Mon Sep 17 00:00:00 2001 From: Christian Kremitzl Date: Mon, 16 Dec 2019 23:16:30 +0100 Subject: [PATCH] First basically working draft. --- .gitignore | 3 ++ README.md | 7 +++ onpoint/__init__.py | 94 +++++++++++++++++++++++++++++++++++++ test/layouts/default.html | 2 + test/layouts/root.html | 4 +- test/layouts/titlepage.html | 2 +- test/slides.yml | 2 + test/slides/chapter1.de.md | 8 +++- test/slides/chapter1.en.md | 7 ++- test/slides/chapter2.de.md | 10 ++++ test/slides/chapter2.en.md | 10 ++++ 11 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 test/layouts/default.html create mode 100644 test/slides/chapter2.de.md create mode 100644 test/slides/chapter2.en.md diff --git a/.gitignore b/.gitignore index c8f7987..912a858 100644 --- a/.gitignore +++ b/.gitignore @@ -119,3 +119,6 @@ dmypy.json .vscode/ *~ +# onpoint-specific +test/slides.*.html + diff --git a/README.md b/README.md index fea9a37..599cb8c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ # onpoint +## installation + +### requirements + +* pypandoc (from pip) + * needs pandoc + diff --git a/onpoint/__init__.py b/onpoint/__init__.py index edc180c..e9cff5b 100644 --- a/onpoint/__init__.py +++ b/onpoint/__init__.py @@ -1 +1,95 @@ # Our python script goes here. + +import yaml +import pypandoc +import re + +def compile(directory, language='en'): + global root + root = directory + global lang + lang = language + wrapper = open(root + 'layouts/root.html', 'r').read() + wrapper = wrapper.replace('@slides', compile_chapters()) + wrapper = insert_metadata(wrapper, lang=lang) + with open(root + 'slides.' + lang + '.html', 'w+') as output: + output.write(wrapper) + print('done') + +def insert_metadata(content, lang=None): + metadata = read_yaml('meta.yml') + for key, value in metadata.items(): + placeholder = '@' + key + filler = value[lang] + print('replace', placeholder, 'with', filler) + if '@' + key in content: + content = content.replace(placeholder, filler) + return content + +def compile_chapters(): + structure = read_yaml('slides.yml') + chapters = "" + for chapter in structure: + chapters += '
\n' + compile_chapter(chapter) + '\n
' + return chapters # string + +def compile_chapter(chapter): + if type(chapter) == str: # plain chapter names + chapter_input = '\n' + open(root + 'slides/' + chapter + '.' + lang + '.md', 'r').read() + delimiter = '\n@slide' + slides = [delimiter + slide for slide in chapter_input.split(delimiter)][1:] + print(slides) + chapter = '' + for slide in slides: + chapter += '
\n' + compile_slide(slide) + '\n
\n' + return chapter # string + else: + raise Exception('Chapters with attributes are not supported yet.\n' + str(chapter)) + +def compile_slide(slide): + slide = slide.strip() + slide_metadata = get_slide_metadata(slide) + slide_data = get_slide_data(slide) + slide = get_slide_layout(slide_metadata['layout']) + for key, value in slide_data.items(): + placeholder = '@' + key + filler = convert_slide_content(value) + print('replace', placeholder, 'with', filler) + if '@' + key in slide: + slide = slide.replace(placeholder, filler) + return slide # string + +def get_slide_metadata(slide): + metadata = { 'layout': 'default' } + metadata_attributes = re.search('^@slide\((.+)\)', slide.splitlines()[0]) + if metadata_attributes: + metadata_attributes = metadata_attributes.group(1).split(' ') + for attribute in metadata_attributes: + metadata[attribute.split('=')[0]] = attribute.split('=')[1] + print("\nMETADATA:\n"+str(metadata)+"\n\n") + return metadata + +def get_slide_data(slide): + if slide.startswith('@slide'): + 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@') } + return slide_data + +def get_slide_layout(layout_name): + try: + return open(root + 'layouts/' + layout_name + '.html', 'r').read() + except FileNotFoundError: + raise Exception('Layout ' + layout_name + ' not found!') + +def convert_slide_content(content): + return pypandoc.convert_text(content, 'html', format='md') + +def read_yaml(path): + with open(root + path, 'r') as stream: + try: + return yaml.safe_load(stream) + except yaml.YAMLError as exc: + print(exc) + +compile('../test/', language='de') +compile('../test/', language='en') diff --git a/test/layouts/default.html b/test/layouts/default.html new file mode 100644 index 0000000..0f17d74 --- /dev/null +++ b/test/layouts/default.html @@ -0,0 +1,2 @@ +

@title

+@content diff --git a/test/layouts/root.html b/test/layouts/root.html index b13efde..007a5d5 100644 --- a/test/layouts/root.html +++ b/test/layouts/root.html @@ -7,6 +7,6 @@ @title - @slides +@slides - \ No newline at end of file + diff --git a/test/layouts/titlepage.html b/test/layouts/titlepage.html index 6e522ff..2346496 100644 --- a/test/layouts/titlepage.html +++ b/test/layouts/titlepage.html @@ -1,2 +1,2 @@

@title(inline)

-
@content
\ No newline at end of file + diff --git a/test/slides.yml b/test/slides.yml index e69de29..6cf2d78 100644 --- a/test/slides.yml +++ b/test/slides.yml @@ -0,0 +1,2 @@ +- chapter1 +- chapter2 diff --git a/test/slides/chapter1.de.md b/test/slides/chapter1.de.md index ae256dc..8b679b0 100644 --- a/test/slides/chapter1.de.md +++ b/test/slides/chapter1.de.md @@ -3,8 +3,14 @@ @title Wirklich auf den Punkt! +@slide + +@title +Wirklich auf den Punkt! + @content * Stichpunkt 1 * Stichpunkt 2 * Stichpunkt 3 -* Stichpunkt 4 \ No newline at end of file +* Stichpunkt 4 + diff --git a/test/slides/chapter1.en.md b/test/slides/chapter1.en.md index 664f991..42c5f87 100644 --- a/test/slides/chapter1.en.md +++ b/test/slides/chapter1.en.md @@ -3,8 +3,13 @@ @title OnPoint for real! +@slide + +@title +OnPoint for real! + @content * bullet point 1 * bullet point 2 * bullet point 3 -* bullet point 4 \ No newline at end of file +* bullet point 4 diff --git a/test/slides/chapter2.de.md b/test/slides/chapter2.de.md new file mode 100644 index 0000000..ae256dc --- /dev/null +++ b/test/slides/chapter2.de.md @@ -0,0 +1,10 @@ +@slide(layout=titlepage) + +@title +Wirklich auf den Punkt! + +@content +* Stichpunkt 1 +* Stichpunkt 2 +* Stichpunkt 3 +* Stichpunkt 4 \ No newline at end of file diff --git a/test/slides/chapter2.en.md b/test/slides/chapter2.en.md new file mode 100644 index 0000000..664f991 --- /dev/null +++ b/test/slides/chapter2.en.md @@ -0,0 +1,10 @@ +@slide(layout=titlepage) + +@title +OnPoint for real! + +@content +* bullet point 1 +* bullet point 2 +* bullet point 3 +* bullet point 4 \ No newline at end of file