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