##// END OF EJS Templates
stateless converter
Matthias BUSSONNIER -
Show More
@@ -57,7 +57,7 b' from IPython.utils.traitlets import ( Unicode, Any, List, Bool)'
57 class ConversionException(Exception):
57 class ConversionException(Exception):
58 pass
58 pass
59
59
60
60 #todo move this out
61 def header_body():
61 def header_body():
62 """Return the body of the header as a list of strings."""
62 """Return the body of the header as a list of strings."""
63
63
@@ -109,7 +109,13 b" texenv.filters['escape_tex'] = escape_tex"
109
109
110
110
111 class ConverterTemplate(Configurable):
111 class ConverterTemplate(Configurable):
112 """ A Jinja2 base converter templates"""
112 """ A Jinja2 base converter templates
113
114 Preprocess the ipynb files, feed it throug jinja templates,
115 and spit an converted files and a data object with other data
116
117 shoudl be mostly configurable
118 """
113
119
114 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
120 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
115 config=True,
121 config=True,
@@ -120,7 +126,7 b' class ConverterTemplate(Configurable):'
120 """
126 """
121 )
127 )
122
128
123 pre_transformer_order = List(['haspyout_transformer', 'Foobar'],
129 pre_transformer_order = List(['haspyout_transformer'],
124 config=True,
130 config=True,
125 help= """
131 help= """
126 An ordered list of pre transformer to apply to the ipynb
132 An ordered list of pre transformer to apply to the ipynb
@@ -142,7 +148,7 b' class ConverterTemplate(Configurable):'
142
148
143 template_file = Unicode('',
149 template_file = Unicode('',
144 config=True,
150 config=True,
145 help=""" whetever """ )
151 help=""" Name of the template file to use """ )
146 #-------------------------------------------------------------------------
152 #-------------------------------------------------------------------------
147 # Instance-level attributes that are set in the constructor for this
153 # Instance-level attributes that are set in the constructor for this
148 # class.
154 # class.
@@ -152,33 +158,38 b' class ConverterTemplate(Configurable):'
152
158
153 infile_dir = Unicode()
159 infile_dir = Unicode()
154
160
161 #todo: move to filter
155 def filter_data_type(self, output):
162 def filter_data_type(self, output):
163 """ return the first availlable format in priority """
156 for fmt in self.display_data_priority:
164 for fmt in self.display_data_priority:
157 if fmt in output:
165 if fmt in output:
158 return [fmt]
166 return [fmt]
159
167
160 def __init__(self, preprocessors=[], config=None, **kw):
168 preprocessors = []
169
170 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
161 """
171 """
162 config: the Configurable confg object to pass around
172 config: the Configurable confg object to pass around
163
173
164 preprocessors: list of function to run on ipynb json data before conversion
174 preprocessors: dict of **availlable** key/value function to run on ipynb json data before conversion
165 to extract/inline file,
175 to extract/inline file,
166
176
167 """
177 """
168 super(ConverterTemplate, self).__init__(config=config, **kw)
178 super(ConverterTemplate, self).__init__(config=config, **kw)
169 self.env = texenv if self.tex_environement else env
179 self.env = texenv if self.tex_environement else env
170 self.ext = '.tplx' if self.tex_environement else '.tpl'
180 self.ext = '.tplx' if self.tex_environement else '.tpl'
171 self.nb = None
172 self.preprocessors = preprocessors
173
181
174 for name in self.pre_transformer_order:
182 for name in self.pre_transformer_order:
175 tr = getattr(trans, name)
183 transformer = getattr(preprocessors, name, getattr(trans, name, None))
176 if isinstance(tr, MetaHasTraits):
184 if isinstance(transformer, MetaHasTraits):
177 tr = tr(config=config)
185 transformer = transformer(config=config)
178 self.preprocessors.append(tr)
186 self.preprocessors.append(transformer)
187
188 ## for compat, remove later
179 if self.extract_figures:
189 if self.extract_figures:
180 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
190 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
181
191
192 ##
182 self.env.filters['filter_data_type'] = self.filter_data_type
193 self.env.filters['filter_data_type'] = self.filter_data_type
183 self.env.filters['pycomment'] = python_comment
194 self.env.filters['pycomment'] = python_comment
184 self.env.filters['indent'] = indent
195 self.env.filters['indent'] = indent
@@ -188,16 +199,17 b' class ConverterTemplate(Configurable):'
188 self.env.filters['highlight'] = highlight
199 self.env.filters['highlight'] = highlight
189 self.env.filters['ansi2html'] = ansi2html
200 self.env.filters['ansi2html'] = ansi2html
190 self.env.filters['markdown2latex'] = markdown2latex
201 self.env.filters['markdown2latex'] = markdown2latex
202 for k, v in jinja_filters.iteritems():
203 self.env.filters[k] = v
191
204
192 self.template = self.env.get_template(self.template_file+self.ext)
205 self.template = self.env.get_template(self.template_file+self.ext)
193
206
194
207
195 def process(self):
208 def process(self, nb):
196 """
209 """
197 preprocess the notebook json for easier use with the templates.
210 preprocess the notebook json for easier use with the templates.
198 will call all the `preprocessor`s in order before returning it.
211 will call all the `preprocessor`s in order before returning it.
199 """
212 """
200 nb = self.nb
201
213
202 # dict of 'resources' that could be made by the preprocessors
214 # dict of 'resources' that could be made by the preprocessors
203 # like key/value data to extract files from ipynb like in latex conversion
215 # like key/value data to extract files from ipynb like in latex conversion
@@ -208,20 +220,18 b' class ConverterTemplate(Configurable):'
208
220
209 return nb, resources
221 return nb, resources
210
222
211 def convert(self):
223 def convert(self, nb):
212 """ convert the ipynb file
224 """ convert the ipynb file
213
225
214 return both the converted ipynb file and a dict containing potential
226 return both the converted ipynb file and a dict containing potential
215 other resources
227 other resources
216 """
228 """
217 nb, resources = self.process()
229 nb, resources = self.process(nb)
218 return self.template.render(nb=nb, inlining=inlining), resources
230 return self.template.render(nb=nb, inlining=inlining), resources
219
231
220
232
221 def read(self, filename):
233 def from_filename(self, filename):
222 "read and parse notebook into NotebookNode called self.nb"
234 "read and parse notebook into NotebookNode called self.nb"
223 with io.open(filename) as f:
235 with io.open(filename) as f:
224 self.nb = nbformat.read(f, 'json')
236 return self.convert(nbformat.read(f, 'json'))
225
226
227
237
@@ -90,9 +90,8 b' class NbconvertApp(Application):'
90 template_file = sys.argv[1]
90 template_file = sys.argv[1]
91
91
92 C = ConverterTemplate(config=self.config)
92 C = ConverterTemplate(config=self.config)
93 C.read(ipynb_file)
94
93
95 output,resources = C.convert()
94 output,resources = C.from_filename(ipynb_file)
96 if self.stdout :
95 if self.stdout :
97 print(output.encode('utf-8'))
96 print(output.encode('utf-8'))
98
97
@@ -24,7 +24,6 b' def check_null_profile(profile):'
24 loader = PyFileConfigLoader(profile, path=[os.path.join(os.getcwdu(),'profile/test')])
24 loader = PyFileConfigLoader(profile, path=[os.path.join(os.getcwdu(),'profile/test')])
25 config = loader.load_config()
25 config = loader.load_config()
26 C = ConverterTemplate(config=config)
26 C = ConverterTemplate(config=config)
27 C.read('tests/ipynbref/IntroNumPy.orig.ipynb')
27 result,_ = C.from_filename('tests/ipynbref/IntroNumPy.orig.ipynb')
28 result,_ = C.convert()
29 nt.assert_equal(result,'')
28 nt.assert_equal(result,'')
30
29
General Comments 0
You need to be logged in to leave comments. Login now