diff --git a/converters/jinja_filters.py b/converters/jinja_filters.py new file mode 100755 index 0000000..981b953 --- /dev/null +++ b/converters/jinja_filters.py @@ -0,0 +1,42 @@ +#----------------------------------------------------------------------------- +# 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. +#----------------------------------------------------------------------------- + +from __future__ import absolute_import + +# Stdlib imports +import re + +from IPython.utils.text import indent +from markdown import markdown +from .utils import remove_ansi +from .utils import highlight, ansi2html +from .utils import markdown2latex +#----------------------------------------------------------------------------- +# Class declarations +#----------------------------------------------------------------------------- +def rm_fake(strng): + return strng.replace('/files/', '') + +def python_comment(string): + return '# '+'\n# '.join(string.split('\n')) + +LATEX_SUBS = ( + (re.compile(r'\\'), r'\\textbackslash'), + (re.compile(r'([{}_#%&$])'), r'\\\1'), + (re.compile(r'~'), r'\~{}'), + (re.compile(r'\^'), r'\^{}'), + (re.compile(r'"'), r"''"), + (re.compile(r'\.\.\.+'), r'\\ldots'), +) + +def escape_tex(value): + newval = value + for pattern, replacement in LATEX_SUBS: + newval = pattern.sub(replacement, newval) + return newval + diff --git a/converters/template.py b/converters/template.py index d3871ec..6466e62 100755 --- a/converters/template.py +++ b/converters/template.py @@ -18,12 +18,14 @@ a conversion of IPython notebooks to some other format should inherit. from __future__ import print_function, absolute_import from .transformers import extract_figure_transformer import converters.transformers as trans +from converters.jinja_filters import (python_comment, indent, + rm_fake, remove_ansi, markdown, highlight, + ansi2html, markdown2latex, escape_tex) # Stdlib imports import io import os -import re from IPython.utils import path from jinja2 import Environment, FileSystemLoader @@ -48,27 +50,13 @@ from IPython.nbformat import current as nbformat from IPython.config.configurable import Configurable from IPython.utils.traitlets import ( Unicode, Any, List, Bool) -# Our own imports -from IPython.utils.text import indent -from .utils import remove_ansi -from markdown import markdown -from .utils import highlight, ansi2html -from .utils import markdown2latex #----------------------------------------------------------------------------- # Class declarations #----------------------------------------------------------------------------- -def rm_fake(strng): - return strng.replace('/files/', '') - class ConversionException(Exception): pass -def python_comment(string): - return '# '+'\n# '.join(string.split('\n')) - - - def header_body(): """Return the body of the header as a list of strings.""" @@ -101,19 +89,6 @@ def header_body(): header.append(pygments_css) return header -# todo, make the key part configurable. -def _new_figure(data, fmt, count): - """Create a new figure file in the given format. - - Returns a path relative to the input file. - """ - figname = '_fig_%02i.%s' % (count, fmt) - - # Binary files are base64-encoded, SVG is already XML - if fmt in ('png', 'jpg', 'pdf'): - data = data.decode('base64') - - return figname,data @@ -121,20 +96,7 @@ def _new_figure(data, fmt, count): inlining = {} inlining['css'] = header_body() -LATEX_SUBS = ( - (re.compile(r'\\'), r'\\textbackslash'), - (re.compile(r'([{}_#%&$])'), r'\\\1'), - (re.compile(r'~'), r'\~{}'), - (re.compile(r'\^'), r'\^{}'), - (re.compile(r'"'), r"''"), - (re.compile(r'\.\.\.+'), r'\\ldots'), -) - -def escape_tex(value): - newval = value - for pattern, replacement in LATEX_SUBS: - newval = pattern.sub(replacement, newval) - return newval + texenv.block_start_string = '((*' texenv.block_end_string = '*))' @@ -144,23 +106,6 @@ texenv.comment_start_string = '((=' texenv.comment_end_string = '=))' texenv.filters['escape_tex'] = escape_tex -def cell_preprocessor(function): - """ wrap a function to be executed on all cells of a notebook - - wrapped function parameters : - cell : the cell - other : external resources - index : index of the cell - """ - def wrappedfunc(nb,other): - for worksheet in nb.worksheets : - for index, cell in enumerate(worksheet.cells): - worksheet.cells[index],other= function(cell,other,index) - return nb,other - return wrappedfunc - - - class ConverterTemplate(Configurable): """ A Jinja2 base converter templates""" @@ -203,7 +148,7 @@ class ConverterTemplate(Configurable): infile_dir = Unicode() - def filter_data_type(self,output): + def filter_data_type(self, output): for fmt in self.display_data_priority: if fmt in output: return [fmt] @@ -224,7 +169,7 @@ class ConverterTemplate(Configurable): self.nb = None self.preprocessors = preprocessors for name in self.pre_transformer_order: - self.preprocessors.append(getattr(trans,'haspyout_transformer')) + self.preprocessors.append(getattr(trans, name)) if self.extract_figures: self.preprocessors.append(extract_figure_transformer) @@ -253,7 +198,7 @@ class ConverterTemplate(Configurable): resources = {} for preprocessor in self.preprocessors: - nb,resources = preprocessor(nb,resources) + nb, resources = preprocessor(nb, resources) return nb, resources @@ -263,7 +208,7 @@ class ConverterTemplate(Configurable): return both the converted ipynb file and a dict containing potential other resources """ - nb,resources = self.process() + nb, resources = self.process() return self.template.render(nb=nb, inlining=inlining), resources diff --git a/converters/transformers.py b/converters/transformers.py index 94cff3d..d79b2eb 100755 --- a/converters/transformers.py +++ b/converters/transformers.py @@ -10,11 +10,11 @@ def cell_preprocessor(function): other : external resources index : index of the cell """ - def wrappedfunc(nb,other): + def wrappedfunc(nb, other): for worksheet in nb.worksheets : for index, cell in enumerate(worksheet.cells): - worksheet.cells[index],other= function(cell,other,index) - return nb,other + worksheet.cells[index], other = function(cell, other, index) + return nb, other return wrappedfunc @@ -33,18 +33,32 @@ def haspyout_transformer(cell, other, count): if out.output_type == 'pyout': cell.haspyout = True break - return cell,other + return cell, other +# todo, make the key part configurable. +def _new_figure(data, fmt, count): + """Create a new figure file in the given format. + + Returns a path relative to the input file. + """ + figname = '_fig_%02i.%s' % (count, fmt) + + # Binary files are base64-encoded, SVG is already XML + if fmt in ('png', 'jpg', 'pdf'): + data = data.decode('base64') + + return figname, data + @cell_preprocessor -def extract_figure_transformer(cell,other,count): - for i,out in enumerate(cell.get('outputs', [])): +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): - figname,data = _new_figure(out[type], type, count) + figname, data = _new_figure(out[type], type, count) cell.outputs[i][type] = figname out['key_'+type] = figname other[figname] = data count = count+1 - return cell,other + return cell, other