aboutsummaryrefslogtreecommitdiff
path: root/scripts/md/render.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/md/render.py')
-rwxr-xr-xscripts/md/render.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/scripts/md/render.py b/scripts/md/render.py
new file mode 100755
index 0000000..c634ea0
--- /dev/null
+++ b/scripts/md/render.py
@@ -0,0 +1,53 @@
1#!/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python
2import codecs
3import re
4import jinja2
5import markdown
6
7def process_slides():
8 with codecs.open('../presentation.html', 'w', encoding='utf8') as outfile:
9 md = codecs.open('slides.md', encoding='utf8').read()
10 md_slides = md.split('\n---\n')
11 print len(md_slides)
12
13 slides = []
14 # Process each slide separately.
15 for md_slide in md_slides:
16 slide = {}
17 sections = md_slide.split('\n\n')
18 # Extract metadata at the beginning of the slide (look for key: value)
19 # pairs.
20 metadata_section = sections[0]
21 metadata = parse_metadata(metadata_section)
22 slide.update(metadata)
23 remainder_index = metadata and 1 or 0
24 # Get the content from the rest of the slide.
25 content_section = '\n\n'.join(sections[remainder_index:])
26 html = markdown.markdown(content_section)
27 slide['content'] = postprocess_html(html, markdown)
28
29 slides.append(slide)
30
31 template = jinja2.Template(open('base.html').read())
32
33 outfile.write(template.render(locals()))
34
35def parse_metadata(section):
36 """Given the first part of a slide, returns metadata associated with it."""
37 metadata = {}
38 metadata_lines = section.split('\n')
39 for line in metadata_lines:
40 colon_index = line.find(':')
41 if colon_index != -1:
42 key = line[:colon_index].strip()
43 val = line[colon_index + 1:].strip()
44 metadata[key] = val
45
46 return metadata
47
48def postprocess_html(html, metadata):
49 """Returns processed HTML to fit into the slide template format."""
50 return html
51
52if __name__ == '__main__':
53 process_slides()