diff options
author | Eric Bidelman | 2012-07-11 10:43:37 -0700 |
---|---|---|
committer | Eric Bidelman | 2012-07-11 10:43:37 -0700 |
commit | f208307cbdd403d3fa92a1aae1dc9c094c7ec9d8 (patch) | |
tree | eb8ac9966f182313db6b11e12c2f4fd5bab070b7 /scripts/md/render.py | |
parent | d78d1c5069ecdb6723a8933dbb86f4b7a20c59e9 (diff) | |
download | io-slides-remote-f208307cbdd403d3fa92a1aae1dc9c094c7ec9d8.tar.gz |
Bringing in upstream changes
Diffstat (limited to 'scripts/md/render.py')
-rwxr-xr-x | scripts/md/render.py | 53 |
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 | ||
2 | import codecs | ||
3 | import re | ||
4 | import jinja2 | ||
5 | import markdown | ||
6 | |||
7 | def 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 | |||
35 | def 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 | |||
48 | def postprocess_html(html, metadata): | ||
49 | """Returns processed HTML to fit into the slide template format.""" | ||
50 | return html | ||
51 | |||
52 | if __name__ == '__main__': | ||
53 | process_slides() | ||