Show More
@@ -1,110 +1,111 b'' | |||||
1 | """ |
|
1 | """ | |
2 | Module that re-groups preprocessor that would be applied to ipynb files |
|
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. | |
6 | """ |
|
6 | """ | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | # Copyright (c) 2013, the IPython Development Team. |
|
8 | # Copyright (c) 2013, the IPython Development Team. | |
9 | # |
|
9 | # | |
10 | # Distributed under the terms of the Modified BSD License. |
|
10 | # Distributed under the terms of the Modified BSD License. | |
11 | # |
|
11 | # | |
12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Imports |
|
16 | # Imports | |
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 |
|
18 | |||
19 | from ..utils.base import NbConvertBase |
|
19 | from ..utils.base import NbConvertBase | |
20 | from IPython.utils.traitlets import Bool |
|
20 | from IPython.utils.traitlets import Bool | |
21 |
|
21 | |||
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 | # Classes and Functions |
|
23 | # Classes and Functions | |
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 |
|
25 | |||
26 | class Preprocessor(NbConvertBase): |
|
26 | class Preprocessor(NbConvertBase): | |
27 | """ A configurable preprocessor |
|
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 | preprocessor. |
|
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:`preprocess_cell` to apply a |
|
35 | you can overwrite :meth:`preprocess_cell` to apply a transformation | |
36 | or :meth:`preprocess` 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.YourPreprocessorName.enabled = True' |
|
40 | 'c.YourPreprocessorName.enabled = True' | |
40 | """ |
|
41 | """ | |
41 |
|
42 | |||
42 | enabled = Bool(False, config=True) |
|
43 | enabled = Bool(False, config=True) | |
43 |
|
44 | |||
44 | def __init__(self, **kw): |
|
45 | def __init__(self, **kw): | |
45 | """ |
|
46 | """ | |
46 | Public constructor |
|
47 | Public constructor | |
47 |
|
48 | |||
48 | Parameters |
|
49 | Parameters | |
49 | ---------- |
|
50 | ---------- | |
50 | config : Config |
|
51 | config : Config | |
51 | Configuration file structure |
|
52 | Configuration file structure | |
52 | **kw : misc |
|
53 | **kw : misc | |
53 | Additional arguments |
|
54 | Additional arguments | |
54 | """ |
|
55 | """ | |
55 |
|
56 | |||
56 | super(Preprocessor, self).__init__(**kw) |
|
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.preprocess(nb,resources) |
|
62 | return self.preprocess(nb,resources) | |
62 | else: |
|
63 | else: | |
63 | return nb, resources |
|
64 | return nb, resources | |
64 |
|
65 | |||
65 |
|
66 | |||
66 | def preprocess(self, nb, resources): |
|
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 preprocess o |
|
72 | If you wish to apply your preprocessing to each cell, you might want | |
72 | overwrite preprocess_cell method instead. |
|
73 | to overwrite preprocess_cell method instead. | |
73 |
|
74 | |||
74 | Parameters |
|
75 | Parameters | |
75 | ---------- |
|
76 | ---------- | |
76 | nb : NotebookNode |
|
77 | nb : NotebookNode | |
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 | preprocessors to pass variables into the Jinja engine. |
|
81 | preprocessors to pass variables into the Jinja engine. | |
81 | """ |
|
82 | """ | |
82 | self.log.debug("Applying preprocess: %s", self.__class__.__name__) |
|
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.preprocess_cell(cell, resources, index) |
|
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 preprocess_cell(self, cell, resources, index): |
|
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 | |
98 | ---------- |
|
99 | ---------- | |
99 | cell : NotebookNode cell |
|
100 | cell : NotebookNode cell | |
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 | preprocessors to pass variables into the Jinja engine. |
|
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 | """ | |
107 |
|
108 | |||
108 | raise NotImplementedError('should be implemented by subclass') |
|
109 | raise NotImplementedError('should be implemented by subclass') | |
109 | return cell, resources |
|
110 | return cell, resources | |
110 |
|
111 |
@@ -1,53 +1,53 b'' | |||||
1 | """Module that allows latex output notebooks to be conditioned before |
|
1 | """Module that allows latex output notebooks to be conditioned before | |
2 | they are converted. |
|
2 | they are converted. | |
3 | """ |
|
3 | """ | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
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 preprocessor |
|
19 | # Needed to override preprocessor | |
20 | from .base import (Preprocessor) |
|
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 LatexPreprocessor(Preprocessor): |
|
27 | class LatexPreprocessor(Preprocessor): | |
28 | """ |
|
28 | """ | |
29 | Converter for latex destined documents. |
|
29 | Converter for latex destined documents. | |
30 | """ |
|
30 | """ | |
31 |
|
31 | |||
32 | def preprocess_cell(self, cell, resources, index): |
|
32 | def preprocess_cell(self, cell, resources, index): | |
33 | """ |
|
33 | """ | |
34 |
Apply a |
|
34 | Apply a transformation on each cell, | |
35 |
|
35 | |||
36 | Parameters |
|
36 | Parameters | |
37 | ---------- |
|
37 | ---------- | |
38 | cell : NotebookNode cell |
|
38 | cell : NotebookNode cell | |
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 | preprocessors to pass variables into the Jinja engine. |
|
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 | """ | |
46 |
|
46 | |||
47 | #If the cell is a markdown cell, preprocess the ampersands used to |
|
47 | #If the cell is a markdown cell, preprocess the ampersands used to | |
48 | #remove the space between them and their contents. Latex will complain |
|
48 | #remove the space between them and their contents. Latex will complain | |
49 | #if spaces exist between the ampersands and the math content. |
|
49 | #if spaces exist between the ampersands and the math content. | |
50 | #See filters.latex.rm_math_space for more information. |
|
50 | #See filters.latex.rm_math_space for more information. | |
51 | if hasattr(cell, "source") and cell.cell_type == "markdown": |
|
51 | if hasattr(cell, "source") and cell.cell_type == "markdown": | |
52 | cell.source = filters.strip_math_space(cell.source) |
|
52 | cell.source = filters.strip_math_space(cell.source) | |
53 | return cell, resources |
|
53 | return cell, resources |
General Comments 0
You need to be logged in to leave comments.
Login now