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