Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eccc507

Browse files
committed
new quick-html converter which has no dependencies
1 parent 2b3bd1f commit eccc507

1 file changed

Lines changed: 105 additions & 0 deletions

File tree

nbconvert.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,17 @@ def extension(self):
5656
def dispatch(self, cell_type):
5757
"""return cell_type dependent render method, for example render_code
5858
"""
59+
# XXX: unknown_cell here is RST specific - make it generic
5960
return getattr(self, 'render_' + cell_type, unknown_cell)
6061

6162
def convert(self):
6263
lines = []
64+
lines.extend(self.optional_header())
6365
for cell in self.nb.worksheets[0].cells:
6466
conv_fn = self.dispatch(cell.cell_type)
6567
lines.extend(conv_fn(cell))
6668
lines.append('')
69+
lines.extend(self.optional_footer())
6770
return '\n'.join(lines)
6871

6972
def render(self):
@@ -87,6 +90,12 @@ def save(self, infile=None, encoding=None):
8790
f.write(self.output.encode(encoding))
8891
return infile
8992

93+
def optional_header():
94+
pass
95+
96+
def optional_footer():
97+
pass
98+
9099
def render_heading(self, cell):
91100
"""convert a heading cell
92101
@@ -201,6 +210,99 @@ def render_stream(self, output):
201210

202211
return lines
203212

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
204306

205307
def rst2simplehtml(infile):
206308
"""Convert a rst file to simplified html suitable for blogger.
@@ -261,6 +363,9 @@ def main(infile, format='rst'):
261363
converter = ConverterRST(infile)
262364
rstfname = converter.render()
263365
rst2simplehtml(rstfname)
366+
elif format == 'quick-html':
367+
converter = ConverterQuickHTML(infile)
368+
rstfname = converter.render()
264369

265370

266371
if __name__ == '__main__':

0 commit comments

Comments
 (0)