##// END OF EJS Templates
Added ability support for already instanciate exporter
Jonathan Frederic -
Show More
@@ -1,224 +1,226
1 """
1 """
2 Module containing single call export functions.
2 Module containing single call export functions.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from functools import wraps
16 from functools import wraps
17
17
18 from IPython.nbformat.v3.nbbase import NotebookNode
18 from IPython.nbformat.v3.nbbase import NotebookNode
19 from IPython.config import Config
19 from IPython.config import Config
20
20
21 from .exporter import Exporter
21 from .exporter import Exporter
22 from .basichtml import BasicHTMLExporter
22 from .basichtml import BasicHTMLExporter
23 from .fullhtml import FullHTMLExporter
23 from .fullhtml import FullHTMLExporter
24 from .latex import LatexExporter
24 from .latex import LatexExporter
25 from .markdown import MarkdownExporter
25 from .markdown import MarkdownExporter
26 from .python import PythonExporter
26 from .python import PythonExporter
27 from .reveal import RevealExporter
27 from .reveal import RevealExporter
28 from .rst import RstExporter
28 from .rst import RstExporter
29 from .sphinx_howto import SphinxHowtoExporter
29 from .sphinx_howto import SphinxHowtoExporter
30 from .sphinx_manual import SphinxManualExporter
30 from .sphinx_manual import SphinxManualExporter
31
31
32 #-----------------------------------------------------------------------------
32 #-----------------------------------------------------------------------------
33 # Classes
33 # Classes
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 def DocDecorator(f):
36 def DocDecorator(f):
37
37
38 #Set docstring of function
38 #Set docstring of function
39 f.__doc__ = f.__doc__ + """
39 f.__doc__ = f.__doc__ + """
40 nb : Notebook node
40 nb : Notebook node
41 config : config (optional, keyword arg)
41 config : config (optional, keyword arg)
42 User configuration instance.
42 User configuration instance.
43 resources : dict (optional, keyword arg)
43 resources : dict (optional, keyword arg)
44 Resources used in the conversion process.
44 Resources used in the conversion process.
45
45
46 Returns
46 Returns
47 ----------
47 ----------
48 tuple- output, resources, exporter_instance
48 tuple- output, resources, exporter_instance
49 output : str
49 output : str
50 Jinja 2 output. This is the resulting converted notebook.
50 Jinja 2 output. This is the resulting converted notebook.
51 resources : dictionary
51 resources : dictionary
52 Dictionary of resources used prior to and during the conversion
52 Dictionary of resources used prior to and during the conversion
53 process.
53 process.
54 exporter_instance : Exporter
54 exporter_instance : Exporter
55 Instance of the Exporter class used to export the document. Useful
55 Instance of the Exporter class used to export the document. Useful
56 to caller because it provides a 'file_extension' property which
56 to caller because it provides a 'file_extension' property which
57 specifies what extension the output should be saved as."""
57 specifies what extension the output should be saved as."""
58
58
59 @wraps(f)
59 @wraps(f)
60 def decorator(*args, **kwargs):
60 def decorator(*args, **kwargs):
61 return f(*args, **kwargs)
61 return f(*args, **kwargs)
62
62
63 return decorator
63 return decorator
64
64
65
65
66 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
67 # Functions
67 # Functions
68 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
69
69
70 __all__ = [
70 __all__ = [
71 'export',
71 'export',
72 'export_sphinx_manual',
72 'export_sphinx_manual',
73 'export_sphinx_howto',
73 'export_sphinx_howto',
74 'export_basic_html',
74 'export_basic_html',
75 'export_full_html',
75 'export_full_html',
76 'export_latex',
76 'export_latex',
77 'export_markdown',
77 'export_markdown',
78 'export_python',
78 'export_python',
79 'export_reveal',
79 'export_reveal',
80 'export_rst',
80 'export_rst',
81 'export_by_name',
81 'export_by_name',
82 'get_export_names',
82 'get_export_names',
83 'ExporterNameError'
83 'ExporterNameError'
84 ]
84 ]
85
85
86
86
87 class ExporterNameError(NameError):
87 class ExporterNameError(NameError):
88 pass
88 pass
89
89
90
90
91 @DocDecorator
91 @DocDecorator
92 def export(exporter_type, nb, **kw):
92 def export(exporter, nb, **kw):
93 """
93 """
94 Export a notebook object using specific exporter class.
94 Export a notebook object using specific exporter class.
95
95
96 exporter_type : Exporter class type
96 exporter : Exporter class type or instance
97 Class type of the exporter that should be used. This method
97 Class type or instance of the exporter that should be used. If the
98 will initialize it's own instance of the class. It is
98 method initializes it's own instance of the class, it is ASSUMED that
99 ASSUMED that the class type provided exposes a
99 the class type provided exposes a constructor (__init__) with the same
100 constructor (__init__) with the same signature as the
100 signature as the base Exporter class.
101 base Exporter class.}
102 """
101 """
103
102
104 #Check arguments
103 #Check arguments
105 if exporter_type is None:
104 if exporter is None:
106 raise TypeError("Exporter is None")
105 raise TypeError("Exporter is None")
107 elif not issubclass(exporter_type, Exporter):
106 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
108 raise TypeError("Exporter type does not inherit from Exporter (base)")
107 raise TypeError("exporter does not inherit from Exporter (base)")
109 if nb is None:
108 if nb is None:
110 raise TypeError("nb is None")
109 raise TypeError("nb is None")
111
110
112 #Create the exporter
111 #Create the exporter
113 resources = kw.pop('resources', None)
112 resources = kw.pop('resources', None)
114 exporter_instance = exporter_type(**kw)
113 if isinstance(exporter, Exporter):
114 exporter_instance = exporter
115 else:
116 exporter_instance = exporter(**kw)
115
117
116 #Try to convert the notebook using the appropriate conversion function.
118 #Try to convert the notebook using the appropriate conversion function.
117 if isinstance(nb, NotebookNode):
119 if isinstance(nb, NotebookNode):
118 output, resources = exporter_instance.from_notebook_node(nb, resources)
120 output, resources = exporter_instance.from_notebook_node(nb, resources)
119 elif isinstance(nb, basestring):
121 elif isinstance(nb, basestring):
120 output, resources = exporter_instance.from_filename(nb, resources)
122 output, resources = exporter_instance.from_filename(nb, resources)
121 else:
123 else:
122 output, resources = exporter_instance.from_file(nb, resources)
124 output, resources = exporter_instance.from_file(nb, resources)
123 return output, resources
125 return output, resources
124
126
125
127
126 @DocDecorator
128 @DocDecorator
127 def export_sphinx_manual(nb, **kw):
129 def export_sphinx_manual(nb, **kw):
128 """
130 """
129 Export a notebook object to Sphinx Manual LaTeX
131 Export a notebook object to Sphinx Manual LaTeX
130 """
132 """
131 return export(SphinxManualExporter, nb, **kw)
133 return export(SphinxManualExporter, nb, **kw)
132
134
133
135
134 @DocDecorator
136 @DocDecorator
135 def export_sphinx_howto(nb, **kw):
137 def export_sphinx_howto(nb, **kw):
136 """
138 """
137 Export a notebook object to Sphinx HowTo LaTeX
139 Export a notebook object to Sphinx HowTo LaTeX
138 """
140 """
139 return export(SphinxHowtoExporter, nb, **kw)
141 return export(SphinxHowtoExporter, nb, **kw)
140
142
141
143
142 @DocDecorator
144 @DocDecorator
143 def export_basic_html(nb, **kw):
145 def export_basic_html(nb, **kw):
144 """
146 """
145 Export a notebook object to Basic HTML
147 Export a notebook object to Basic HTML
146 """
148 """
147 return export(BasicHTMLExporter, nb, **kw)
149 return export(BasicHTMLExporter, nb, **kw)
148
150
149
151
150 @DocDecorator
152 @DocDecorator
151 def export_full_html(nb, **kw):
153 def export_full_html(nb, **kw):
152 """
154 """
153 Export a notebook object to Full HTML
155 Export a notebook object to Full HTML
154 """
156 """
155 return export(FullHTMLExporter, nb, **kw)
157 return export(FullHTMLExporter, nb, **kw)
156
158
157
159
158 @DocDecorator
160 @DocDecorator
159 def export_latex(nb, **kw):
161 def export_latex(nb, **kw):
160 """
162 """
161 Export a notebook object to LaTeX
163 Export a notebook object to LaTeX
162 """
164 """
163 return export(LatexExporter, nb, **kw)
165 return export(LatexExporter, nb, **kw)
164
166
165
167
166 @DocDecorator
168 @DocDecorator
167 def export_markdown(nb, **kw):
169 def export_markdown(nb, **kw):
168 """
170 """
169 Export a notebook object to Markdown
171 Export a notebook object to Markdown
170 """
172 """
171 return export(MarkdownExporter, nb, **kw)
173 return export(MarkdownExporter, nb, **kw)
172
174
173
175
174 @DocDecorator
176 @DocDecorator
175 def export_python(nb, **kw):
177 def export_python(nb, **kw):
176 """
178 """
177 Export a notebook object to Python
179 Export a notebook object to Python
178 """
180 """
179 return export(PythonExporter, nb, **kw)
181 return export(PythonExporter, nb, **kw)
180
182
181
183
182 @DocDecorator
184 @DocDecorator
183 def export_reveal(nb, **kw):
185 def export_reveal(nb, **kw):
184 """
186 """
185 Export a notebook object to Reveal
187 Export a notebook object to Reveal
186 """
188 """
187 return export(RevealExporter, nb, **kw)
189 return export(RevealExporter, nb, **kw)
188
190
189
191
190 @DocDecorator
192 @DocDecorator
191 def export_rst(nb, **kw):
193 def export_rst(nb, **kw):
192 """
194 """
193 Export a notebook object to RST
195 Export a notebook object to RST
194 """
196 """
195 return export(RstExporter, nb, **kw)
197 return export(RstExporter, nb, **kw)
196
198
197
199
198 @DocDecorator
200 @DocDecorator
199 def export_by_name(format_name, nb, **kw):
201 def export_by_name(format_name, nb, **kw):
200 """
202 """
201 Export a notebook object to a template type by its name. Reflection
203 Export a notebook object to a template type by its name. Reflection
202 (Inspect) is used to find the template's corresponding explicit export
204 (Inspect) is used to find the template's corresponding explicit export
203 method defined in this module. That method is then called directly.
205 method defined in this module. That method is then called directly.
204
206
205 format_name : str
207 format_name : str
206 Name of the template style to export to.
208 Name of the template style to export to.
207 """
209 """
208
210
209 function_name = "export_" + format_name.lower()
211 function_name = "export_" + format_name.lower()
210
212
211 if function_name in globals():
213 if function_name in globals():
212 return globals()[function_name](nb, **kw)
214 return globals()[function_name](nb, **kw)
213 else:
215 else:
214 raise ExporterNameError("template for `%s` not found" % function_name)
216 raise ExporterNameError("template for `%s` not found" % function_name)
215
217
216
218
217 def get_export_names():
219 def get_export_names():
218 "Return a list of the currently supported export targets"
220 "Return a list of the currently supported export targets"
219 # grab everything after 'export_'
221 # grab everything after 'export_'
220 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
222 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
221
223
222 # filter out the one method that is not a template
224 # filter out the one method that is not a template
223 l = [x for x in l if 'by_name' not in x]
225 l = [x for x in l if 'by_name' not in x]
224 return sorted(l)
226 return sorted(l)
General Comments 0
You need to be logged in to leave comments. Login now