##// END OF EJS Templates
Replacement of text with vaiable containing text.
damianavila -
Show More
@@ -1,254 +1,254 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&nbsp;[%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 107 u'slide_type = notes',
108 108 u'slide_type = skip'] # keep this one the last
109 109 text_cell_render = \
110 110 u'<div class="text_cell_render border-box-sizing rendered_html">'
111 111 for i, j in enumerate(text):
112 112 if j in self.delim and text[i - 1] == text_cell_render:
113 113 if j == self.delim[0]:
114 114 text[i - 1] = self.delim[0]
115 115 elif j == self.delim[1]:
116 116 text[i - 1] = self.delim[1]
117 117 elif j == self.delim[2]:
118 118 text[i - 1] = self.delim[2]
119 119 elif j == self.delim[3]:
120 120 text[i - 1] = self.delim[3]
121 121 elif j == self.delim[4]:
122 122 text[i - 1] = self.delim[4]
123 123 elif j == self.delim[5]:
124 124 text[i - 1] = self.delim[5]
125 125 else:
126 126 text[i - 1] = self.delim[6]
127 127 text[i] = text_cell_render
128 128 text[0] = u'slide_type = header_slide' # defensive code
129 129 text.append(u'slide_type = untouched') # to end search of skipped
130 130 return text
131 131
132 132 def build_slides(self):
133 133 "build the slides structure from text list and delimiters"
134 134 text = self.clean_text()
135 135 left = '<section>'
136 136 right = '</section>'
137 137 notes_start = '<aside class="notes">'
138 138 notes_end = '</aside>'
139 139 set_delim_skip = self.delim[:6] # to skip adjacent skkiped cells
140 140 set_delim_notes = self.delim[:5] # to show adjacent speaker notes
141 141 #elimination of skipped cells
142 142 for i, j in enumerate(text):
143 143 if j == u'slide_type = skip':
144 144 text.pop(i)
145 145 while not text[i] in set_delim_skip:
146 146 text.pop(i)
147 147 #encapsulation of notes cells
148 148 for i, j in enumerate(text):
149 149 if j == u'slide_type = notes':
150 150 text.pop(i)
151 151 temp_list = []
152 152 while not text[i] in set_delim_notes:
153 153 temp_list.append(text.pop(i))
154 154 else:
155 155 temp_list.insert(0, notes_start)
156 156 temp_list.append(notes_end)
157 157 text[i:i] = temp_list
158 158 # elimination of none names
159 159 for i, j in enumerate(text):
160 160 if j in [u'slide_type = untouched', u'slide_type = -']:
161 161 text.pop(i)
162 162 #generation of slides as a list of list
163 163 slides = [list(x[1]) for x in itertools.groupby(text,
164 164 lambda x: x == u'slide_type = header_slide') if not x[0]]
165 165 for slide in slides:
166 166 slide.insert(0, left)
167 167 slide.append(right)
168 168 # encapsulation of each fragment
169 169 for i, j in enumerate(slide):
170 170 if j == u'slide_type = fragment':
171 171 slide.pop(i)
172 172 slide[i] = slide[i][:4] + \
173 173 ' class="fragment"' + slide[i][4:]
174 174 # encapsulation of each nested slide
175 175 if u'slide_type = slide' in slide:
176 slide.insert(0, '<section>')
177 slide.append('</section>')
176 slide.insert(0, left)
177 slide.append(right)
178 178 for i, j in enumerate(slide):
179 179 if j == u'slide_type = slide':
180 180 slide[i] = right + left
181 181 return list(itertools.chain(*slides))
182 182
183 183 def render(self):
184 184 "read, convert, and save self.infile"
185 185 if not hasattr(self, 'nb'):
186 186 self.read()
187 187 self.output = self.convert()
188 188 assert(type(self.output) == unicode)
189 189 return self.save()
190 190
191 191 def save(self, outfile=None, encoding=None):
192 192 "read and parse notebook into self.nb"
193 193 if outfile is None:
194 194 outfile = self.outbase + '_slides.' + 'html'
195 195 if encoding is None:
196 196 encoding = self.default_encoding
197 197 with io.open(outfile, 'w', encoding=encoding) as f:
198 198 f.write(self.output)
199 199 return os.path.abspath(outfile)
200 200
201 201 def header_body(self):
202 202 "return the body of the header as a list of strings"
203 203 from pygments.formatters import HtmlFormatter
204 204 header = []
205 205 static = os.path.join(path.get_ipython_package_dir(),
206 206 'frontend', 'html', 'notebook', 'static',)
207 207 here = os.path.split(os.path.realpath(__file__))[0]
208 208 css = os.path.join(static, 'css')
209 209 for sheet in [
210 210 # do we need jquery and prettify?
211 211 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
212 212 # 'jquery-ui.min.css'),
213 213 # os.path.join(static, 'prettify', 'prettify.css'),
214 214 os.path.join(css, 'boilerplate.css'),
215 215 os.path.join(css, 'fbm.css'),
216 216 os.path.join(css, 'notebook.css'),
217 217 os.path.join(css, 'renderedhtml.css'),
218 218 # our overrides:
219 219 os.path.join(here, '..', 'css', 'reveal_html.css'),
220 220 ]:
221 221 header.extend(self._stylesheet(sheet))
222 222 # pygments css
223 223 pygments_css = HtmlFormatter().get_style_defs('.highlight')
224 224 header.extend(['<meta charset="UTF-8">'])
225 225 header.extend(self.in_tag('style', pygments_css,
226 226 dict(type='"text/css"')))
227 227 return header
228 228
229 229 def template_read(self, templ):
230 230 "read the reveal_template.html"
231 231 here = os.path.split(os.path.realpath(__file__))[0]
232 232 reveal_template = os.path.join(here, '..', 'templates',
233 233 templ)
234 234 with io.open(reveal_template, 'r', encoding='utf-8') as f:
235 235 template = f.readlines()
236 236 template = [s.strip() for s in template]
237 237 return template
238 238
239 239 def template_split(self):
240 240 "split the reveal_template.html in header and footer lists"
241 241 temp = self.template_read('reveal_base.html')
242 242 splitted_temp = [list(x[1]) for x in itertools.groupby(temp,
243 243 lambda x: x == u'%slides%') if not x[0]]
244 244 return splitted_temp
245 245
246 246 def optional_header(self):
247 247 optional_header_body = self.template_split()
248 248 return ['<!DOCTYPE html>', '<html>', '<head>'] + \
249 249 optional_header_body[0] + self.header_body() + \
250 250 ['</head>', '<body>']
251 251
252 252 def optional_footer(self):
253 253 optional_footer_body = self.template_split()
254 254 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