##// END OF EJS Templates
docs
docs

File last commit:

r8948:c38103d2 merge
r9528:389a4fd3
Show More
markdown.py
128 lines | 4.5 KiB | text/x-python | PythonLexer
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
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
Anthony Scopatz
convters sub-package use relative imports
r8933 from .base import Converter
from .utils import highlight, remove_ansi
Matthias BUSSONNIER
all seem to convert again
r8620 from IPython.utils.text import indent
Matthias BUSSONNIER
latex working
r8618
David Warde-Farley
PEP8-ify rest of the repository.
r8749
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 #-----------------------------------------------------------------------------
David Warde-Farley
More descriptive comment headers.
r8808 # Class declarations
David Warde-Farley
Introduce standard structure from coding guidelines in converters/.
r8789 #-----------------------------------------------------------------------------
Matthias BUSSONNIER
latex working
r8618 class ConverterMarkdown(Converter):
David Warde-Farley
More documentation.
r8811 #-------------------------------------------------------------------------
# Class-level attributes determining the behaviour of the class but
# probably not varying from instance to instance.
#-------------------------------------------------------------------------
Matthias BUSSONNIER
latex working
r8618 extension = 'md'
David Warde-Farley
More documentation.
r8811 #-------------------------------------------------------------------------
# Instance-level attributes that are set in the constructor for this
# class.
#-------------------------------------------------------------------------
David Warde-Farley
Declare several members at the class level.
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
latex working
r8618
def __init__(self, infile, highlight_source=True, show_prompts=False,
Bussonnier Matthias
fix text for exclude
r8945 inline_prompt=False, **kw):
super(ConverterMarkdown, self).__init__(infile, **kw)
Matthias BUSSONNIER
latex working
r8618 self.highlight_source = highlight_source
self.show_prompts = show_prompts
self.inline_prompt = inline_prompt
def render_heading(self, cell):
David Warde-Farley
PEP8-ify rest of the repository.
r8749 return ['{0} {1}'.format('#' * cell.level, cell.source), '']
Matthias BUSSONNIER
latex working
r8618
def render_code(self, cell):
if not cell.input:
return []
lines = []
Maximilian Albert
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
r8746 n = self._get_prompt_number(cell)
David Warde-Farley
Simplify control flow with nesting....
r8807 if self.show_prompts:
if not self.inline_prompt:
lines.extend(['*In[%s]:*' % n, ''])
else:
prompt = 'In[%s]: ' % n
input_lines = cell.input.split('\n')
src = (prompt + input_lines[0] + '\n' +
indent('\n'.join(input_lines[1:]), nspaces=len(prompt)))
Matthias BUSSONNIER
latex working
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
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
r8746 lines.extend(['*Out[%s]:*' % n, ''])
Matthias BUSSONNIER
latex working
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
PEP8-ify rest of the repository.
r8749
Matthias BUSSONNIER
latex working
r8618 ## if 'text' in output:
Maximilian Albert
Replace ad-hoc/broken code with safe method to extract the prompt_number of a cell....
r8746 ## lines.extend(['*Out[%s]:*' % self._get_prompt_number(cell), ''])
Matthias BUSSONNIER
latex working
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
PEP8-ify rest of the repository.
r8749
Matthias BUSSONNIER
latex working
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)]