diff --git a/converters/template.py b/converters/template.py index 06d12f1..019db37 100755 --- a/converters/template.py +++ b/converters/template.py @@ -20,6 +20,7 @@ from __future__ import print_function, absolute_import # Stdlib imports import io import os +import re from IPython.utils import path from jinja2 import Environment, FileSystemLoader @@ -28,6 +29,11 @@ env = Environment( extensions=['jinja2.ext.loopcontrols'] ) +texenv = Environment( + loader=FileSystemLoader('./templates/tex/'), + extensions=['jinja2.ext.loopcontrols'] + ) + # IPython imports from IPython.nbformat import current as nbformat from IPython.config.configurable import Configurable @@ -105,6 +111,40 @@ env.filters['highlight'] = highlight env.filters['ansi2html'] = ansi2html + +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 = '*))' +texenv.variable_start_string = '(((' +texenv.variable_end_string = ')))' +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 +texenv.filters['rm_ansi'] = remove_ansi +texenv.filters['markdown'] = markdown +texenv.filters['highlight'] = highlight +texenv.filters['ansi2html'] = ansi2html + + def haspyout_transformer(nb,_): print('calling...') for worksheet in nb.worksheets: @@ -131,7 +171,7 @@ class ConverterTemplate(Configurable): infile_dir = Unicode() - def __init__(self, tplfile='fullhtml', preprocessors=[], config=None, **kw): + def __init__(self, tplfile='fullhtml', preprocessors=[], config=None,tex_environement=False, **kw): """ tplfile : jinja template file to process. @@ -141,7 +181,9 @@ class ConverterTemplate(Configurable): to extract/inline file, """ - self.template = env.get_template(tplfile+'.tpl') + 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) diff --git a/runme.py b/runme.py index f14663d..ad81d06 100755 --- a/runme.py +++ b/runme.py @@ -5,7 +5,15 @@ from __future__ import print_function import sys import io from converters.template import * -C = ConverterTemplate(tplfile=sys.argv[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) C.read(sys.argv[2]) output,rest = C.convert() diff --git a/templates/tex/latex_base.tplx b/templates/tex/latex_base.tplx new file mode 100644 index 0000000..e7d556c --- /dev/null +++ b/templates/tex/latex_base.tplx @@ -0,0 +1,56 @@ +((*- extends 'null.tplx' -*)) + +((* block in_prompt *)) +# In[(((cell.prompt_number if cell.prompt_number else ' ')))]: +((* endblock in_prompt *)) + +((* block output_prompt *)) +# Out[(((cell.prompt_number)))]:((* endblock output_prompt *)) + +((* block input *))((( cell.input ))) +((* endblock input *)) + + +((= Those Two are for error displaying +even if the first one seem to do nothing, +it introduces a new line + +=)) +((* block pyerr *))((( super() ))) +((* endblock pyerr *)) + +((* block traceback_line *)) +((( line |indent| rm_ansi )))((* endblock traceback_line *)) +((= .... =)) + + +((* block pyout *)) +((( output.text| indent | pycomment))) +((* endblock pyout *)) + +((* block stream *)) +((( output.text| indent | pycomment))) +((* endblock stream *)) + + + + +((* block display_data scoped *)) +# image file: +((* endblock display_data *)) + +((* block markdowncell scoped *)) +((( cell.source | pycomment ))) +((* 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 *))