Show More
@@ -2,6 +2,6 b'' | |||
|
2 | 2 | |
|
3 | 3 | from .exporters import * |
|
4 | 4 | import filters |
|
5 |
import |
|
|
5 | import preprocessors | |
|
6 | 6 | import post_processors |
|
7 | 7 | import writers |
@@ -36,7 +36,7 b' from IPython.utils.importstring import import_item' | |||
|
36 | 36 | from IPython.utils.text import indent |
|
37 | 37 | from IPython.utils import py3compat |
|
38 | 38 | |
|
39 |
from IPython.nbconvert import |
|
|
39 | from IPython.nbconvert import preprocessors as nbpreprocessors | |
|
40 | 40 | from IPython.nbconvert import filters |
|
41 | 41 | |
|
42 | 42 | #----------------------------------------------------------------------------- |
@@ -83,8 +83,8 b' class Exporter(LoggingConfigurable):' | |||
|
83 | 83 | """ |
|
84 | 84 | Exports notebooks into other file formats. Uses Jinja 2 templating engine |
|
85 | 85 | to output new formats. Inherit from this class if you are creating a new |
|
86 |
template type along with new filters/ |
|
|
87 |
|
|
|
86 | template type along with new filters/preprocessors. If the filters/ | |
|
87 | preprocessors provided by default suffice, there is no need to inherit from | |
|
88 | 88 | this class. Instead, override the template_file and file_extension |
|
89 | 89 | traits via a config file. |
|
90 | 90 | |
@@ -138,23 +138,23 b' class Exporter(LoggingConfigurable):' | |||
|
138 | 138 | #Extension that the template files use. |
|
139 | 139 | template_extension = Unicode(".tpl", config=True) |
|
140 | 140 | |
|
141 |
#Configurability, allows the user to easily add filters and |
|
|
142 |
|
|
|
143 |
help="""List of |
|
|
141 | #Configurability, allows the user to easily add filters and preprocessors. | |
|
142 | preprocessors = List(config=True, | |
|
143 | help="""List of preprocessors, by name or namespace, to enable.""") | |
|
144 | 144 | |
|
145 | 145 | filters = Dict(config=True, |
|
146 | 146 | help="""Dictionary of filters, by name and namespace, to add to the Jinja |
|
147 | 147 | environment.""") |
|
148 | 148 | |
|
149 |
default_ |
|
|
150 |
nb |
|
|
151 |
nb |
|
|
152 |
nb |
|
|
153 |
nb |
|
|
154 |
nb |
|
|
155 |
nb |
|
|
149 | default_preprocessors = List([nbpreprocessors.coalesce_streams, | |
|
150 | nbpreprocessors.SVG2PDFPreprocessor, | |
|
151 | nbpreprocessors.ExtractOutputPreprocessor, | |
|
152 | nbpreprocessors.CSSHTMLHeaderPreprocessor, | |
|
153 | nbpreprocessors.RevealHelpPreprocessor, | |
|
154 | nbpreprocessors.LatexPreprocessor, | |
|
155 | nbpreprocessors.SphinxPreprocessor], | |
|
156 | 156 | config=True, |
|
157 |
help="""List of |
|
|
157 | help="""List of preprocessors available by default, by name, namespace, | |
|
158 | 158 | instance, or type.""") |
|
159 | 159 | |
|
160 | 160 | |
@@ -180,7 +180,7 b' class Exporter(LoggingConfigurable):' | |||
|
180 | 180 | #Init |
|
181 | 181 | self._init_template() |
|
182 | 182 | self._init_environment(extra_loaders=extra_loaders) |
|
183 |
self._init_ |
|
|
183 | self._init_preprocessors() | |
|
184 | 184 | self._init_filters() |
|
185 | 185 | |
|
186 | 186 | |
@@ -245,13 +245,13 b' class Exporter(LoggingConfigurable):' | |||
|
245 | 245 | nb : Notebook node |
|
246 | 246 | resources : dict (**kw) |
|
247 | 247 | of additional resources that can be accessed read/write by |
|
248 |
|
|
|
248 | preprocessors and filters. | |
|
249 | 249 | """ |
|
250 | 250 | nb_copy = copy.deepcopy(nb) |
|
251 | 251 | resources = self._init_resources(resources) |
|
252 | 252 | |
|
253 | 253 | # Preprocess |
|
254 |
nb_copy, resources = self._ |
|
|
254 | nb_copy, resources = self._preprocess(nb_copy, resources) | |
|
255 | 255 | |
|
256 | 256 | self._load_template() |
|
257 | 257 | |
@@ -300,51 +300,51 b' class Exporter(LoggingConfigurable):' | |||
|
300 | 300 | return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw) |
|
301 | 301 | |
|
302 | 302 | |
|
303 |
def register_ |
|
|
303 | def register_preprocessor(self, preprocessor, enabled=False): | |
|
304 | 304 | """ |
|
305 |
Register a |
|
|
306 |
|
|
|
307 |
passed into the Jinja templating engine. |
|
|
305 | Register a preprocessor. | |
|
306 | Preprocessors are classes that act upon the notebook before it is | |
|
307 | passed into the Jinja templating engine. Preprocessors are also | |
|
308 | 308 | capable of passing additional information to the Jinja |
|
309 | 309 | templating engine. |
|
310 | 310 | |
|
311 | 311 | Parameters |
|
312 | 312 | ---------- |
|
313 | transformer : transformer | |
|
313 | preprocessor : preprocessor | |
|
314 | 314 | """ |
|
315 |
if |
|
|
316 |
raise TypeError(' |
|
|
317 |
isclass = isinstance( |
|
|
315 | if preprocessor is None: | |
|
316 | raise TypeError('preprocessor') | |
|
317 | isclass = isinstance(preprocessor, type) | |
|
318 | 318 | constructed = not isclass |
|
319 | 319 | |
|
320 |
#Handle |
|
|
321 |
if constructed and isinstance( |
|
|
322 |
# |
|
|
323 |
#this register_ |
|
|
324 |
|
|
|
325 |
return self.register_ |
|
|
320 | #Handle preprocessor's registration based on it's type | |
|
321 | if constructed and isinstance(preprocessor, py3compat.string_types): | |
|
322 | #Preprocessor is a string, import the namespace and recursively call | |
|
323 | #this register_preprocessor method | |
|
324 | preprocessor_cls = import_item(preprocessor) | |
|
325 | return self.register_preprocessor(preprocessor_cls, enabled) | |
|
326 | 326 | |
|
327 |
if constructed and hasattr( |
|
|
328 |
# |
|
|
329 |
#Register and return the |
|
|
327 | if constructed and hasattr(preprocessor, '__call__'): | |
|
328 | #Preprocessor is a function, no need to construct it. | |
|
329 | #Register and return the preprocessor. | |
|
330 | 330 | if enabled: |
|
331 |
|
|
|
332 |
self._ |
|
|
333 |
return |
|
|
331 | preprocessor.enabled = True | |
|
332 | self._preprocessors.append(preprocessor) | |
|
333 | return preprocessor | |
|
334 | 334 | |
|
335 |
elif isclass and isinstance( |
|
|
336 |
# |
|
|
335 | elif isclass and isinstance(preprocessor, MetaHasTraits): | |
|
336 | #Preprocessor is configurable. Make sure to pass in new default for | |
|
337 | 337 | #the enabled flag if one was specified. |
|
338 |
self.register_ |
|
|
338 | self.register_preprocessor(preprocessor(parent=self), enabled) | |
|
339 | 339 | |
|
340 | 340 | elif isclass: |
|
341 |
# |
|
|
342 |
self.register_ |
|
|
341 | #Preprocessor is not configurable, construct it | |
|
342 | self.register_preprocessor(preprocessor(), enabled) | |
|
343 | 343 | |
|
344 | 344 | else: |
|
345 |
# |
|
|
345 | #Preprocessor is an instance of something without a __call__ | |
|
346 | 346 | #attribute. |
|
347 |
raise TypeError(' |
|
|
347 | raise TypeError('preprocessor') | |
|
348 | 348 | |
|
349 | 349 | |
|
350 | 350 | def register_filter(self, name, jinja_filter): |
@@ -435,22 +435,22 b' class Exporter(LoggingConfigurable):' | |||
|
435 | 435 | self.environment.comment_end_string = self.jinja_comment_block_end |
|
436 | 436 | |
|
437 | 437 | |
|
438 |
def _init_ |
|
|
438 | def _init_preprocessors(self): | |
|
439 | 439 | """ |
|
440 |
Register all of the |
|
|
440 | Register all of the preprocessors needed for this exporter, disabled | |
|
441 | 441 | unless specified explicitly. |
|
442 | 442 | """ |
|
443 |
self._ |
|
|
443 | self._preprocessors = [] | |
|
444 | 444 | |
|
445 |
#Load default |
|
|
446 |
if self.default_ |
|
|
447 |
for |
|
|
448 |
self.register_ |
|
|
445 | #Load default preprocessors (not necessarly enabled by default). | |
|
446 | if self.default_preprocessors: | |
|
447 | for preprocessor in self.default_preprocessors: | |
|
448 | self.register_preprocessor(preprocessor) | |
|
449 | 449 | |
|
450 |
#Load user |
|
|
451 |
if self. |
|
|
452 |
for |
|
|
453 |
self.register_ |
|
|
450 | #Load user preprocessors. Enable by default. | |
|
451 | if self.preprocessors: | |
|
452 | for preprocessor in self.preprocessors: | |
|
453 | self.register_preprocessor(preprocessor, enabled=True) | |
|
454 | 454 | |
|
455 | 455 | |
|
456 | 456 | def _init_filters(self): |
@@ -492,7 +492,7 b' class Exporter(LoggingConfigurable):' | |||
|
492 | 492 | return resources |
|
493 | 493 | |
|
494 | 494 | |
|
495 |
def _ |
|
|
495 | def _preprocess(self, nb, resources): | |
|
496 | 496 | """ |
|
497 | 497 | Preprocess the notebook before passing it into the Jinja engine. |
|
498 | 498 | To preprocess the notebook is to apply all of the |
@@ -502,17 +502,17 b' class Exporter(LoggingConfigurable):' | |||
|
502 | 502 | nb : notebook node |
|
503 | 503 | notebook that is being exported. |
|
504 | 504 | resources : a dict of additional resources that |
|
505 |
can be accessed read/write by |
|
|
505 | can be accessed read/write by preprocessors | |
|
506 | 506 | and filters. |
|
507 | 507 | """ |
|
508 | 508 | |
|
509 | 509 | # Do a copy.deepcopy first, |
|
510 |
# we are never safe enough with what the |
|
|
510 | # we are never safe enough with what the preprocessors could do. | |
|
511 | 511 | nbc = copy.deepcopy(nb) |
|
512 | 512 | resc = copy.deepcopy(resources) |
|
513 | 513 | |
|
514 |
#Run each |
|
|
515 |
#to each |
|
|
516 |
for |
|
|
517 |
nbc, resc = |
|
|
514 | #Run each preprocessor on the notebook. Carry the output along | |
|
515 | #to each preprocessor | |
|
516 | for preprocessor in self._preprocessors: | |
|
517 | nbc, resc = preprocessor(nbc, resc) | |
|
518 | 518 | return nbc, resc |
@@ -16,7 +16,7 b' Exporter that exports Basic HTML.' | |||
|
16 | 16 | |
|
17 | 17 | from IPython.utils.traitlets import Unicode, List |
|
18 | 18 | |
|
19 |
from IPython.nbconvert import |
|
|
19 | from IPython.nbconvert import preprocessors | |
|
20 | 20 | from IPython.config import Config |
|
21 | 21 | |
|
22 | 22 | from .exporter import Exporter |
@@ -29,7 +29,7 b' class HTMLExporter(Exporter):' | |||
|
29 | 29 | """ |
|
30 | 30 | Exports a basic HTML document. This exporter assists with the export of |
|
31 | 31 | HTML. Inherit from it if you are writing your own HTML template and need |
|
32 |
custom |
|
|
32 | custom preprocessors/filters. If you don't need custom preprocessors/ | |
|
33 | 33 | filters, just change the 'template_file' config option. |
|
34 | 34 | """ |
|
35 | 35 | |
@@ -44,7 +44,7 b' class HTMLExporter(Exporter):' | |||
|
44 | 44 | @property |
|
45 | 45 | def default_config(self): |
|
46 | 46 | c = Config({ |
|
47 |
'CSSHTMLHeader |
|
|
47 | 'CSSHTMLHeaderPreprocessor':{ | |
|
48 | 48 | 'enabled':True |
|
49 | 49 | } |
|
50 | 50 | }) |
@@ -23,7 +23,7 b' import os' | |||
|
23 | 23 | from IPython.utils.traitlets import Unicode, List |
|
24 | 24 | from IPython.config import Config |
|
25 | 25 | |
|
26 |
from IPython.nbconvert import filters, |
|
|
26 | from IPython.nbconvert import filters, preprocessors | |
|
27 | 27 | from .exporter import Exporter |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
@@ -74,16 +74,16 b' class LatexExporter(Exporter):' | |||
|
74 | 74 | 'NbConvertBase': { |
|
75 | 75 | 'display_data_priority' : ['latex', 'pdf', 'png', 'jpg', 'svg', 'jpeg', 'text'] |
|
76 | 76 | }, |
|
77 |
'ExtractOutput |
|
|
77 | 'ExtractOutputPreprocessor': { | |
|
78 | 78 | 'enabled':True |
|
79 | 79 | }, |
|
80 |
'SVG2PDF |
|
|
80 | 'SVG2PDFPreprocessor': { | |
|
81 | 81 | 'enabled':True |
|
82 | 82 | }, |
|
83 |
'Latex |
|
|
83 | 'LatexPreprocessor': { | |
|
84 | 84 | 'enabled':True |
|
85 | 85 | }, |
|
86 |
'Sphinx |
|
|
86 | 'SphinxPreprocessor': { | |
|
87 | 87 | 'enabled':True |
|
88 | 88 | } |
|
89 | 89 | }) |
@@ -33,6 +33,6 b' class RSTExporter(Exporter):' | |||
|
33 | 33 | |
|
34 | 34 | @property |
|
35 | 35 | def default_config(self): |
|
36 |
c = Config({'ExtractOutput |
|
|
36 | c = Config({'ExtractOutputPreprocessor':{'enabled':True}}) | |
|
37 | 37 | c.merge(super(RSTExporter,self).default_config) |
|
38 | 38 | return c |
@@ -16,7 +16,7 b' Contains slide show exporter' | |||
|
16 | 16 | |
|
17 | 17 | from IPython.utils.traitlets import Unicode |
|
18 | 18 | |
|
19 |
from IPython.nbconvert import |
|
|
19 | from IPython.nbconvert import preprocessors | |
|
20 | 20 | from IPython.config import Config |
|
21 | 21 | |
|
22 | 22 | from .exporter import Exporter |
@@ -41,10 +41,10 b' class SlidesExporter(Exporter):' | |||
|
41 | 41 | @property |
|
42 | 42 | def default_config(self): |
|
43 | 43 | c = Config({ |
|
44 |
'CSSHTMLHeader |
|
|
44 | 'CSSHTMLHeaderPreprocessor':{ | |
|
45 | 45 | 'enabled':True |
|
46 | 46 | }, |
|
47 |
'RevealHelp |
|
|
47 | 'RevealHelpPreprocessor':{ | |
|
48 | 48 | 'enabled':True, |
|
49 | 49 | }, |
|
50 | 50 | }) |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Contains Cheese |
|
|
2 | Contains CheesePreprocessor | |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2013, the IPython Development Team. |
@@ -13,13 +13,13 b' Contains CheeseTransformer' | |||
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 |
from ... |
|
|
16 | from ...preprocessors.base import Preprocessor | |
|
17 | 17 | |
|
18 | 18 | #----------------------------------------------------------------------------- |
|
19 | 19 | # Classes |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | 21 | |
|
22 |
class Cheese |
|
|
22 | class CheesePreprocessor(Preprocessor): | |
|
23 | 23 | """ |
|
24 | 24 | Adds a cheese tag to the resources object |
|
25 | 25 | """ |
@@ -29,12 +29,12 b' class CheeseTransformer(Transformer):' | |||
|
29 | 29 | """ |
|
30 | 30 | Public constructor |
|
31 | 31 | """ |
|
32 |
super(Cheese |
|
|
32 | super(CheesePreprocessor, self).__init__(**kw) | |
|
33 | 33 | |
|
34 | 34 | |
|
35 |
def |
|
|
35 | def preprocess(self, nb, resources): | |
|
36 | 36 | """ |
|
37 |
Sphinx |
|
|
37 | Sphinx preprocessing to apply on each notebook. | |
|
38 | 38 | |
|
39 | 39 | Parameters |
|
40 | 40 | ---------- |
@@ -42,7 +42,7 b' class CheeseTransformer(Transformer):' | |||
|
42 | 42 | Notebook being converted |
|
43 | 43 | resources : dictionary |
|
44 | 44 | Additional resources used in the conversion process. Allows |
|
45 |
|
|
|
45 | preprocessors to pass variables into the Jinja engine. | |
|
46 | 46 | """ |
|
47 | 47 | resources['cheese'] = 'real' |
|
48 | 48 | return nb, resources |
@@ -17,7 +17,7 b' Module with tests for exporter.py' | |||
|
17 | 17 | from IPython.config import Config |
|
18 | 18 | |
|
19 | 19 | from .base import ExportersTestsBase |
|
20 |
from .cheese import Cheese |
|
|
20 | from .cheese import CheesePreprocessor | |
|
21 | 21 | from ..exporter import Exporter |
|
22 | 22 | |
|
23 | 23 | |
@@ -47,9 +47,9 b' class TestExporter(ExportersTestsBase):' | |||
|
47 | 47 | |
|
48 | 48 | def test_extract_outputs(self): |
|
49 | 49 | """ |
|
50 |
If the ExtractOutput |
|
|
50 | If the ExtractOutputPreprocessor is enabled, are outputs extracted? | |
|
51 | 51 | """ |
|
52 |
config = Config({'ExtractOutput |
|
|
52 | config = Config({'ExtractOutputPreprocessor': {'enabled': True}}) | |
|
53 | 53 | exporter = self._make_exporter(config=config) |
|
54 | 54 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
55 | 55 | assert resources is not None |
@@ -57,45 +57,45 b' class TestExporter(ExportersTestsBase):' | |||
|
57 | 57 | assert len(resources['outputs']) > 0 |
|
58 | 58 | |
|
59 | 59 | |
|
60 |
def test_ |
|
|
60 | def test_preprocessor_class(self): | |
|
61 | 61 | """ |
|
62 |
Can a |
|
|
62 | Can a preprocessor be added to the preprocessors list by class type? | |
|
63 | 63 | """ |
|
64 |
config = Config({'Exporter': {' |
|
|
64 | config = Config({'Exporter': {'preprocessors': [CheesePreprocessor]}}) | |
|
65 | 65 | exporter = self._make_exporter(config=config) |
|
66 | 66 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
67 | 67 | assert resources is not None |
|
68 | 68 | assert resources['cheese'] == 'real' |
|
69 | 69 | |
|
70 | 70 | |
|
71 |
def test_ |
|
|
71 | def test_preprocessor_instance(self): | |
|
72 | 72 | """ |
|
73 |
Can a |
|
|
73 | Can a preprocessor be added to the preprocessors list by instance? | |
|
74 | 74 | """ |
|
75 |
config = Config({'Exporter': {' |
|
|
75 | config = Config({'Exporter': {'preprocessors': [CheesePreprocessor()]}}) | |
|
76 | 76 | exporter = self._make_exporter(config=config) |
|
77 | 77 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
78 | 78 | assert resources is not None |
|
79 | 79 | assert resources['cheese'] == 'real' |
|
80 | 80 | |
|
81 | 81 | |
|
82 |
def test_ |
|
|
82 | def test_preprocessor_dottedobjectname(self): | |
|
83 | 83 | """ |
|
84 |
Can a |
|
|
84 | Can a preprocessor be added to the preprocessors list by dotted object name? | |
|
85 | 85 | """ |
|
86 |
config = Config({'Exporter': {' |
|
|
86 | config = Config({'Exporter': {'preprocessors': ['IPython.nbconvert.exporters.tests.cheese.CheesePreprocessor']}}) | |
|
87 | 87 | exporter = self._make_exporter(config=config) |
|
88 | 88 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
89 | 89 | assert resources is not None |
|
90 | 90 | assert resources['cheese'] == 'real' |
|
91 | 91 | |
|
92 | 92 | |
|
93 |
def test_ |
|
|
93 | def test_preprocessor_via_method(self): | |
|
94 | 94 | """ |
|
95 |
Can a |
|
|
95 | Can a preprocessor be added via the Exporter convenience method? | |
|
96 | 96 | """ |
|
97 | 97 | exporter = self._make_exporter() |
|
98 |
exporter.register_ |
|
|
98 | exporter.register_preprocessor(CheesePreprocessor, enabled=True) | |
|
99 | 99 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
100 | 100 | assert resources is not None |
|
101 | 101 | assert resources['cheese'] == 'real' |
@@ -33,7 +33,7 b' from IPython.utils.importstring import import_item' | |||
|
33 | 33 | from IPython.utils.text import dedent |
|
34 | 34 | |
|
35 | 35 | from .exporters.export import get_export_names, exporter_map |
|
36 |
from IPython.nbconvert import exporters, |
|
|
36 | from IPython.nbconvert import exporters, preprocessors, writers, post_processors | |
|
37 | 37 | from .utils.base import NbConvertBase |
|
38 | 38 | from .utils.exceptions import ConversionException |
|
39 | 39 | |
@@ -88,7 +88,7 b' class NbConvertApp(BaseIPythonApplication):' | |||
|
88 | 88 | |
|
89 | 89 | def _classes_default(self): |
|
90 | 90 | classes = [NbConvertBase] |
|
91 |
for pkg in (exporters, |
|
|
91 | for pkg in (exporters, preprocessors, writers): | |
|
92 | 92 | for name in dir(pkg): |
|
93 | 93 | cls = getattr(pkg, name) |
|
94 | 94 | if isinstance(cls, type) and issubclass(cls, Configurable): |
@@ -1,12 +1,12 b'' | |||
|
1 |
# Class base |
|
|
2 |
from .base import |
|
|
3 |
from .convertfigures import ConvertFigures |
|
|
4 |
from .svg2pdf import SVG2PDF |
|
|
5 |
from .extractoutput import ExtractOutput |
|
|
6 |
from .revealhelp import RevealHelp |
|
|
7 |
from .latex import Latex |
|
|
8 |
from .sphinx import Sphinx |
|
|
9 |
from .csshtmlheader import CSSHTMLHeader |
|
|
1 | # Class base Preprocessors | |
|
2 | from .base import Preprocessor | |
|
3 | from .convertfigures import ConvertFiguresPreprocessor | |
|
4 | from .svg2pdf import SVG2PDFPreprocessor | |
|
5 | from .extractoutput import ExtractOutputPreprocessor | |
|
6 | from .revealhelp import RevealHelpPreprocessor | |
|
7 | from .latex import LatexPreprocessor | |
|
8 | from .sphinx import SphinxPreprocessor | |
|
9 | from .csshtmlheader import CSSHTMLHeaderPreprocessor | |
|
10 | 10 | |
|
11 |
# decorated function |
|
|
11 | # decorated function Preprocessors | |
|
12 | 12 | from .coalescestreams import coalesce_streams |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module that re-groups |
|
|
2 | Module that re-groups preprocessor that would be applied to ipynb files | |
|
3 | 3 | before going through the templating machinery. |
|
4 | 4 | |
|
5 | 5 | It exposes a convenient class to inherit from to access configurability. |
@@ -23,20 +23,20 b' from IPython.utils.traitlets import Bool' | |||
|
23 | 23 | # Classes and Functions |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 |
class |
|
|
27 |
""" A configurable |
|
|
26 | class Preprocessor(NbConvertBase): | |
|
27 | """ A configurable preprocessor | |
|
28 | 28 | |
|
29 | 29 | Inherit from this class if you wish to have configurability for your |
|
30 | transformer. | |
|
30 | preprocessor. | |
|
31 | 31 | |
|
32 | 32 | Any configurable traitlets this class exposed will be configurable in profiles |
|
33 | 33 | using c.SubClassName.atribute=value |
|
34 | 34 | |
|
35 |
you can overwrite :meth:` |
|
|
36 |
or :meth:` |
|
|
35 | you can overwrite :meth:`preprocess_cell` to apply a preprocessation independently on each cell | |
|
36 | or :meth:`preprocess` if you prefer your own logic. See corresponding docstring for informations. | |
|
37 | 37 | |
|
38 | 38 | Disabled by default and can be enabled via the config by |
|
39 |
'c.Your |
|
|
39 | 'c.YourPreprocessorName.enabled = True' | |
|
40 | 40 | """ |
|
41 | 41 | |
|
42 | 42 | enabled = Bool(False, config=True) |
@@ -53,23 +53,23 b' class Transformer(NbConvertBase):' | |||
|
53 | 53 | Additional arguments |
|
54 | 54 | """ |
|
55 | 55 | |
|
56 |
super( |
|
|
56 | super(Preprocessor, self).__init__(**kw) | |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | def __call__(self, nb, resources): |
|
60 | 60 | if self.enabled: |
|
61 |
return self. |
|
|
61 | return self.preprocess(nb,resources) | |
|
62 | 62 | else: |
|
63 | 63 | return nb, resources |
|
64 | 64 | |
|
65 | 65 | |
|
66 |
def |
|
|
66 | def preprocess(self, nb, resources): | |
|
67 | 67 | """ |
|
68 |
|
|
|
68 | preprocessation to apply on each notebook. | |
|
69 | 69 | |
|
70 | 70 | You should return modified nb, resources. |
|
71 |
If you wish to apply your |
|
|
72 |
overwrite |
|
|
71 | If you wish to apply your preprocess on each cell, you might want to | |
|
72 | overwrite preprocess_cell method instead. | |
|
73 | 73 | |
|
74 | 74 | Parameters |
|
75 | 75 | ---------- |
@@ -77,21 +77,21 b' class Transformer(NbConvertBase):' | |||
|
77 | 77 | Notebook being converted |
|
78 | 78 | resources : dictionary |
|
79 | 79 | Additional resources used in the conversion process. Allows |
|
80 |
|
|
|
80 | preprocessors to pass variables into the Jinja engine. | |
|
81 | 81 | """ |
|
82 |
self.log.debug("Applying |
|
|
82 | self.log.debug("Applying preprocess: %s", self.__class__.__name__) | |
|
83 | 83 | try : |
|
84 | 84 | for worksheet in nb.worksheets: |
|
85 | 85 | for index, cell in enumerate(worksheet.cells): |
|
86 |
worksheet.cells[index], resources = self. |
|
|
86 | worksheet.cells[index], resources = self.preprocess_cell(cell, resources, index) | |
|
87 | 87 | return nb, resources |
|
88 | 88 | except NotImplementedError: |
|
89 | 89 | raise NotImplementedError('should be implemented by subclass') |
|
90 | 90 | |
|
91 | 91 | |
|
92 |
def |
|
|
92 | def preprocess_cell(self, cell, resources, index): | |
|
93 | 93 | """ |
|
94 |
Overwrite if you want to apply a |
|
|
94 | Overwrite if you want to apply a preprocessation on each cell. You | |
|
95 | 95 | should return modified cell and resource dictionary. |
|
96 | 96 | |
|
97 | 97 | Parameters |
@@ -100,7 +100,7 b' class Transformer(NbConvertBase):' | |||
|
100 | 100 | Notebook cell being processed |
|
101 | 101 | resources : dictionary |
|
102 | 102 | Additional resources used in the conversion process. Allows |
|
103 |
|
|
|
103 | preprocessors to pass variables into the Jinja engine. | |
|
104 | 104 | index : int |
|
105 | 105 | Index of the cell being processed |
|
106 | 106 | """ |
@@ -24,7 +24,7 b' def cell_preprocessor(function):' | |||
|
24 | 24 | Notebook cell being processed |
|
25 | 25 | resources : dictionary |
|
26 | 26 | Additional resources used in the conversion process. Allows |
|
27 |
|
|
|
27 | preprocessors to pass variables into the Jinja engine. | |
|
28 | 28 | index : int |
|
29 | 29 | Index of the cell being processed |
|
30 | 30 | """ |
@@ -1,4 +1,4 b'' | |||
|
1 |
"""Module containing a |
|
|
1 | """Module containing a preprocessor that converts outputs in the notebook from | |
|
2 | 2 | one format to another. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
@@ -13,14 +13,14 b' one format to another.' | |||
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 |
from .base import |
|
|
16 | from .base import Preprocessor | |
|
17 | 17 | from IPython.utils.traitlets import Unicode |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Classes |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 |
class ConvertFigures |
|
|
23 | class ConvertFiguresPreprocessor(Preprocessor): | |
|
24 | 24 | """ |
|
25 | 25 | Converts all of the outputs in a notebook from one format to another. |
|
26 | 26 | """ |
@@ -32,14 +32,14 b' class ConvertFiguresTransformer(Transformer):' | |||
|
32 | 32 | """ |
|
33 | 33 | Public constructor |
|
34 | 34 | """ |
|
35 |
super(ConvertFigures |
|
|
35 | super(ConvertFiguresPreprocessor, self).__init__(**kw) | |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | def convert_figure(self, data_format, data): |
|
39 | 39 | raise NotImplementedError() |
|
40 | 40 | |
|
41 | 41 | |
|
42 |
def |
|
|
42 | def preprocess_cell(self, cell, resources, cell_index): | |
|
43 | 43 | """ |
|
44 | 44 | Apply a transformation on each cell, |
|
45 | 45 |
@@ -19,7 +19,7 b' from pygments.formatters import HtmlFormatter' | |||
|
19 | 19 | |
|
20 | 20 | from IPython.utils import path |
|
21 | 21 | |
|
22 |
from .base import |
|
|
22 | from .base import Preprocessor | |
|
23 | 23 | |
|
24 | 24 | from IPython.utils.traitlets import Unicode |
|
25 | 25 | |
@@ -27,9 +27,9 b' from IPython.utils.traitlets import Unicode' | |||
|
27 | 27 | # Classes and functions |
|
28 | 28 | #----------------------------------------------------------------------------- |
|
29 | 29 | |
|
30 |
class CSSHTMLHeader |
|
|
30 | class CSSHTMLHeaderPreprocessor(Preprocessor): | |
|
31 | 31 | """ |
|
32 |
|
|
|
32 | Preprocessor used to pre-process notebook for HTML output. Adds IPython notebook | |
|
33 | 33 | front-end CSS and Pygments CSS to HTML output. |
|
34 | 34 | """ |
|
35 | 35 | |
@@ -50,13 +50,13 b' class CSSHTMLHeaderTransformer(Transformer):' | |||
|
50 | 50 | Additional arguments |
|
51 | 51 | """ |
|
52 | 52 | |
|
53 |
super(CSSHTMLHeader |
|
|
53 | super(CSSHTMLHeaderPreprocessor, self).__init__(config=config, **kw) | |
|
54 | 54 | |
|
55 | 55 | if self.enabled : |
|
56 | 56 | self._regen_header() |
|
57 | 57 | |
|
58 | 58 | |
|
59 |
def |
|
|
59 | def preprocess(self, nb, resources): | |
|
60 | 60 | """Fetch and add CSS to the resource dictionary |
|
61 | 61 | |
|
62 | 62 | Fetch CSS from IPython and Pygments to add at the beginning |
@@ -69,7 +69,7 b' class CSSHTMLHeaderTransformer(Transformer):' | |||
|
69 | 69 | Notebook being converted |
|
70 | 70 | resources : dictionary |
|
71 | 71 | Additional resources used in the conversion process. Allows |
|
72 |
|
|
|
72 | preprocessors to pass variables into the Jinja engine. | |
|
73 | 73 | """ |
|
74 | 74 | |
|
75 | 75 | resources['inlining'] = {} |
@@ -1,4 +1,4 b'' | |||
|
1 |
"""Module containing a |
|
|
1 | """Module containing a preprocessor that extracts all of the outputs from the | |
|
2 | 2 | notebook file. The extracted outputs are returned in the 'resources' dictionary. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
@@ -18,14 +18,14 b' import sys' | |||
|
18 | 18 | import os |
|
19 | 19 | |
|
20 | 20 | from IPython.utils.traitlets import Unicode |
|
21 |
from .base import |
|
|
21 | from .base import Preprocessor | |
|
22 | 22 | from IPython.utils import py3compat |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Classes |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 |
class ExtractOutput |
|
|
28 | class ExtractOutputPreprocessor(Preprocessor): | |
|
29 | 29 | """ |
|
30 | 30 | Extracts all of the outputs from the notebook file. The extracted |
|
31 | 31 | outputs are returned in the 'resources' dictionary. |
@@ -35,7 +35,7 b' class ExtractOutputTransformer(Transformer):' | |||
|
35 | 35 | "{unique_key}_{cell_index}_{index}.{extension}", config=True) |
|
36 | 36 | |
|
37 | 37 | |
|
38 |
def |
|
|
38 | def preprocess_cell(self, cell, resources, cell_index): | |
|
39 | 39 | """ |
|
40 | 40 | Apply a transformation on each cell, |
|
41 | 41 | |
@@ -45,7 +45,7 b' class ExtractOutputTransformer(Transformer):' | |||
|
45 | 45 | Notebook cell being processed |
|
46 | 46 | resources : dictionary |
|
47 | 47 | Additional resources used in the conversion process. Allows |
|
48 |
|
|
|
48 | preprocessors to pass variables into the Jinja engine. | |
|
49 | 49 | cell_index : int |
|
50 | 50 | Index of the cell being processed (see base.py) |
|
51 | 51 | """ |
@@ -16,22 +16,22 b' they are converted.' | |||
|
16 | 16 | from __future__ import print_function, absolute_import |
|
17 | 17 | |
|
18 | 18 | # Our own imports |
|
19 |
# Needed to override |
|
|
20 |
from .base import ( |
|
|
19 | # Needed to override preprocessor | |
|
20 | from .base import (Preprocessor) | |
|
21 | 21 | from IPython.nbconvert import filters |
|
22 | 22 | |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | # Classes |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | |
|
27 | class LatexTransformer(Transformer): | |
|
27 | class LatexPreprocessor(Preprocessor): | |
|
28 | 28 | """ |
|
29 | 29 | Converter for latex destined documents. |
|
30 | 30 | """ |
|
31 | 31 | |
|
32 |
def |
|
|
32 | def preprocess_cell(self, cell, resources, index): | |
|
33 | 33 | """ |
|
34 |
Apply a |
|
|
34 | Apply a preprocessation on each cell, | |
|
35 | 35 | |
|
36 | 36 | Parameters |
|
37 | 37 | ---------- |
@@ -39,7 +39,7 b' class LatexTransformer(Transformer):' | |||
|
39 | 39 | Notebook cell being processed |
|
40 | 40 | resources : dictionary |
|
41 | 41 | Additional resources used in the conversion process. Allows |
|
42 |
|
|
|
42 | preprocessors to pass variables into the Jinja engine. | |
|
43 | 43 | index : int |
|
44 | 44 | Modified index of the cell being processed (see base.py) |
|
45 | 45 | """ |
@@ -15,14 +15,14 b'' | |||
|
15 | 15 | import os |
|
16 | 16 | import urllib2 |
|
17 | 17 | |
|
18 |
from .base import |
|
|
18 | from .base import Preprocessor | |
|
19 | 19 | from IPython.utils.traitlets import Unicode, Bool |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Classes and functions |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 |
class RevealHelp |
|
|
25 | class RevealHelpPreprocessor(Preprocessor): | |
|
26 | 26 | |
|
27 | 27 | url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0', |
|
28 | 28 | config=True, |
@@ -34,9 +34,9 b' class RevealHelpTransformer(Transformer):' | |||
|
34 | 34 | help="""If you want to use the speaker notes |
|
35 | 35 | set this to True.""") |
|
36 | 36 | |
|
37 |
def |
|
|
37 | def preprocess(self, nb, resources): | |
|
38 | 38 | """ |
|
39 |
Called once to ' |
|
|
39 | Called once to 'preprocess' contents of the notebook. | |
|
40 | 40 | |
|
41 | 41 | Parameters |
|
42 | 42 | ---------- |
@@ -44,7 +44,7 b' class RevealHelpTransformer(Transformer):' | |||
|
44 | 44 | Notebook being converted |
|
45 | 45 | resources : dictionary |
|
46 | 46 | Additional resources used in the conversion process. Allows |
|
47 |
|
|
|
47 | preprocessors to pass variables into the Jinja engine. | |
|
48 | 48 | """ |
|
49 | 49 | |
|
50 | 50 | for worksheet in nb.worksheets : |
@@ -30,8 +30,8 b' from pygments.formatters import LatexFormatter' | |||
|
30 | 30 | # Configurable traitlets |
|
31 | 31 | from IPython.utils.traitlets import Unicode, Bool |
|
32 | 32 | |
|
33 |
# Needed to override |
|
|
34 |
from .base import ( |
|
|
33 | # Needed to override preprocessor | |
|
34 | from .base import (Preprocessor) | |
|
35 | 35 | |
|
36 | 36 | from IPython.nbconvert.utils import console |
|
37 | 37 | |
@@ -39,11 +39,11 b' from IPython.nbconvert.utils import console' | |||
|
39 | 39 | # Classes and functions |
|
40 | 40 | #----------------------------------------------------------------------------- |
|
41 | 41 | |
|
42 |
class Sphinx |
|
|
42 | class SphinxPreprocessor(Preprocessor): | |
|
43 | 43 | """ |
|
44 |
Sphinx utility |
|
|
44 | Sphinx utility preprocessor. | |
|
45 | 45 | |
|
46 |
This |
|
|
46 | This preprocessor is used to set variables needed by the latex to build | |
|
47 | 47 | Sphinx stylized templates. |
|
48 | 48 | """ |
|
49 | 49 | |
@@ -109,9 +109,9 b' class SphinxTransformer(Transformer):' | |||
|
109 | 109 | overridetitle = Unicode("", config=True, help="") |
|
110 | 110 | |
|
111 | 111 | |
|
112 |
def |
|
|
112 | def preprocess(self, nb, resources): | |
|
113 | 113 | """ |
|
114 |
Sphinx |
|
|
114 | Sphinx preprocessing to apply on each notebook. | |
|
115 | 115 | |
|
116 | 116 | Parameters |
|
117 | 117 | ---------- |
@@ -119,7 +119,7 b' class SphinxTransformer(Transformer):' | |||
|
119 | 119 | Notebook being converted |
|
120 | 120 | resources : dictionary |
|
121 | 121 | Additional resources used in the conversion process. Allows |
|
122 |
|
|
|
122 | preprocessors to pass variables into the Jinja engine. | |
|
123 | 123 | """ |
|
124 | 124 | # import sphinx here, so that sphinx is not a dependency when it's not used |
|
125 | 125 | import sphinx |
@@ -1,4 +1,4 b'' | |||
|
1 |
"""Module containing a |
|
|
1 | """Module containing a preprocessor that converts outputs in the notebook from | |
|
2 | 2 | one format to another. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
@@ -22,7 +22,7 b' import subprocess' | |||
|
22 | 22 | from IPython.utils.tempdir import TemporaryDirectory |
|
23 | 23 | from IPython.utils.traitlets import Unicode |
|
24 | 24 | |
|
25 |
from .convertfigures import ConvertFigures |
|
|
25 | from .convertfigures import ConvertFiguresPreprocessor | |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | #----------------------------------------------------------------------------- |
@@ -35,7 +35,7 b" INKSCAPE_APP = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape'" | |||
|
35 | 35 | # Classes |
|
36 | 36 | #----------------------------------------------------------------------------- |
|
37 | 37 | |
|
38 |
class SVG2PDF |
|
|
38 | class SVG2PDFPreprocessor(ConvertFiguresPreprocessor): | |
|
39 | 39 | """ |
|
40 | 40 | Converts all of the outputs in a notebook from SVG to PDF. |
|
41 | 41 | """ |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with utility functions for |
|
|
2 | Module with utility functions for preprocessor tests | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -23,12 +23,12 b' from ...exporters.exporter import ResourcesDict' | |||
|
23 | 23 | # Class |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 |
class |
|
|
27 |
"""Contains test functions |
|
|
26 | class PreprocessorTestsBase(TestsBase): | |
|
27 | """Contains test functions preprocessor tests""" | |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | def build_notebook(self): |
|
31 |
"""Build a notebook in memory for use with |
|
|
31 | """Build a notebook in memory for use with preprocessor tests""" | |
|
32 | 32 | |
|
33 | 33 | outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a"), |
|
34 | 34 | nbformat.new_output(output_type="text", output_text="b"), |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with tests for the coalescestreams |
|
|
2 | Module with tests for the coalescestreams preprocessor | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -14,7 +14,7 b' Module with tests for the coalescestreams transformer' | |||
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 |
from .base import |
|
|
17 | from .base import PreprocessorTestsBase | |
|
18 | 18 | from ..coalescestreams import coalesce_streams |
|
19 | 19 | |
|
20 | 20 | |
@@ -22,11 +22,11 b' from ..coalescestreams import coalesce_streams' | |||
|
22 | 22 | # Class |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 |
class TestCoalesceStreams( |
|
|
25 | class TestCoalesceStreams(PreprocessorTestsBase): | |
|
26 | 26 | """Contains test functions for coalescestreams.py""" |
|
27 | 27 | |
|
28 | 28 | def test_coalesce_streams(self): |
|
29 |
"""coalesce_streams |
|
|
29 | """coalesce_streams preprocessor output test""" | |
|
30 | 30 | nb = self.build_notebook() |
|
31 | 31 | res = self.build_resources() |
|
32 | 32 | nb, res = coalesce_streams(nb, res) |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with tests for the csshtmlheader |
|
|
2 | Module with tests for the csshtmlheader preprocessor | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -14,34 +14,34 b' Module with tests for the csshtmlheader transformer' | |||
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 |
from .base import |
|
|
18 |
from ..csshtmlheader import CSSHTMLHeader |
|
|
17 | from .base import PreprocessorTestsBase | |
|
18 | from ..csshtmlheader import CSSHTMLHeaderPreprocessor | |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Class |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 |
class TestCSSHTMLHeader( |
|
|
25 | class TestCSSHTMLHeader(PreprocessorTestsBase): | |
|
26 | 26 | """Contains test functions for csshtmlheader.py""" |
|
27 | 27 | |
|
28 | 28 | |
|
29 |
def build_ |
|
|
30 |
"""Make an instance of a |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
return |
|
|
29 | def build_preprocessor(self): | |
|
30 | """Make an instance of a preprocessor""" | |
|
31 | preprocessor = CSSHTMLHeaderPreprocessor() | |
|
32 | preprocessor.enabled = True | |
|
33 | return preprocessor | |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def test_constructor(self): |
|
37 |
"""Can a CSSHTMLHeader |
|
|
38 |
self.build_ |
|
|
37 | """Can a CSSHTMLHeaderPreprocessor be constructed?""" | |
|
38 | self.build_preprocessor() | |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | def test_output(self): |
|
42 |
"""Test the output of the CSSHTMLHeader |
|
|
42 | """Test the output of the CSSHTMLHeaderPreprocessor""" | |
|
43 | 43 | nb = self.build_notebook() |
|
44 | 44 | res = self.build_resources() |
|
45 |
|
|
|
46 |
nb, res = |
|
|
45 | preprocessor = self.build_preprocessor() | |
|
46 | nb, res = preprocessor(nb, res) | |
|
47 | 47 | assert 'css' in res['inlining'] |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with tests for the extractoutput |
|
|
2 | Module with tests for the extractoutput preprocessor | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -14,36 +14,36 b' Module with tests for the extractoutput transformer' | |||
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 |
from .base import |
|
|
18 |
from ..extractoutput import ExtractOutput |
|
|
17 | from .base import PreprocessorTestsBase | |
|
18 | from ..extractoutput import ExtractOutputPreprocessor | |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Class |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 |
class TestExtractOutput( |
|
|
25 | class TestExtractOutput(PreprocessorTestsBase): | |
|
26 | 26 | """Contains test functions for extractoutput.py""" |
|
27 | 27 | |
|
28 | 28 | |
|
29 |
def build_ |
|
|
30 |
"""Make an instance of a |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
return |
|
|
29 | def build_preprocessor(self): | |
|
30 | """Make an instance of a preprocessor""" | |
|
31 | preprocessor = ExtractOutputPreprocessor() | |
|
32 | preprocessor.enabled = True | |
|
33 | return preprocessor | |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def test_constructor(self): |
|
37 |
"""Can a ExtractOutput |
|
|
38 |
self.build_ |
|
|
37 | """Can a ExtractOutputPreprocessor be constructed?""" | |
|
38 | self.build_preprocessor() | |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | def test_output(self): |
|
42 |
"""Test the output of the ExtractOutput |
|
|
42 | """Test the output of the ExtractOutputPreprocessor""" | |
|
43 | 43 | nb = self.build_notebook() |
|
44 | 44 | res = self.build_resources() |
|
45 |
|
|
|
46 |
nb, res = |
|
|
45 | preprocessor = self.build_preprocessor() | |
|
46 | nb, res = preprocessor(nb, res) | |
|
47 | 47 | |
|
48 | 48 | # Check if text was extracted. |
|
49 | 49 | assert 'text_filename' in nb.worksheets[0].cells[0].outputs[1] |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with tests for the latex |
|
|
2 | Module with tests for the latex preprocessor | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -14,35 +14,35 b' Module with tests for the latex transformer' | |||
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 |
from .base import |
|
|
18 |
from ..latex import Latex |
|
|
17 | from .base import PreprocessorTestsBase | |
|
18 | from ..latex import LatexPreprocessor | |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Class |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 |
class TestLatex( |
|
|
25 | class TestLatex(PreprocessorTestsBase): | |
|
26 | 26 | """Contains test functions for latex.py""" |
|
27 | 27 | |
|
28 | 28 | |
|
29 |
def build_ |
|
|
30 |
"""Make an instance of a |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
return |
|
|
29 | def build_preprocessor(self): | |
|
30 | """Make an instance of a preprocessor""" | |
|
31 | preprocessor = LatexPreprocessor() | |
|
32 | preprocessor.enabled = True | |
|
33 | return preprocessor | |
|
34 | 34 | |
|
35 | 35 | def test_constructor(self): |
|
36 |
"""Can a Latex |
|
|
37 |
self.build_ |
|
|
36 | """Can a LatexPreprocessor be constructed?""" | |
|
37 | self.build_preprocessor() | |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | def test_output(self): |
|
41 |
"""Test the output of the Latex |
|
|
41 | """Test the output of the LatexPreprocessor""" | |
|
42 | 42 | nb = self.build_notebook() |
|
43 | 43 | res = self.build_resources() |
|
44 |
|
|
|
45 |
nb, res = |
|
|
44 | preprocessor = self.build_preprocessor() | |
|
45 | nb, res = preprocessor(nb, res) | |
|
46 | 46 | |
|
47 | 47 | # Make sure the code cell wasn't modified. |
|
48 | 48 | self.assertEqual(nb.worksheets[0].cells[0].input, '$ e $') |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with tests for the revealhelp |
|
|
2 | Module with tests for the revealhelp preprocessor | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -16,20 +16,20 b' Module with tests for the revealhelp transformer' | |||
|
16 | 16 | |
|
17 | 17 | from IPython.nbformat import current as nbformat |
|
18 | 18 | |
|
19 |
from .base import |
|
|
20 |
from ..revealhelp import RevealHelp |
|
|
19 | from .base import PreprocessorTestsBase | |
|
20 | from ..revealhelp import RevealHelpPreprocessor | |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | # Class |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | |
|
27 |
class Testrevealhelp( |
|
|
27 | class Testrevealhelp(PreprocessorTestsBase): | |
|
28 | 28 | """Contains test functions for revealhelp.py""" |
|
29 | 29 | |
|
30 | 30 | def build_notebook(self): |
|
31 | 31 | """Build a reveal slides notebook in memory for use with tests. |
|
32 |
Overrides base in |
|
|
32 | Overrides base in PreprocessorTestsBase""" | |
|
33 | 33 | |
|
34 | 34 | outputs = [nbformat.new_output(output_type="stream", stream="stdout", output_text="a")] |
|
35 | 35 | |
@@ -46,34 +46,34 b' class Testrevealhelp(TransformerTestsBase):' | |||
|
46 | 46 | return nbformat.new_notebook(name="notebook1", worksheets=worksheets) |
|
47 | 47 | |
|
48 | 48 | |
|
49 |
def build_ |
|
|
50 |
"""Make an instance of a |
|
|
51 |
|
|
|
52 |
|
|
|
53 |
return |
|
|
49 | def build_preprocessor(self): | |
|
50 | """Make an instance of a preprocessor""" | |
|
51 | preprocessor = RevealHelpPreprocessor() | |
|
52 | preprocessor.enabled = True | |
|
53 | return preprocessor | |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | def test_constructor(self): |
|
57 |
"""Can a RevealHelp |
|
|
58 |
self.build_ |
|
|
57 | """Can a RevealHelpPreprocessor be constructed?""" | |
|
58 | self.build_preprocessor() | |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | def test_reveal_attribute(self): |
|
62 | 62 | """Make sure the reveal url_prefix resources is set""" |
|
63 | 63 | nb = self.build_notebook() |
|
64 | 64 | res = self.build_resources() |
|
65 |
|
|
|
66 |
nb, res = |
|
|
65 | preprocessor = self.build_preprocessor() | |
|
66 | nb, res = preprocessor(nb, res) | |
|
67 | 67 | assert 'reveal' in res |
|
68 | 68 | assert 'url_prefix' in res['reveal'] |
|
69 | 69 | |
|
70 | 70 | |
|
71 | 71 | def test_reveal_output(self): |
|
72 |
"""Make sure that the reveal |
|
|
72 | """Make sure that the reveal preprocessor """ | |
|
73 | 73 | nb = self.build_notebook() |
|
74 | 74 | res = self.build_resources() |
|
75 |
|
|
|
76 |
nb, res = |
|
|
75 | preprocessor = self.build_preprocessor() | |
|
76 | nb, res = preprocessor(nb, res) | |
|
77 | 77 | cells = nb.worksheets[0].cells |
|
78 | 78 | |
|
79 | 79 | # Make sure correct metadata tags are available on every cell. |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with tests for the sphinx |
|
|
2 | Module with tests for the sphinx preprocessor | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -14,37 +14,37 b' Module with tests for the sphinx transformer' | |||
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 |
from .base import |
|
|
18 |
from ..sphinx import Sphinx |
|
|
17 | from .base import PreprocessorTestsBase | |
|
18 | from ..sphinx import SphinxPreprocessor | |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | # Class |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | |
|
25 |
class TestSphinx( |
|
|
25 | class TestSphinx(PreprocessorTestsBase): | |
|
26 | 26 | """Contains test functions for sphinx.py""" |
|
27 | 27 | |
|
28 | 28 | |
|
29 |
def build_ |
|
|
30 |
"""Make an instance of a |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
return |
|
|
29 | def build_preprocessor(self): | |
|
30 | """Make an instance of a preprocessor""" | |
|
31 | preprocessor = SphinxPreprocessor() | |
|
32 | preprocessor.enabled = True | |
|
33 | return preprocessor | |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def test_constructor(self): |
|
37 |
"""Can a Sphinx |
|
|
38 |
self.build_ |
|
|
37 | """Can a SphinxPreprocessor be constructed?""" | |
|
38 | self.build_preprocessor() | |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | def test_resources(self): |
|
42 |
"""Make sure the Sphinx |
|
|
42 | """Make sure the SphinxPreprocessor adds the appropriate resources to the | |
|
43 | 43 | resources dict.""" |
|
44 | 44 | nb = self.build_notebook() |
|
45 | 45 | res = self.build_resources() |
|
46 |
|
|
|
47 |
nb, res = |
|
|
46 | preprocessor = self.build_preprocessor() | |
|
47 | nb, res = preprocessor(nb, res) | |
|
48 | 48 | assert "author" in res['sphinx'] |
|
49 | 49 | assert "version" in res['sphinx'] |
|
50 | 50 | assert "release" in res['sphinx'] |
@@ -1,5 +1,5 b'' | |||
|
1 | 1 | """ |
|
2 |
Module with tests for the svg2pdf |
|
|
2 | Module with tests for the svg2pdf preprocessor | |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
@@ -17,15 +17,15 b' Module with tests for the svg2pdf transformer' | |||
|
17 | 17 | from IPython.testing import decorators as dec |
|
18 | 18 | from IPython.nbformat import current as nbformat |
|
19 | 19 | |
|
20 |
from .base import |
|
|
21 |
from ..svg2pdf import SVG2PDF |
|
|
20 | from .base import PreprocessorTestsBase | |
|
21 | from ..svg2pdf import SVG2PDFPreprocessor | |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Class |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 |
class Testsvg2pdf( |
|
|
28 | class Testsvg2pdf(PreprocessorTestsBase): | |
|
29 | 29 | """Contains test functions for svg2pdf.py""" |
|
30 | 30 | |
|
31 | 31 | simple_svg = """<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
@@ -55,7 +55,7 b' class Testsvg2pdf(TransformerTestsBase):' | |||
|
55 | 55 | |
|
56 | 56 | def build_notebook(self): |
|
57 | 57 | """Build a reveal slides notebook in memory for use with tests. |
|
58 |
Overrides base in |
|
|
58 | Overrides base in PreprocessorTestsBase""" | |
|
59 | 59 | |
|
60 | 60 | outputs = [nbformat.new_output(output_type="svg", output_svg=self.simple_svg)] |
|
61 | 61 | |
@@ -68,23 +68,23 b' class Testsvg2pdf(TransformerTestsBase):' | |||
|
68 | 68 | return nbformat.new_notebook(name="notebook1", worksheets=worksheets) |
|
69 | 69 | |
|
70 | 70 | |
|
71 |
def build_ |
|
|
72 |
"""Make an instance of a |
|
|
73 |
|
|
|
74 |
|
|
|
75 |
return |
|
|
71 | def build_preprocessor(self): | |
|
72 | """Make an instance of a preprocessor""" | |
|
73 | preprocessor = SVG2PDFPreprocessor() | |
|
74 | preprocessor.enabled = True | |
|
75 | return preprocessor | |
|
76 | 76 | |
|
77 | 77 | |
|
78 | 78 | def test_constructor(self): |
|
79 |
"""Can a SVG2PDF |
|
|
80 |
self.build_ |
|
|
79 | """Can a SVG2PDFPreprocessor be constructed?""" | |
|
80 | self.build_preprocessor() | |
|
81 | 81 | |
|
82 | 82 | |
|
83 | 83 | @dec.onlyif_cmds_exist('inkscape') |
|
84 | 84 | def test_output(self): |
|
85 |
"""Test the output of the SVG2PDF |
|
|
85 | """Test the output of the SVG2PDFPreprocessor""" | |
|
86 | 86 | nb = self.build_notebook() |
|
87 | 87 | res = self.build_resources() |
|
88 |
|
|
|
89 |
nb, res = |
|
|
88 | preprocessor = self.build_preprocessor() | |
|
89 | nb, res = preprocessor(nb, res) | |
|
90 | 90 | assert 'svg' in nb.worksheets[0].cells[0].outputs[0] |
@@ -70,7 +70,7 b' class FilesWriter(WriterBase):' | |||
|
70 | 70 | |
|
71 | 71 | # Write all of the extracted resources to the destination directory. |
|
72 | 72 | # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG |
|
73 |
# |
|
|
73 | # PREPROCESSOR SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS... | |
|
74 | 74 | for filename, data in resources.get('outputs', {}).items(): |
|
75 | 75 | |
|
76 | 76 | # Determine where to write the file to |
@@ -10,7 +10,7 b' Backwards incompatible changes' | |||
|
10 | 10 | |
|
11 | 11 | * Python 2.6 and 3.2 are no longer supported: the minimum required |
|
12 | 12 | Python versions are now 2.7 and 3.3. |
|
13 | * The `call` methods for nbconvert transformers has been renamed to | |
|
14 | `transform`. | |
|
13 | * The Transformer classes have been renamed to Preprocessor in nbconvert and | |
|
14 | their `call` methods for them have been renamed to `preprocess`. | |
|
15 | 15 | * The `call` methods of nbconvert post-processsors have been renamed to |
|
16 | 16 | `postprocess`. |
General Comments 0
You need to be logged in to leave comments.
Login now