markdown.py
119 lines
| 4.0 KiB
| text/x-python
|
PythonLexer
/ converters / markdown.py
David Warde-Farley
|
r8789 | """Converter implementing Markdown export. | ||
Implements a Converter that allows IPython notebooks to reasonably rendered | ||||
as a Markdown document. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# Copyright (c) 2012, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
# Our own imports | ||||
Matthias BUSSONNIER
|
r8618 | from converters.base import Converter | ||
Matthias BUSSONNIER
|
r8620 | from converters.utils import highlight, remove_ansi | ||
from IPython.utils.text import indent | ||||
Matthias BUSSONNIER
|
r8618 | |||
David Warde-Farley
|
r8749 | |||
David Warde-Farley
|
r8789 | #----------------------------------------------------------------------------- | ||
# Classes and functions | ||||
#----------------------------------------------------------------------------- | ||||
Matthias BUSSONNIER
|
r8618 | class ConverterMarkdown(Converter): | ||
extension = 'md' | ||||
David Warde-Farley
|
r8791 | # show_prompts controls the display of In[} and Out[] prompts for code | ||
# cells. | ||||
show_prompts = False | ||||
# If inline_prompt is False, display prompts on a separate line. | ||||
inline_prompt = False | ||||
Matthias BUSSONNIER
|
r8618 | |||
def __init__(self, infile, highlight_source=True, show_prompts=False, | ||||
inline_prompt=False): | ||||
super(ConverterMarkdown, self).__init__(infile) | ||||
self.highlight_source = highlight_source | ||||
self.show_prompts = show_prompts | ||||
self.inline_prompt = inline_prompt | ||||
def render_heading(self, cell): | ||||
David Warde-Farley
|
r8749 | return ['{0} {1}'.format('#' * cell.level, cell.source), ''] | ||
Matthias BUSSONNIER
|
r8618 | |||
def render_code(self, cell): | ||||
if not cell.input: | ||||
return [] | ||||
lines = [] | ||||
Maximilian Albert
|
r8746 | n = self._get_prompt_number(cell) | ||
Matthias BUSSONNIER
|
r8618 | if self.show_prompts and not self.inline_prompt: | ||
Maximilian Albert
|
r8746 | lines.extend(['*In[%s]:*' % n, '']) | ||
Matthias BUSSONNIER
|
r8618 | if self.show_prompts and self.inline_prompt: | ||
Maximilian Albert
|
r8746 | prompt = 'In[%s]: ' % n | ||
Matthias BUSSONNIER
|
r8618 | input_lines = cell.input.split('\n') | ||
David Warde-Farley
|
r8749 | src = (prompt + input_lines[0] + '\n' + | ||
indent('\n'.join(input_lines[1:]), nspaces=len(prompt))) | ||||
Matthias BUSSONNIER
|
r8618 | else: | ||
src = cell.input | ||||
src = highlight(src) if self.highlight_source else indent(src) | ||||
lines.extend([src, '']) | ||||
if cell.outputs and self.show_prompts and not self.inline_prompt: | ||||
Maximilian Albert
|
r8746 | lines.extend(['*Out[%s]:*' % n, '']) | ||
Matthias BUSSONNIER
|
r8618 | for output in cell.outputs: | ||
conv_fn = self.dispatch(output.output_type) | ||||
lines.extend(conv_fn(output)) | ||||
#lines.append('----') | ||||
lines.append('') | ||||
return lines | ||||
def render_markdown(self, cell): | ||||
return [cell.source, ''] | ||||
def render_raw(self, cell): | ||||
if self.raw_as_verbatim: | ||||
return [indent(cell.source), ''] | ||||
else: | ||||
return [cell.source, ''] | ||||
def render_pyout(self, output): | ||||
lines = [] | ||||
David Warde-Farley
|
r8749 | |||
Matthias BUSSONNIER
|
r8618 | ## if 'text' in output: | ||
Maximilian Albert
|
r8746 | ## lines.extend(['*Out[%s]:*' % self._get_prompt_number(cell), '']) | ||
Matthias BUSSONNIER
|
r8618 | |||
# output is a dictionary like object with type as a key | ||||
if 'latex' in output: | ||||
pass | ||||
if 'text' in output: | ||||
lines.extend(['<pre>', indent(output.text), '</pre>']) | ||||
lines.append('') | ||||
return lines | ||||
def render_pyerr(self, output): | ||||
# Note: a traceback is a *list* of frames. | ||||
return [indent(remove_ansi('\n'.join(output.traceback))), ''] | ||||
def _img_lines(self, img_file): | ||||
return ['', '![](%s)' % img_file, ''] | ||||
David Warde-Farley
|
r8749 | |||
Matthias BUSSONNIER
|
r8618 | def render_display_format_text(self, output): | ||
return [indent(output.text)] | ||||
def _unknown_lines(self, data): | ||||
return ['Warning: Unknown cell', data] | ||||
def render_display_format_html(self, output): | ||||
return [output.html] | ||||
def render_display_format_latex(self, output): | ||||
return ['LaTeX::', indent(output.latex)] | ||||
def render_display_format_json(self, output): | ||||
return ['JSON:', indent(output.json)] | ||||
def render_display_format_javascript(self, output): | ||||
return ['JavaScript:', indent(output.javascript)] | ||||