Show More
@@ -43,6 +43,9 b' class BasicHtmlExporter(exporter.Exporter):' | |||
|
43 | 43 | help="Name of the template file to use") |
|
44 | 44 | |
|
45 | 45 | def _register_transformers(self): |
|
46 | """ | |
|
47 | Register all of the transformers needed for this exporter. | |
|
48 | """ | |
|
46 | 49 | |
|
47 | 50 | #Register the transformers of the base class. |
|
48 | 51 | super(BasicHtmlExporter, self)._register_transformers() |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module containing |
|
|
2 | Module containing single call export functions. | |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2013, the IPython Development Team. |
@@ -9,15 +9,69 b' Module containing easy to use conversion functions.' | |||
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | import sys | |
|
17 | import inspect | |
|
18 | ||
|
12 | 19 | from .exporter import Exporter |
|
13 | 20 | |
|
14 | 21 | from IPython.nbformat.v3.nbbase import NotebookNode |
|
15 | 22 | |
|
23 | from .fullhtml import FullHtmlExporter | |
|
24 | from .basichtml import BasicHtmlExporter | |
|
25 | from .latex import LatexExporter | |
|
26 | from .markdown import MarkdownExporter | |
|
27 | from .python import PythonExporter | |
|
28 | from .python_armor import PythonArmorExporter | |
|
29 | from .reveal import RevealExporter | |
|
30 | from .rst import RstExporter | |
|
31 | from .sphinx_howto import SphinxHowtoExporter | |
|
32 | from .sphinx_manual import SphinxManualExporter | |
|
33 | ||
|
16 | 34 | #----------------------------------------------------------------------------- |
|
17 | 35 | # Functions |
|
18 | 36 | #----------------------------------------------------------------------------- |
|
19 | 37 | |
|
20 | 38 | def export(nb, config=None, transformers=None, filters=None, exporter_type=Exporter): |
|
39 | """ | |
|
40 | Export a notebook object using specific exporter class. | |
|
41 | ||
|
42 | Parameters | |
|
43 | ---------- | |
|
44 | config : config | |
|
45 | User configuration instance. | |
|
46 | transformers : list[of transformer] | |
|
47 | Custom transformers to apply to the notebook prior to engaging | |
|
48 | the Jinja template engine. Any transformers specified here | |
|
49 | will override existing transformers if a naming conflict | |
|
50 | occurs. | |
|
51 | filters : list[of filter] | |
|
52 | Custom filters to make accessible to the Jinja templates. Any | |
|
53 | filters specified here will override existing filters if a | |
|
54 | naming conflict occurs. | |
|
55 | exporter_type: | |
|
56 | Class type of the exporter that should be used. This method | |
|
57 | will initialize it's own instance of the class. It is | |
|
58 | ASSUMED that the class type provided exposes a | |
|
59 | constructor (__init__) with the same signature as the | |
|
60 | base Exporter class. | |
|
61 | ||
|
62 | Returns | |
|
63 | ---------- | |
|
64 | tuple- output, resources, exporter_instance | |
|
65 | output : str | |
|
66 | Jinja 2 output. This is the resulting converted notebook. | |
|
67 | resources : dictionary | |
|
68 | Dictionary of resources used prior to and during the conversion | |
|
69 | process. | |
|
70 | exporter_instance : Exporter | |
|
71 | Instance of the Exporter class used to export the document. Useful | |
|
72 | to caller because it provides a 'file_extension' property which | |
|
73 | specifies what extension the output should be saved as. | |
|
74 | """ | |
|
21 | 75 | |
|
22 | 76 | #Check arguments |
|
23 | 77 | if exporter_type is None: |
@@ -29,7 +83,8 b' def export(nb, config=None, transformers=None, filters=None, exporter_type=Expor' | |||
|
29 | 83 | raise TypeError("nb is None") |
|
30 | 84 | |
|
31 | 85 | #Create the exporter |
|
32 |
exporter_instance = exporter_type(preprocessors=transformers, |
|
|
86 | exporter_instance = exporter_type(preprocessors=transformers, | |
|
87 | jinja_filters=filters, config=config) | |
|
33 | 88 | |
|
34 | 89 | #Try to convert the notebook using the appropriate conversion function. |
|
35 | 90 | if isinstance(nb, NotebookNode): |
@@ -41,11 +96,388 b' def export(nb, config=None, transformers=None, filters=None, exporter_type=Expor' | |||
|
41 | 96 | return output, resources, exporter_instance |
|
42 | 97 | |
|
43 | 98 | |
|
44 | def load_class(template_name): | |
|
45 | class_name = template_name[0].upper() + template_name[1:] + "Exporter" | |
|
46 | module = __import__('nbconvert.api.' + template_name, fromlist=[class_name]) | |
|
47 | return getattr(module, class_name) | |
|
99 | def export_sphinx_manual(nb, config=None, transformers=None, filters=None): | |
|
100 | """ | |
|
101 | Export a notebook object to Sphinx Manual LaTeX | |
|
102 | ||
|
103 | Parameters | |
|
104 | ---------- | |
|
105 | config : config | |
|
106 | User configuration instance. | |
|
107 | transformers : list[of transformer] | |
|
108 | Custom transformers to apply to the notebook prior to engaging | |
|
109 | the Jinja template engine. Any transformers specified here | |
|
110 | will override existing transformers if a naming conflict | |
|
111 | occurs. | |
|
112 | filters : list[of filter] | |
|
113 | Custom filters to make accessible to the Jinja templates. Any | |
|
114 | filters specified here will override existing filters if a | |
|
115 | naming conflict occurs. | |
|
116 | ||
|
117 | Returns | |
|
118 | ---------- | |
|
119 | tuple- output, resources, exporter_instance | |
|
120 | output : str | |
|
121 | Jinja 2 output. This is the resulting converted notebook. | |
|
122 | resources : dictionary | |
|
123 | Dictionary of resources used prior to and during the conversion | |
|
124 | process. | |
|
125 | exporter_instance : Exporter | |
|
126 | Instance of the Exporter class used to export the document. Useful | |
|
127 | to caller because it provides a 'file_extension' property which | |
|
128 | specifies what extension the output should be saved as. | |
|
129 | """ | |
|
130 | return export(nb, config, transformers, filters, SphinxManualExporter) | |
|
131 | ||
|
132 | ||
|
133 | def export_sphinx_howto(nb, config=None, transformers=None, filters=None): | |
|
134 | """ | |
|
135 | Export a notebook object to Sphinx HowTo LaTeX | |
|
136 | ||
|
137 | Parameters | |
|
138 | ---------- | |
|
139 | config : config | |
|
140 | User configuration instance. | |
|
141 | transformers : list[of transformer] | |
|
142 | Custom transformers to apply to the notebook prior to engaging | |
|
143 | the Jinja template engine. Any transformers specified here | |
|
144 | will override existing transformers if a naming conflict | |
|
145 | occurs. | |
|
146 | filters : list[of filter] | |
|
147 | Custom filters to make accessible to the Jinja templates. Any | |
|
148 | filters specified here will override existing filters if a | |
|
149 | naming conflict occurs. | |
|
150 | ||
|
151 | Returns | |
|
152 | ---------- | |
|
153 | tuple- output, resources, exporter_instance | |
|
154 | output : str | |
|
155 | Jinja 2 output. This is the resulting converted notebook. | |
|
156 | resources : dictionary | |
|
157 | Dictionary of resources used prior to and during the conversion | |
|
158 | process. | |
|
159 | exporter_instance : Exporter | |
|
160 | Instance of the Exporter class used to export the document. Useful | |
|
161 | to caller because it provides a 'file_extension' property which | |
|
162 | specifies what extension the output should be saved as. | |
|
163 | """ | |
|
164 | return export(nb, config, transformers, filters, SphinxHowtoExporter) | |
|
165 | ||
|
166 | ||
|
167 | def export_basic_html(nb, config=None, transformers=None, filters=None): | |
|
168 | """ | |
|
169 | Export a notebook object to Basic HTML | |
|
170 | ||
|
171 | Parameters | |
|
172 | ---------- | |
|
173 | config : config | |
|
174 | User configuration instance. | |
|
175 | transformers : list[of transformer] | |
|
176 | Custom transformers to apply to the notebook prior to engaging | |
|
177 | the Jinja template engine. Any transformers specified here | |
|
178 | will override existing transformers if a naming conflict | |
|
179 | occurs. | |
|
180 | filters : list[of filter] | |
|
181 | Custom filters to make accessible to the Jinja templates. Any | |
|
182 | filters specified here will override existing filters if a | |
|
183 | naming conflict occurs. | |
|
184 | ||
|
185 | Returns | |
|
186 | ---------- | |
|
187 | tuple- output, resources, exporter_instance | |
|
188 | output : str | |
|
189 | Jinja 2 output. This is the resulting converted notebook. | |
|
190 | resources : dictionary | |
|
191 | Dictionary of resources used prior to and during the conversion | |
|
192 | process. | |
|
193 | exporter_instance : Exporter | |
|
194 | Instance of the Exporter class used to export the document. Useful | |
|
195 | to caller because it provides a 'file_extension' property which | |
|
196 | specifies what extension the output should be saved as. | |
|
197 | """ | |
|
198 | return export(nb, config, transformers, filters, BasicHtmlExporter) | |
|
199 | ||
|
200 | ||
|
201 | def export_full_html(nb, config=None, transformers=None, filters=None): | |
|
202 | """ | |
|
203 | Export a notebook object to Full HTML | |
|
204 | ||
|
205 | Parameters | |
|
206 | ---------- | |
|
207 | config : config | |
|
208 | User configuration instance. | |
|
209 | transformers : list[of transformer] | |
|
210 | Custom transformers to apply to the notebook prior to engaging | |
|
211 | the Jinja template engine. Any transformers specified here | |
|
212 | will override existing transformers if a naming conflict | |
|
213 | occurs. | |
|
214 | filters : list[of filter] | |
|
215 | Custom filters to make accessible to the Jinja templates. Any | |
|
216 | filters specified here will override existing filters if a | |
|
217 | naming conflict occurs. | |
|
218 | ||
|
219 | Returns | |
|
220 | ---------- | |
|
221 | tuple- output, resources, exporter_instance | |
|
222 | output : str | |
|
223 | Jinja 2 output. This is the resulting converted notebook. | |
|
224 | resources : dictionary | |
|
225 | Dictionary of resources used prior to and during the conversion | |
|
226 | process. | |
|
227 | exporter_instance : Exporter | |
|
228 | Instance of the Exporter class used to export the document. Useful | |
|
229 | to caller because it provides a 'file_extension' property which | |
|
230 | specifies what extension the output should be saved as. | |
|
231 | """ | |
|
232 | return export(nb, config, transformers, filters, FullHtmlExporter) | |
|
233 | ||
|
234 | ||
|
235 | def export_latex(nb, config=None, transformers=None, filters=None): | |
|
236 | """ | |
|
237 | Export a notebook object to LaTeX | |
|
238 | ||
|
239 | Parameters | |
|
240 | ---------- | |
|
241 | config : config | |
|
242 | User configuration instance. | |
|
243 | transformers : list[of transformer] | |
|
244 | Custom transformers to apply to the notebook prior to engaging | |
|
245 | the Jinja template engine. Any transformers specified here | |
|
246 | will override existing transformers if a naming conflict | |
|
247 | occurs. | |
|
248 | filters : list[of filter] | |
|
249 | Custom filters to make accessible to the Jinja templates. Any | |
|
250 | filters specified here will override existing filters if a | |
|
251 | naming conflict occurs. | |
|
252 | ||
|
253 | Returns | |
|
254 | ---------- | |
|
255 | tuple- output, resources, exporter_instance | |
|
256 | output : str | |
|
257 | Jinja 2 output. This is the resulting converted notebook. | |
|
258 | resources : dictionary | |
|
259 | Dictionary of resources used prior to and during the conversion | |
|
260 | process. | |
|
261 | exporter_instance : Exporter | |
|
262 | Instance of the Exporter class used to export the document. Useful | |
|
263 | to caller because it provides a 'file_extension' property which | |
|
264 | specifies what extension the output should be saved as. | |
|
265 | """ | |
|
266 | return export(nb, config, transformers, filters, LatexExporter) | |
|
267 | ||
|
268 | ||
|
269 | def export_markdown(nb, config=None, transformers=None, filters=None): | |
|
270 | """ | |
|
271 | Export a notebook object to Markdown | |
|
272 | ||
|
273 | Parameters | |
|
274 | ---------- | |
|
275 | config : config | |
|
276 | User configuration instance. | |
|
277 | transformers : list[of transformer] | |
|
278 | Custom transformers to apply to the notebook prior to engaging | |
|
279 | the Jinja template engine. Any transformers specified here | |
|
280 | will override existing transformers if a naming conflict | |
|
281 | occurs. | |
|
282 | filters : list[of filter] | |
|
283 | Custom filters to make accessible to the Jinja templates. Any | |
|
284 | filters specified here will override existing filters if a | |
|
285 | naming conflict occurs. | |
|
286 | ||
|
287 | Returns | |
|
288 | ---------- | |
|
289 | tuple- output, resources, exporter_instance | |
|
290 | output : str | |
|
291 | Jinja 2 output. This is the resulting converted notebook. | |
|
292 | resources : dictionary | |
|
293 | Dictionary of resources used prior to and during the conversion | |
|
294 | process. | |
|
295 | exporter_instance : Exporter | |
|
296 | Instance of the Exporter class used to export the document. Useful | |
|
297 | to caller because it provides a 'file_extension' property which | |
|
298 | specifies what extension the output should be saved as. | |
|
299 | """ | |
|
300 | return export(nb, config, transformers, filters, MarkdownExporter) | |
|
301 | ||
|
302 | ||
|
303 | def export_python(nb, config=None, transformers=None, filters=None): | |
|
304 | """ | |
|
305 | Export a notebook object to Python | |
|
306 | ||
|
307 | Parameters | |
|
308 | ---------- | |
|
309 | config : config | |
|
310 | User configuration instance. | |
|
311 | transformers : list[of transformer] | |
|
312 | Custom transformers to apply to the notebook prior to engaging | |
|
313 | the Jinja template engine. Any transformers specified here | |
|
314 | will override existing transformers if a naming conflict | |
|
315 | occurs. | |
|
316 | filters : list[of filter] | |
|
317 | Custom filters to make accessible to the Jinja templates. Any | |
|
318 | filters specified here will override existing filters if a | |
|
319 | naming conflict occurs. | |
|
320 | ||
|
321 | Returns | |
|
322 | ---------- | |
|
323 | tuple- output, resources, exporter_instance | |
|
324 | output : str | |
|
325 | Jinja 2 output. This is the resulting converted notebook. | |
|
326 | resources : dictionary | |
|
327 | Dictionary of resources used prior to and during the conversion | |
|
328 | process. | |
|
329 | exporter_instance : Exporter | |
|
330 | Instance of the Exporter class used to export the document. Useful | |
|
331 | to caller because it provides a 'file_extension' property which | |
|
332 | specifies what extension the output should be saved as. | |
|
333 | """ | |
|
334 | return export(nb, config, transformers, filters, PythonExporter) | |
|
335 | ||
|
336 | ||
|
337 | def export_python_armor(nb, config=None, transformers=None, filters=None): | |
|
338 | """ | |
|
339 | Export a notebook object to Python (Armor) | |
|
340 | ||
|
341 | Parameters | |
|
342 | ---------- | |
|
343 | config : config | |
|
344 | User configuration instance. | |
|
345 | transformers : list[of transformer] | |
|
346 | Custom transformers to apply to the notebook prior to engaging | |
|
347 | the Jinja template engine. Any transformers specified here | |
|
348 | will override existing transformers if a naming conflict | |
|
349 | occurs. | |
|
350 | filters : list[of filter] | |
|
351 | Custom filters to make accessible to the Jinja templates. Any | |
|
352 | filters specified here will override existing filters if a | |
|
353 | naming conflict occurs. | |
|
354 | ||
|
355 | Returns | |
|
356 | ---------- | |
|
357 | tuple- output, resources, exporter_instance | |
|
358 | output : str | |
|
359 | Jinja 2 output. This is the resulting converted notebook. | |
|
360 | resources : dictionary | |
|
361 | Dictionary of resources used prior to and during the conversion | |
|
362 | process. | |
|
363 | exporter_instance : Exporter | |
|
364 | Instance of the Exporter class used to export the document. Useful | |
|
365 | to caller because it provides a 'file_extension' property which | |
|
366 | specifies what extension the output should be saved as. | |
|
367 | """ | |
|
368 | return export(nb, config, transformers, filters, PythonArmorExporter) | |
|
369 | ||
|
370 | ||
|
371 | def export_reveal(nb, config=None, transformers=None, filters=None): | |
|
372 | """ | |
|
373 | Export a notebook object to Reveal | |
|
374 | ||
|
375 | Parameters | |
|
376 | ---------- | |
|
377 | config : config | |
|
378 | User configuration instance. | |
|
379 | transformers : list[of transformer] | |
|
380 | Custom transformers to apply to the notebook prior to engaging | |
|
381 | the Jinja template engine. Any transformers specified here | |
|
382 | will override existing transformers if a naming conflict | |
|
383 | occurs. | |
|
384 | filters : list[of filter] | |
|
385 | Custom filters to make accessible to the Jinja templates. Any | |
|
386 | filters specified here will override existing filters if a | |
|
387 | naming conflict occurs. | |
|
388 | ||
|
389 | Returns | |
|
390 | ---------- | |
|
391 | tuple- output, resources, exporter_instance | |
|
392 | output : str | |
|
393 | Jinja 2 output. This is the resulting converted notebook. | |
|
394 | resources : dictionary | |
|
395 | Dictionary of resources used prior to and during the conversion | |
|
396 | process. | |
|
397 | exporter_instance : Exporter | |
|
398 | Instance of the Exporter class used to export the document. Useful | |
|
399 | to caller because it provides a 'file_extension' property which | |
|
400 | specifies what extension the output should be saved as. | |
|
401 | """ | |
|
402 | return export(nb, config, transformers, filters, RevealExporter) | |
|
403 | ||
|
404 | ||
|
405 | def export_rst(nb, config=None, transformers=None, filters=None): | |
|
406 | """ | |
|
407 | Export a notebook object to RST | |
|
408 | ||
|
409 | Parameters | |
|
410 | ---------- | |
|
411 | config : config | |
|
412 | User configuration instance. | |
|
413 | transformers : list[of transformer] | |
|
414 | Custom transformers to apply to the notebook prior to engaging | |
|
415 | the Jinja template engine. Any transformers specified here | |
|
416 | will override existing transformers if a naming conflict | |
|
417 | occurs. | |
|
418 | filters : list[of filter] | |
|
419 | Custom filters to make accessible to the Jinja templates. Any | |
|
420 | filters specified here will override existing filters if a | |
|
421 | naming conflict occurs. | |
|
422 | ||
|
423 | Returns | |
|
424 | ---------- | |
|
425 | tuple- output, resources, exporter_instance | |
|
426 | output : str | |
|
427 | Jinja 2 output. This is the resulting converted notebook. | |
|
428 | resources : dictionary | |
|
429 | Dictionary of resources used prior to and during the conversion | |
|
430 | process. | |
|
431 | exporter_instance : Exporter | |
|
432 | Instance of the Exporter class used to export the document. Useful | |
|
433 | to caller because it provides a 'file_extension' property which | |
|
434 | specifies what extension the output should be saved as. | |
|
435 | """ | |
|
436 | return export(nb, config, transformers, filters, RstExporter) | |
|
48 | 437 | |
|
49 | 438 | |
|
50 | 439 | def export_by_name(nb, template_name, config=None, transformers=None, filters=None): |
|
51 | return export(nb, config, transformers, filters, load_class(template_name)) No newline at end of file | |
|
440 | """ | |
|
441 | Export a notebook object to a template type by its name. Reflection | |
|
442 | (Inspect) is used to find the template's corresponding explicit export | |
|
443 | method defined in this module. That method is then called directly. | |
|
444 | ||
|
445 | Parameters | |
|
446 | ---------- | |
|
447 | template_name : str | |
|
448 | Name of the template style to export to. | |
|
449 | config : config | |
|
450 | User configuration instance. | |
|
451 | transformers : list[of transformer] | |
|
452 | Custom transformers to apply to the notebook prior to engaging | |
|
453 | the Jinja template engine. Any transformers specified here | |
|
454 | will override existing transformers if a naming conflict | |
|
455 | occurs. | |
|
456 | filters : list[of filter] | |
|
457 | Custom filters to make accessible to the Jinja templates. Any | |
|
458 | filters specified here will override existing filters if a | |
|
459 | naming conflict occurs. | |
|
460 | ||
|
461 | Returns | |
|
462 | ---------- | |
|
463 | tuple- output, resources, exporter_instance | |
|
464 | output : str | |
|
465 | Jinja 2 output. This is the resulting converted notebook. | |
|
466 | resources : dictionary | |
|
467 | Dictionary of resources used prior to and during the conversion | |
|
468 | process. | |
|
469 | exporter_instance : Exporter | |
|
470 | Instance of the Exporter class used to export the document. Useful | |
|
471 | to caller because it provides a 'file_extension' property which | |
|
472 | specifies what extension the output should be saved as. | |
|
473 | """ | |
|
474 | ||
|
475 | #Use reflection to get functions defined in this module. | |
|
476 | cls_functions = inspect.getmembers(sys.modules[__name__], inspect.isfunction) | |
|
477 | ||
|
478 | #Check if the characters following "export_" (7 char) equals the template name. | |
|
479 | for function in cls_functions: | |
|
480 | function_name = function.__name__.lower() | |
|
481 | if (len(function_name) > 7 and function_name[7:] == template_name.lower()): | |
|
482 | return function(nb, config, transformers, filters) | |
|
483 | No newline at end of file |
@@ -1,10 +1,5 b'' | |||
|
1 | """Exporter for the notebook conversion pipeline. | |
|
2 | ||
|
3 | This module defines Exporter, a highly configurable converter | |
|
4 | that uses Jinja2 to export notebook files into different format. | |
|
5 | ||
|
6 | You can register both pre-transformers that will act on the notebook format | |
|
7 | before conversion and jinja filter that would then be available in the templates | |
|
1 | """This module defines Exporter, a highly configurable converter | |
|
2 | that uses Jinja2 to export notebook files into different formats. | |
|
8 | 3 | """ |
|
9 | 4 | |
|
10 | 5 | #----------------------------------------------------------------------------- |
@@ -47,15 +42,10 b' import nbconvert.filters.ansi' | |||
|
47 | 42 | import nbconvert.transformers.extractfigure |
|
48 | 43 | import nbconvert.transformers.coalescestreams |
|
49 | 44 | |
|
50 | ||
|
51 | 45 | #----------------------------------------------------------------------------- |
|
52 | 46 | # Globals and constants |
|
53 | 47 | #----------------------------------------------------------------------------- |
|
54 | 48 | |
|
55 | #Standard Jinja2 environment constants | |
|
56 | TEMPLATE_PATH = "/../templates/" | |
|
57 | TEMPLATE_SKELETON_PATH = "/../templates/skeleton/" | |
|
58 | ||
|
59 | 49 | #Jinja2 extensions to load. |
|
60 | 50 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] |
|
61 | 51 | |
@@ -64,7 +54,15 b" JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols']" | |||
|
64 | 54 | #----------------------------------------------------------------------------- |
|
65 | 55 | |
|
66 | 56 | class Exporter(Configurable): |
|
67 | ||
|
57 | """ | |
|
58 | Exports notebooks into other file formats. Uses Jinja 2 templating engine | |
|
59 | to output new formats. Inherit from this class if you are creating a new | |
|
60 | template type along with new filters/transformers. If the filters/ | |
|
61 | transformers provided by default suffice, there is no need to inherit from | |
|
62 | this class. Instead, override the template_file and file_extension | |
|
63 | traits via a config file. | |
|
64 | """ | |
|
65 | ||
|
68 | 66 | template_file = Unicode( |
|
69 | 67 | '', config=True, |
|
70 | 68 | help="Name of the template file to use") |
@@ -74,17 +72,49 b' class Exporter(Configurable):' | |||
|
74 | 72 | help="Extension of the file that should be written to disk" |
|
75 | 73 | ) |
|
76 | 74 | |
|
75 | template_path = Unicode( | |
|
76 | "/../templates/", config=True, | |
|
77 | help="Path where the template files are located.") | |
|
78 | ||
|
79 | template_skeleton_path = Unicode( | |
|
80 | "/../templates/skeleton/", config=True, | |
|
81 | help="Path where the template skeleton files are located.") | |
|
82 | ||
|
83 | #Jinja block definitions | |
|
84 | jinja_comment_block_start = Unicode(None, config=True) | |
|
85 | jinja_comment_block_end = Unicode(None, config=True) | |
|
86 | jinja_variable_block_start = Unicode(None, config=True) | |
|
87 | jinja_variable_block_end = Unicode(None, config=True) | |
|
88 | jinja_logic_block_start = Unicode(None, config=True) | |
|
89 | jinja_logic_block_end = Unicode(None, config=True) | |
|
90 | ||
|
77 | 91 | #Extension that the template files use. |
|
78 | template_extension = ".tpl" | |
|
92 | template_extension = Unicode(".tpl", config=True) | |
|
79 | 93 | |
|
80 | 94 | #Processors that process the input data prior to the export, set in the |
|
81 | 95 | #constructor for this class. |
|
82 | 96 | preprocessors = [] |
|
83 | 97 | |
|
84 | # Public Constructor ##################################################### | |
|
85 | 98 | |
|
86 | 99 | def __init__(self, transformers=None, filters=None, config=None, **kw): |
|
100 | """ | |
|
101 | Public constructor | |
|
87 | 102 |
|
|
103 | Parameters | |
|
104 | ---------- | |
|
105 | transformers : list[of transformer] | |
|
106 | Custom transformers to apply to the notebook prior to engaging | |
|
107 | the Jinja template engine. Any transformers specified here | |
|
108 | will override existing transformers if a naming conflict | |
|
109 | occurs. | |
|
110 | filters : list[of filter] | |
|
111 | Custom filters to make accessible to the Jinja templates. Any | |
|
112 | filters specified here will override existing filters if a | |
|
113 | naming conflict occurs. | |
|
114 | config : config | |
|
115 | User configuration instance. | |
|
116 | """ | |
|
117 | ||
|
88 | 118 | #Call the base class constructor |
|
89 | 119 | super(Exporter, self).__init__(config=config, **kw) |
|
90 | 120 | |
@@ -110,9 +140,16 b' class Exporter(Configurable):' | |||
|
110 | 140 | else: |
|
111 | 141 | self.environment.filters[key] = user_filter |
|
112 | 142 | |
|
113 | # Public methods ######################################### | |
|
114 | 143 | |
|
115 | 144 | def from_notebook_node(self, nb): |
|
145 | """ | |
|
146 | Convert a notebook from a notebook node instance. | |
|
147 | ||
|
148 | Parameters | |
|
149 | ---------- | |
|
150 | nb : Notebook node | |
|
151 | """ | |
|
152 | ||
|
116 | 153 | nb, resources = self._preprocess(nb) |
|
117 | 154 | |
|
118 | 155 | #Load the template file. |
@@ -122,15 +159,44 b' class Exporter(Configurable):' | |||
|
122 | 159 | |
|
123 | 160 | |
|
124 | 161 | def from_filename(self, filename): |
|
162 | """ | |
|
163 | Convert a notebook from a notebook file. | |
|
164 | ||
|
165 | Parameters | |
|
166 | ---------- | |
|
167 | filename : str | |
|
168 | Full filename of the notebook file to open and convert. | |
|
169 | """ | |
|
170 | ||
|
125 | 171 | with io.open(filename) as f: |
|
126 | 172 | return self.from_notebook_node(nbformat.read(f, 'json')) |
|
127 | 173 | |
|
128 | 174 | |
|
129 | 175 | def from_file(self, file_stream): |
|
176 | """ | |
|
177 | Convert a notebook from a notebook file. | |
|
178 | ||
|
179 | Parameters | |
|
180 | ---------- | |
|
181 | file_stream : file-like object | |
|
182 | Notebook file-like object to convert. | |
|
183 | """ | |
|
130 | 184 | return self.from_notebook_node(nbformat.read(file_stream, 'json')) |
|
131 | 185 | |
|
132 | 186 | |
|
133 | 187 | def register_transformer(self, transformer): |
|
188 | """ | |
|
189 | Register a transformer. | |
|
190 | Transformers are classes that act upon the notebook before it is | |
|
191 | passed into the Jinja templating engine. Transformers are also | |
|
192 | capable of passing additional information to the Jinja | |
|
193 | templating engine. | |
|
194 | ||
|
195 | Parameters | |
|
196 | ---------- | |
|
197 | transformer : transformer | |
|
198 | """ | |
|
199 | ||
|
134 | 200 | if inspect.isfunction(transformer): |
|
135 | 201 | self.preprocessors.append(transformer) |
|
136 | 202 | return transformer |
@@ -145,6 +211,18 b' class Exporter(Configurable):' | |||
|
145 | 211 | |
|
146 | 212 | |
|
147 | 213 | def register_filter(self, name, filter): |
|
214 | """ | |
|
215 | Register a filter. | |
|
216 | A filter is a function that accepts and acts on one string. | |
|
217 | The filters are accesible within the Jinja templating engine. | |
|
218 | ||
|
219 | Parameters | |
|
220 | ---------- | |
|
221 | name : str | |
|
222 | name to give the filter in the Jinja engine | |
|
223 | filter : filter | |
|
224 | """ | |
|
225 | ||
|
148 | 226 | if inspect.isfunction(filter): |
|
149 | 227 | self.environment.filters[name] = filter |
|
150 | 228 | elif isinstance(filter, MetaHasTraits): |
@@ -153,10 +231,12 b' class Exporter(Configurable):' | |||
|
153 | 231 | self.environment.filters[name] = filter() |
|
154 | 232 | return self.environment.filters[name] |
|
155 | 233 | |
|
156 | ||
|
157 | # Protected and Private methods ######################################### | |
|
158 | 234 | |
|
159 | 235 | def _register_transformers(self): |
|
236 | """ | |
|
237 | Register all of the transformers needed for this exporter. | |
|
238 | """ | |
|
239 | ||
|
160 | 240 | self.register_transformer(nbconvert.transformers.coalescestreams.coalesce_streams) |
|
161 | 241 | |
|
162 | 242 | #Remember the figure extraction transformer so it can be enabled and |
@@ -165,6 +245,10 b' class Exporter(Configurable):' | |||
|
165 | 245 | |
|
166 | 246 | |
|
167 | 247 | def _register_filters(self): |
|
248 | """ | |
|
249 | Register all of the filters required for the exporter. | |
|
250 | """ | |
|
251 | ||
|
168 | 252 | self.register_filter('indent', indent) |
|
169 | 253 | self.register_filter('markdown', markdown) |
|
170 | 254 | self.register_filter('ansi2html', nbconvert.filters.ansi.ansi2html) |
@@ -184,17 +268,44 b' class Exporter(Configurable):' | |||
|
184 | 268 | |
|
185 | 269 | |
|
186 | 270 | def _init_environment(self): |
|
271 | """ | |
|
272 | Create the Jinja templating environment. | |
|
273 | """ | |
|
274 | ||
|
187 | 275 | self.environment = Environment( |
|
188 | 276 | loader=FileSystemLoader([ |
|
189 |
os.path.dirname(os.path.realpath(__file__)) + |
|
|
190 |
os.path.dirname(os.path.realpath(__file__)) + |
|
|
277 | os.path.dirname(os.path.realpath(__file__)) + self.template_path, | |
|
278 | os.path.dirname(os.path.realpath(__file__)) + self.template_skeleton_path, | |
|
191 | 279 | ]), |
|
192 | 280 | extensions=JINJA_EXTENSIONS |
|
193 | 281 | ) |
|
282 | ||
|
283 | #Set special Jinja2 syntax that will not conflict with latex. | |
|
284 | if not self.jinja_logic_block_start == None: | |
|
285 | self.environment.block_start_string = self.jinja_logic_block_start | |
|
286 | if not self.jinja_logic_block_end == None: | |
|
287 | self.environment.block_end_string = self.jinja_logic_block_end | |
|
288 | if not self.jinja_variable_block_start == None: | |
|
289 | self.environment.variable_start_string = self.jinja_variable_block_start | |
|
290 | if not self.jinja_variable_block_end == None: | |
|
291 | self.environment.variable_end_string = self.jinja_variable_block_end | |
|
292 | if not self.jinja_comment_block_start == None: | |
|
293 | self.environment.comment_start_string = self.jinja_comment_block_start | |
|
294 | if not self.jinja_comment_block_end == None: | |
|
295 | self.environment.comment_end_string = self.jinja_comment_block_end | |
|
194 | 296 | |
|
195 | 297 | |
|
196 | 298 | def _preprocess(self, nb): |
|
197 | ||
|
299 | """ | |
|
300 | Preprocess the notebook before passing it into the Jinja engine. | |
|
301 | To preprocess the notebook is to apply all of the | |
|
302 | ||
|
303 | Parameters | |
|
304 | ---------- | |
|
305 | nb : notebook node | |
|
306 | notebook that is being exported. | |
|
307 | """ | |
|
308 | ||
|
198 | 309 | #Dict of 'resources' that can be filled by the preprocessors. |
|
199 | 310 | resources = {} |
|
200 | 311 |
@@ -33,19 +33,6 b' from nbconvert.transformers.latex import LatexTransformer' | |||
|
33 | 33 | import exporter |
|
34 | 34 | |
|
35 | 35 | #----------------------------------------------------------------------------- |
|
36 | # Globals and constants | |
|
37 | #----------------------------------------------------------------------------- | |
|
38 | ||
|
39 | #Latex Jinja2 constants | |
|
40 | LATEX_TEMPLATE_PATH = "/../templates/latex/" | |
|
41 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/latex/skeleton/" | |
|
42 | ||
|
43 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
|
44 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] | |
|
45 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] | |
|
46 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] | |
|
47 | ||
|
48 | #----------------------------------------------------------------------------- | |
|
49 | 36 | # Classes and functions |
|
50 | 37 | #----------------------------------------------------------------------------- |
|
51 | 38 | |
@@ -59,45 +46,64 b' class LatexExporter(exporter.Exporter):' | |||
|
59 | 46 | subfolder of the "../templates" folder. |
|
60 | 47 | """ |
|
61 | 48 | |
|
62 | #Extension that the template files use. | |
|
63 | template_extension = ".tplx" | |
|
64 | ||
|
65 | 49 | file_extension = Unicode( |
|
66 | 50 | 'tex', config=True, |
|
67 | 51 | help="Extension of the file that should be written to disk") |
|
68 | 52 | |
|
69 | 53 | template_file = Unicode( |
|
70 |
|
|
|
71 |
|
|
|
54 | 'base', config=True, | |
|
55 | help="Name of the template file to use") | |
|
56 | ||
|
57 | #Latex constants | |
|
58 | template_path = Unicode( | |
|
59 | "/../templates/latex/", config=True, | |
|
60 | help="Path where the template files are located.") | |
|
61 | ||
|
62 | template_skeleton_path = Unicode( | |
|
63 | "/../templates/latex/skeleton/", config=True, | |
|
64 | help="Path where the template skeleton files are located.") | |
|
65 | ||
|
66 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
|
67 | jinja_comment_block_start = Unicode("((=", config=True) | |
|
68 | jinja_comment_block_end = Unicode("=))", config=True) | |
|
69 | jinja_variable_block_start = Unicode("(((", config=True) | |
|
70 | jinja_variable_block_end = Unicode(")))", config=True) | |
|
71 | jinja_logic_block_start = Unicode("((*", config=True) | |
|
72 | jinja_logic_block_end = Unicode("*))", config=True) | |
|
73 | ||
|
74 | #Extension that the template files use. | |
|
75 | template_extension = Unicode(".tplx", config=True) | |
|
72 | 76 | |
|
73 | 77 | def __init__(self, transformers=None, filters=None, config=None, **kw): |
|
78 | """ | |
|
79 | Public constructor | |
|
80 | ||
|
81 | Parameters | |
|
82 | ---------- | |
|
83 | transformers : list[of transformer] | |
|
84 | Custom transformers to apply to the notebook prior to engaging | |
|
85 | the Jinja template engine. Any transformers specified here | |
|
86 | will override existing transformers if a naming conflict | |
|
87 | occurs. | |
|
88 | filters : list[of filter] | |
|
89 | Custom filters to make accessible to the Jinja templates. Any | |
|
90 | filters specified here will override existing filters if a | |
|
91 | naming conflict occurs. | |
|
92 | config : config | |
|
93 | User configuration instance. | |
|
94 | """ | |
|
74 | 95 | |
|
75 | 96 | #Call base class constructor. |
|
76 | 97 | super(LatexExporter, self).__init__(transformers, filters, config, **kw) |
|
77 | 98 | |
|
78 | 99 | self.extract_figure_transformer.display_data_priority = ['latex', 'svg', 'png', 'jpg', 'jpeg' , 'text'] |
|
79 | 100 | self.extract_figure_transformer.extra_ext_map={'svg':'pdf'} |
|
80 | ||
|
81 | ||
|
82 | def _init_environment(self): | |
|
83 | self.environment = Environment( | |
|
84 | loader=FileSystemLoader([ | |
|
85 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, | |
|
86 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, | |
|
87 | ]), | |
|
88 | extensions=exporter.JINJA_EXTENSIONS | |
|
89 | ) | |
|
90 | 101 | |
|
91 | #Set special Jinja2 syntax that will not conflict with latex. | |
|
92 | self.environment.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] | |
|
93 | self.environment.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] | |
|
94 | self.environment.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] | |
|
95 | self.environment.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] | |
|
96 | self.environment.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] | |
|
97 | self.environment.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] | |
|
98 | ||
|
99 | 102 | |
|
100 | 103 | def _register_filters(self): |
|
104 | """ | |
|
105 | Register all of the filters required for the exporter. | |
|
106 | """ | |
|
101 | 107 | |
|
102 | 108 | #Register the filters of the base class. |
|
103 | 109 | super(LatexExporter, self)._register_filters() |
@@ -108,6 +114,9 b' class LatexExporter(exporter.Exporter):' | |||
|
108 | 114 | |
|
109 | 115 | |
|
110 | 116 | def _register_transformers(self): |
|
117 | """ | |
|
118 | Register all of the transformers needed for this exporter. | |
|
119 | """ | |
|
111 | 120 | |
|
112 | 121 | #Register the transformers of the base class. |
|
113 | 122 | super(LatexExporter, self)._register_transformers() |
@@ -36,6 +36,23 b' class PythonExporter(exporter.Exporter):' | |||
|
36 | 36 | help="Name of the template file to use") |
|
37 | 37 | |
|
38 | 38 | def __init__(self, transformers=None, filters=None, config=None, armor=False, **kw): |
|
39 | """ | |
|
40 | Public constructor | |
|
41 | ||
|
42 | Parameters | |
|
43 | ---------- | |
|
44 | transformers : list[of transformer] | |
|
45 | Custom transformers to apply to the notebook prior to engaging | |
|
46 | the Jinja template engine. Any transformers specified here | |
|
47 | will override existing transformers if a naming conflict | |
|
48 | occurs. | |
|
49 | filters : list[of filter] | |
|
50 | Custom filters to make accessible to the Jinja templates. Any | |
|
51 | filters specified here will override existing filters if a | |
|
52 | naming conflict occurs. | |
|
53 | config : config | |
|
54 | User configuration instance. | |
|
55 | """ | |
|
39 | 56 | |
|
40 | 57 | #Call base class constructor. |
|
41 | 58 | super(PythonExporter, self).__init__(transformers, filters, config, **kw) |
@@ -38,6 +38,9 b' class RevealExporter(basichtml.BasicHtmlExporter):' | |||
|
38 | 38 | help="Name of the template file to use") |
|
39 | 39 | |
|
40 | 40 | def _register_transformers(self): |
|
41 | """ | |
|
42 | Register all of the transformers needed for this exporter. | |
|
43 | """ | |
|
41 | 44 | |
|
42 | 45 | #Register the transformers of the base class. |
|
43 | 46 | super(RevealExporter, self)._register_transformers() |
General Comments 0
You need to be logged in to leave comments.
Login now