##// END OF EJS Templates
Merge pull request #57 from maxalbert/cleanup...
Merge pull request #57 from maxalbert/cleanup Cleanup of format selection Two small cleanups/fixes: Much cleaner way of finding the right converter. This will result in much cleaner diffs when changing the API, adding arguments, etc. It also allows us to extract the known formats automatically. Fixed an assertion which didn't actually check the file extension but an arbitrary occurrence of 'rst'

File last commit:

r8620:66b7dfea
r8738:fa0e3fea merge
Show More
rst.py
73 lines | 2.3 KiB | text/x-python | PythonLexer
from converters.base import Converter
from converters.utils import markdown2rst, rst_directive, remove_ansi
from IPython.utils.text import indent
class ConverterRST(Converter):
extension = 'rst'
heading_level = {1: '=', 2: '-', 3: '`', 4: '\'', 5: '.', 6: '~'}
def render_heading(self, cell):
marker = self.heading_level[cell.level]
return ['{0}\n{1}\n'.format(cell.source, marker * len(cell.source))]
def render_code(self, cell):
if not cell.input:
return []
lines = ['In[%s]:' % cell.prompt_number, '']
lines.extend(rst_directive('.. code:: python', cell.input))
for output in cell.outputs:
conv_fn = self.dispatch(output.output_type)
lines.extend(conv_fn(output))
return lines
def render_markdown(self, cell):
#return [cell.source]
return [markdown2rst(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 = ['Out[%s]:' % output.prompt_number, '']
# output is a dictionary like object with type as a key
if 'latex' in output:
lines.extend(rst_directive('.. math::', output.latex))
if 'text' in output:
lines.extend(rst_directive('.. parsed-literal::', output.text))
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 ['.. image:: %s' % img_file, '']
def render_display_format_text(self, output):
return rst_directive('.. parsed-literal::', output.text)
def _unknown_lines(self, data):
return rst_directive('.. warning:: Unknown cell') + [data]
def render_display_format_html(self, output):
return rst_directive('.. raw:: html', output.html)
def render_display_format_latex(self, output):
return rst_directive('.. math::', output.latex)
def render_display_format_json(self, output):
return rst_directive('.. raw:: json', output.json)
def render_display_format_javascript(self, output):
return rst_directive('.. raw:: javascript', output.javascript)