Show More
@@ -0,0 +1,104 | |||||
|
1 | """This preprocessor detect cells using a different language through | |||
|
2 | magic extensions such as `%%R` or `%%octave`. Cell's metadata is marked | |||
|
3 | so that the appropriate highlighter can be used in the `highlight` | |||
|
4 | filter. | |||
|
5 | """ | |||
|
6 | ||||
|
7 | #----------------------------------------------------------------------------- | |||
|
8 | # Copyright (c) 2013, the IPython Development Team. | |||
|
9 | # | |||
|
10 | # Distributed under the terms of the Modified BSD License. | |||
|
11 | # | |||
|
12 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | ||||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | # Imports | |||
|
17 | #----------------------------------------------------------------------------- | |||
|
18 | ||||
|
19 | from __future__ import print_function, absolute_import | |||
|
20 | ||||
|
21 | import re | |||
|
22 | ||||
|
23 | # Our own imports | |||
|
24 | # Needed to override preprocessor | |||
|
25 | from .base import (Preprocessor) | |||
|
26 | from IPython.utils.traitlets import Dict | |||
|
27 | ||||
|
28 | #----------------------------------------------------------------------------- | |||
|
29 | # Classes | |||
|
30 | #----------------------------------------------------------------------------- | |||
|
31 | ||||
|
32 | ||||
|
33 | class HighlightMagicsPreprocessor(Preprocessor): | |||
|
34 | """ | |||
|
35 | Detects and tags code cells that use a different languages than Python. | |||
|
36 | """ | |||
|
37 | ||||
|
38 | # list of magic language extensions and their associated pygment lexers | |||
|
39 | languages = Dict( | |||
|
40 | default_value={ | |||
|
41 | '%%R': 'r', | |||
|
42 | '%%bash': 'bash', | |||
|
43 | '%%octave': 'octave', | |||
|
44 | '%%perl': 'perl', | |||
|
45 | '%%ruby': 'ruby'}, | |||
|
46 | config=True, | |||
|
47 | help=("Syntax highlighting for magic's extension languages. " | |||
|
48 | "Each item associates a language magic extension such as %%R, " | |||
|
49 | "with a pygments lexer such as r.")) | |||
|
50 | ||||
|
51 | def __init__(self, config=None, **kw): | |||
|
52 | """Public constructor""" | |||
|
53 | ||||
|
54 | super(HighlightMagicsPreprocessor, self).__init__(config=config, **kw) | |||
|
55 | ||||
|
56 | # build a regular expression to catch language extensions and choose | |||
|
57 | # an adequate pygments lexer | |||
|
58 | any_language = "|".join(self.languages.keys()) | |||
|
59 | self.re_magic_language = re.compile( | |||
|
60 | r'^\s*({0})\s+'.format(any_language)) | |||
|
61 | ||||
|
62 | def which_magic_language(self, source): | |||
|
63 | """ | |||
|
64 | When a cell uses another language through a magic extension, | |||
|
65 | the other language is returned. | |||
|
66 | If no language magic is detected, this function returns None. | |||
|
67 | ||||
|
68 | Parameters | |||
|
69 | ---------- | |||
|
70 | source: str | |||
|
71 | Source code of the cell to highlight | |||
|
72 | """ | |||
|
73 | ||||
|
74 | m = self.re_magic_language.match(source) | |||
|
75 | ||||
|
76 | if m: | |||
|
77 | # By construction of the re, the matched language must be in the | |||
|
78 | # languages dictionnary | |||
|
79 | assert(m.group(1) in self.languages) | |||
|
80 | return self.languages[m.group(1)] | |||
|
81 | else: | |||
|
82 | return None | |||
|
83 | ||||
|
84 | def preprocess_cell(self, cell, resources, cell_index): | |||
|
85 | """ | |||
|
86 | Tags cells using a magic extension language | |||
|
87 | ||||
|
88 | Parameters | |||
|
89 | ---------- | |||
|
90 | cell : NotebookNode cell | |||
|
91 | Notebook cell being processed | |||
|
92 | resources : dictionary | |||
|
93 | Additional resources used in the conversion process. Allows | |||
|
94 | preprocessors to pass variables into the Jinja engine. | |||
|
95 | cell_index : int | |||
|
96 | Index of the cell being processed (see base.py) | |||
|
97 | """ | |||
|
98 | ||||
|
99 | # Only tag code cells | |||
|
100 | if hasattr(cell, "input") and cell.cell_type == "code": | |||
|
101 | magic_language = self.which_magic_language(cell.input) | |||
|
102 | if magic_language: | |||
|
103 | cell['metadata']['magics_language'] = magic_language | |||
|
104 | return cell, resources |
@@ -0,0 +1,68 | |||||
|
1 | """ | |||
|
2 | Module with tests for the HighlightMagics preprocessor | |||
|
3 | """ | |||
|
4 | ||||
|
5 | #----------------------------------------------------------------------------- | |||
|
6 | # Copyright (c) 2013, the IPython Development Team. | |||
|
7 | # | |||
|
8 | # Distributed under the terms of the Modified BSD License. | |||
|
9 | # | |||
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |||
|
11 | #----------------------------------------------------------------------------- | |||
|
12 | ||||
|
13 | #----------------------------------------------------------------------------- | |||
|
14 | # Imports | |||
|
15 | #----------------------------------------------------------------------------- | |||
|
16 | ||||
|
17 | from .base import PreprocessorTestsBase | |||
|
18 | from ..highlightmagics import HighlightMagicsPreprocessor | |||
|
19 | ||||
|
20 | ||||
|
21 | #----------------------------------------------------------------------------- | |||
|
22 | # Class | |||
|
23 | #----------------------------------------------------------------------------- | |||
|
24 | ||||
|
25 | class TestHighlightMagics(PreprocessorTestsBase): | |||
|
26 | """Contains test functions for highlightmagics.py""" | |||
|
27 | ||||
|
28 | ||||
|
29 | def build_preprocessor(self): | |||
|
30 | """Make an instance of a preprocessor""" | |||
|
31 | preprocessor = HighlightMagicsPreprocessor() | |||
|
32 | preprocessor.enabled = True | |||
|
33 | return preprocessor | |||
|
34 | ||||
|
35 | def test_constructor(self): | |||
|
36 | """Can a HighlightMagicsPreprocessor be constructed?""" | |||
|
37 | self.build_preprocessor() | |||
|
38 | ||||
|
39 | def test_tagging(self): | |||
|
40 | """Test the HighlightMagicsPreprocessor tagging""" | |||
|
41 | nb = self.build_notebook() | |||
|
42 | res = self.build_resources() | |||
|
43 | preprocessor = self.build_preprocessor() | |||
|
44 | nb.worksheets[0].cells[0].input = """%%R -i x,y -o XYcoef | |||
|
45 | lm.fit <- lm(y~x) | |||
|
46 | par(mfrow=c(2,2)) | |||
|
47 | print(summary(lm.fit)) | |||
|
48 | plot(lm.fit) | |||
|
49 | XYcoef <- coef(lm.fit)""" | |||
|
50 | ||||
|
51 | nb, res = preprocessor(nb, res) | |||
|
52 | ||||
|
53 | assert('magics_language' in nb.worksheets[0].cells[0]['metadata']) | |||
|
54 | ||||
|
55 | self.assertEqual(nb.worksheets[0].cells[0]['metadata']['magics_language'], 'r') | |||
|
56 | ||||
|
57 | def test_no_false_positive(self): | |||
|
58 | """Test that HighlightMagicsPreprocessor does not tag false positives""" | |||
|
59 | nb = self.build_notebook() | |||
|
60 | res = self.build_resources() | |||
|
61 | preprocessor = self.build_preprocessor() | |||
|
62 | nb.worksheets[0].cells[0].input = """# this should not be detected | |||
|
63 | print(\""" | |||
|
64 | %%R -i x, y | |||
|
65 | \""")""" | |||
|
66 | nb, res = preprocessor(nb, res) | |||
|
67 | ||||
|
68 | assert('magics_language' not in nb.worksheets[0].cells[0]['metadata']) No newline at end of file |
@@ -1,279 +1,280 | |||||
1 | """This module defines Exporter, a highly configurable converter |
|
1 | """This module defines Exporter, a highly configurable converter | |
2 | that uses Jinja2 to export notebook files into different formats. |
|
2 | that uses Jinja2 to export notebook files into different formats. | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | from __future__ import print_function, absolute_import |
|
17 | from __future__ import print_function, absolute_import | |
18 |
|
18 | |||
19 | # Stdlib imports |
|
19 | # Stdlib imports | |
20 | import io |
|
20 | import io | |
21 | import os |
|
21 | import os | |
22 | import copy |
|
22 | import copy | |
23 | import collections |
|
23 | import collections | |
24 | import datetime |
|
24 | import datetime | |
25 |
|
25 | |||
26 |
|
26 | |||
27 | # IPython imports |
|
27 | # IPython imports | |
28 | from IPython.config.configurable import LoggingConfigurable |
|
28 | from IPython.config.configurable import LoggingConfigurable | |
29 | from IPython.config import Config |
|
29 | from IPython.config import Config | |
30 | from IPython.nbformat import current as nbformat |
|
30 | from IPython.nbformat import current as nbformat | |
31 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List |
|
31 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List | |
32 | from IPython.utils.importstring import import_item |
|
32 | from IPython.utils.importstring import import_item | |
33 | from IPython.utils import text, py3compat |
|
33 | from IPython.utils import text, py3compat | |
34 |
|
34 | |||
35 | from IPython.nbconvert import preprocessors as nbpreprocessors |
|
35 | from IPython.nbconvert import preprocessors as nbpreprocessors | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | #----------------------------------------------------------------------------- |
|
38 | #----------------------------------------------------------------------------- | |
39 | # Class |
|
39 | # Class | |
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 |
|
41 | |||
42 | class ResourcesDict(collections.defaultdict): |
|
42 | class ResourcesDict(collections.defaultdict): | |
43 | def __missing__(self, key): |
|
43 | def __missing__(self, key): | |
44 | return '' |
|
44 | return '' | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | class Exporter(LoggingConfigurable): |
|
47 | class Exporter(LoggingConfigurable): | |
48 | """ |
|
48 | """ | |
49 | Class containing methods that sequentially run a list of preprocessors on a |
|
49 | Class containing methods that sequentially run a list of preprocessors on a | |
50 | NotebookNode object and then return the modified NotebookNode object and |
|
50 | NotebookNode object and then return the modified NotebookNode object and | |
51 | accompanying resources dict. |
|
51 | accompanying resources dict. | |
52 | """ |
|
52 | """ | |
53 |
|
53 | |||
54 | file_extension = Unicode( |
|
54 | file_extension = Unicode( | |
55 | 'txt', config=True, |
|
55 | 'txt', config=True, | |
56 | help="Extension of the file that should be written to disk" |
|
56 | help="Extension of the file that should be written to disk" | |
57 | ) |
|
57 | ) | |
58 |
|
58 | |||
59 | #Configurability, allows the user to easily add filters and preprocessors. |
|
59 | #Configurability, allows the user to easily add filters and preprocessors. | |
60 | preprocessors = List(config=True, |
|
60 | preprocessors = List(config=True, | |
61 | help="""List of preprocessors, by name or namespace, to enable.""") |
|
61 | help="""List of preprocessors, by name or namespace, to enable.""") | |
62 |
|
62 | |||
63 | _preprocessors = None |
|
63 | _preprocessors = None | |
64 |
|
64 | |||
65 | default_preprocessors = List([nbpreprocessors.coalesce_streams, |
|
65 | default_preprocessors = List([nbpreprocessors.coalesce_streams, | |
66 | nbpreprocessors.SVG2PDFPreprocessor, |
|
66 | nbpreprocessors.SVG2PDFPreprocessor, | |
67 | nbpreprocessors.ExtractOutputPreprocessor, |
|
67 | nbpreprocessors.ExtractOutputPreprocessor, | |
68 | nbpreprocessors.CSSHTMLHeaderPreprocessor, |
|
68 | nbpreprocessors.CSSHTMLHeaderPreprocessor, | |
69 | nbpreprocessors.RevealHelpPreprocessor, |
|
69 | nbpreprocessors.RevealHelpPreprocessor, | |
70 | nbpreprocessors.LatexPreprocessor, |
|
70 | nbpreprocessors.LatexPreprocessor, | |
71 |
nbpreprocessors.SphinxPreprocessor |
|
71 | nbpreprocessors.SphinxPreprocessor, | |
|
72 | nbpreprocessors.HighlightMagicsPreprocessor], | |||
72 | config=True, |
|
73 | config=True, | |
73 | help="""List of preprocessors available by default, by name, namespace, |
|
74 | help="""List of preprocessors available by default, by name, namespace, | |
74 | instance, or type.""") |
|
75 | instance, or type.""") | |
75 |
|
76 | |||
76 |
|
77 | |||
77 | def __init__(self, config=None, **kw): |
|
78 | def __init__(self, config=None, **kw): | |
78 | """ |
|
79 | """ | |
79 | Public constructor |
|
80 | Public constructor | |
80 |
|
81 | |||
81 | Parameters |
|
82 | Parameters | |
82 | ---------- |
|
83 | ---------- | |
83 | config : config |
|
84 | config : config | |
84 | User configuration instance. |
|
85 | User configuration instance. | |
85 | """ |
|
86 | """ | |
86 | if not config: |
|
87 | if not config: | |
87 | config = self.default_config |
|
88 | config = self.default_config | |
88 |
|
89 | |||
89 | super(Exporter, self).__init__(config=config, **kw) |
|
90 | super(Exporter, self).__init__(config=config, **kw) | |
90 |
|
91 | |||
91 | #Init |
|
92 | #Init | |
92 | self._init_preprocessors() |
|
93 | self._init_preprocessors() | |
93 |
|
94 | |||
94 |
|
95 | |||
95 | @property |
|
96 | @property | |
96 | def default_config(self): |
|
97 | def default_config(self): | |
97 | return Config() |
|
98 | return Config() | |
98 |
|
99 | |||
99 | def _config_changed(self, name, old, new): |
|
100 | def _config_changed(self, name, old, new): | |
100 | """When setting config, make sure to start with our default_config""" |
|
101 | """When setting config, make sure to start with our default_config""" | |
101 | c = self.default_config |
|
102 | c = self.default_config | |
102 | if new: |
|
103 | if new: | |
103 | c.merge(new) |
|
104 | c.merge(new) | |
104 | if c != old: |
|
105 | if c != old: | |
105 | self.config = c |
|
106 | self.config = c | |
106 | super(Exporter, self)._config_changed(name, old, c) |
|
107 | super(Exporter, self)._config_changed(name, old, c) | |
107 |
|
108 | |||
108 |
|
109 | |||
109 | def from_notebook_node(self, nb, resources=None, **kw): |
|
110 | def from_notebook_node(self, nb, resources=None, **kw): | |
110 | """ |
|
111 | """ | |
111 | Convert a notebook from a notebook node instance. |
|
112 | Convert a notebook from a notebook node instance. | |
112 |
|
113 | |||
113 | Parameters |
|
114 | Parameters | |
114 | ---------- |
|
115 | ---------- | |
115 | nb : Notebook node |
|
116 | nb : Notebook node | |
116 | resources : dict (**kw) |
|
117 | resources : dict (**kw) | |
117 | of additional resources that can be accessed read/write by |
|
118 | of additional resources that can be accessed read/write by | |
118 | preprocessors. |
|
119 | preprocessors. | |
119 | """ |
|
120 | """ | |
120 | nb_copy = copy.deepcopy(nb) |
|
121 | nb_copy = copy.deepcopy(nb) | |
121 | resources = self._init_resources(resources) |
|
122 | resources = self._init_resources(resources) | |
122 |
|
123 | |||
123 | # Preprocess |
|
124 | # Preprocess | |
124 | nb_copy, resources = self._preprocess(nb_copy, resources) |
|
125 | nb_copy, resources = self._preprocess(nb_copy, resources) | |
125 |
|
126 | |||
126 | return nb_copy, resources |
|
127 | return nb_copy, resources | |
127 |
|
128 | |||
128 |
|
129 | |||
129 | def from_filename(self, filename, resources=None, **kw): |
|
130 | def from_filename(self, filename, resources=None, **kw): | |
130 | """ |
|
131 | """ | |
131 | Convert a notebook from a notebook file. |
|
132 | Convert a notebook from a notebook file. | |
132 |
|
133 | |||
133 | Parameters |
|
134 | Parameters | |
134 | ---------- |
|
135 | ---------- | |
135 | filename : str |
|
136 | filename : str | |
136 | Full filename of the notebook file to open and convert. |
|
137 | Full filename of the notebook file to open and convert. | |
137 | """ |
|
138 | """ | |
138 |
|
139 | |||
139 | # Pull the metadata from the filesystem. |
|
140 | # Pull the metadata from the filesystem. | |
140 | if resources is None: |
|
141 | if resources is None: | |
141 | resources = ResourcesDict() |
|
142 | resources = ResourcesDict() | |
142 | if not 'metadata' in resources or resources['metadata'] == '': |
|
143 | if not 'metadata' in resources or resources['metadata'] == '': | |
143 | resources['metadata'] = ResourcesDict() |
|
144 | resources['metadata'] = ResourcesDict() | |
144 | basename = os.path.basename(filename) |
|
145 | basename = os.path.basename(filename) | |
145 | notebook_name = basename[:basename.rfind('.')] |
|
146 | notebook_name = basename[:basename.rfind('.')] | |
146 | resources['metadata']['name'] = notebook_name |
|
147 | resources['metadata']['name'] = notebook_name | |
147 |
|
148 | |||
148 | modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) |
|
149 | modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename)) | |
149 | resources['metadata']['modified_date'] = modified_date.strftime(text.date_format) |
|
150 | resources['metadata']['modified_date'] = modified_date.strftime(text.date_format) | |
150 |
|
151 | |||
151 | with io.open(filename) as f: |
|
152 | with io.open(filename) as f: | |
152 | return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw) |
|
153 | return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources, **kw) | |
153 |
|
154 | |||
154 |
|
155 | |||
155 | def from_file(self, file_stream, resources=None, **kw): |
|
156 | def from_file(self, file_stream, resources=None, **kw): | |
156 | """ |
|
157 | """ | |
157 | Convert a notebook from a notebook file. |
|
158 | Convert a notebook from a notebook file. | |
158 |
|
159 | |||
159 | Parameters |
|
160 | Parameters | |
160 | ---------- |
|
161 | ---------- | |
161 | file_stream : file-like object |
|
162 | file_stream : file-like object | |
162 | Notebook file-like object to convert. |
|
163 | Notebook file-like object to convert. | |
163 | """ |
|
164 | """ | |
164 | return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw) |
|
165 | return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw) | |
165 |
|
166 | |||
166 |
|
167 | |||
167 | def register_preprocessor(self, preprocessor, enabled=False): |
|
168 | def register_preprocessor(self, preprocessor, enabled=False): | |
168 | """ |
|
169 | """ | |
169 | Register a preprocessor. |
|
170 | Register a preprocessor. | |
170 | Preprocessors are classes that act upon the notebook before it is |
|
171 | Preprocessors are classes that act upon the notebook before it is | |
171 | passed into the Jinja templating engine. preprocessors are also |
|
172 | passed into the Jinja templating engine. preprocessors are also | |
172 | capable of passing additional information to the Jinja |
|
173 | capable of passing additional information to the Jinja | |
173 | templating engine. |
|
174 | templating engine. | |
174 |
|
175 | |||
175 | Parameters |
|
176 | Parameters | |
176 | ---------- |
|
177 | ---------- | |
177 | preprocessor : preprocessor |
|
178 | preprocessor : preprocessor | |
178 | """ |
|
179 | """ | |
179 | if preprocessor is None: |
|
180 | if preprocessor is None: | |
180 | raise TypeError('preprocessor') |
|
181 | raise TypeError('preprocessor') | |
181 | isclass = isinstance(preprocessor, type) |
|
182 | isclass = isinstance(preprocessor, type) | |
182 | constructed = not isclass |
|
183 | constructed = not isclass | |
183 |
|
184 | |||
184 | #Handle preprocessor's registration based on it's type |
|
185 | #Handle preprocessor's registration based on it's type | |
185 | if constructed and isinstance(preprocessor, py3compat.string_types): |
|
186 | if constructed and isinstance(preprocessor, py3compat.string_types): | |
186 | #preprocessor is a string, import the namespace and recursively call |
|
187 | #preprocessor is a string, import the namespace and recursively call | |
187 | #this register_preprocessor method |
|
188 | #this register_preprocessor method | |
188 | preprocessor_cls = import_item(preprocessor) |
|
189 | preprocessor_cls = import_item(preprocessor) | |
189 | return self.register_preprocessor(preprocessor_cls, enabled) |
|
190 | return self.register_preprocessor(preprocessor_cls, enabled) | |
190 |
|
191 | |||
191 | if constructed and hasattr(preprocessor, '__call__'): |
|
192 | if constructed and hasattr(preprocessor, '__call__'): | |
192 | #preprocessor is a function, no need to construct it. |
|
193 | #preprocessor is a function, no need to construct it. | |
193 | #Register and return the preprocessor. |
|
194 | #Register and return the preprocessor. | |
194 | if enabled: |
|
195 | if enabled: | |
195 | preprocessor.enabled = True |
|
196 | preprocessor.enabled = True | |
196 | self._preprocessors.append(preprocessor) |
|
197 | self._preprocessors.append(preprocessor) | |
197 | return preprocessor |
|
198 | return preprocessor | |
198 |
|
199 | |||
199 | elif isclass and isinstance(preprocessor, MetaHasTraits): |
|
200 | elif isclass and isinstance(preprocessor, MetaHasTraits): | |
200 | #preprocessor is configurable. Make sure to pass in new default for |
|
201 | #preprocessor is configurable. Make sure to pass in new default for | |
201 | #the enabled flag if one was specified. |
|
202 | #the enabled flag if one was specified. | |
202 | self.register_preprocessor(preprocessor(parent=self), enabled) |
|
203 | self.register_preprocessor(preprocessor(parent=self), enabled) | |
203 |
|
204 | |||
204 | elif isclass: |
|
205 | elif isclass: | |
205 | #preprocessor is not configurable, construct it |
|
206 | #preprocessor is not configurable, construct it | |
206 | self.register_preprocessor(preprocessor(), enabled) |
|
207 | self.register_preprocessor(preprocessor(), enabled) | |
207 |
|
208 | |||
208 | else: |
|
209 | else: | |
209 | #preprocessor is an instance of something without a __call__ |
|
210 | #preprocessor is an instance of something without a __call__ | |
210 | #attribute. |
|
211 | #attribute. | |
211 | raise TypeError('preprocessor') |
|
212 | raise TypeError('preprocessor') | |
212 |
|
213 | |||
213 |
|
214 | |||
214 | def _init_preprocessors(self): |
|
215 | def _init_preprocessors(self): | |
215 | """ |
|
216 | """ | |
216 | Register all of the preprocessors needed for this exporter, disabled |
|
217 | Register all of the preprocessors needed for this exporter, disabled | |
217 | unless specified explicitly. |
|
218 | unless specified explicitly. | |
218 | """ |
|
219 | """ | |
219 | if self._preprocessors is None: |
|
220 | if self._preprocessors is None: | |
220 | self._preprocessors = [] |
|
221 | self._preprocessors = [] | |
221 |
|
222 | |||
222 | #Load default preprocessors (not necessarly enabled by default). |
|
223 | #Load default preprocessors (not necessarly enabled by default). | |
223 | if self.default_preprocessors: |
|
224 | if self.default_preprocessors: | |
224 | for preprocessor in self.default_preprocessors: |
|
225 | for preprocessor in self.default_preprocessors: | |
225 | self.register_preprocessor(preprocessor) |
|
226 | self.register_preprocessor(preprocessor) | |
226 |
|
227 | |||
227 | #Load user preprocessors. Enable by default. |
|
228 | #Load user preprocessors. Enable by default. | |
228 | if self.preprocessors: |
|
229 | if self.preprocessors: | |
229 | for preprocessor in self.preprocessors: |
|
230 | for preprocessor in self.preprocessors: | |
230 | self.register_preprocessor(preprocessor, enabled=True) |
|
231 | self.register_preprocessor(preprocessor, enabled=True) | |
231 |
|
232 | |||
232 |
|
233 | |||
233 | def _init_resources(self, resources): |
|
234 | def _init_resources(self, resources): | |
234 |
|
235 | |||
235 | #Make sure the resources dict is of ResourcesDict type. |
|
236 | #Make sure the resources dict is of ResourcesDict type. | |
236 | if resources is None: |
|
237 | if resources is None: | |
237 | resources = ResourcesDict() |
|
238 | resources = ResourcesDict() | |
238 | if not isinstance(resources, ResourcesDict): |
|
239 | if not isinstance(resources, ResourcesDict): | |
239 | new_resources = ResourcesDict() |
|
240 | new_resources = ResourcesDict() | |
240 | new_resources.update(resources) |
|
241 | new_resources.update(resources) | |
241 | resources = new_resources |
|
242 | resources = new_resources | |
242 |
|
243 | |||
243 | #Make sure the metadata extension exists in resources |
|
244 | #Make sure the metadata extension exists in resources | |
244 | if 'metadata' in resources: |
|
245 | if 'metadata' in resources: | |
245 | if not isinstance(resources['metadata'], ResourcesDict): |
|
246 | if not isinstance(resources['metadata'], ResourcesDict): | |
246 | resources['metadata'] = ResourcesDict(resources['metadata']) |
|
247 | resources['metadata'] = ResourcesDict(resources['metadata']) | |
247 | else: |
|
248 | else: | |
248 | resources['metadata'] = ResourcesDict() |
|
249 | resources['metadata'] = ResourcesDict() | |
249 | if not resources['metadata']['name']: |
|
250 | if not resources['metadata']['name']: | |
250 | resources['metadata']['name'] = 'Notebook' |
|
251 | resources['metadata']['name'] = 'Notebook' | |
251 |
|
252 | |||
252 | #Set the output extension |
|
253 | #Set the output extension | |
253 | resources['output_extension'] = self.file_extension |
|
254 | resources['output_extension'] = self.file_extension | |
254 | return resources |
|
255 | return resources | |
255 |
|
256 | |||
256 |
|
257 | |||
257 | def _preprocess(self, nb, resources): |
|
258 | def _preprocess(self, nb, resources): | |
258 | """ |
|
259 | """ | |
259 | Preprocess the notebook before passing it into the Jinja engine. |
|
260 | Preprocess the notebook before passing it into the Jinja engine. | |
260 | To preprocess the notebook is to apply all of the |
|
261 | To preprocess the notebook is to apply all of the | |
261 |
|
262 | |||
262 | Parameters |
|
263 | Parameters | |
263 | ---------- |
|
264 | ---------- | |
264 | nb : notebook node |
|
265 | nb : notebook node | |
265 | notebook that is being exported. |
|
266 | notebook that is being exported. | |
266 | resources : a dict of additional resources that |
|
267 | resources : a dict of additional resources that | |
267 | can be accessed read/write by preprocessors |
|
268 | can be accessed read/write by preprocessors | |
268 | """ |
|
269 | """ | |
269 |
|
270 | |||
270 | # Do a copy.deepcopy first, |
|
271 | # Do a copy.deepcopy first, | |
271 | # we are never safe enough with what the preprocessors could do. |
|
272 | # we are never safe enough with what the preprocessors could do. | |
272 | nbc = copy.deepcopy(nb) |
|
273 | nbc = copy.deepcopy(nb) | |
273 | resc = copy.deepcopy(resources) |
|
274 | resc = copy.deepcopy(resources) | |
274 |
|
275 | |||
275 | #Run each preprocessor on the notebook. Carry the output along |
|
276 | #Run each preprocessor on the notebook. Carry the output along | |
276 | #to each preprocessor |
|
277 | #to each preprocessor | |
277 | for preprocessor in self._preprocessors: |
|
278 | for preprocessor in self._preprocessors: | |
278 | nbc, resc = preprocessor(nbc, resc) |
|
279 | nbc, resc = preprocessor(nbc, resc) | |
279 | return nbc, resc |
|
280 | return nbc, resc |
@@ -1,52 +1,55 | |||||
1 | """ |
|
1 | """ | |
2 | Exporter that exports Basic HTML. |
|
2 | Exporter that exports Basic HTML. | |
3 | """ |
|
3 | """ | |
4 |
|
4 | |||
5 | #----------------------------------------------------------------------------- |
|
5 | #----------------------------------------------------------------------------- | |
6 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | # Copyright (c) 2013, the IPython Development Team. | |
7 | # |
|
7 | # | |
8 | # Distributed under the terms of the Modified BSD License. |
|
8 | # Distributed under the terms of the Modified BSD License. | |
9 | # |
|
9 | # | |
10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | # The full license is in the file COPYING.txt, distributed with this software. | |
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Imports |
|
14 | # Imports | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
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 preprocessors |
|
19 | from IPython.nbconvert import preprocessors | |
20 | from IPython.config import Config |
|
20 | from IPython.config import Config | |
21 |
|
21 | |||
22 | from .templateexporter import TemplateExporter |
|
22 | from .templateexporter import TemplateExporter | |
23 |
|
23 | |||
24 | #----------------------------------------------------------------------------- |
|
24 | #----------------------------------------------------------------------------- | |
25 | # Classes |
|
25 | # Classes | |
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 | class HTMLExporter(TemplateExporter): |
|
28 | class HTMLExporter(TemplateExporter): | |
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 preprocessors/filters. If you don't need custom preprocessors/ |
|
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 | |||
36 | file_extension = Unicode( |
|
36 | file_extension = Unicode( | |
37 | 'html', config=True, |
|
37 | 'html', config=True, | |
38 | help="Extension of the file that should be written to disk" |
|
38 | help="Extension of the file that should be written to disk" | |
39 | ) |
|
39 | ) | |
40 |
|
40 | |||
41 | default_template = Unicode('full', config=True, help="""Flavor of the data |
|
41 | default_template = Unicode('full', config=True, help="""Flavor of the data | |
42 | format to use. I.E. 'full' or 'basic'""") |
|
42 | format to use. I.E. 'full' or 'basic'""") | |
43 |
|
43 | |||
44 | @property |
|
44 | @property | |
45 | def default_config(self): |
|
45 | def default_config(self): | |
46 | c = Config({ |
|
46 | c = Config({ | |
47 | 'CSSHTMLHeaderPreprocessor':{ |
|
47 | 'CSSHTMLHeaderPreprocessor':{ | |
48 | 'enabled':True |
|
48 | 'enabled':True | |
|
49 | }, | |||
|
50 | 'HighlightMagicsPreprocessor': { | |||
|
51 | 'enabled':True | |||
49 |
} |
|
52 | } | |
50 | }) |
|
53 | }) | |
51 | c.merge(super(HTMLExporter,self).default_config) |
|
54 | c.merge(super(HTMLExporter,self).default_config) | |
52 | return c |
|
55 | return c |
@@ -1,91 +1,94 | |||||
1 | """ |
|
1 | """ | |
2 | Exporter that allows Latex Jinja templates to work. Contains logic to |
|
2 | Exporter that allows Latex Jinja templates to work. Contains logic to | |
3 | appropriately prepare IPYNB files for export to LaTeX. Including but |
|
3 | appropriately prepare IPYNB files for export to LaTeX. Including but | |
4 | not limited to escaping LaTeX, fixing math region tags, using special |
|
4 | not limited to escaping LaTeX, fixing math region tags, using special | |
5 | tags to circumvent Jinja/Latex syntax conflicts. |
|
5 | tags to circumvent Jinja/Latex syntax conflicts. | |
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 | # Stdlib imports |
|
19 | # Stdlib imports | |
20 | import os |
|
20 | import os | |
21 |
|
21 | |||
22 | # IPython imports |
|
22 | # IPython imports | |
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, preprocessors |
|
26 | from IPython.nbconvert import filters, preprocessors | |
27 | from .templateexporter import TemplateExporter |
|
27 | from .templateexporter import TemplateExporter | |
28 |
|
28 | |||
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 | # Classes and functions |
|
30 | # Classes and functions | |
31 | #----------------------------------------------------------------------------- |
|
31 | #----------------------------------------------------------------------------- | |
32 |
|
32 | |||
33 | class LatexExporter(TemplateExporter): |
|
33 | class LatexExporter(TemplateExporter): | |
34 | """ |
|
34 | """ | |
35 | Exports to a Latex template. Inherit from this class if your template is |
|
35 | Exports to a Latex template. Inherit from this class if your template is | |
36 | LaTeX based and you need custom tranformers/filters. Inherit from it if |
|
36 | LaTeX based and you need custom tranformers/filters. Inherit from it if | |
37 | you are writing your own HTML template and need custom tranformers/filters. |
|
37 | you are writing your own HTML template and need custom tranformers/filters. | |
38 | If you don't need custom tranformers/filters, just change the |
|
38 | If you don't need custom tranformers/filters, just change the | |
39 | 'template_file' config option. Place your template in the special "/latex" |
|
39 | 'template_file' config option. Place your template in the special "/latex" | |
40 | subfolder of the "../templates" folder. |
|
40 | subfolder of the "../templates" folder. | |
41 | """ |
|
41 | """ | |
42 |
|
42 | |||
43 | file_extension = Unicode( |
|
43 | file_extension = Unicode( | |
44 | 'tex', config=True, |
|
44 | 'tex', config=True, | |
45 | help="Extension of the file that should be written to disk") |
|
45 | help="Extension of the file that should be written to disk") | |
46 |
|
46 | |||
47 | default_template = Unicode('article', config=True, help="""Template of the |
|
47 | default_template = Unicode('article', config=True, help="""Template of the | |
48 | data format to use. I.E. 'full' or 'basic'""") |
|
48 | data format to use. I.E. 'full' or 'basic'""") | |
49 |
|
49 | |||
50 | #Latex constants |
|
50 | #Latex constants | |
51 | default_template_path = Unicode( |
|
51 | default_template_path = Unicode( | |
52 | os.path.join("..", "templates", "latex"), config=True, |
|
52 | os.path.join("..", "templates", "latex"), config=True, | |
53 | help="Path where the template files are located.") |
|
53 | help="Path where the template files are located.") | |
54 |
|
54 | |||
55 | template_skeleton_path = Unicode( |
|
55 | template_skeleton_path = Unicode( | |
56 | os.path.join("..", "templates", "latex", "skeleton"), config=True, |
|
56 | os.path.join("..", "templates", "latex", "skeleton"), config=True, | |
57 | help="Path where the template skeleton files are located.") |
|
57 | help="Path where the template skeleton files are located.") | |
58 |
|
58 | |||
59 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
59 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
60 | jinja_comment_block_start = Unicode("((=", config=True) |
|
60 | jinja_comment_block_start = Unicode("((=", config=True) | |
61 | jinja_comment_block_end = Unicode("=))", config=True) |
|
61 | jinja_comment_block_end = Unicode("=))", config=True) | |
62 | jinja_variable_block_start = Unicode("(((", config=True) |
|
62 | jinja_variable_block_start = Unicode("(((", config=True) | |
63 | jinja_variable_block_end = Unicode(")))", config=True) |
|
63 | jinja_variable_block_end = Unicode(")))", config=True) | |
64 | jinja_logic_block_start = Unicode("((*", config=True) |
|
64 | jinja_logic_block_start = Unicode("((*", config=True) | |
65 | jinja_logic_block_end = Unicode("*))", config=True) |
|
65 | jinja_logic_block_end = Unicode("*))", config=True) | |
66 |
|
66 | |||
67 | #Extension that the template files use. |
|
67 | #Extension that the template files use. | |
68 | template_extension = Unicode(".tplx", config=True) |
|
68 | template_extension = Unicode(".tplx", config=True) | |
69 |
|
69 | |||
70 |
|
70 | |||
71 | @property |
|
71 | @property | |
72 | def default_config(self): |
|
72 | def default_config(self): | |
73 | c = Config({ |
|
73 | c = Config({ | |
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 | 'ExtractOutputPreprocessor': { |
|
77 | 'ExtractOutputPreprocessor': { | |
78 | 'enabled':True |
|
78 | 'enabled':True | |
79 | }, |
|
79 | }, | |
80 | 'SVG2PDFPreprocessor': { |
|
80 | 'SVG2PDFPreprocessor': { | |
81 | 'enabled':True |
|
81 | 'enabled':True | |
82 | }, |
|
82 | }, | |
83 | 'LatexPreprocessor': { |
|
83 | 'LatexPreprocessor': { | |
84 | 'enabled':True |
|
84 | 'enabled':True | |
85 | }, |
|
85 | }, | |
86 | 'SphinxPreprocessor': { |
|
86 | 'SphinxPreprocessor': { | |
87 | 'enabled':True |
|
87 | 'enabled':True | |
|
88 | }, | |||
|
89 | 'HighlightMagicsPreprocessor': { | |||
|
90 | 'enabled':True | |||
88 | } |
|
91 | } | |
89 | }) |
|
92 | }) | |
90 | c.merge(super(LatexExporter,self).default_config) |
|
93 | c.merge(super(LatexExporter,self).default_config) | |
91 | return c |
|
94 | return c |
@@ -1,12 +1,13 | |||||
1 | # Class base Preprocessors |
|
1 | # Class base Preprocessors | |
2 | from .base import Preprocessor |
|
2 | from .base import Preprocessor | |
3 | from .convertfigures import ConvertFiguresPreprocessor |
|
3 | from .convertfigures import ConvertFiguresPreprocessor | |
4 | from .svg2pdf import SVG2PDFPreprocessor |
|
4 | from .svg2pdf import SVG2PDFPreprocessor | |
5 | from .extractoutput import ExtractOutputPreprocessor |
|
5 | from .extractoutput import ExtractOutputPreprocessor | |
6 | from .revealhelp import RevealHelpPreprocessor |
|
6 | from .revealhelp import RevealHelpPreprocessor | |
7 | from .latex import LatexPreprocessor |
|
7 | from .latex import LatexPreprocessor | |
8 | from .sphinx import SphinxPreprocessor |
|
8 | from .sphinx import SphinxPreprocessor | |
9 | from .csshtmlheader import CSSHTMLHeaderPreprocessor |
|
9 | from .csshtmlheader import CSSHTMLHeaderPreprocessor | |
|
10 | from .highlightmagics import HighlightMagicsPreprocessor | |||
10 |
|
11 | |||
11 | # decorated function Preprocessors |
|
12 | # decorated function Preprocessors | |
12 | from .coalescestreams import coalesce_streams |
|
13 | from .coalescestreams import coalesce_streams |
General Comments 0
You need to be logged in to leave comments.
Login now