Show More
@@ -1,217 +1,176 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Module containing single call export functions. |
|
3 | 3 | """ |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (c) 2013, the IPython Development Team. |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the Modified BSD License. |
|
8 | 8 | # |
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | # Imports |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | |
|
16 | 16 | from functools import wraps |
|
17 | 17 | |
|
18 | 18 | from IPython.nbformat.v3.nbbase import NotebookNode |
|
19 | 19 | from IPython.config import Config |
|
20 | 20 | |
|
21 | 21 | from .exporter import Exporter |
|
22 | 22 | from .html import HTMLExporter |
|
23 | 23 | from .slides import SlidesExporter |
|
24 | 24 | from .latex import LatexExporter |
|
25 | 25 | from .markdown import MarkdownExporter |
|
26 | 26 | from .python import PythonExporter |
|
27 | 27 | from .rst import RSTExporter |
|
28 | 28 | |
|
29 | 29 | #----------------------------------------------------------------------------- |
|
30 | 30 | # Classes |
|
31 | 31 | #----------------------------------------------------------------------------- |
|
32 | 32 | |
|
33 | 33 | def DocDecorator(f): |
|
34 | 34 | |
|
35 | 35 | #Set docstring of function |
|
36 | 36 | f.__doc__ = f.__doc__ + """ |
|
37 | 37 | nb : Notebook node |
|
38 | 38 | config : config (optional, keyword arg) |
|
39 | 39 | User configuration instance. |
|
40 | 40 | resources : dict (optional, keyword arg) |
|
41 | 41 | Resources used in the conversion process. |
|
42 | 42 | |
|
43 | 43 | Returns |
|
44 | 44 | ---------- |
|
45 | 45 | tuple- output, resources, exporter_instance |
|
46 | 46 | output : str |
|
47 | 47 | Jinja 2 output. This is the resulting converted notebook. |
|
48 | 48 | resources : dictionary |
|
49 | 49 | Dictionary of resources used prior to and during the conversion |
|
50 | 50 | process. |
|
51 | 51 | exporter_instance : Exporter |
|
52 | 52 | Instance of the Exporter class used to export the document. Useful |
|
53 | 53 | to caller because it provides a 'file_extension' property which |
|
54 | 54 | specifies what extension the output should be saved as. |
|
55 | 55 | |
|
56 | 56 | WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT |
|
57 | 57 | """ |
|
58 | 58 | |
|
59 | 59 | @wraps(f) |
|
60 | 60 | def decorator(*args, **kwargs): |
|
61 | 61 | return f(*args, **kwargs) |
|
62 | 62 | |
|
63 | 63 | return decorator |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | #----------------------------------------------------------------------------- |
|
67 | 67 | # Functions |
|
68 | 68 | #----------------------------------------------------------------------------- |
|
69 | 69 | |
|
70 | 70 | __all__ = [ |
|
71 | 71 | 'export', |
|
72 | 72 | 'export_html', |
|
73 | 73 | 'export_custom', |
|
74 | 74 | 'export_slides', |
|
75 | 75 | 'export_latex', |
|
76 | 76 | 'export_markdown', |
|
77 | 77 | 'export_python', |
|
78 | 78 | 'export_rst', |
|
79 | 79 | 'export_by_name', |
|
80 | 80 | 'get_export_names', |
|
81 | 81 | 'ExporterNameError' |
|
82 | 82 | ] |
|
83 | 83 | |
|
84 | 84 | |
|
85 | 85 | class ExporterNameError(NameError): |
|
86 | 86 | pass |
|
87 | 87 | |
|
88 | 88 | _exporters = {} |
|
89 | 89 | |
|
90 | 90 | @DocDecorator |
|
91 | 91 | def export(exporter, nb, **kw): |
|
92 | 92 | """ |
|
93 | 93 | Export a notebook object using specific exporter class. |
|
94 | 94 | |
|
95 | 95 | exporter : Exporter class type or instance |
|
96 | 96 | Class type or instance of the exporter that should be used. If the |
|
97 | 97 | method initializes it's own instance of the class, it is ASSUMED that |
|
98 | 98 | the class type provided exposes a constructor (__init__) with the same |
|
99 | 99 | signature as the base Exporter class. |
|
100 | 100 | """ |
|
101 | 101 | |
|
102 | 102 | #Check arguments |
|
103 | 103 | if exporter is None: |
|
104 | 104 | raise TypeError("Exporter is None") |
|
105 | 105 | elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter): |
|
106 | 106 | raise TypeError("exporter does not inherit from Exporter (base)") |
|
107 | 107 | if nb is None: |
|
108 | 108 | raise TypeError("nb is None") |
|
109 | 109 | |
|
110 | 110 | #Create the exporter |
|
111 | 111 | resources = kw.pop('resources', None) |
|
112 | 112 | if isinstance(exporter, Exporter): |
|
113 | 113 | exporter_instance = exporter |
|
114 | 114 | else: |
|
115 | 115 | if exporter not in _exporters: |
|
116 | 116 | exporter_instance = _exporters[exporter] = exporter(**kw) |
|
117 | 117 | else: |
|
118 | 118 | exporter_instance = _exporters[exporter] |
|
119 | 119 | for attr, value in kw.items(): |
|
120 | 120 | setattr(exporter_instance, attr, value) |
|
121 | 121 | |
|
122 | 122 | #Try to convert the notebook using the appropriate conversion function. |
|
123 | 123 | if isinstance(nb, NotebookNode): |
|
124 | 124 | output, resources = exporter_instance.from_notebook_node(nb, resources) |
|
125 | 125 | elif isinstance(nb, basestring): |
|
126 | 126 | output, resources = exporter_instance.from_filename(nb, resources) |
|
127 | 127 | else: |
|
128 | 128 | output, resources = exporter_instance.from_file(nb, resources) |
|
129 | 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 | |
|
133 | def export_custom(nb, **kw): | |
|
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 | ||
|
150 | for name, E in exporter_map.items(): | |
|
151 | g['export_%s' % name] = DocDecorator(_make_exporter(name, E)) | |
|
187 | 152 | |
|
188 | 153 | @DocDecorator |
|
189 | 154 | def export_by_name(format_name, nb, **kw): |
|
190 | 155 | """ |
|
191 | 156 | Export a notebook object to a template type by its name. Reflection |
|
192 | 157 | (Inspect) is used to find the template's corresponding explicit export |
|
193 | 158 | method defined in this module. That method is then called directly. |
|
194 | 159 | |
|
195 | 160 | format_name : str |
|
196 | 161 | Name of the template style to export to. |
|
197 | 162 | """ |
|
198 | 163 | |
|
199 | 164 | function_name = "export_" + format_name.lower() |
|
200 | 165 | |
|
201 | 166 | if function_name in globals(): |
|
202 | 167 | return globals()[function_name](nb, **kw) |
|
203 | 168 | else: |
|
204 | 169 | raise ExporterNameError("template for `%s` not found" % function_name) |
|
205 | 170 | |
|
206 | 171 | |
|
207 | 172 | def get_export_names(): |
|
208 | 173 | """Return a list of the currently supported export targets |
|
209 | 174 | |
|
210 | 175 | WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT""" |
|
211 | ||
|
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) | |
|
176 | return sorted(exporter_map.keys()) |
General Comments 0
You need to be logged in to leave comments.
Login now