##// END OF EJS Templates
document
Matthias BUSSONNIER -
Show More
@@ -1,10 +1,13 b''
1 """Base classes for the notebook conversion pipeline.
1 """Base classes for the notebook conversion pipeline.
2
2
3 This module defines Converter, from which all objects designed to implement
3 This module defines ConverterTemplate, a highly configurable converter
4 a conversion of IPython notebooks to some other format should inherit.
4 that uses Jinja2 to convert notebook files into different format.
5
6 You can register both pre-transformers that will act on the notebook format
7 befor conversion and jinja filter that would then be availlable in the templates
5 """
8 """
6 #-----------------------------------------------------------------------------
9 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
10 # Copyright (c) 2013, the IPython Development Team.
8 #
11 #
9 # Distributed under the terms of the Modified BSD License.
12 # Distributed under the terms of the Modified BSD License.
10 #
13 #
@@ -16,7 +19,24 b' a conversion of IPython notebooks to some other format should inherit.'
16 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
17
20
18 from __future__ import print_function, absolute_import
21 from __future__ import print_function, absolute_import
22
23 # Stdlib imports
24 import io
25
26 # IPython imports
27 from IPython.utils.traitlets import MetaHasTraits
28 from IPython.utils.traitlets import (Unicode, List, Bool)
29 from IPython.config.configurable import Configurable
30 from IPython.nbformat import current as nbformat
31
32 # other libs/dependencies
33 from jinja2 import Environment, FileSystemLoader
34
35
36 # local import (pre-transformers)
19 import converters.transformers as trans
37 import converters.transformers as trans
38
39 # some jinja filters
20 from converters.jinja_filters import (python_comment, indent,
40 from converters.jinja_filters import (python_comment, indent,
21 rm_fake, remove_ansi, markdown, highlight,
41 rm_fake, remove_ansi, markdown, highlight,
22 ansi2html, markdown2latex, escape_tex, FilterDataType)
42 ansi2html, markdown2latex, escape_tex, FilterDataType)
@@ -25,12 +45,10 b' from converters.utils import markdown2rst'
25
45
26
46
27
47
28 # Stdlib imports
29 import io
30
48
31 from IPython.utils.traitlets import MetaHasTraits
49 # define differents environemnt with different
50 # delimiters not to conflict with languages inside
32
51
33 from jinja2 import Environment, FileSystemLoader
34 env = Environment(
52 env = Environment(
35 loader=FileSystemLoader([
53 loader=FileSystemLoader([
36 './templates/',
54 './templates/',
@@ -47,18 +65,6 b' texenv = Environment('
47 extensions=['jinja2.ext.loopcontrols']
65 extensions=['jinja2.ext.loopcontrols']
48 )
66 )
49
67
50 # IPython imports
51 from IPython.nbformat import current as nbformat
52 from IPython.config.configurable import Configurable
53 from IPython.utils.traitlets import ( Unicode, List, Bool)
54
55 #-----------------------------------------------------------------------------
56 # Class declarations
57 #-----------------------------------------------------------------------------
58 class ConversionException(Exception):
59 pass
60
61
62
68
63 texenv.block_start_string = '((*'
69 texenv.block_start_string = '((*'
64 texenv.block_end_string = '*))'
70 texenv.block_end_string = '*))'
@@ -71,6 +77,11 b" texenv.comment_end_string = '=))'"
71
77
72 texenv.filters['escape_tex'] = escape_tex
78 texenv.filters['escape_tex'] = escape_tex
73
79
80 #-----------------------------------------------------------------------------
81 # Class declarations
82 #-----------------------------------------------------------------------------
83 class ConversionException(Exception):
84 pass
74
85
75 class ConverterTemplate(Configurable):
86 class ConverterTemplate(Configurable):
76 """ A Jinja2 base converter templates
87 """ A Jinja2 base converter templates
@@ -107,22 +118,33 b' class ConverterTemplate(Configurable):'
107 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
118 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
108 """ Init a new converter.
119 """ Init a new converter.
109
120
110
121 config: the Configurable config object to pass around.
111 config: the Configurable confgg object to pass around
112
122
113 preprocessors: dict of **availlable** key/value function to run on
123 preprocessors: dict of **availlable** key/value function to run on
114 ipynb json data before conversion to extract/inline file,
124 ipynb json data before conversion to extract/inline file.
125 See `transformer.py` and `ConfigurableTransformers`
115
126
116 jinja_filter : dict of supplementary jinja filter that should be made
127 set the order in which the transformers should apply
117 availlable in template. If those are of Configurable Class type, they
128 with the `pre_transformer_order` trait of this class
118 will be instanciated with the config object as argument.
119
129
130 transformers registerd by this key will take precedence on
131 default one.
132
133
134 jinja_filters: dict of supplementary jinja filter that should be made
135 availlable in template. If those are of Configurable Class type,
136 they will be instanciated with the config object as argument.
137
138 user defined filter will overwrite the one availlable by default.
120 """
139 """
121 super(ConverterTemplate, self).__init__(config=config, **kw)
140 super(ConverterTemplate, self).__init__(config=config, **kw)
141
142 # variable parameters depending on the pype of jinja environement
122 self.env = texenv if self.tex_environement else env
143 self.env = texenv if self.tex_environement else env
123 self.ext = '.tplx' if self.tex_environement else '.tpl'
144 self.ext = '.tplx' if self.tex_environement else '.tpl'
124
145
125 for name in self.pre_transformer_order:
146 for name in self.pre_transformer_order:
147 # get the user-defined transformer first
126 transformer = getattr(preprocessors, name, getattr(trans, name, None))
148 transformer = getattr(preprocessors, name, getattr(trans, name, None))
127 if isinstance(transformer, MetaHasTraits):
149 if isinstance(transformer, MetaHasTraits):
128 transformer = transformer(config=config)
150 transformer = transformer(config=config)
@@ -145,6 +167,8 b' class ConverterTemplate(Configurable):'
145 self.env.filters['ansi2html'] = ansi2html
167 self.env.filters['ansi2html'] = ansi2html
146 self.env.filters['markdown2latex'] = markdown2latex
168 self.env.filters['markdown2latex'] = markdown2latex
147 self.env.filters['markdown2rst'] = markdown2rst
169 self.env.filters['markdown2rst'] = markdown2rst
170
171 ## user filter will overwrite
148 for key, filtr in jinja_filters.iteritems():
172 for key, filtr in jinja_filters.iteritems():
149 if isinstance(filtr, MetaHasTraits):
173 if isinstance(filtr, MetaHasTraits):
150 self.env.filters[key] = filtr(config=config)
174 self.env.filters[key] = filtr(config=config)
General Comments 0
You need to be logged in to leave comments. Login now