diff options
author | Pacien TRAN-GIRARD | 2014-06-16 19:30:53 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2014-06-16 19:30:53 +0200 |
commit | add331408b0f207b82f3ec1b76251c700197e807 (patch) | |
tree | efee398fbd8811adbad16dbe98b1b0506dddc363 /slides/final/scripts/md/render.py | |
parent | c59a0f4780502c949c0d19287bea51a4c40fb109 (diff) | |
download | labviewplayer-master.tar.gz |
Diffstat (limited to 'slides/final/scripts/md/render.py')
-rwxr-xr-x | slides/final/scripts/md/render.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/slides/final/scripts/md/render.py b/slides/final/scripts/md/render.py new file mode 100755 index 0000000..a035b90 --- /dev/null +++ b/slides/final/scripts/md/render.py | |||
@@ -0,0 +1,57 @@ | |||
1 | #!/usr/bin/env python | ||
2 | |||
3 | import codecs | ||
4 | import re | ||
5 | import jinja2 | ||
6 | import markdown | ||
7 | |||
8 | def process_slides(): | ||
9 | with codecs.open('../../presentation-output.html', 'w', encoding='utf8') as outfile: | ||
10 | md = codecs.open('slides.md', encoding='utf8').read() | ||
11 | md_slides = md.split('\n---\n') | ||
12 | print 'Compiled %s slides.' % len(md_slides) | ||
13 | |||
14 | slides = [] | ||
15 | # Process each slide separately. | ||
16 | for md_slide in md_slides: | ||
17 | slide = {} | ||
18 | sections = md_slide.split('\n\n') | ||
19 | # Extract metadata at the beginning of the slide (look for key: value) | ||
20 | # pairs. | ||
21 | metadata_section = sections[0] | ||
22 | metadata = parse_metadata(metadata_section) | ||
23 | slide.update(metadata) | ||
24 | remainder_index = metadata and 1 or 0 | ||
25 | # Get the content from the rest of the slide. | ||
26 | content_section = '\n\n'.join(sections[remainder_index:]) | ||
27 | html = markdown.markdown(content_section) | ||
28 | slide['content'] = postprocess_html(html, metadata) | ||
29 | |||
30 | slides.append(slide) | ||
31 | |||
32 | template = jinja2.Template(open('base.html').read()) | ||
33 | |||
34 | outfile.write(template.render(locals())) | ||
35 | |||
36 | def parse_metadata(section): | ||
37 | """Given the first part of a slide, returns metadata associated with it.""" | ||
38 | metadata = {} | ||
39 | metadata_lines = section.split('\n') | ||
40 | for line in metadata_lines: | ||
41 | colon_index = line.find(':') | ||
42 | if colon_index != -1: | ||
43 | key = line[:colon_index].strip() | ||
44 | val = line[colon_index + 1:].strip() | ||
45 | metadata[key] = val | ||
46 | |||
47 | return metadata | ||
48 | |||
49 | def postprocess_html(html, metadata): | ||
50 | """Returns processed HTML to fit into the slide template format.""" | ||
51 | if metadata.get('build_lists') and metadata['build_lists'] == 'true': | ||
52 | html = html.replace('<ul>', '<ul class="build">') | ||
53 | html = html.replace('<ol>', '<ol class="build">') | ||
54 | return html | ||
55 | |||
56 | if __name__ == '__main__': | ||
57 | process_slides() | ||