comparison presen/scripts/md/render.py @ 10:5c57e35e19b6

add presen
author sugi
date Tue, 23 Apr 2013 23:31:26 +0900
parents
children
comparison
equal deleted inserted replaced
9:e17bc730af1a 10:5c57e35e19b6
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()