##// END OF EJS Templates
generate `export_foo` methods...
MinRK -
Show More
@@ -1,217 +1,176 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 _exporters = {}
89
89
90 @DocDecorator
90 @DocDecorator
91 def export(exporter, nb, **kw):
91 def export(exporter, nb, **kw):
92 """
92 """
93 Export a notebook object using specific exporter class.
93 Export a notebook object using specific exporter class.
94
94
95 exporter : Exporter class type or instance
95 exporter : Exporter class type or instance
96 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
97 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
98 the class type provided exposes a constructor (__init__) with the same
98 the class type provided exposes a constructor (__init__) with the same
99 signature as the base Exporter class.
99 signature as the base Exporter class.
100 """
100 """
101
101
102 #Check arguments
102 #Check arguments
103 if exporter is None:
103 if exporter is None:
104 raise TypeError("Exporter is None")
104 raise TypeError("Exporter is None")
105 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
105 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
106 raise TypeError("exporter does not inherit from Exporter (base)")
106 raise TypeError("exporter does not inherit from Exporter (base)")
107 if nb is None:
107 if nb is None:
108 raise TypeError("nb is None")
108 raise TypeError("nb is None")
109
109
110 #Create the exporter
110 #Create the exporter
111 resources = kw.pop('resources', None)
111 resources = kw.pop('resources', None)
112 if isinstance(exporter, Exporter):
112 if isinstance(exporter, Exporter):
113 exporter_instance = exporter
113 exporter_instance = exporter
114 else:
114 else:
115 if exporter not in _exporters:
115 if exporter not in _exporters:
116 exporter_instance = _exporters[exporter] = exporter(**kw)
116 exporter_instance = _exporters[exporter] = exporter(**kw)
117 else:
117 else:
118 exporter_instance = _exporters[exporter]
118 exporter_instance = _exporters[exporter]
119 for attr, value in kw.items():
119 for attr, value in kw.items():
120 setattr(exporter_instance, attr, value)
120 setattr(exporter_instance, attr, value)
121
121
122 #Try to convert the notebook using the appropriate conversion function.
122 #Try to convert the notebook using the appropriate conversion function.
123 if isinstance(nb, NotebookNode):
123 if isinstance(nb, NotebookNode):
124 output, resources = exporter_instance.from_notebook_node(nb, resources)
124 output, resources = exporter_instance.from_notebook_node(nb, resources)
125 elif isinstance(nb, basestring):
125 elif isinstance(nb, basestring):
126 output, resources = exporter_instance.from_filename(nb, resources)
126 output, resources = exporter_instance.from_filename(nb, resources)
127 else:
127 else:
128 output, resources = exporter_instance.from_file(nb, resources)
128 output, resources = exporter_instance.from_file(nb, resources)
129 return output, resources
129 return output, resources
130
130
131 exporter_map = dict(
132 custom=Exporter,
133 html=HTMLExporter,
134 slides=SlidesExporter,
135 latex=LatexExporter,
136 markdown=MarkdownExporter,
137 python=PythonExporter,
138 rst=RSTExporter,
139 )
140
141 def _make_exporter(name, E):
142 """make an export_foo function from a short key and Exporter class E"""
143 def _export(nb, **kw):
144 return export(E, nb, **kw)
145 _export.__doc__ = """Export a notebook object to {0} format""".format(name)
146 return _export
147
148 g = globals()
131
149
132 @DocDecorator
150 for name, E in exporter_map.items():
133 def export_custom(nb, **kw):
151 g['export_%s' % name] = DocDecorator(_make_exporter(name, E))
134 """
135 Export a notebook object to a custom format
136 """
137 return export(Exporter, nb, **kw)
138
139
140 @DocDecorator
141 def export_html(nb, **kw):
142 """
143 Export a notebook object to HTML
144 """
145 return export(HTMLExporter, nb, **kw)
146
147
148 @DocDecorator
149 def export_slides(nb, **kw):
150 """
151 Export a notebook object to Slides
152 """
153 return export(SlidesExporter, nb, **kw)
154
155
156 @DocDecorator
157 def export_latex(nb, **kw):
158 """
159 Export a notebook object to LaTeX
160 """
161 return export(LatexExporter, nb, **kw)
162
163
164 @DocDecorator
165 def export_markdown(nb, **kw):
166 """
167 Export a notebook object to Markdown
168 """
169 return export(MarkdownExporter, nb, **kw)
170
171
172 @DocDecorator
173 def export_python(nb, **kw):
174 """
175 Export a notebook object to Python
176 """
177 return export(PythonExporter, nb, **kw)
178
179
180 @DocDecorator
181 def export_rst(nb, **kw):
182 """
183 Export a notebook object to reStructuredText
184 """
185 return export(RSTExporter, nb, **kw)
186
187
152
188 @DocDecorator
153 @DocDecorator
189 def export_by_name(format_name, nb, **kw):
154 def export_by_name(format_name, nb, **kw):
190 """
155 """
191 Export a notebook object to a template type by its name. Reflection
156 Export a notebook object to a template type by its name. Reflection
192 (Inspect) is used to find the template's corresponding explicit export
157 (Inspect) is used to find the template's corresponding explicit export
193 method defined in this module. That method is then called directly.
158 method defined in this module. That method is then called directly.
194
159
195 format_name : str
160 format_name : str
196 Name of the template style to export to.
161 Name of the template style to export to.
197 """
162 """
198
163
199 function_name = "export_" + format_name.lower()
164 function_name = "export_" + format_name.lower()
200
165
201 if function_name in globals():
166 if function_name in globals():
202 return globals()[function_name](nb, **kw)
167 return globals()[function_name](nb, **kw)
203 else:
168 else:
204 raise ExporterNameError("template for `%s` not found" % function_name)
169 raise ExporterNameError("template for `%s` not found" % function_name)
205
170
206
171
207 def get_export_names():
172 def get_export_names():
208 """Return a list of the currently supported export targets
173 """Return a list of the currently supported export targets
209
174
210 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT"""
175 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT"""
211
176 return sorted(exporter_map.keys())
212 # grab everything after 'export_'
213 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
214
215 # filter out the one method that is not a template
216 l = [x for x in l if 'by_name' not in x]
217 return sorted(l)
General Comments 0
You need to be logged in to leave comments. Login now