##// END OF EJS Templates
Finished a rough draft of the exporters.
Jonathan Frederic -
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
@@ -14,10 +14,8 b' they are converted.'
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Functions
15 # Functions
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17 def export_sphinx_report(nb, config=None):
18 def export_sphinx_report(nb, config=None):
18 pass
19 pass
19 def export_sphinx_report(nb, fileName, config=None):
20 pass
21
20
22 #TODO: Add basic export/import utility functions.
21 #TODO: Add basic export/import utility functions.
23 __author__ = 'root'
@@ -23,6 +23,7 b' from __future__ import print_function, absolute_import'
23 # Stdlib imports
23 # Stdlib imports
24 import io
24 import io
25 import os
25 import os
26 import copy
26
27
27 # IPython imports
28 # IPython imports
28 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
@@ -43,8 +44,6 b' import filters.pygments'
43 import filters.ansi
44 import filters.ansi
44
45
45 import transformers.extractfigure
46 import transformers.extractfigure
46 import transformers.csshtmlheader
47 import transformers.revealhelp
48 import transformers.coalescestreams
47 import transformers.coalescestreams
49
48
50
49
@@ -104,6 +103,8 b' class Exporter(Configurable):'
104 #constructor for this class.
103 #constructor for this class.
105 preprocessors = []
104 preprocessors = []
106
105
106 # Public Constructor #####################################################
107
107 def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw):
108 def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw):
108 """ Init a new converter.
109 """ Init a new converter.
109
110
@@ -156,10 +157,13 b' class Exporter(Configurable):'
156 else:
157 else:
157 self.environment.filters[key] = user_filter
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 def from_notebook_node(self, nb):
167 def from_notebook_node(self, nb):
164 """Export NotebookNode instance
168 """Export NotebookNode instance
165
169
@@ -171,6 +175,10 b' class Exporter(Configurable):'
171 """
175 """
172
176
173 nb, resources = self._preprocess(nb)
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 return self.template.render(nb=nb, resources=resources), resources
182 return self.template.render(nb=nb, resources=resources), resources
175
183
176
184
@@ -212,13 +220,17 b' class Exporter(Configurable):'
212 self.environment.filters[name] = filter(config=self.config)
220 self.environment.filters[name] = filter(config=self.config)
213 else:
221 else:
214 self.environment.filters[name] = filter
222 self.environment.filters[name] = filter
223 return self.environment.filters[name]
215
224
216
225 # Protected and Private methods #########################################
226
217 def _register_transformers(self):
227 def _register_transformers(self):
218 self.register_transformer(transformers.coalescestreams.coalesce_streams)
228 self.register_transformer(transformers.coalescestreams.coalesce_streams)
219 self.register_transformer(transformers.extractfigure.ExtractFigureTransformer)
229
220 self.register_transformer(transformers.revealhelp.RevealHelpTransformer)
230 #Remember the figure extraction transformer so it can be enabled and
221 self.register_transformer(transformers.csshtmlheader.CSSHtmlHeaderTransformer)
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 def _register_filters(self):
236 def _register_filters(self):
@@ -240,6 +252,11 b' class Exporter(Configurable):'
240 self.register_filter('wrap', filters.strings.wrap)
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 def _init_environment(self):
260 def _init_environment(self):
244 self.environment = Environment(
261 self.environment = Environment(
245 loader=FileSystemLoader([
262 loader=FileSystemLoader([
@@ -64,31 +64,18 b' class LatexExporter(exporter.Exporter):'
64 """
64 """
65
65
66 def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw):
66 def __init__(self, preprocessors=None, jinja_filters=None, config=None, **kw):
67 """ Init a new converter.
67
68
68 #Call base class constructor.
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
89 super(exporter.Exporter, self).__init__(preprocessors, jinja_filters, config, **kw)
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 def _init_environment(self):
79 def _init_environment(self):
93 self.ext = LATEX_TEMPLATE_EXTENSION
80 self.ext = LATEX_TEMPLATE_EXTENSION
94 self.environment = Environment(
81 self.environment = Environment(
General Comments 0
You need to be logged in to leave comments. Login now