Show More
1 | NO CONTENT: file renamed from nbconvert/api/converter.py to nbconvert/api/convert.py |
|
NO CONTENT: file renamed from nbconvert/api/converter.py to nbconvert/api/convert.py |
@@ -1,257 +1,268 b'' | |||||
1 | """Exporter for the notebook conversion pipeline. |
|
1 | """Exporter for the notebook conversion pipeline. | |
2 |
|
2 | |||
3 | This module defines Exporter, a highly configurable converter |
|
3 | This module defines Exporter, a highly configurable converter | |
4 | that uses Jinja2 to export notebook files into different format. |
|
4 | that uses Jinja2 to export notebook files into different format. | |
5 |
|
5 | |||
6 | You can register both pre-transformers that will act on the notebook format |
|
6 | You can register both pre-transformers that will act on the notebook format | |
7 |
befor conversion and jinja filter that would then be avai |
|
7 | before conversion and jinja filter that would then be available in the templates | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (c) 2013, the IPython Development Team. |
|
11 | # Copyright (c) 2013, the IPython Development Team. | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 | # |
|
14 | # | |
15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | # The full license is in the file COPYING.txt, distributed with this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | from __future__ import print_function, absolute_import |
|
21 | from __future__ import print_function, absolute_import | |
22 |
|
22 | |||
23 | # Stdlib imports |
|
23 | # Stdlib imports | |
24 | import io |
|
24 | import io | |
25 | import os |
|
25 | import os | |
26 |
|
26 | |||
27 | # IPython imports |
|
27 | # IPython imports | |
28 | from IPython.config.configurable import Configurable |
|
28 | from IPython.config.configurable import Configurable | |
29 | from IPython.nbformat import current as nbformat |
|
29 | from IPython.nbformat import current as nbformat | |
30 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool |
|
30 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool | |
31 | from IPython.utils.text import indent |
|
31 | from IPython.utils.text import indent | |
32 |
|
32 | |||
33 | # other libs/dependencies |
|
33 | # other libs/dependencies | |
34 | from jinja2 import Environment, FileSystemLoader |
|
34 | from jinja2 import Environment, FileSystemLoader | |
35 | from markdown import markdown |
|
35 | from markdown import markdown | |
36 |
|
36 | |||
37 | # local import |
|
37 | # local import | |
38 | import filters.strings |
|
38 | import filters.strings | |
39 | import filters.markdown |
|
39 | import filters.markdown | |
40 | import filters.latex |
|
40 | import filters.latex | |
41 | import filters.datatypefilter |
|
41 | import filters.datatypefilter | |
42 | import filters.pygments |
|
42 | import filters.pygments | |
43 | import filters.ansi |
|
43 | import filters.ansi | |
44 |
|
44 | |||
45 | import transformers.extractfigure |
|
45 | import transformers.extractfigure | |
46 | import transformers.csshtmlheader |
|
46 | import transformers.csshtmlheader | |
47 | import transformers.revealhelp |
|
47 | import transformers.revealhelp | |
48 | import transformers.coalescestreams |
|
48 | import transformers.coalescestreams | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | #----------------------------------------------------------------------------- |
|
51 | #----------------------------------------------------------------------------- | |
52 | # Globals and constants |
|
52 | # Globals and constants | |
53 | #----------------------------------------------------------------------------- |
|
53 | #----------------------------------------------------------------------------- | |
54 |
|
54 | |||
55 | #Standard Jinja2 environment constants |
|
55 | #Standard Jinja2 environment constants | |
56 | TEMPLATE_PATH = "/../templates/" |
|
56 | TEMPLATE_PATH = "/../templates/" | |
57 | TEMPLATE_SKELETON_PATH = "/../templates/skeleton/" |
|
57 | TEMPLATE_SKELETON_PATH = "/../templates/skeleton/" | |
58 | TEMPLATE_EXTENSION = ".tpl" |
|
58 | TEMPLATE_EXTENSION = ".tpl" | |
59 |
|
59 | |||
60 | #Jinja2 extensions to load. |
|
60 | #Jinja2 extensions to load. | |
61 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] |
|
61 | JINJA_EXTENSIONS = ['jinja2.ext.loopcontrols'] | |
62 |
|
62 | |||
63 | #----------------------------------------------------------------------------- |
|
63 | #----------------------------------------------------------------------------- | |
64 | # Classes and functions |
|
64 | # Classes and functions | |
65 | #----------------------------------------------------------------------------- |
|
65 | #----------------------------------------------------------------------------- | |
66 | class Exporter(Configurable): |
|
66 | class Exporter(Configurable): | |
67 | """ A Jinja2 base converter templates |
|
67 | """ A Jinja2 base converter templates | |
68 |
|
68 | |||
69 | Pre-process the IPYNB files, feed it through Jinja2 templates, |
|
69 | Pre-process the IPYNB files, feed it through Jinja2 templates, | |
70 | and spit an converted files and a data object with other data |
|
70 | and spit an converted files and a data object with other data | |
71 | should be mostly configurable |
|
71 | should be mostly configurable | |
72 | """ |
|
72 | """ | |
73 |
|
73 | |||
74 | pre_transformer_order = List(['haspyout_transformer'], |
|
74 | pre_transformer_order = List(['haspyout_transformer'], | |
75 | config=True, |
|
75 | config=True, | |
76 | help= """ |
|
76 | help= """ | |
77 | An ordered list of pre-transformer to apply to the IPYNB |
|
77 | An ordered list of pre-transformer to apply to the IPYNB | |
78 | file before running through templates |
|
78 | file before running through templates | |
79 | """ |
|
79 | """ | |
80 | ) |
|
80 | ) | |
81 |
|
81 | |||
82 | template_file = Unicode( |
|
82 | template_file = Unicode( | |
83 | '', config=True, |
|
83 | '', config=True, | |
84 | help="Name of the template file to use") |
|
84 | help="Name of the template file to use") | |
85 |
|
85 | |||
86 | fileext = Unicode( |
|
86 | file_extension = Unicode( | |
87 | 'txt', config=True, |
|
87 | 'txt', config=True, | |
88 | help="Extension of the file that should be written to disk" |
|
88 | help="Extension of the file that should be written to disk" | |
89 | ) |
|
89 | ) | |
90 |
|
90 | |||
91 | stdout = Bool( |
|
91 | stdout = Bool( | |
92 | True, config=True, |
|
92 | True, config=True, | |
93 | help="""Whether to print the converted IPYNB file to stdout |
|
93 | help="""Whether to print the converted IPYNB file to stdout | |
94 | "use full do diff files without actually writing a new file""" |
|
94 | "use full do diff files without actually writing a new file""" | |
95 | ) |
|
95 | ) | |
96 |
|
96 | |||
97 | write = Bool( |
|
97 | write = Bool( | |
98 | False, config=True, |
|
98 | False, config=True, | |
99 | help="""Should the converted notebook file be written to disk |
|
99 | help="""Should the converted notebook file be written to disk | |
100 | along with potential extracted resources.""" |
|
100 | along with potential extracted resources.""" | |
101 | ) |
|
101 | ) | |
102 |
|
102 | |||
103 | #Processors that process the input data prior to the export, set in the |
|
103 | #Processors that process the input data prior to the export, set in the | |
104 | #constructor for this class. |
|
104 | #constructor for this class. | |
105 | preprocessors = [] |
|
105 | preprocessors = [] | |
106 |
|
106 | |||
107 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): |
|
107 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): | |
108 | """ Init a new converter. |
|
108 | """ Init a new converter. | |
109 |
|
109 | |||
110 | config: the Configurable config object to pass around. |
|
110 | config: the Configurable config object to pass around. | |
111 |
|
111 | |||
112 | preprocessors: dict of **available** key/value function to run on |
|
112 | preprocessors: dict of **available** key/value function to run on | |
113 | ipynb json data before conversion to extract/in-line file. |
|
113 | ipynb json data before conversion to extract/in-line file. | |
114 | See `transformer.py` and `ConfigurableTransformers` |
|
114 | See `transformer.py` and `ConfigurableTransformers` | |
115 |
|
115 | |||
116 | set the order in which the transformers should apply |
|
116 | set the order in which the transformers should apply | |
117 | with the `pre_transformer_order` trait of this class |
|
117 | with the `pre_transformer_order` trait of this class | |
118 |
|
118 | |||
119 | transformers registered by this key will take precedence on |
|
119 | transformers registered by this key will take precedence on | |
120 | default one. |
|
120 | default one. | |
121 |
|
121 | |||
122 | jinja_filters: dict of supplementary jinja filter that should be made |
|
122 | jinja_filters: dict of supplementary jinja filter that should be made | |
123 | available in template. If those are of Configurable Class type, |
|
123 | available in template. If those are of Configurable Class type, | |
124 | they will be instanciated with the config object as argument. |
|
124 | they will be instanciated with the config object as argument. | |
125 |
|
125 | |||
126 | user defined filter will overwrite the one available by default. |
|
126 | user defined filter will overwrite the one available by default. | |
127 | """ |
|
127 | """ | |
128 |
|
128 | |||
129 | #Call the base class constructor |
|
129 | #Call the base class constructor | |
130 | super(Exporter, self).__init__(config=config, **kw) |
|
130 | super(Exporter, self).__init__(config=config, **kw) | |
131 |
|
131 | |||
132 | #Standard environment |
|
132 | #Standard environment | |
133 | self.ext = TEMPLATE_EXTENSION |
|
133 | self.ext = TEMPLATE_EXTENSION | |
134 | self._init_environment() |
|
134 | self._init_environment() | |
135 |
|
135 | |||
136 | #TODO: Implement reflection style methods to get user transformers. |
|
136 | #TODO: Implement reflection style methods to get user transformers. | |
137 | #if not preprocessors is None: |
|
137 | #if not preprocessors is None: | |
138 | # for name in self.pre_transformer_order: |
|
138 | # for name in self.pre_transformer_order: | |
139 | # # get the user-defined transformer first |
|
139 | # # get the user-defined transformer first | |
140 | # transformer = preprocessors.get(name, getattr(trans, name, None)) |
|
140 | # transformer = preprocessors.get(name, getattr(trans, name, None)) | |
141 | # if isinstance(transformer, MetaHasTraits): |
|
141 | # if isinstance(transformer, MetaHasTraits): | |
142 | # transformer = transformer(config=config) |
|
142 | # transformer = transformer(config=config) | |
143 | # self.preprocessors.append(transformer) |
|
143 | # self.preprocessors.append(transformer) | |
144 |
|
144 | |||
145 | #For compatibility, TODO: remove later. |
|
145 | #Add transformers | |
146 | self.preprocessors.append(transformers.coalescestreams.coalesce_streams) |
|
146 | self._register_transformers() | |
147 | self.preprocessors.append(transformers.extractfigure.ExtractFigureTransformer(config=config)) |
|
|||
148 | self.preprocessors.append(transformers.revealhelp.RevealHelpTransformer(config=config)) |
|
|||
149 | self.preprocessors.append(transformers.csshtmlheader.CSSHtmlHeaderTransformer(config=config)) |
|
|||
150 |
|
147 | |||
151 | #Add filters to the Jinja2 environment |
|
148 | #Add filters to the Jinja2 environment | |
152 | self._register_filters() |
|
149 | self._register_filters() | |
153 |
|
150 | |||
154 | #Load user filters. Overwrite existing filters if need be. |
|
151 | #Load user filters. Overwrite existing filters if need be. | |
155 | if not jinja_filters is None: |
|
152 | if not jinja_filters is None: | |
156 | for key, user_filter in jinja_filters.iteritems(): |
|
153 | for key, user_filter in jinja_filters.iteritems(): | |
157 | if isinstance(user_filter, MetaHasTraits): |
|
154 | if isinstance(user_filter, MetaHasTraits): | |
158 | self.env.filters[key] = user_filter(config=config) |
|
155 | self.environment.filters[key] = user_filter(config=config) | |
159 | else: |
|
156 | else: | |
160 | self.env.filters[key] = user_filter |
|
157 | self.environment.filters[key] = user_filter | |
161 |
|
158 | |||
162 | #Load the template file. |
|
159 | #Load the template file. | |
163 | self.template = self.env.get_template(self.template_file+self.ext) |
|
160 | self.template = self.environment.get_template(self.template_file+self.ext) | |
164 |
|
161 | |||
165 |
|
162 | |||
166 | def from_notebook_node(self, nb): |
|
163 | def from_notebook_node(self, nb): | |
167 | """Export NotebookNode instance |
|
164 | """Export NotebookNode instance | |
168 |
|
165 | |||
169 | nb: NotebookNode to export. |
|
166 | nb: NotebookNode to export. | |
170 |
|
167 | |||
171 | Returns both the converted ipynb file and a dict containing the |
|
168 | Returns both the converted ipynb file and a dict containing the | |
172 | resources created along the way via the transformers and Jinja2 |
|
169 | resources created along the way via the transformers and Jinja2 | |
173 | processing. |
|
170 | processing. | |
174 | """ |
|
171 | """ | |
175 |
|
172 | |||
176 | nb, resources = self._preprocess(nb) |
|
173 | nb, resources = self._preprocess(nb) | |
177 | return self.template.render(nb=nb, resources=resources), resources |
|
174 | return self.template.render(nb=nb, resources=resources), resources | |
178 |
|
175 | |||
179 |
|
176 | |||
180 | def from_filename(self, filename): |
|
177 | def from_filename(self, filename): | |
181 | """Read and export a notebook from a filename |
|
178 | """Read and export a notebook from a filename | |
182 |
|
179 | |||
183 | filename: Filename of the notebook file to export. |
|
180 | filename: Filename of the notebook file to export. | |
184 |
|
181 | |||
185 | Returns both the converted ipynb file and a dict containing the |
|
182 | Returns both the converted ipynb file and a dict containing the | |
186 | resources created along the way via the transformers and Jinja2 |
|
183 | resources created along the way via the transformers and Jinja2 | |
187 | processing. |
|
184 | processing. | |
188 | """ |
|
185 | """ | |
189 | with io.open(filename) as f: |
|
186 | with io.open(filename) as f: | |
190 | return self.from_notebook_node(nbformat.read(f, 'json')) |
|
187 | return self.from_notebook_node(nbformat.read(f, 'json')) | |
191 |
|
188 | |||
192 |
|
189 | |||
193 | def from_file(self, file_stream): |
|
190 | def from_file(self, file_stream): | |
194 | """Read and export a notebook from a file stream |
|
191 | """Read and export a notebook from a file stream | |
195 |
|
192 | |||
196 | file_stream: File handle of file that contains notebook data. |
|
193 | file_stream: File handle of file that contains notebook data. | |
197 |
|
194 | |||
198 | Returns both the converted ipynb file and a dict containing the |
|
195 | Returns both the converted ipynb file and a dict containing the | |
199 | resources created along the way via the transformers and Jinja2 |
|
196 | resources created along the way via the transformers and Jinja2 | |
200 | processing. |
|
197 | processing. | |
201 | """ |
|
198 | """ | |
202 |
|
199 | |||
203 | return self.from_notebook_node(nbformat.read(file_stream, 'json')) |
|
200 | return self.from_notebook_node(nbformat.read(file_stream, 'json')) | |
204 |
|
201 | |||
205 |
|
202 | |||
|
203 | def register_transformer(self, transformer): | |||
|
204 | if MetaHasTraits(transformer): | |||
|
205 | self.preprocessors.append(transformer(config=self.config)) | |||
|
206 | else: | |||
|
207 | self.preprocessors.append(transformer) | |||
|
208 | ||||
|
209 | ||||
206 | def register_filter(self, name, filter): |
|
210 | def register_filter(self, name, filter): | |
207 | if MetaHasTraits(filter): |
|
211 | if MetaHasTraits(filter): | |
208 | self.env.filters[name] = filter(config=self.config) |
|
212 | self.environment.filters[name] = filter(config=self.config) | |
209 | else: |
|
213 | else: | |
210 | self.env.filters[name] = filter |
|
214 | self.environment.filters[name] = filter | |
211 |
|
215 | |||
212 |
|
216 | |||
|
217 | def _register_transformers(self): | |||
|
218 | self.register_transformer(transformers.coalescestreams.coalesce_streams) | |||
|
219 | self.register_transformer(transformers.extractfigure.ExtractFigureTransformer) | |||
|
220 | self.register_transformer(transformers.revealhelp.RevealHelpTransformer) | |||
|
221 | self.register_transformer(transformers.csshtmlheader.CSSHtmlHeaderTransformer) | |||
|
222 | ||||
|
223 | ||||
213 | def _register_filters(self): |
|
224 | def _register_filters(self): | |
214 | self.register_filter('indent', indent) |
|
225 | self.register_filter('indent', indent) | |
215 | self.register_filter('markdown', markdown) |
|
226 | self.register_filter('markdown', markdown) | |
216 | self.register_filter('ansi2html', filters.ansi.ansi2html) |
|
227 | self.register_filter('ansi2html', filters.ansi.ansi2html) | |
217 | self.register_filter('filter_data_type', filters.datatypefilter.DataTypeFilter) |
|
228 | self.register_filter('filter_data_type', filters.datatypefilter.DataTypeFilter) | |
218 | self.register_filter('get_lines', filters.strings.get_lines) |
|
229 | self.register_filter('get_lines', filters.strings.get_lines) | |
219 | self.register_filter('highlight', filters.pygments.highlight) |
|
230 | self.register_filter('highlight', filters.pygments.highlight) | |
220 | self.register_filter('highlight2html', filters.pygments.highlight) |
|
231 | self.register_filter('highlight2html', filters.pygments.highlight) | |
221 | self.register_filter('highlight2latex', filters.pygments.highlight2latex) |
|
232 | self.register_filter('highlight2latex', filters.pygments.highlight2latex) | |
222 | self.register_filter('markdown2latex', filters.markdown.markdown2latex) |
|
233 | self.register_filter('markdown2latex', filters.markdown.markdown2latex) | |
223 | self.register_filter('markdown2rst', filters.markdown.markdown2rst) |
|
234 | self.register_filter('markdown2rst', filters.markdown.markdown2rst) | |
224 | self.register_filter('pycomment', filters.strings.python_comment) |
|
235 | self.register_filter('pycomment', filters.strings.python_comment) | |
225 | self.register_filter('rm_ansi', filters.ansi.remove_ansi) |
|
236 | self.register_filter('rm_ansi', filters.ansi.remove_ansi) | |
226 | self.register_filter('rm_dollars', filters.strings.strip_dollars) |
|
237 | self.register_filter('rm_dollars', filters.strings.strip_dollars) | |
227 | self.register_filter('rm_fake', filters.strings.rm_fake) |
|
238 | self.register_filter('rm_fake', filters.strings.rm_fake) | |
228 | self.register_filter('rm_math_space', filters.latex.rm_math_space) |
|
239 | self.register_filter('rm_math_space', filters.latex.rm_math_space) | |
229 | self.register_filter('wrap', filters.strings.wrap) |
|
240 | self.register_filter('wrap', filters.strings.wrap) | |
230 |
|
241 | |||
231 |
|
242 | |||
232 | def _init_environment(self): |
|
243 | def _init_environment(self): | |
233 | self.env = Environment( |
|
244 | self.environment = Environment( | |
234 | loader=FileSystemLoader([ |
|
245 | loader=FileSystemLoader([ | |
235 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_PATH, |
|
246 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_PATH, | |
236 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_SKELETON_PATH, |
|
247 | os.path.dirname(os.path.realpath(__file__)) + TEMPLATE_SKELETON_PATH, | |
237 | ]), |
|
248 | ]), | |
238 | extensions=JINJA_EXTENSIONS |
|
249 | extensions=JINJA_EXTENSIONS | |
239 | ) |
|
250 | ) | |
240 |
|
251 | |||
241 |
|
252 | |||
242 | def _preprocess(self, nb): |
|
253 | def _preprocess(self, nb): | |
243 | """ Preprocess the notebook using the transformers specific |
|
254 | """ Preprocess the notebook using the transformers specific | |
244 | for the current export format. |
|
255 | for the current export format. | |
245 |
|
256 | |||
246 | nb: Notebook to preprocess |
|
257 | nb: Notebook to preprocess | |
247 | """ |
|
258 | """ | |
248 |
|
259 | |||
249 | #Dict of 'resources' that can be filled by the preprocessors. |
|
260 | #Dict of 'resources' that can be filled by the preprocessors. | |
250 | resources = {} |
|
261 | resources = {} | |
251 |
|
262 | |||
252 | #Run each transformer on the notebook. Carry the output along |
|
263 | #Run each transformer on the notebook. Carry the output along | |
253 | #to each transformer |
|
264 | #to each transformer | |
254 | for transformer in self.preprocessors: |
|
265 | for transformer in self.preprocessors: | |
255 | nb, resources = transformer(nb, resources) |
|
266 | nb, resources = transformer(nb, resources) | |
256 | return nb, resources |
|
267 | return nb, resources | |
257 |
|
268 |
@@ -1,140 +1,127 b'' | |||||
1 | """Latex exporter for the notebook conversion pipeline. |
|
1 | """Latex exporter for the notebook conversion pipeline. | |
2 |
|
2 | |||
3 | This module defines Exporter, a highly configurable converter |
|
3 | This module defines Exporter, a highly configurable converter | |
4 | that uses Jinja2 to export notebook files into different format. |
|
4 | that uses Jinja2 to export notebook files into different format. | |
5 |
|
5 | |||
6 | You can register both pre-transformers that will act on the notebook format |
|
6 | You can register both pre-transformers that will act on the notebook format | |
7 | before conversion and jinja filter that would then be available in the templates |
|
7 | before conversion and jinja filter that would then be available in the templates | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (c) 2013, the IPython Development Team. |
|
11 | # Copyright (c) 2013, the IPython Development Team. | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the Modified BSD License. |
|
13 | # Distributed under the terms of the Modified BSD License. | |
14 | # |
|
14 | # | |
15 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | # The full license is in the file COPYING.txt, distributed with this software. | |
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 |
|
17 | |||
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 | # Imports |
|
19 | # Imports | |
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 |
|
21 | |||
22 | # Stdlib imports |
|
22 | # Stdlib imports | |
23 | import io |
|
23 | import io | |
24 | import os |
|
24 | import os | |
25 |
|
25 | |||
26 | # IPython imports |
|
26 | # IPython imports | |
27 | from IPython.config.configurable import Configurable |
|
27 | from IPython.config.configurable import Configurable | |
28 | from IPython.nbformat import current as nbformat |
|
28 | from IPython.nbformat import current as nbformat | |
29 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool |
|
29 | from IPython.utils.traitlets import MetaHasTraits, Unicode, List, Bool | |
30 | from IPython.utils.text import indent |
|
30 | from IPython.utils.text import indent | |
31 |
|
31 | |||
32 | # other libs/dependencies |
|
32 | # other libs/dependencies | |
33 | from jinja2 import Environment, FileSystemLoader |
|
33 | from jinja2 import Environment, FileSystemLoader | |
34 | from markdown import markdown |
|
34 | from markdown import markdown | |
35 |
|
35 | |||
36 | import base.Exporter as Exporter |
|
36 | # local import | |
|
37 | import exporter | |||
37 | import filters.latex |
|
38 | import filters.latex | |
38 | import filters.pygments |
|
39 | import filters.pygments | |
39 |
|
40 | from transformers.latex import LatexTransformer | ||
40 | #Try to import the Sphinx exporter. If the user doesn't have Sphinx isntalled |
|
|||
41 | #on his/her machine, fail silently. |
|
|||
42 | try: |
|
|||
43 | from .sphinx_transformer import (SphinxTransformer) #TODO |
|
|||
44 | except ImportError: |
|
|||
45 | SphinxTransformer = None |
|
|||
46 |
|
||||
47 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
48 | # Globals and constants |
|
42 | # Globals and constants | |
49 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
50 |
|
44 | |||
51 | #Latex Jinja2 constants |
|
45 | #Latex Jinja2 constants | |
52 | LATEX_TEMPLATE_PATH = "/../templates/tex/" |
|
46 | LATEX_TEMPLATE_PATH = "/../templates/tex/" | |
53 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" |
|
47 | LATEX_TEMPLATE_SKELETON_PATH = "/../templates/tex/skeleton/" | |
54 | LATEX_TEMPLATE_EXTENSION = ".tplx" |
|
48 | LATEX_TEMPLATE_EXTENSION = ".tplx" | |
55 |
|
49 | |||
56 | #Special Jinja2 syntax that will not conflict when exporting latex. |
|
50 | #Special Jinja2 syntax that will not conflict when exporting latex. | |
57 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] |
|
51 | LATEX_JINJA_COMMENT_BLOCK = ["((=", "=))"] | |
58 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] |
|
52 | LATEX_JINJA_VARIABLE_BLOCK = ["(((", ")))"] | |
59 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] |
|
53 | LATEX_JINJA_LOGIC_BLOCK = ["((*", "*))"] | |
60 |
|
54 | |||
61 | #----------------------------------------------------------------------------- |
|
55 | #----------------------------------------------------------------------------- | |
62 | # Classes and functions |
|
56 | # Classes and functions | |
63 | #----------------------------------------------------------------------------- |
|
57 | #----------------------------------------------------------------------------- | |
64 | class LatexExporter(Exporter): |
|
58 | class LatexExporter(exporter.Exporter): | |
65 |
""" A Jinja2 |
|
59 | """ A Jinja2 latex exporter | |
66 |
|
60 | |||
67 | Preprocess the ipynb files, feed it throug jinja templates, |
|
61 | Preprocess the ipynb files, feed it through jinja templates, | |
68 | and spit an converted files and a data object with other data |
|
62 | and spit an converted files and a data object with other data | |
69 | should be mostly configurable |
|
63 | should be mostly configurable | |
70 | """ |
|
64 | """ | |
71 |
|
65 | |||
72 | #Processors that process the input data prior to the export, set in the |
|
66 | def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw): | |
73 | #constructor for this class. |
|
|||
74 | preprocessors = [] |
|
|||
75 |
|
||||
76 | def __init__(self, preprocessors={}, jinja_filters={}, config=None, export_format, **kw): |
|
|||
77 | """ Init a new converter. |
|
67 | """ Init a new converter. | |
78 |
|
68 | |||
79 | config: the Configurable config object to pass around. |
|
69 | config: the Configurable config object to pass around. | |
80 |
|
70 | |||
81 | preprocessors: dict of **available** key/value function to run on |
|
71 | preprocessors: dict of **available** key/value function to run on | |
82 | ipynb json data before conversion to extract/inline file. |
|
72 | ipynb json data before conversion to extract/inline file. | |
83 | See `transformer.py` and `ConfigurableTransformers` |
|
73 | See `transformer.py` and `ConfigurableTransformers` | |
84 |
|
74 | |||
85 | set the order in which the transformers should apply |
|
75 | set the order in which the transformers should apply | |
86 | with the `pre_transformer_order` trait of this class |
|
76 | with the `pre_transformer_order` trait of this class | |
87 |
|
77 | |||
88 | transformers registerd by this key will take precedence on |
|
78 | transformers registerd by this key will take precedence on | |
89 | default one. |
|
79 | default one. | |
90 |
|
80 | |||
91 | jinja_filters: dict of supplementary jinja filter that should be made |
|
81 | jinja_filters: dict of supplementary jinja filter that should be made | |
92 | available in template. If those are of Configurable Class type, |
|
82 | available in template. If those are of Configurable Class type, | |
93 | they will be instanciated with the config object as argument. |
|
83 | they will be instanciated with the config object as argument. | |
94 |
|
84 | |||
95 | user defined filter will overwrite the one available by default. |
|
85 | user defined filter will overwrite the one available by default. | |
96 | """ |
|
86 | """ | |
97 |
|
87 | |||
98 | #Call the base class constructor |
|
88 | #Call the base class constructor | |
99 |
super(Exporter, self).__init__( |
|
89 | super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw) | |
100 |
|
||||
101 | #For compatibility, TODO: remove later. |
|
|||
102 | self.preprocessors.append(LatexTransformer(config=config)) |
|
|||
103 |
|
||||
104 | #Only load the sphinx transformer if the file reference worked |
|
|||
105 | #(Sphinx dependencies exist on the user's machine.) |
|
|||
106 | if SphinxTransformer: |
|
|||
107 | self.preprocessors.append(SphinxTransformer(config=config)) |
|
|||
108 |
|
||||
109 | #Add filters to the Jinja2 environment |
|
|||
110 | self.register_filter('escape_tex', filters.latex.escape_tex) |
|
|||
111 | self.register_filter('highlight', filters.pygments.highlight2latex) |
|
|||
112 |
|
||||
113 | #Load user filters. Overwrite existing filters if need be. |
|
|||
114 | for key, user_filter in jinja_filters.iteritems(): |
|
|||
115 | if isinstance(user_filter, MetaHasTraits): |
|
|||
116 | self.env.filters[key] = user_filter(config=config) |
|
|||
117 | else: |
|
|||
118 | self.env.filters[key] = user_filter |
|
|||
119 |
|
||||
120 | #Load the template file. |
|
|||
121 | self.template = self.env.get_template(self.template_file+self.ext) |
|
|||
122 |
|
90 | |||
123 |
|
91 | |||
124 | def _init_environment(self): |
|
92 | def _init_environment(self): | |
125 | self.ext = LATEX_TEMPLATE_EXTENSION |
|
93 | self.ext = LATEX_TEMPLATE_EXTENSION | |
126 | self.env = Environment( |
|
94 | self.environment = Environment( | |
127 | loader=FileSystemLoader([ |
|
95 | loader=FileSystemLoader([ | |
128 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, |
|
96 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_PATH, | |
129 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, |
|
97 | os.path.dirname(os.path.realpath(__file__)) + LATEX_TEMPLATE_SKELETON_PATH, | |
130 | ]), |
|
98 | ]), | |
131 | extensions=JINJA_EXTENSIONS |
|
99 | extensions=exporter.JINJA_EXTENSIONS | |
132 | ) |
|
100 | ) | |
133 |
|
101 | |||
134 | #Set special Jinja2 syntax that will not conflict with latex. |
|
102 | #Set special Jinja2 syntax that will not conflict with latex. | |
135 | self.env.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] |
|
103 | self.environment.block_start_string = LATEX_JINJA_LOGIC_BLOCK[0] | |
136 | self.env.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] |
|
104 | self.environment.block_end_string = LATEX_JINJA_LOGIC_BLOCK[1] | |
137 | self.env.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] |
|
105 | self.environment.variable_start_string = LATEX_JINJA_VARIABLE_BLOCK[0] | |
138 | self.env.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] |
|
106 | self.environment.variable_end_string = LATEX_JINJA_VARIABLE_BLOCK[1] | |
139 | self.env.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] |
|
107 | self.environment.comment_start_string = LATEX_JINJA_COMMENT_BLOCK[0] | |
140 | self.env.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] No newline at end of file |
|
108 | self.environment.comment_end_string = LATEX_JINJA_COMMENT_BLOCK[1] | |
|
109 | ||||
|
110 | ||||
|
111 | def _register_filters(self): | |||
|
112 | ||||
|
113 | #Register the filters of the base class. | |||
|
114 | super(exporter.Exporter, self)._register_filters() | |||
|
115 | ||||
|
116 | #Add latex filters to the Jinja2 environment | |||
|
117 | self.register_filter('escape_tex', filters.latex.escape_tex) | |||
|
118 | self.register_filter('highlight', filters.pygments.highlight2latex) | |||
|
119 | ||||
|
120 | def _register_transformers(self): | |||
|
121 | ||||
|
122 | #Register the transformers of the base class. | |||
|
123 | super(exporter.Exporter, self)._register_transformers() | |||
|
124 | ||||
|
125 | #Register latex transformer | |||
|
126 | self.register_transformer(LatexTransformer) | |||
|
127 | No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now