Show More
@@ -1,228 +1,228 b'' | |||
|
1 | 1 | from __future__ import absolute_import |
|
2 | 2 | |
|
3 | 3 | from converters.html import ConverterHTML |
|
4 | 4 | from converters.utils import text_cell |
|
5 | 5 | from converters.utils import highlight, coalesce_streams |
|
6 | 6 | |
|
7 | 7 | from IPython.utils import path |
|
8 | 8 | from markdown import markdown |
|
9 | 9 | |
|
10 | 10 | import os |
|
11 | 11 | import io |
|
12 | 12 | import itertools |
|
13 | 13 | |
|
14 | 14 | |
|
15 | 15 | class ConverterReveal(ConverterHTML): |
|
16 | 16 | #""" |
|
17 | 17 | #Convert a ipython notebook to a html slideshow |
|
18 | 18 | #based in reveal.js library. |
|
19 | 19 | #""" |
|
20 | 20 | |
|
21 | 21 | @text_cell |
|
22 | 22 | def render_heading(self, cell): |
|
23 | 23 | marker = cell.level |
|
24 | 24 | return [self.meta2str(cell.metadata), |
|
25 | 25 | u'<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)] |
|
26 | 26 | |
|
27 | 27 | def render_code(self, cell): |
|
28 | 28 | if not cell.input: |
|
29 | 29 | return [] |
|
30 | 30 | lines = [] |
|
31 | 31 | meta_code = self.meta2str(cell.metadata) |
|
32 | 32 | lines.extend([meta_code]) |
|
33 | 33 | lines.extend(['<div class="cell border-box-sizing code_cell vbox">']) |
|
34 | 34 | lines.append('<div class="input hbox">') |
|
35 | 35 | n = self._get_prompt_number(cell) |
|
36 | 36 | lines.append( |
|
37 | 37 | '<div class="prompt input_prompt">In [%s]:</div>' % n |
|
38 | 38 | ) |
|
39 | 39 | lines.append('<div class="input_area box-flex1">') |
|
40 | 40 | lines.append(highlight(cell.input)) |
|
41 | 41 | lines.append('</div>') # input_area |
|
42 | 42 | lines.append('</div>') # input |
|
43 | 43 | if cell.outputs: |
|
44 | 44 | lines.append('<div class="vbox output_wrapper">') |
|
45 | 45 | lines.append('<div class="output vbox">') |
|
46 | 46 | for output in coalesce_streams(cell.outputs): |
|
47 | 47 | conv_fn = self.dispatch(output.output_type) |
|
48 | 48 | lines.extend(conv_fn(output)) |
|
49 | 49 | lines.append('</div>') # output |
|
50 | 50 | lines.append('</div>') # output_wrapper |
|
51 | 51 | lines.append('</div>') # cell |
|
52 | 52 | return lines |
|
53 | 53 | |
|
54 | 54 | @text_cell |
|
55 | 55 | def render_markdown(self, cell): |
|
56 | 56 | return [self.meta2str(cell.metadata), markdown(cell.source)] |
|
57 | 57 | |
|
58 | 58 | def render_raw(self, cell): |
|
59 | 59 | if self.raw_as_verbatim: |
|
60 | 60 | return [self.in_tag('pre', self.meta2str(cell.metadata)), |
|
61 | 61 | self.in_tag('pre', cell.source)] |
|
62 | 62 | else: |
|
63 | 63 | return [self.meta2str(cell.metadata), cell.source] |
|
64 | 64 | |
|
65 | 65 | def meta2str(self, meta): |
|
66 | 66 | "transform metadata dict (containing slides delimiters) to string " |
|
67 | 67 | try: |
|
68 | 68 | meta_tuple = meta[u'slideshow'].items() |
|
69 | 69 | except KeyError as e: # if there is not slideshow metadata |
|
70 | 70 | meta_tuple = [(u'slide_type', u'untouched')] |
|
71 | 71 | meta_list = [[x + ' = ' + unicode(y)] for x, y in meta_tuple] |
|
72 | 72 | return u'\n'.join(list(itertools.chain(*meta_list))) |
|
73 | 73 | |
|
74 | 74 | def convert(self, cell_separator='\n'): |
|
75 | 75 | """ |
|
76 | 76 | Specific method to converts notebook to a string representation. |
|
77 | 77 | |
|
78 | 78 | Parameters |
|
79 | 79 | ---------- |
|
80 | 80 | cell_separator : string |
|
81 | 81 | Character or string to join cells with. Default is "\n" |
|
82 | 82 | |
|
83 | 83 | Returns |
|
84 | 84 | ------- |
|
85 | 85 | out : string |
|
86 | 86 | """ |
|
87 | 87 | |
|
88 | 88 | lines = [] |
|
89 | 89 | lines.extend(self.optional_header()) |
|
90 | 90 | begin = ['<div class="reveal"><div class="slides">'] |
|
91 | 91 | lines.extend(begin) |
|
92 | 92 | slides_list = self.build_slides() |
|
93 | 93 | lines.extend(slides_list) |
|
94 | 94 | end = ['</div></div>'] |
|
95 | 95 | lines.extend(end) |
|
96 | 96 | lines.extend(self.optional_footer()) |
|
97 | 97 | return u'\n'.join(lines) |
|
98 | 98 | |
|
99 | 99 | def clean_text(self, cell_separator='\n'): |
|
100 | 100 | "clean and reorganize the text list to be slided" |
|
101 | 101 | text = self.main_body(cell_separator) |
|
102 | 102 | self.delim = [u'slide_type = untouched', |
|
103 | 103 | u'slide_type = -', |
|
104 | 104 | u'slide_type = header_slide', |
|
105 | 105 | u'slide_type = slide', |
|
106 | 106 | u'slide_type = fragment', |
|
107 | u'slide_type = skip'] | |
|
107 | u'slide_type = skip'] # keep this one the last | |
|
108 | 108 | text_cell_render = \ |
|
109 | 109 | u'<div class="text_cell_render border-box-sizing rendered_html">' |
|
110 | 110 | for i, j in enumerate(text): |
|
111 | 111 | if j in self.delim and text[i - 1] == text_cell_render: |
|
112 | 112 | if j == self.delim[0]: |
|
113 | 113 | text[i - 1] = self.delim[0] |
|
114 | 114 | elif j == self.delim[1]: |
|
115 | 115 | text[i - 1] = self.delim[1] |
|
116 | 116 | elif j == self.delim[2]: |
|
117 | 117 | text[i - 1] = self.delim[2] |
|
118 | 118 | elif j == self.delim[3]: |
|
119 | 119 | text[i - 1] = self.delim[3] |
|
120 | 120 | elif j == self.delim[4]: |
|
121 | 121 | text[i - 1] = self.delim[4] |
|
122 | 122 | else: |
|
123 | 123 | text[i - 1] = self.delim[5] |
|
124 | 124 | text[i] = text_cell_render |
|
125 | 125 | text[0] = u'slide_type = header_slide' # defensive code |
|
126 | 126 | text.append(u'slide_type = untouched') # to end search of skipped |
|
127 | 127 | return text |
|
128 | 128 | |
|
129 | 129 | def build_slides(self): |
|
130 | 130 | "build the slides structure from text list and delimiters" |
|
131 | 131 | text = self.clean_text() |
|
132 | 132 | left = '<section>' |
|
133 | 133 | right = '</section>' |
|
134 | 134 | set_delim = self.delim[:5] |
|
135 | 135 | #elimination of skipped cells |
|
136 | 136 | for i, j in enumerate(text): |
|
137 | 137 | if j == u'slide_type = skip': |
|
138 | 138 | text.pop(i) |
|
139 | 139 | while not text[i] in set_delim: |
|
140 | 140 | text.pop(i) |
|
141 | 141 | # elimination of none names |
|
142 | 142 | for i, j in enumerate(text): |
|
143 | 143 | if j in [u'slide_type = untouched', u'slide_type = -']: |
|
144 | 144 | text.pop(i) |
|
145 | 145 | #generation of slides as a list of list |
|
146 | 146 | slides = [list(x[1]) for x in itertools.groupby(text, |
|
147 | 147 | lambda x: x == u'slide_type = header_slide') if not x[0]] |
|
148 | 148 | for slide in slides: |
|
149 | 149 | slide.insert(0, left) |
|
150 | 150 | slide.append(right) |
|
151 | 151 | # encapsulation of each fragment |
|
152 | 152 | for i, j in enumerate(slide): |
|
153 | 153 | if j == u'slide_type = fragment': |
|
154 | 154 | slide.pop(i) |
|
155 | 155 | slide[i] = slide[i][:4] + ' class="fragment"' + slide[i][4:] |
|
156 | 156 | # encapsulation of each nested slide |
|
157 | 157 | if u'slide_type = slide' in slide: |
|
158 | 158 | slide.insert(0, '<section>') |
|
159 | 159 | slide.append('</section>') |
|
160 | 160 | for i, j in enumerate(slide): |
|
161 | 161 | if j == u'slide_type = slide': |
|
162 | 162 | slide[i] = right + left |
|
163 | 163 | return list(itertools.chain(*slides)) |
|
164 | 164 | |
|
165 | 165 | def save(self, outfile=None, encoding=None): |
|
166 | 166 | "read and parse notebook into self.nb" |
|
167 | 167 | if outfile is None: |
|
168 | 168 | outfile = self.outbase + '_slides.' + 'html' |
|
169 | 169 | if encoding is None: |
|
170 | 170 | encoding = self.default_encoding |
|
171 | 171 | with io.open(outfile, 'w', encoding=encoding) as f: |
|
172 | 172 | f.write(self.output) |
|
173 | 173 | return os.path.abspath(outfile) |
|
174 | 174 | |
|
175 | 175 | def header_body(self): |
|
176 | 176 | "return the body of the header as a list of strings" |
|
177 | 177 | from pygments.formatters import HtmlFormatter |
|
178 | 178 | header = [] |
|
179 | 179 | static = os.path.join(path.get_ipython_package_dir(), |
|
180 | 180 | 'frontend', 'html', 'notebook', 'static',) |
|
181 | 181 | here = os.path.split(os.path.realpath(__file__))[0] |
|
182 | 182 | css = os.path.join(static, 'css') |
|
183 | 183 | for sheet in [ |
|
184 | 184 | # do we need jquery and prettify? |
|
185 | 185 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', |
|
186 | 186 | # 'jquery-ui.min.css'), |
|
187 | 187 | # os.path.join(static, 'prettify', 'prettify.css'), |
|
188 | 188 | os.path.join(css, 'boilerplate.css'), |
|
189 | 189 | os.path.join(css, 'fbm.css'), |
|
190 | 190 | os.path.join(css, 'notebook.css'), |
|
191 | 191 | os.path.join(css, 'renderedhtml.css'), |
|
192 | 192 | # our overrides: |
|
193 | 193 | os.path.join(here, '..', 'css', 'reveal_html.css'), |
|
194 | 194 | ]: |
|
195 | 195 | header.extend(self._stylesheet(sheet)) |
|
196 | 196 | # pygments css |
|
197 | 197 | pygments_css = HtmlFormatter().get_style_defs('.highlight') |
|
198 | 198 | header.extend(['<meta charset="UTF-8">']) |
|
199 | 199 | header.extend(self.in_tag('style', pygments_css, |
|
200 | 200 | dict(type='"text/css"'))) |
|
201 | 201 | return header |
|
202 | 202 | |
|
203 | 203 | def template_read(self): |
|
204 | 204 | "read the reveal_template.html" |
|
205 | 205 | here = os.path.split(os.path.realpath(__file__))[0] |
|
206 | 206 | reveal_template = os.path.join(here, '..', 'templates', |
|
207 | 207 | 'reveal_base.html') |
|
208 | 208 | with io.open(reveal_template, 'r', encoding='utf-8') as f: |
|
209 | 209 | template = f.readlines() |
|
210 | 210 | template = [s.strip() for s in template] |
|
211 | 211 | return template |
|
212 | 212 | |
|
213 | 213 | def template_split(self): |
|
214 | 214 | "split the reveal_template.html in header and footer lists" |
|
215 | 215 | temp = self.template_read() |
|
216 | 216 | splitted_temp = [list(x[1]) for x in itertools.groupby(temp, |
|
217 | 217 | lambda x: x == u'%slides%') if not x[0]] |
|
218 | 218 | return splitted_temp |
|
219 | 219 | |
|
220 | 220 | def optional_header(self): |
|
221 | 221 | optional_header_body = self.template_split() |
|
222 | 222 | return ['<!DOCTYPE html>', '<html>', '<head>'] + \ |
|
223 | 223 | optional_header_body[0] + self.header_body() + \ |
|
224 | 224 | ['</head>', '<body>'] |
|
225 | 225 | |
|
226 | 226 | def optional_footer(self): |
|
227 | 227 | optional_footer_body = self.template_split() |
|
228 | 228 | return optional_footer_body[1] + ['</body>', '</html>'] No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now