diff --git a/converters/template.py b/converters/template.py index 890c9c7..c42896a 100755 --- a/converters/template.py +++ b/converters/template.py @@ -39,6 +39,8 @@ from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessSt # Our own imports from IPython.utils.text import indent from .utils import remove_ansi +from markdown import markdown +from .utils import highlight #----------------------------------------------------------------------------- # Class declarations #----------------------------------------------------------------------------- @@ -56,6 +58,8 @@ env.filters['pycomment'] = python_comment env.filters['indent'] = indent env.filters['rm_fake'] = rm_fake env.filters['rm_ansi'] = remove_ansi +env.filters['markdown'] = markdown +env.filters['highlight'] = highlight class ConverterTemplate(Configurable): @@ -69,8 +73,8 @@ class ConverterTemplate(Configurable): infile_dir = Unicode() - def __init__(self, config=None, **kw): - self.template = env.get_template('python.tpl') + def __init__(self, tplfile='fullhtml', config=None, **kw): + self.template = env.get_template(tplfile+'.tpl') super(ConverterTemplate,self).__init__(config=config) def _get_prompt_number(self, cell): diff --git a/runme.py b/runme.py index ec48df1..6b05f08 100644 --- a/runme.py +++ b/runme.py @@ -1,7 +1,9 @@ # coding: utf-8 +import sys from converters.template import * -C = ConverterTemplate() +C = ConverterTemplate(tplfile=sys.argv[1]) C.read('tests/ipynbref/IntroNumPy.orig.ipynb') + S = C.convert() with open('temp.txt','w') as f: f.write(S) diff --git a/templates/basichtml.tpl b/templates/basichtml.tpl new file mode 100644 index 0000000..9b6e7d8 --- /dev/null +++ b/templates/basichtml.tpl @@ -0,0 +1,61 @@ +{%- extends 'basic.tpl' -%} + + + + +{% block codecell scoped %} +
+
+
In [{{cell.prompt_number if cell.prompt_number else ' '}}]:
+
+{{ cell.input|highlight -}} +
+
+{% if cell.outputs %} +{%- for output in cell.outputs -%} +
+
+
+
+ {%- if output.output_type in ['pyout','stream'] %} +
+
{{ output.text }}
+
+
+
+ {%- elif output.output_type in ['display_data'] %} +
+{{"# image file: 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 | markdown| rm_fake}} +
+{%- endblock markdowncell %} + +{% block headingcell scoped %} +
+ + {{cell.source}} + +
+{% endblock headingcell %} + +{% block rawcell scoped %} +{{ cell.source | pycomment }} +{% endblock rawcell %} + +{% block unknowncell scoped %} +unknown type {{cell.type}} +{% endblock unknowncell %} diff --git a/templates/fullhtml.tpl b/templates/fullhtml.tpl new file mode 100644 index 0000000..a602b56 --- /dev/null +++ b/templates/fullhtml.tpl @@ -0,0 +1,787 @@ +{%- extends 'basichtml.tpl' -%} + +{%- block header -%} + + + + + + + + + + + + +{%- endblock header -%} + + +{% block body %} +{{ super() }} + +{% endblock body %} + + + + + + + + +{% block footer %} + +{% endblock footer %} diff --git a/templates/null.tpl b/templates/null.tpl new file mode 100644 index 0000000..bf51c9a --- /dev/null +++ b/templates/null.tpl @@ -0,0 +1,38 @@ +{%- extends 'basic.tpl' -%} + +{% block codecell scoped %} +{% block in_prompt %}{% endblock in_prompt %} +{% block input %}{% endblock input %} +{% if cell.outputs %} +{% block output_prompt %}{% endblock output_prompt %} +{%- for output in cell.outputs -%} + {%- if output.output_type in ['pyout']%} + {% block pyout scoped %}{% endblock pyout %} + {%- elif output.output_type in ['stream'] %} + {% block stream scoped %}{% endblock stream %} + {%- elif output.output_type in ['display_data'] %} + {% block display_data scoped %}{% endblock display_data %} + {%- elif output.output_type in ['pyerr'] %} + {% block pyerr scoped %} + {%- for line in output.traceback %} + {% block traceback_line scoped %} + {% endblock traceback_line %} + {%- endfor %} + {% endblock pyerr %} + {%- endif %} +{%- endfor -%} +{% endif %} + +{% endblock codecell %} + +{% block markdowncell scoped %} +{% endblock markdowncell %} + +{% block headingcell scoped %} +{% endblock headingcell %} + +{% block rawcell scoped %} +{% endblock rawcell %} + +{% block unknowncell scoped %} +{% endblock unknowncell %} diff --git a/templates/python.tpl b/templates/python.tpl index 40cfc5e..7ad1c02 100644 --- a/templates/python.tpl +++ b/templates/python.tpl @@ -1,5 +1,20 @@ -{%- extends 'basic.tpl' -%} +{%- extends 'null.tpl' -%} +{% block in_prompt %} +# In[{{cell.prompt_number if cell.prompt_number else ' '}}]: +{% endblock in_prompt %} + +{% block traceback_line %} +{{ line |indent| rm_ansi }} +{% endblock traceback_line %} + + +{% block pyout %} +{{ output.text| indent | pycomment}} +{% endblock pyout %} + + +{# {% block codecell scoped %} # In[{{cell.prompt_number if cell.prompt_number else ' '}}]: {{ cell.input }} @@ -7,31 +22,27 @@ # 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'] %} {{"# image file: 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}} +{#{{ cell.source | pycomment | rm_fake}}#} {% endblock markdowncell %} {% block headingcell scoped %} -{{ '#' * cell.level }}{{ cell.source | pycomment}} +{#{{ '#' * cell.level }}{{ cell.source | pycomment}}#} {% endblock headingcell %} {% block rawcell scoped %} -{{ cell.source | pycomment }} +{#{{ cell.source | pycomment }}#} {% endblock rawcell %} {% block unknowncell scoped %} -unknown type {{cell.type}} +{#unknown type {{cell.type}}#} {% endblock unknowncell %}