This repository has been archived by the owner on Jul 20, 2018. It is now read-only.
forked from ogom/python-mcider
/
converter.py
229 lines (199 loc) · 7.85 KB
/
converter.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
""" mcider - converter
Copyright(c) 2012 ogom
slide maker
"""
import os
import shutil
from markdown import Markdown
import util
class Slide():
""" Create a slide from the content and theme. """
""" opts
.themes themes path
.theme io2012(default) or io2011 or bnr
.contents
.extensions extra fenced_code tables, ...
.clean (boolean, default False)
"""
def __init__(self, opts):
self.options = opts
if 'themes' not in self.options:
raise KeyError('themes')
if 'theme' not in self.options:
self.options['theme'] = 'io2012'
if 'contents' not in self.options:
self.options['contents'] = None
if 'extensions' not in self.options:
self.options['extensions'] = []
if 'clean' not in self.options:
self.options['clean'] = False
if self.options['extensions'] is None:
self.options['extensions'] = []
def maker(self, output_path=None):
theme_path = os.path.abspath(os.path.join(self.options['themes'], self.options['theme']))
template = self._get_template(output_path, theme_path, self.options['clean'])
slide = self._get_slide(self.options['theme'], self.options['contents'], self.options['extensions'])
return template.replace('{{ slide }}', slide)
def _get_template(self, output_path=None, theme_path=None, clean=False):
""" copy theme assets and read base.html """
assets = ['css', 'js', 'images']
for asset in assets:
src_path = os.path.join(theme_path, asset)
if os.path.isdir(src_path):
dst_path = os.path.join(output_path, asset)
if clean and os.path.isdir(dst_path):
shutil.rmtree(dst_path)
if not os.path.isdir(dst_path):
shutil.copytree(src_path, dst_path)
return util.fs_reader(os.path.join(theme_path, 'base.html'))
def _get_slide(self, theme=None, contents=None, extensions=[]):
html = None
if theme == 'io2011':
html = self._get_slide_io2011(contents, extensions)
elif theme == 'io2012':
html = self._get_slide_io2012(contents, extensions)
elif theme == 'bnr':
html = self._get_slide_bnr(contents, extensions)
else:
html = self._get_slide_none(contents, extensions)
return html
def _get_slide_none(self, contents=None, extensions=[]):
""" none style """
md = Markdown(extensions=extensions)
# from pages to slides
pages = md.convert(contents) + '\n'
slides = pages.split('<hr />\n')
# from slides to html
html = '\n'
for slide in slides:
html += '<article>\n'
html += slide
html += '</article>\n\n'
return html
def _get_slide_bnr(self, contents=None, extensions=[]):
""" bnr style """
md = Markdown(extensions=extensions)
splits = [
{'horizon': '---', 'style': 'none'},
{'horizon': '___', 'style': 'smaller'},
{'horizon': '***', 'style': 'fill'}
]
styles = []
for split in splits:
styles.append(split['style'])
horizon = '\n' + split['horizon'] + '\n'
contents = contents.replace(horizon, '\n---\n' + split['style'] + '\n')
pages = contents.split('\n---\n')
# from pages to slides
slides = []
for page in pages:
sections = page.split('\n\n', 2)
slide = {}
if not sections[0] in styles:
if len(sections) > 2:
sections[1] += '\n\n' + sections[2]
sections.insert(0, 'none')
slide['style'] = sections[0]
if len(sections) > 1:
slide['hgroup'] = sections[1]
if len(sections) > 2:
slide['article'] = sections[2]
slides.append(slide)
# from slides to html
html = '\n'
for slide in slides:
html += '<slide>\n'
if slide.has_key('hgroup'):
html += '<hgroup>\n'
html += md.convert(slide['hgroup']) + '\n'
html += '</hgroup>\n'
if slide.has_key('article'):
html += '<article class="' + slide['style'] + '">\n'
html += md.convert(slide['article']) + '\n'
html += '</article>\n'
html += '</slide>\n\n'
# from comment out to presener note
html = html.replace('\n<!--\n', '\n<aside class="note">\n')
html = html.replace('\n-->\n', '\n</aside>\n')
return html
def _get_slide_io2012(self, contents=None, extensions=[]):
""" io-2012 style """
md = Markdown(extensions=extensions)
splits = [
{'horizon': '---', 'style': 'none'},
{'horizon': '___', 'style': 'smaller'},
{'horizon': '***', 'style': 'fill'}
]
styles = []
for split in splits:
styles.append(split['style'])
horizon = '\n' + split['horizon'] + '\n'
contents = contents.replace(horizon, '\n---\n' + split['style'] + '\n')
pages = contents.split('\n---\n')
# from pages to slides
slides = []
for page in pages:
sections = page.split('\n\n', 2)
slide = {}
if not sections[0] in styles:
if len(sections) > 2:
sections[1] += '\n\n' + sections[2]
sections.insert(0, 'none')
slide['style'] = sections[0]
if len(sections) > 1:
slide['hgroup'] = sections[1]
if len(sections) > 2:
slide['article'] = sections[2]
slides.append(slide)
# from slides to html
html = '\n'
for slide in slides:
html += '<slide>\n'
if slide.has_key('hgroup'):
html += '<hgroup>\n'
html += md.convert(slide['hgroup']) + '\n'
html += '</hgroup>\n'
if slide.has_key('article'):
html += '<article class="' + slide['style'] + '">\n'
html += md.convert(slide['article']) + '\n'
html += '</article>\n'
html += '</slide>\n\n'
# from comment out to presener note
html = html.replace('\n<!--\n', '\n<aside class="note">\n')
html = html.replace('\n-->\n', '\n</aside>\n')
return html
def _get_slide_io2011(self, contents=None, extensions=[]):
""" io-2011 style """
md = Markdown(extensions=extensions)
splits = [
{'horizon': '---', 'style': 'none'},
{'horizon': '___', 'style': 'smaller'},
{'horizon': '***', 'style': 'fill'}
]
styles = []
for split in splits:
styles.append(split['style'])
horizon = '\n' + split['horizon'] + '\n'
contents = contents.replace(horizon, '\n---\n' + split['style'] + '\n')
pages = contents.split('\n---\n')
# from pages to slides
slides = []
for page in pages:
sections = page.split('\n\n', 1)
slide = {}
if not sections[0] in styles:
if len(sections) > 1:
sections[0] += '\n\n' + sections[1]
sections.insert(0, 'none')
slide['style'] = sections[0]
if len(sections) > 1:
slide['article'] = sections[1]
slides.append(slide)
# from slides to html
html = '\n'
for slide in slides:
if slide.has_key('article'):
html += '<article class="' + slide['style'] + '">\n'
html += md.convert(slide['article']) + '\n'
html += '</article>\n\n'
return html