diff --git a/converters/template.py b/converters/template.py new file mode 100755 index 0000000..890c9c7 --- /dev/null +++ b/converters/template.py @@ -0,0 +1,103 @@ +"""Base classes for the notebook conversion pipeline. + +This module defines Converter, from which all objects designed to implement +a conversion of IPython notebooks to some other format should inherit. +""" +#----------------------------------------------------------------------------- +# 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 +#----------------------------------------------------------------------------- + +from __future__ import print_function, absolute_import + +# Stdlib imports +import jinja2 +import codecs +import io +import logging +import os +import pprint +import re +from types import FunctionType + +from jinja2 import Environment, PackageLoader, FileSystemLoader +env = Environment(loader=FileSystemLoader('./templates/')) + +# IPython imports +from IPython.nbformat import current as nbformat +from IPython.config.configurable import Configurable, SingletonConfigurable +from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum, + Any) + +# Our own imports +from IPython.utils.text import indent +from .utils import remove_ansi +#----------------------------------------------------------------------------- +# Class declarations +#----------------------------------------------------------------------------- +def rm_fake(strng): + return strng.replace('/files/', '') + +class ConversionException(Exception): + pass + + +def python_comment(string): + return '# '+'\n# '.join(string.split('\n')) + +env.filters['pycomment'] = python_comment +env.filters['indent'] = indent +env.filters['rm_fake'] = rm_fake +env.filters['rm_ansi'] = remove_ansi + +class ConverterTemplate(Configurable): + + display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] + #------------------------------------------------------------------------- + # Instance-level attributes that are set in the constructor for this + # class. + #------------------------------------------------------------------------- + infile = Any() + + + infile_dir = Unicode() + + def __init__(self, config=None, **kw): + self.template = env.get_template('python.tpl') + super(ConverterTemplate,self).__init__(config=config) + + def _get_prompt_number(self, cell): + return cell.prompt_number if hasattr(cell, 'prompt_number') \ + else self.blank_symbol + + + def process(self): + converted_cells = [] + for worksheet in self.nb.worksheets: + for cell in worksheet.cells: + cell.type = cell.cell_type + converted_cells.append(cell) + continue + if cell.cell_type in ('code'): + converted_cells.append({'type':'code','source':cell.input}) + else : + converted_cells.append({'type':cell.cell_type,'source': python_comment(cell.source)}) + + return converted_cells + + def convert(self, cell_separator='\n'): + return self.template.render(cells=self.process()) + + + def read(self, filename): + "read and parse notebook into NotebookNode called self.nb" + with open(filename) as f: + self.nb = nbformat.read(f, 'json') + diff --git a/runme.py b/runme.py new file mode 100644 index 0000000..ec48df1 --- /dev/null +++ b/runme.py @@ -0,0 +1,7 @@ +# coding: utf-8 +from converters.template import * +C = ConverterTemplate() +C.read('tests/ipynbref/IntroNumPy.orig.ipynb') +S = C.convert() +with open('temp.txt','w') as f: + f.write(S) diff --git a/templates/basic.tpl b/templates/basic.tpl new file mode 100644 index 0000000..294aed2 --- /dev/null +++ b/templates/basic.tpl @@ -0,0 +1,25 @@ +{%- block header -%} +{%- endblock header -%} +{%- block body -%} +{%- for cell in cells -%} + {%- if cell.type in ['code'] -%} + {%- block codecell scoped-%} + {%- endblock codecell-%} + {%- elif cell.type in ['markdown'] -%} + {%- block markdowncell scoped-%} + {%- endblock markdowncell -%} + {%- elif cell.type in ['heading'] -%} + {%- block headingcell scoped-%} + {%- endblock headingcell -%} + {%- elif cell.type in ['raw'] -%} + {%- block rawcell scoped-%} + {%- endblock rawcell -%} + {%- else -%} + {%- block unknowncell scoped-%} + {%- endblock unknowncell -%} + {%- endif -%} +{%- endfor -%} +{%- endblock body -%} + +{%- block footer -%} +{%- endblock footer -%} diff --git a/templates/python.tpl b/templates/python.tpl new file mode 100644 index 0000000..8d2f880 --- /dev/null +++ b/templates/python.tpl @@ -0,0 +1,37 @@ +{%- extends 'basic.tpl' -%} + +{% block codecell scoped %} +# In[{{cell.prompt_number if cell.prompt_number else ' '}}]: +{{ cell.input }} +{% if cell.outputs %} +# Out[{{cell.prompt_number}}]: +{%- for output in cell.outputs -%} + {% if output.output_type in ['pyout','stream']%} +{{ output.text| indent | pycomment}} + {% elif output.output_type in ['display_data'] %} +{{"# fucking display_data"}} + {% elif output.output_type in ['pyerr'] %} +{% for line in output.traceback %} +{{ line |indent| rm_ansi}} +{%- endfor -%} + {%- endif -%} +{%- endfor -%} +{%endif%} + +{% endblock codecell %} + +{% block markdowncell scoped %} +{{ cell.source | pycomment | rm_fake}} +{% endblock markdowncell %} + +{% block headingcell scoped %} +{{ '#' * cell.level }}{{ cell.source | pycomment}} +{% endblock headingcell %} + +{% block rawcell scoped %} +{{ cell.source | pycomment }} +{% endblock rawcell %} + +{% block unknowncell scoped %} +unknown type {{cell.type}} +{% endblock unknowncell %}