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