forked from shell909090/utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
md2slide
executable file
·150 lines (133 loc) · 5.29 KB
/
md2slide
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
@date: 2012-09-11
@author: shell.xu
@license:
Copyright (C) 2012 Shell Xu <shell909090@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
import os, re, sys, getopt
from mako.template import Template
from BeautifulSoup import BeautifulSoup
from os import path
basehtml = '''
<!doctype html>
<html lang="en">
<head>
<title>${title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link href='http://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="reveal/main.css">
<link rel="stylesheet" href="reveal/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="reveal/lib/css/zenburn.css">
<script>
// If the query includes 'print-pdf' we'll use the PDF print sheet
document.write( '<link rel="stylesheet" href="reveal/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
</script>
</head>
<body>
<script src="reveal/lib/js/head.min.js"></script>
<script src="reveal/reveal.min.js"></script>
<div class="reveal">
<div class="state-background"></div>
% for slide in slides:
<div class="slides">
<section>
${slide}
</section>
</div>
% endfor
<!-- The navigational controls UI -->
<aside class="controls">
<a class="left" href="#">◄</a>
<a class="right" href="#">►</a>
<a class="up" href="#">▲</a>
<a class="down" href="#">▼</a>
</aside>
<!-- Presentation progress bar -->
<div class="progress"><span></span></div>
</div>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
theme: Reveal.getQueryHash().theme || 'default', // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/linear(2d)
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'reveal/lib/js/highlight.js', async: true, callback: function() { window.hljs.initHighlightingOnLoad(); } },
{ src: 'reveal/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal/lib/js/showdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'reveal/lib/js/data-markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
]
});
</script>
</body>
</html>'''
template = Template(basehtml)
def bsjoin(l):
bs = BeautifulSoup()
for i in l: bs.append(i)
return bs
def get_slides(src):
def splitbs(bs):
prevname = 'h1'
for i, b in enumerate(bs):
if not hasattr(b, 'name'): continue
if b.name == 'hr': return bs[:i], bs[i+1:]
elif b.name.startswith('h') and not prevname.startswith('h'):
return bs[:i], bs[i:]
prevname = b.name
bs = list(BeautifulSoup(src))
r = splitbs(bs)
while r:
yield bsjoin(r[0])
bs = r[1]
r = splitbs(bs)
yield bsjoin(bs)
def translate(src, title):
return template.render(slides=get_slides(src), title=title)
def translate_file(src, dst):
md = os.popen('markdown "%s"' % src).read().decode('utf-8')
html = translate(md, path.splitext(path.basename(src))[0])
if dst != '-':
with open(dst, 'w') as fo: fo.write(html.encode('utf-8'))
else: print html
def main():
'''
-t: title
-h: help
'''
global optdict
optl, args = getopt.getopt(sys.argv[1:], 'ht:')
optdict = dict(optl)
if '-h' in optdict or not args:
exe = path.basename(sys.argv[0])
print '%s v1.0\nusage: %s [-h] [-t]' % (exe, exe)
print main.__doc__
return
output = path.splitext(args[0])[0] + '.html'
if len(args) > 1: output = args[1]
translate_file(args[0], output)
if __name__ == '__main__': main()