Show More
@@ -1,110 +1,111 b'' | |||
|
1 | 1 | """ |
|
2 | 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. |
|
6 | 6 | """ |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (c) 2013, the IPython Development Team. |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the Modified BSD License. |
|
11 | 11 | # |
|
12 | 12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from ..utils.base import NbConvertBase |
|
20 | 20 | from IPython.utils.traitlets import Bool |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Classes and Functions |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | class Preprocessor(NbConvertBase): |
|
27 | 27 | """ A configurable preprocessor |
|
28 | 28 | |
|
29 | 29 | Inherit from this class if you wish to have configurability for your |
|
30 | 30 | preprocessor. |
|
31 | 31 | |
|
32 |
Any configurable traitlets this class exposed will be configurable in |
|
|
33 | using c.SubClassName.atribute=value | |
|
32 | Any configurable traitlets this class exposed will be configurable in | |
|
33 | profiles using c.SubClassName.atribute=value | |
|
34 | 34 | |
|
35 |
you can overwrite :meth:`preprocess_cell` to apply a |
|
|
36 | or :meth:`preprocess` if you prefer your own logic. See corresponding docstring for informations. | |
|
35 | you can overwrite :meth:`preprocess_cell` to apply a transformation | |
|
36 | independently on each cell or :meth:`preprocess` if you prefer your own | |
|
37 | logic. See corresponding docstring for informations. | |
|
37 | 38 | |
|
38 | 39 | Disabled by default and can be enabled via the config by |
|
39 | 40 | 'c.YourPreprocessorName.enabled = True' |
|
40 | 41 | """ |
|
41 | 42 | |
|
42 | 43 | enabled = Bool(False, config=True) |
|
43 | 44 | |
|
44 | 45 | def __init__(self, **kw): |
|
45 | 46 | """ |
|
46 | 47 | Public constructor |
|
47 | 48 | |
|
48 | 49 | Parameters |
|
49 | 50 | ---------- |
|
50 | 51 | config : Config |
|
51 | 52 | Configuration file structure |
|
52 | 53 | **kw : misc |
|
53 | 54 | Additional arguments |
|
54 | 55 | """ |
|
55 | 56 | |
|
56 | 57 | super(Preprocessor, self).__init__(**kw) |
|
57 | 58 | |
|
58 | 59 | |
|
59 | 60 | def __call__(self, nb, resources): |
|
60 | 61 | if self.enabled: |
|
61 | 62 | return self.preprocess(nb,resources) |
|
62 | 63 | else: |
|
63 | 64 | return nb, resources |
|
64 | 65 | |
|
65 | 66 | |
|
66 | 67 | def preprocess(self, nb, resources): |
|
67 | 68 | """ |
|
68 |
|
|
|
69 | Preprocessing to apply on each notebook. | |
|
69 | 70 | |
|
70 | 71 | You should return modified nb, resources. |
|
71 |
If you wish to apply your preprocess o |
|
|
72 | overwrite preprocess_cell method instead. | |
|
72 | If you wish to apply your preprocessing to each cell, you might want | |
|
73 | to overwrite preprocess_cell method instead. | |
|
73 | 74 | |
|
74 | 75 | Parameters |
|
75 | 76 | ---------- |
|
76 | 77 | nb : NotebookNode |
|
77 | 78 | Notebook being converted |
|
78 | 79 | resources : dictionary |
|
79 | 80 | Additional resources used in the conversion process. Allows |
|
80 | 81 | preprocessors to pass variables into the Jinja engine. |
|
81 | 82 | """ |
|
82 | 83 | self.log.debug("Applying preprocess: %s", self.__class__.__name__) |
|
83 | 84 | try : |
|
84 | 85 | for worksheet in nb.worksheets: |
|
85 | 86 | for index, cell in enumerate(worksheet.cells): |
|
86 | 87 | worksheet.cells[index], resources = self.preprocess_cell(cell, resources, index) |
|
87 | 88 | return nb, resources |
|
88 | 89 | except NotImplementedError: |
|
89 | 90 | raise NotImplementedError('should be implemented by subclass') |
|
90 | 91 | |
|
91 | 92 | |
|
92 | 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 | 96 | should return modified cell and resource dictionary. |
|
96 | 97 | |
|
97 | 98 | Parameters |
|
98 | 99 | ---------- |
|
99 | 100 | cell : NotebookNode cell |
|
100 | 101 | Notebook cell being processed |
|
101 | 102 | resources : dictionary |
|
102 | 103 | Additional resources used in the conversion process. Allows |
|
103 | 104 | preprocessors to pass variables into the Jinja engine. |
|
104 | 105 | index : int |
|
105 | 106 | Index of the cell being processed |
|
106 | 107 | """ |
|
107 | 108 | |
|
108 | 109 | raise NotImplementedError('should be implemented by subclass') |
|
109 | 110 | return cell, resources |
|
110 | 111 |
@@ -1,53 +1,53 b'' | |||
|
1 | 1 | """Module that allows latex output notebooks to be conditioned before |
|
2 | 2 | they are converted. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | # |
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | from __future__ import print_function, absolute_import |
|
17 | 17 | |
|
18 | 18 | # Our own imports |
|
19 | 19 | # Needed to override preprocessor |
|
20 | 20 | from .base import (Preprocessor) |
|
21 | 21 | from IPython.nbconvert import filters |
|
22 | 22 | |
|
23 | 23 | #----------------------------------------------------------------------------- |
|
24 | 24 | # Classes |
|
25 | 25 | #----------------------------------------------------------------------------- |
|
26 | 26 | |
|
27 | 27 | class LatexPreprocessor(Preprocessor): |
|
28 | 28 | """ |
|
29 | 29 | Converter for latex destined documents. |
|
30 | 30 | """ |
|
31 | 31 | |
|
32 | 32 | def preprocess_cell(self, cell, resources, index): |
|
33 | 33 | """ |
|
34 |
Apply a |
|
|
34 | Apply a transformation on each cell, | |
|
35 | 35 | |
|
36 | 36 | Parameters |
|
37 | 37 | ---------- |
|
38 | 38 | cell : NotebookNode cell |
|
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 | """ |
|
46 | 46 | |
|
47 | 47 | #If the cell is a markdown cell, preprocess the ampersands used to |
|
48 | 48 | #remove the space between them and their contents. Latex will complain |
|
49 | 49 | #if spaces exist between the ampersands and the math content. |
|
50 | 50 | #See filters.latex.rm_math_space for more information. |
|
51 | 51 | if hasattr(cell, "source") and cell.cell_type == "markdown": |
|
52 | 52 | cell.source = filters.strip_math_space(cell.source) |
|
53 | 53 | return cell, resources |
General Comments 0
You need to be logged in to leave comments.
Login now