diff --git a/converters/template.py b/converters/template.py index 1b718ce..6e7f0ce 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, List) +from IPython.utils.traitlets import ( Unicode, Any, List, Bool) # Our own imports from IPython.utils.text import indent @@ -112,21 +112,6 @@ def _new_figure(data, fmt, count): inlining = {} inlining['css'] = header_body() - - - - - -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 -env.filters['ansi2html'] = ansi2html - - - LATEX_SUBS = ( (re.compile(r'\\'), r'\\textbackslash'), (re.compile(r'([{}_#%&$])'), r'\\\1'), @@ -150,15 +135,6 @@ texenv.comment_start_string = '((=' texenv.comment_end_string = '=))' texenv.filters['escape_tex'] = escape_tex -texenv.filters['pycomment'] = python_comment -texenv.filters['indent'] = indent -texenv.filters['rm_fake'] = rm_fake -texenv.filters['rm_ansi'] = remove_ansi -texenv.filters['markdown'] = markdown -texenv.filters['highlight'] = highlight -texenv.filters['ansi2html'] = ansi2html -texenv.filters['markdown2latex'] = markdown2latex - def cell_preprocessor(function): """ wrap a function to be executed on all cells of a notebook @@ -195,7 +171,7 @@ def haspyout_transformer(cell, other, count): @cell_preprocessor -def outline_figure_transformer(cell,other,count): +def extract_figure_transformer(cell,other,count): for i,out in enumerate(cell.get('outputs', [])): for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']: if out.hasattr(type): @@ -204,7 +180,7 @@ def outline_figure_transformer(cell,other,count): out['key_'+type] = figname other[figname] = data count = count+1 - return nb,other + return cell,other class ConverterTemplate(Configurable): @@ -249,14 +225,25 @@ class ConverterTemplate(Configurable): to extract/inline file, """ + super(ConverterTemplate, self).__init__(config=config, **kw) self.env = texenv if tex_environement else env self.ext = '.tplx' if tex_environement else '.tpl' self.nb = None self.preprocessors = preprocessors self.preprocessors.append(haspyout_transformer) - self.preprocessors.append(outline_figure_transformer) - super(ConverterTemplate, self).__init__(config=config, **kw) + if self.extract_figures: + self.preprocessors.append(extract_figure_transformer) + self.env.filters['filter_data_type'] = self.filter_data_type + self.env.filters['pycomment'] = python_comment + self.env.filters['indent'] = indent + self.env.filters['rm_fake'] = rm_fake + self.env.filters['rm_ansi'] = remove_ansi + self.env.filters['markdown'] = markdown + self.env.filters['highlight'] = highlight + self.env.filters['ansi2html'] = ansi2html + self.env.filters['markdown2latex'] = markdown2latex + self.template = self.env.get_template(tplfile+self.ext) @@ -268,10 +255,14 @@ class ConverterTemplate(Configurable): """ nb = self.nb + # dict of 'resources' that could be made by the preprocessors + # like key/value data to extract files from ipynb like in latex conversion + resources = {} + for preprocessor in self.preprocessors: - nb,others = preprocessor(nb,{}) + nb,resources = preprocessor(nb,resources) - return nb + return nb, resources def convert(self): """ convert the ipynb file @@ -279,7 +270,8 @@ class ConverterTemplate(Configurable): return both the converted ipynb file and a dict containing potential other resources """ - return self.template.render(nb=self.process(), inlining=inlining), {} + nb,resources = self.process() + return self.template.render(nb=nb, inlining=inlining), resources def read(self, filename): diff --git a/runme.py b/runme.py index ab5269c..b10908a 100755 --- a/runme.py +++ b/runme.py @@ -44,13 +44,28 @@ class NbconvertApp(Application): else: tex_environement=False - C = ConverterTemplate(tplfile=sys.argv[1], tex_environement=tex_environement, config=self.config) + C = ConverterTemplate(tplfile=sys.argv[1], + tex_environement=tex_environement, + config=self.config) C.read(ipynb_file) - output,rest = C.convert() + output,resources = C.convert() print(output.encode('utf-8')) + keys = resources.keys() + if keys : + print(''' +====================== Keys in Resources ================================== +''') + print(resources.keys()) + print(""" +=========================================================================== +you are responsible from writing those data do a file in the right place if +they need to be. +=========================================================================== + """) + def main(): """Convert a notebook to html in one step""" app = NbconvertApp.instance()