##// END OF EJS Templates
new quick-html converter which has no dependencies
Paul Ivanov -
Show More
@@ -56,14 +56,17 b' class Converter(object):'
56 def dispatch(self, cell_type):
56 def dispatch(self, cell_type):
57 """return cell_type dependent render method, for example render_code
57 """return cell_type dependent render method, for example render_code
58 """
58 """
59 # XXX: unknown_cell here is RST specific - make it generic
59 return getattr(self, 'render_' + cell_type, unknown_cell)
60 return getattr(self, 'render_' + cell_type, unknown_cell)
60
61
61 def convert(self):
62 def convert(self):
62 lines = []
63 lines = []
64 lines.extend(self.optional_header())
63 for cell in self.nb.worksheets[0].cells:
65 for cell in self.nb.worksheets[0].cells:
64 conv_fn = self.dispatch(cell.cell_type)
66 conv_fn = self.dispatch(cell.cell_type)
65 lines.extend(conv_fn(cell))
67 lines.extend(conv_fn(cell))
66 lines.append('')
68 lines.append('')
69 lines.extend(self.optional_footer())
67 return '\n'.join(lines)
70 return '\n'.join(lines)
68
71
69 def render(self):
72 def render(self):
@@ -87,6 +90,12 b' class Converter(object):'
87 f.write(self.output.encode(encoding))
90 f.write(self.output.encode(encoding))
88 return infile
91 return infile
89
92
93 def optional_header():
94 pass
95
96 def optional_footer():
97 pass
98
90 def render_heading(self, cell):
99 def render_heading(self, cell):
91 """convert a heading cell
100 """convert a heading cell
92
101
@@ -201,6 +210,99 b' class ConverterRST(Converter):'
201
210
202 return lines
211 return lines
203
212
213 class ConverterQuickHTML(Converter):
214 extension = 'html'
215 figures_counter = 0
216
217 def optional_header(self):
218 # XXX: inject the IPython standard CSS into here
219 s = """<html>
220 <head>
221 </head>
222
223 <body>
224 """
225 return s.splitlines()
226
227 def optional_footer(self):
228 s = """</body>
229 </html>
230 """
231 return s.splitlines()
232
233 @DocInherit
234 def render_heading(self, cell):
235 marker = cell.level
236 return ['<h{1}>\n {0}\n</h{1}>'.format(cell.source, marker)]
237
238 @DocInherit
239 def render_code(self, cell):
240 if not cell.input:
241 return []
242
243 lines = ['<table>']
244 lines.append('<tr><td><tt>In [<b>%s</b>]:</tt></td><td><tt>' % cell.prompt_number)
245 lines.append("<br>\n".join(cell.input.splitlines()))
246 lines.append('</tt></td></tr>')
247
248 for output in cell.outputs:
249 lines.append('<tr><td></td><td>')
250 conv_fn = self.dispatch(output.output_type)
251 lines.extend(conv_fn(output))
252 lines.append('</td></tr>')
253
254 lines.append('</table>')
255 return lines
256
257 @DocInherit
258 def render_markdown(self, cell):
259 return ["<pre>"+cell.source+"</pre>"]
260
261 @DocInherit
262 def render_plaintext(self, cell):
263 return ["<pre>"+cell.source+"</pre>"]
264
265 @DocInherit
266 def render_pyout(self, output):
267 lines = ['<tr><td><tt>Out[<b>%s</b>]:</tt></td></tr>' % output.prompt_number, '<td>']
268
269 # output is a dictionary like object with type as a key
270 if 'latex' in output:
271 lines.append("<pre>")
272 lines.extend(indent(output.latex))
273 lines.append("</pre>")
274
275 if 'text' in output:
276 lines.append("<pre>")
277 lines.extend(indent(output.text))
278 lines.append("</pre>")
279
280 return lines
281
282 @DocInherit
283 def render_display_data(self, output):
284 lines = []
285
286 if 'png' in output:
287 infile = 'nb_figure_%s.png' % self.figures_counter
288 fullname = os.path.join(self.dirpath, infile)
289 with open(fullname, 'w') as f:
290 f.write(output.png.decode('base64'))
291
292 self.figures_counter += 1
293 lines.append('<img src="%s">' % infile)
294 lines.append('')
295
296 return lines
297
298 @DocInherit
299 def render_stream(self, output):
300 lines = []
301
302 if 'text' in output:
303 lines.append(output.text)
304
305 return lines
204
306
205 def rst2simplehtml(infile):
307 def rst2simplehtml(infile):
206 """Convert a rst file to simplified html suitable for blogger.
308 """Convert a rst file to simplified html suitable for blogger.
@@ -261,6 +363,9 b" def main(infile, format='rst'):"
261 converter = ConverterRST(infile)
363 converter = ConverterRST(infile)
262 rstfname = converter.render()
364 rstfname = converter.render()
263 rst2simplehtml(rstfname)
365 rst2simplehtml(rstfname)
366 elif format == 'quick-html':
367 converter = ConverterQuickHTML(infile)
368 rstfname = converter.render()
264
369
265
370
266 if __name__ == '__main__':
371 if __name__ == '__main__':
General Comments 0
You need to be logged in to leave comments. Login now