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