-
Notifications
You must be signed in to change notification settings - Fork 75
/
main.py
67 lines (56 loc) · 1.69 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import codecs
import shutil
import optparse
from markdown import markdown
HERE = os.path.abspath(os.path.dirname(__file__))
markdown_options = ['extra', 'codehilite']
def copy(dst):
try:
shutil.copytree(os.path.join(HERE, 'templates', 'css'),
os.path.join(dst, "css"))
shutil.copytree(os.path.join(HERE, 'templates', 'js'),
os.path.join(dst, 'js'))
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'
index_template = os.path.join(HERE, 'templates', 'index.html')
html = codecs.open(index_template, 'r', 'utf-8')
html = html.read().replace('<slide>', data)
f = codecs.open(os.path.join(dst, 'index.html'), 'w', 'utf-8')
f.write(html)
f.close()
def main():
'''Main entry point for the pydown CLI.'''
parser = optparse.OptionParser()
(options, args) = parser.parse_args()
if len(args) != 2:
print 'usage: pydown mdfile directory'
else:
handle(*args)
if __name__ == '__main__':
main()