##// END OF EJS Templates
maintain instances of exporters for use in export_by_name...
MinRK -
Show More
@@ -1,211 +1,214 b''
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 .html import HTMLExporter
22 from .html import HTMLExporter
23 from .slides import SlidesExporter
23 from .slides import SlidesExporter
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 .rst import RSTExporter
27 from .rst import RSTExporter
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Classes
30 # Classes
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 def DocDecorator(f):
33 def DocDecorator(f):
34
34
35 #Set docstring of function
35 #Set docstring of function
36 f.__doc__ = f.__doc__ + """
36 f.__doc__ = f.__doc__ + """
37 nb : Notebook node
37 nb : Notebook node
38 config : config (optional, keyword arg)
38 config : config (optional, keyword arg)
39 User configuration instance.
39 User configuration instance.
40 resources : dict (optional, keyword arg)
40 resources : dict (optional, keyword arg)
41 Resources used in the conversion process.
41 Resources used in the conversion process.
42
42
43 Returns
43 Returns
44 ----------
44 ----------
45 tuple- output, resources, exporter_instance
45 tuple- output, resources, exporter_instance
46 output : str
46 output : str
47 Jinja 2 output. This is the resulting converted notebook.
47 Jinja 2 output. This is the resulting converted notebook.
48 resources : dictionary
48 resources : dictionary
49 Dictionary of resources used prior to and during the conversion
49 Dictionary of resources used prior to and during the conversion
50 process.
50 process.
51 exporter_instance : Exporter
51 exporter_instance : Exporter
52 Instance of the Exporter class used to export the document. Useful
52 Instance of the Exporter class used to export the document. Useful
53 to caller because it provides a 'file_extension' property which
53 to caller because it provides a 'file_extension' property which
54 specifies what extension the output should be saved as.
54 specifies what extension the output should be saved as.
55
55
56 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT
56 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT
57 """
57 """
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_html',
72 'export_html',
73 'export_custom',
73 'export_custom',
74 'export_slides',
74 'export_slides',
75 'export_latex',
75 'export_latex',
76 'export_markdown',
76 'export_markdown',
77 'export_python',
77 'export_python',
78 'export_rst',
78 'export_rst',
79 'export_by_name',
79 'export_by_name',
80 'get_export_names',
80 'get_export_names',
81 'ExporterNameError'
81 'ExporterNameError'
82 ]
82 ]
83
83
84
84
85 class ExporterNameError(NameError):
85 class ExporterNameError(NameError):
86 pass
86 pass
87
87
88 _exporters = {}
88
89
89 @DocDecorator
90 @DocDecorator
90 def export(exporter, nb, **kw):
91 def export(exporter, nb, **kw):
91 """
92 """
92 Export a notebook object using specific exporter class.
93 Export a notebook object using specific exporter class.
93
94
94 exporter : Exporter class type or instance
95 exporter : Exporter class type or instance
95 Class type or instance of the exporter that should be used. If the
96 Class type or instance of the exporter that should be used. If the
96 method initializes it's own instance of the class, it is ASSUMED that
97 method initializes it's own instance of the class, it is ASSUMED that
97 the class type provided exposes a constructor (__init__) with the same
98 the class type provided exposes a constructor (__init__) with the same
98 signature as the base Exporter class.
99 signature as the base Exporter class.
99 """
100 """
100
101
101 #Check arguments
102 #Check arguments
102 if exporter is None:
103 if exporter is None:
103 raise TypeError("Exporter is None")
104 raise TypeError("Exporter is None")
104 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
105 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
105 raise TypeError("exporter does not inherit from Exporter (base)")
106 raise TypeError("exporter does not inherit from Exporter (base)")
106 if nb is None:
107 if nb is None:
107 raise TypeError("nb is None")
108 raise TypeError("nb is None")
108
109
109 #Create the exporter
110 #Create the exporter
110 resources = kw.pop('resources', None)
111 resources = kw.pop('resources', None)
111 if isinstance(exporter, Exporter):
112 if isinstance(exporter, Exporter):
112 exporter_instance = exporter
113 exporter_instance = exporter
113 else:
114 else:
114 exporter_instance = exporter(**kw)
115 if exporter not in _exporters:
116 _exporters[exporter] = exporter(**kw)
117 exporter_instance = _exporters[exporter]
115
118
116 #Try to convert the notebook using the appropriate conversion function.
119 #Try to convert the notebook using the appropriate conversion function.
117 if isinstance(nb, NotebookNode):
120 if isinstance(nb, NotebookNode):
118 output, resources = exporter_instance.from_notebook_node(nb, resources)
121 output, resources = exporter_instance.from_notebook_node(nb, resources)
119 elif isinstance(nb, basestring):
122 elif isinstance(nb, basestring):
120 output, resources = exporter_instance.from_filename(nb, resources)
123 output, resources = exporter_instance.from_filename(nb, resources)
121 else:
124 else:
122 output, resources = exporter_instance.from_file(nb, resources)
125 output, resources = exporter_instance.from_file(nb, resources)
123 return output, resources
126 return output, resources
124
127
125
128
126 @DocDecorator
129 @DocDecorator
127 def export_custom(nb, **kw):
130 def export_custom(nb, **kw):
128 """
131 """
129 Export a notebook object to a custom format
132 Export a notebook object to a custom format
130 """
133 """
131 return export(Exporter, nb, **kw)
134 return export(Exporter, nb, **kw)
132
135
133
136
134 @DocDecorator
137 @DocDecorator
135 def export_html(nb, **kw):
138 def export_html(nb, **kw):
136 """
139 """
137 Export a notebook object to HTML
140 Export a notebook object to HTML
138 """
141 """
139 return export(HTMLExporter, nb, **kw)
142 return export(HTMLExporter, nb, **kw)
140
143
141
144
142 @DocDecorator
145 @DocDecorator
143 def export_slides(nb, **kw):
146 def export_slides(nb, **kw):
144 """
147 """
145 Export a notebook object to Slides
148 Export a notebook object to Slides
146 """
149 """
147 return export(SlidesExporter, nb, **kw)
150 return export(SlidesExporter, nb, **kw)
148
151
149
152
150 @DocDecorator
153 @DocDecorator
151 def export_latex(nb, **kw):
154 def export_latex(nb, **kw):
152 """
155 """
153 Export a notebook object to LaTeX
156 Export a notebook object to LaTeX
154 """
157 """
155 return export(LatexExporter, nb, **kw)
158 return export(LatexExporter, nb, **kw)
156
159
157
160
158 @DocDecorator
161 @DocDecorator
159 def export_markdown(nb, **kw):
162 def export_markdown(nb, **kw):
160 """
163 """
161 Export a notebook object to Markdown
164 Export a notebook object to Markdown
162 """
165 """
163 return export(MarkdownExporter, nb, **kw)
166 return export(MarkdownExporter, nb, **kw)
164
167
165
168
166 @DocDecorator
169 @DocDecorator
167 def export_python(nb, **kw):
170 def export_python(nb, **kw):
168 """
171 """
169 Export a notebook object to Python
172 Export a notebook object to Python
170 """
173 """
171 return export(PythonExporter, nb, **kw)
174 return export(PythonExporter, nb, **kw)
172
175
173
176
174 @DocDecorator
177 @DocDecorator
175 def export_rst(nb, **kw):
178 def export_rst(nb, **kw):
176 """
179 """
177 Export a notebook object to reStructuredText
180 Export a notebook object to reStructuredText
178 """
181 """
179 return export(RSTExporter, nb, **kw)
182 return export(RSTExporter, nb, **kw)
180
183
181
184
182 @DocDecorator
185 @DocDecorator
183 def export_by_name(format_name, nb, **kw):
186 def export_by_name(format_name, nb, **kw):
184 """
187 """
185 Export a notebook object to a template type by its name. Reflection
188 Export a notebook object to a template type by its name. Reflection
186 (Inspect) is used to find the template's corresponding explicit export
189 (Inspect) is used to find the template's corresponding explicit export
187 method defined in this module. That method is then called directly.
190 method defined in this module. That method is then called directly.
188
191
189 format_name : str
192 format_name : str
190 Name of the template style to export to.
193 Name of the template style to export to.
191 """
194 """
192
195
193 function_name = "export_" + format_name.lower()
196 function_name = "export_" + format_name.lower()
194
197
195 if function_name in globals():
198 if function_name in globals():
196 return globals()[function_name](nb, **kw)
199 return globals()[function_name](nb, **kw)
197 else:
200 else:
198 raise ExporterNameError("template for `%s` not found" % function_name)
201 raise ExporterNameError("template for `%s` not found" % function_name)
199
202
200
203
201 def get_export_names():
204 def get_export_names():
202 """Return a list of the currently supported export targets
205 """Return a list of the currently supported export targets
203
206
204 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT"""
207 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT"""
205
208
206 # grab everything after 'export_'
209 # grab everything after 'export_'
207 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
210 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
208
211
209 # filter out the one method that is not a template
212 # filter out the one method that is not a template
210 l = [x for x in l if 'by_name' not in x]
213 l = [x for x in l if 'by_name' not in x]
211 return sorted(l)
214 return sorted(l)
General Comments 0
You need to be logged in to leave comments. Login now