Show More
@@ -1,185 +1,227 b'' | |||
|
1 | 1 | from converters.markdown import ConverterMarkdown |
|
2 | from IPython.utils.text import indent | |
|
2 | from IPython.utils.text import indent, dedent | |
|
3 | from converters.utils import highlight, remove_ansi | |
|
3 | 4 | import io |
|
4 | 5 | import os |
|
5 | 6 | import itertools |
|
6 | 7 | |
|
7 | 8 | class ConverterReveal(ConverterMarkdown): |
|
8 | 9 | """ |
|
9 | 10 | Convert a notebook to a html slideshow. |
|
10 | 11 | |
|
11 | 12 | It generates a static html slideshow based in markdown and reveal.js. |
|
12 | 13 | The delimiters for each slide, subslide, and fragment are retrieved |
|
13 | 14 | from the 'slideshow' metadata. |
|
14 | 15 | """ |
|
15 | 16 | |
|
16 | 17 | def __init__(self, infile, highlight_source=False, show_prompts=True, |
|
17 | 18 | inline_prompt=True): |
|
18 | 19 | super(ConverterReveal, self).__init__(infile) |
|
19 | 20 | self.highlight_source = highlight_source |
|
20 | 21 | self.show_prompts = show_prompts |
|
21 | 22 | self.inline_prompt = inline_prompt |
|
22 | 23 | |
|
23 | 24 | def switch_meta(self, m_list): |
|
24 | 25 | "sort order m_list to [new_section, new_subsection, new_fragment]" |
|
25 | 26 | if len(m_list) > 1: |
|
26 | 27 | # do not sort when m_list = [new_subsection, new_fragment] |
|
27 | 28 | if not (len(m_list) == 2 and m_list[1] == [u'new_fragment = True']): |
|
28 | 29 | m_list[0], m_list[1] = m_list[1], m_list[0] |
|
29 | 30 | return m_list |
|
30 | 31 | |
|
31 | 32 | def meta2str(self, meta): |
|
32 | 33 | "transform metadata dict (containing slides delimiters) to string " |
|
33 | 34 | try: |
|
34 | 35 | meta_tuple = meta[u'slideshow'].items() |
|
35 | 36 | except KeyError as e: |
|
36 | 37 | meta_tuple = () # if there is not slideshow metadata |
|
37 | 38 | meta_list = [[x + ' = ' + unicode(y)] for x, y in meta_tuple] |
|
38 | 39 | meta_list = self.switch_meta(meta_list) |
|
39 | 40 | return u'\n'.join(list(itertools.chain(*meta_list))) |
|
40 | 41 | |
|
41 | 42 | def render_heading(self, cell): |
|
42 | 43 | return [self.meta2str(cell.metadata), |
|
43 | 44 | '{0} {1}'.format('#' * cell.level, cell.source), ''] |
|
44 | 45 | |
|
45 | 46 | def render_code(self, cell): |
|
46 | 47 | if not cell.input: |
|
47 | 48 | return [] |
|
48 | 49 | lines = [] |
|
49 | 50 | meta_code = self.meta2str(cell.metadata) |
|
50 | 51 | lines.extend([meta_code]) |
|
51 | 52 | lines.extend(['<!-- hack -->']) # to be proper parsed |
|
52 | 53 | n = self._get_prompt_number(cell) |
|
53 | 54 | if self.show_prompts and not self.inline_prompt: |
|
54 | 55 | lines.extend(['*In[%s]:*' % n, '']) |
|
55 | 56 | if self.show_prompts and self.inline_prompt: |
|
56 | 57 | prompt = 'In[%s]: ' % n |
|
57 | 58 | input_lines = cell.input.split('\n') |
|
58 | 59 | src = (prompt + input_lines[0] + '\n' + |
|
59 | 60 | indent('\n'.join(input_lines[1:]), nspaces=len(prompt))) |
|
60 | 61 | else: |
|
61 | 62 | src = cell.input |
|
62 | 63 | src = highlight(src) if self.highlight_source else indent(src) |
|
63 | 64 | lines.extend([src, '']) |
|
64 | 65 | if cell.outputs and self.show_prompts and not self.inline_prompt: |
|
65 | 66 | lines.extend(['*Out[%s]:*' % n, '']) |
|
66 | 67 | for output in cell.outputs: |
|
67 | 68 | conv_fn = self.dispatch(output.output_type) |
|
68 | 69 | lines.extend(conv_fn(output)) |
|
69 | 70 | #lines.append('') |
|
70 | 71 | return lines |
|
71 | 72 | |
|
72 | 73 | def render_markdown(self, cell): |
|
73 | 74 | return [self.meta2str(cell.metadata), cell.source, ''] |
|
74 | 75 | |
|
75 | 76 | def render_raw(self, cell): |
|
76 | 77 | if self.raw_as_verbatim: |
|
77 | 78 | return [indent(self.meta2str(cell.metadata)), |
|
78 | 79 | indent(cell.source), ''] |
|
79 | 80 | else: |
|
80 | 81 | return [self.meta2str(cell.metadata), cell.source, ''] |
|
81 | 82 | |
|
83 | def render_pyout(self, output): | |
|
84 | for fmt in ['html', 'latex', 'png', 'jpeg', 'svg', 'text']: | |
|
85 | if fmt in output: | |
|
86 | conv_fn = self.dispatch_display_format(fmt) | |
|
87 | return conv_fn(output) | |
|
88 | return [] | |
|
89 | ||
|
90 | def render_pyerr(self, output): | |
|
91 | # Note: a traceback is a *list* of frames. | |
|
92 | return [indent(remove_ansi('\n'.join(output.traceback))), ''] | |
|
93 | ||
|
94 | def _img_lines(self, img_file): | |
|
95 | return ['![](%s)' % img_file, ''] | |
|
96 | ||
|
97 | def render_display_format_png(self, output): | |
|
98 | return ['<img src="data:image/png;base64,%s"></img>' % output.png, ''] | |
|
99 | ||
|
100 | def render_display_format_svg(self, output): | |
|
101 | return [output.svg, ''] | |
|
102 | ||
|
103 | def render_display_format_jpeg(self, output): | |
|
104 | return ['<img src="data:image/jpeg;base64,%s"></img>' % output.jpeg, ''] | |
|
105 | ||
|
106 | def render_display_format_text(self, output): | |
|
107 | return [indent(output.text), ''] | |
|
108 | ||
|
109 | def _unknown_lines(self, data): | |
|
110 | return ['Warning: Unknown cell', data, ''] | |
|
111 | ||
|
112 | def render_display_format_html(self, output): | |
|
113 | return [dedent(output.html), ''] | |
|
114 | ||
|
115 | def render_display_format_latex(self, output): | |
|
116 | return ['LaTeX::', indent(output.latex), ''] | |
|
117 | ||
|
118 | def render_display_format_json(self, output): | |
|
119 | return ['JSON:', indent(output.json), ''] | |
|
120 | ||
|
121 | def render_display_format_javascript(self, output): | |
|
122 | return ['JavaScript:', indent(output.javascript), ''] | |
|
123 | ||
|
82 | 124 | def convert(self, cell_separator='\n'): |
|
83 | 125 | """ |
|
84 | 126 | Specific method to converts notebook to a string representation. |
|
85 | 127 | |
|
86 | 128 | Parameters |
|
87 | 129 | ---------- |
|
88 | 130 | cell_separator : string |
|
89 | 131 | Character or string to join cells with. Default is "\n" |
|
90 | 132 | |
|
91 | 133 | Returns |
|
92 | 134 | ------- |
|
93 | 135 | out : string |
|
94 | 136 | """ |
|
95 | 137 | |
|
96 | 138 | lines = [] |
|
97 | 139 | lines.extend(self.optional_header()) |
|
98 | 140 | begin = ['<div class="reveal"><div class="slides">'] |
|
99 | 141 | lines.extend(begin) |
|
100 | 142 | slides_list = self.build_slides(cell_separator) |
|
101 | 143 | lines.extend(slides_list) |
|
102 | 144 | end = ['</div></div>'] |
|
103 | 145 | lines.extend(end) |
|
104 | 146 | lines.extend(self.optional_footer()) |
|
105 | 147 | return u'\n'.join(lines) |
|
106 | 148 | |
|
107 | 149 | def build_slides(self, cell_separator='\n'): |
|
108 | 150 | "build the slides structure from text list and delimiters" |
|
109 | 151 | text = self.main_body(cell_separator) |
|
110 | 152 | delim_false = [u'new_section = False', |
|
111 | 153 | u'new_subsection = False', |
|
112 | 154 | u'new_fragment = False'] |
|
113 | 155 | text = [x for x in text if not x in delim_false] |
|
114 | 156 | left = '<section data-markdown><script type="text/template">' |
|
115 | 157 | right = '</script></section>' |
|
116 | 158 | # build lists representing each slide delimited by new_section |
|
117 | 159 | slides = [list(x[1]) for x in itertools.groupby(text, |
|
118 | 160 | lambda x: x == u'new_section = True') if not x[0]] |
|
119 | 161 | for slide in slides: |
|
120 | 162 | slide.insert(0, u'') # for proper interline in html file |
|
121 | 163 | slide.insert(0, left) |
|
122 | 164 | slide.append(right) |
|
123 | 165 | # build each vertical slide delimited by new_subsection |
|
124 | 166 | if slide[2] == u'new_subsection = True': |
|
125 | 167 | slide.pop(2) |
|
126 | 168 | slide.insert(0, '<section>') |
|
127 | 169 | slide.append('</section>') |
|
128 | 170 | for i, j in enumerate(slide): |
|
129 | 171 | if j == u'new_subsection = True': |
|
130 | 172 | slide[i] = right + left |
|
131 | 173 | slide.insert(i + 1, u'') # for proper interline |
|
132 | 174 | #defensive: if user do not begin new_subsection with a new_section |
|
133 | 175 | elif slide[4] == u'new_subsection = True': |
|
134 | 176 | slide[4] = right |
|
135 | 177 | slide.insert(5, u'') # for proper interline |
|
136 | 178 | slide.insert(5, left) |
|
137 | 179 | slide.insert(5, '<section>') |
|
138 | 180 | slide.append('</section>') |
|
139 | 181 | for i, j in enumerate(slide): |
|
140 | 182 | if j == u'new_subsection = True': |
|
141 | 183 | slide[i] = right + left |
|
142 | 184 | slide.insert(i + 1, u'') # for proper interline |
|
143 | 185 | # build each fragment delimited by new_fragment |
|
144 | 186 | for i, j in enumerate(slide): |
|
145 | 187 | if j == u'new_fragment = True': |
|
146 | 188 | slide[i] = '<p class="fragment">' |
|
147 | 189 | slide[i + 2] = '</p>' |
|
148 | 190 | slide.insert(i + 3, u'') # for proper interline |
|
149 | 191 | return list(itertools.chain(*slides)) |
|
150 | 192 | |
|
151 | 193 | def save(self, outfile=None, encoding=None): |
|
152 | 194 | "read and parse notebook into self.nb" |
|
153 | 195 | if outfile is None: |
|
154 | 196 | outfile = self.outbase + '_slides.' + 'html' |
|
155 | 197 | if encoding is None: |
|
156 | 198 | encoding = self.default_encoding |
|
157 | 199 | with io.open(outfile, 'w', encoding=encoding) as f: |
|
158 | 200 | f.write(self.output) |
|
159 | 201 | return os.path.abspath(outfile) |
|
160 | 202 | |
|
161 | 203 | def template_read(self): |
|
162 | 204 | "read the reveal_template.html" |
|
163 | 205 | here = os.path.split(os.path.realpath(__file__))[0] |
|
164 | 206 | reveal_template = os.path.join(here, '..', 'templates', |
|
165 | 207 | 'reveal_base.html') |
|
166 | 208 | with io.open(reveal_template, 'r', encoding='utf-8') as f: |
|
167 | 209 | template = f.readlines() |
|
168 | 210 | template = [s.strip() for s in template] |
|
169 | 211 | return template |
|
170 | 212 | |
|
171 | 213 | def template_split(self): |
|
172 | 214 | "split the reveal_template.html in header and footer lists" |
|
173 | 215 | temp = self.template_read() |
|
174 | 216 | splitted_temp = [list(x[1]) for x in itertools.groupby(temp, |
|
175 | 217 | lambda x: x == u'%slides%') if not x[0]] |
|
176 | 218 | return splitted_temp |
|
177 | 219 | |
|
178 | 220 | def optional_header(self): |
|
179 | 221 | optional_header_body = self.template_split() |
|
180 | 222 | return optional_header_body[0] |
|
181 | 223 | |
|
182 | 224 | def optional_footer(self): |
|
183 | 225 | optional_footer_body = self.template_split() |
|
184 | 226 | return optional_footer_body[1] |
|
185 | 227 |
General Comments 0
You need to be logged in to leave comments.
Login now