First basically working draft.
This commit is contained in:
parent
34bd4bc6a1
commit
f624aebd18
3
.gitignore
vendored
3
.gitignore
vendored
@ -119,3 +119,6 @@ dmypy.json
|
||||
.vscode/
|
||||
*~
|
||||
|
||||
# onpoint-specific
|
||||
test/slides.*.html
|
||||
|
||||
|
||||
@ -1,2 +1,9 @@
|
||||
# onpoint
|
||||
|
||||
## installation
|
||||
|
||||
### requirements
|
||||
|
||||
* pypandoc (from pip)
|
||||
* needs pandoc
|
||||
|
||||
|
||||
@ -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 += '<section>\n' + compile_chapter(chapter) + '\n</section>'
|
||||
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 += '<article>\n' + compile_slide(slide) + '\n</article>\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')
|
||||
|
||||
2
test/layouts/default.html
Normal file
2
test/layouts/default.html
Normal file
@ -0,0 +1,2 @@
|
||||
<h1>@title</h1>
|
||||
@content
|
||||
@ -7,6 +7,6 @@
|
||||
<title>@title</title>
|
||||
</head>
|
||||
<body>
|
||||
@slides <!-- reserved keyword -->
|
||||
@slides <!-- reserved keyword -->
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
<h1>@title(inline)</h1>
|
||||
<div>@content</div>
|
||||
|
||||
|
||||
@ -0,0 +1,2 @@
|
||||
- chapter1
|
||||
- chapter2
|
||||
@ -3,8 +3,14 @@
|
||||
@title
|
||||
Wirklich auf den Punkt!
|
||||
|
||||
@slide
|
||||
|
||||
@title
|
||||
Wirklich auf den Punkt!
|
||||
|
||||
@content
|
||||
* Stichpunkt 1
|
||||
* Stichpunkt 2
|
||||
* Stichpunkt 3
|
||||
* Stichpunkt 4
|
||||
* Stichpunkt 4
|
||||
|
||||
|
||||
@ -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
|
||||
* bullet point 4
|
||||
|
||||
10
test/slides/chapter2.de.md
Normal file
10
test/slides/chapter2.de.md
Normal file
@ -0,0 +1,10 @@
|
||||
@slide(layout=titlepage)
|
||||
|
||||
@title
|
||||
Wirklich auf den Punkt!
|
||||
|
||||
@content
|
||||
* Stichpunkt 1
|
||||
* Stichpunkt 2
|
||||
* Stichpunkt 3
|
||||
* Stichpunkt 4
|
||||
10
test/slides/chapter2.en.md
Normal file
10
test/slides/chapter2.en.md
Normal file
@ -0,0 +1,10 @@
|
||||
@slide(layout=titlepage)
|
||||
|
||||
@title
|
||||
OnPoint for real!
|
||||
|
||||
@content
|
||||
* bullet point 1
|
||||
* bullet point 2
|
||||
* bullet point 3
|
||||
* bullet point 4
|
||||
Loading…
x
Reference in New Issue
Block a user