diff --git a/converters/template.py b/converters/template.py index 2e1d719..658a31f 100755 --- a/converters/template.py +++ b/converters/template.py @@ -37,7 +37,7 @@ texenv = Environment( # IPython imports from IPython.nbformat import current as nbformat from IPython.config.configurable import Configurable -from IPython.utils.traitlets import ( Unicode, Any) +from IPython.utils.traitlets import ( Unicode, Any, List) # Our own imports from IPython.utils.text import indent @@ -96,13 +96,10 @@ inlining = {} inlining['css'] = header_body() -def filter_data_type(output): - for fmt in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']: - if fmt in output: - return [fmt] -env.filters['filter_data_type'] = filter_data_type + + env.filters['pycomment'] = python_comment env.filters['indent'] = indent env.filters['rm_fake'] = rm_fake @@ -136,7 +133,6 @@ texenv.comment_start_string = '((=' texenv.comment_end_string = '=))' texenv.filters['escape_tex'] = escape_tex -texenv.filters['filter_data_type'] = filter_data_type texenv.filters['pycomment'] = python_comment texenv.filters['indent'] = indent texenv.filters['rm_fake'] = rm_fake @@ -164,7 +160,13 @@ def haspyout_transformer(nb,_): class ConverterTemplate(Configurable): """ A Jinja2 base converter templates""" - display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] + display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'], + config=True, + help= """ + A list of ast.NodeTransformer subclass instances, which will be applied + to user input before code is run. + """ + ) #------------------------------------------------------------------------- # Instance-level attributes that are set in the constructor for this # class. @@ -173,6 +175,13 @@ class ConverterTemplate(Configurable): infile_dir = Unicode() + def display_data_priority_changed(self, name, old, new): + print('== changed', name,old,new) + + def filter_data_type(self,output): + for fmt in self.display_data_priority: + if fmt in output: + return [fmt] def __init__(self, tplfile='fullhtml', preprocessors=[], config=None,tex_environement=False, **kw): """ @@ -186,11 +195,13 @@ class ConverterTemplate(Configurable): """ self.env = texenv if tex_environement else env self.ext = '.tplx' if tex_environement else '.tpl' - self.template = self.env.get_template(tplfile+self.ext) self.nb = None self.preprocessors = preprocessors self.preprocessors.append(haspyout_transformer) super(ConverterTemplate, self).__init__(config=config, **kw) + self.env.filters['filter_data_type'] = self.filter_data_type + self.template = self.env.get_template(tplfile+self.ext) + def process(self): diff --git a/runme.py b/runme.py index ad81d06..4712b5c 100755 --- a/runme.py +++ b/runme.py @@ -6,6 +6,7 @@ import sys import io from converters.template import * + template_file = sys.argv[1] if template_file.startswith('latex'): diff --git a/runme2.py b/runme2.py new file mode 100755 index 0000000..ab5269c --- /dev/null +++ b/runme2.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- +from __future__ import print_function +import sys +import io + +from converters.template import * +from converters.template import ConverterTemplate +from converters.html import ConverterHTML +# From IPython + +# All the stuff needed for the configurable things +from IPython.config.application import Application +from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum + + +class NbconvertApp(Application): + + + def __init__(self, **kwargs): + super(NbconvertApp, self).__init__(**kwargs) + self.classes.insert(0,ConverterTemplate) + # ensure those are registerd + + + def initialize(self, argv=None): + self.parse_command_line(argv) + cl_config = self.config + self.update_config(cl_config) + + + + def run(self): + """Convert a notebook to html in one step""" + template_file = (self.extra_args or [None])[0] + ipynb_file = (self.extra_args or [None])[1] + + template_file = sys.argv[1] + + if template_file.startswith('latex'): + tex_environement=True + else: + tex_environement=False + + C = ConverterTemplate(tplfile=sys.argv[1], tex_environement=tex_environement, config=self.config) + C.read(ipynb_file) + + output,rest = C.convert() + + print(output.encode('utf-8')) + +def main(): + """Convert a notebook to html in one step""" + app = NbconvertApp.instance() + app.initialize() + app.start() + app.run() +#----------------------------------------------------------------------------- +# Script main +#----------------------------------------------------------------------------- + +if __name__ == '__main__': + main() diff --git a/templates/Makefile b/templates/Makefile index 0cd594c..bb9b933 100644 --- a/templates/Makefile +++ b/templates/Makefile @@ -14,6 +14,7 @@ tex/%.tplx: %.tpl -e 's/}}/)))/g' \ -e 's/{#/((=/g' \ -e 's/#}/=))/g' \ + -e "s/tpl'/tplx'/g" \ $^ >> $@ diff --git a/templates/tex/display_priority.tplx b/templates/tex/display_priority.tplx index bc57368..78cd12a 100644 --- a/templates/tex/display_priority.tplx +++ b/templates/tex/display_priority.tplx @@ -1,5 +1,5 @@ ((= autogenerated file do not edit =)) -((*- extends 'null.tpl' -*)) +((*- extends 'null.tplx' -*)) ((=display data priority=)) diff --git a/templates/tex/latex_base.tplx b/templates/tex/latex_base.tplx index 4612ab4..9440eb6 100644 --- a/templates/tex/latex_base.tplx +++ b/templates/tex/latex_base.tplx @@ -1,15 +1,10 @@ -((*- extends 'null.tplx' -*)) +((*- extends 'display_priority.tplx' -*)) -((* block in_prompt *)) -# In[(((cell.prompt_number if cell.prompt_number else ' ')))]: -((* endblock in_prompt *)) +((* block in_prompt *))((* endblock in_prompt *)) -((* block output_prompt *)) -# Out[(((cell.prompt_number)))]:((* endblock output_prompt *)) +((* block output_prompt *))((* endblock output_prompt *)) -((* block codecell *)) -\begin{codecell} -((( super() ))) +((* block codecell *))\begin{codecell}((( super() ))) \end{codecell} ((* endblock *)) @@ -35,23 +30,32 @@ it introduces a new line ((= .... =)) +((*- block output_group -*)) +\begin{codeoutput} +((( super() ))) +\end{codeoutput}((* endblock *)) + + ((* block pyout *)) -((( output.text| indent | pycomment))) +((( output.text))) ((* endblock pyout *)) +((* block data_text *)) +\begin{verbatim} +((( output.text ))) +\end{verbatim} +((* endblock *)) + ((* block stream *)) -((( output.text| indent | pycomment))) +\begin{verbatim} +((( output.text))) +\end{verbatim} ((* endblock stream *)) -((* block display_data scoped *)) -# image file: -((* endblock display_data *)) - -((* block markdowncell scoped *)) -((( cell.source | markdown2latex ))) +((* block markdowncell scoped *))((( cell.source | markdown2latex ))) ((* endblock markdowncell *)) ((* block headingcell scoped *)) @@ -68,8 +72,7 @@ unknown type (((cell.type))) -((* block body *)) -\begin{document} +((* block body *))\begin{document} ((( super() ))) \end{document} ((* endblock*))