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