forked from isnowfy/pydown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
52 lines (45 loc) · 1.29 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
import os
import codecs
import shutil
import optparse
from markdown import markdown
markdown_options = ['extra', 'codehilite']
def copy(dst):
try:
shutil.copytree('templates/css', '%s/css' % dst)
shutil.copytree('templates/js', '%s/js' % dst)
except:
pass
def slides_split(slides):
data = ''
css = ''
for item in slides.split('\n'):
if item.startswith('!SLIDE'):
yield css, data
css = ' '.join(item.split(' ')[1:])
data = ''
else:
data += item+'\n'
yield css, data
def handle(md, dst):
copy(dst)
slides = codecs.open(md, 'r', 'utf-8').read()
data = ''
for css, item in slides_split(slides):
if not item:
continue
data += "<section class='slide %s'>" % css\
+ "<div class='content'>"\
+ markdown(item, markdown_options)\
+ "</div>"\
+ "</section>\n"
html = codecs.open('templates/index.html', 'r', 'utf-8')
html = html.read().replace('<slide>', data)
f = codecs.open('%s/index.html' % dst, 'w', 'utf-8')
f.write(html)
f.close()
if __name__ == '__main__':
parser = optparse.OptionParser()
(options, args) = parser.parse_args()
handle(*args)