Show More
@@ -1,220 +1,220 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 .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 | ] |
|
83 | ] | |
84 |
|
84 | |||
85 | class ExporterNameError(NameError): |
|
85 | class ExporterNameError(NameError): | |
86 | pass |
|
86 | pass | |
87 |
|
87 | |||
88 | @DocDecorator |
|
88 | @DocDecorator | |
89 | def export(exporter_type, nb, **kw): |
|
89 | def export(exporter_type, nb, **kw): | |
90 | """ |
|
90 | """ | |
91 | Export a notebook object using specific exporter class. |
|
91 | Export a notebook object using specific exporter class. | |
92 |
|
92 | |||
93 | exporter_type : Exporter class type |
|
93 | exporter_type : Exporter class type | |
94 | Class type of the exporter that should be used. This method |
|
94 | Class type of the exporter that should be used. This method | |
95 | will initialize it's own instance of the class. It is |
|
95 | will initialize it's own instance of the class. It is | |
96 | ASSUMED that the class type provided exposes a |
|
96 | ASSUMED that the class type provided exposes a | |
97 | constructor (__init__) with the same signature as the |
|
97 | constructor (__init__) with the same signature as the | |
98 | base Exporter class.} |
|
98 | base Exporter class.} | |
99 | """ |
|
99 | """ | |
100 |
|
100 | |||
101 | #Check arguments |
|
101 | #Check arguments | |
102 | if exporter_type is None: |
|
102 | if exporter_type is None: | |
103 | raise TypeError("Exporter is None") |
|
103 | raise TypeError("Exporter is None") | |
104 | elif not issubclass(exporter_type, Exporter): |
|
104 | elif not issubclass(exporter_type, Exporter): | |
105 | raise TypeError("Exporter type does not inherit from Exporter (base)") |
|
105 | raise TypeError("Exporter type does not inherit from Exporter (base)") | |
106 | if nb is None: |
|
106 | if nb is None: | |
107 | raise TypeError("nb is None") |
|
107 | raise TypeError("nb is None") | |
108 |
|
108 | |||
109 | #Create the exporter |
|
109 | #Create the exporter | |
110 | exporter_instance = exporter_type(config=kw.get('config', Config())) |
|
110 | exporter_instance = exporter_type(config=kw.get('config', Config())) | |
111 |
|
111 | |||
112 | #Try to convert the notebook using the appropriate conversion function. |
|
112 | #Try to convert the notebook using the appropriate conversion function. | |
113 | resources = kw.get('resources', {}) |
|
113 | resources = kw.get('resources', {}) | |
114 | if isinstance(nb, NotebookNode): |
|
114 | if isinstance(nb, NotebookNode): | |
115 | output, resources = exporter_instance.from_notebook_node(nb, resources) |
|
115 | output, resources = exporter_instance.from_notebook_node(nb, resources) | |
116 | elif isinstance(nb, basestring): |
|
116 | elif isinstance(nb, basestring): | |
117 | output, resources = exporter_instance.from_filename(nb, resources) |
|
117 | output, resources = exporter_instance.from_filename(nb, resources) | |
118 | else: |
|
118 | else: | |
119 | output, resources = exporter_instance.from_file(nb, resources) |
|
119 | output, resources = exporter_instance.from_file(nb, resources) | |
120 | return output, resources |
|
120 | return output, resources | |
121 |
|
121 | |||
122 |
|
122 | |||
123 | @DocDecorator |
|
123 | @DocDecorator | |
124 | def export_sphinx_manual(nb, **kw): |
|
124 | def export_sphinx_manual(nb, **kw): | |
125 | """ |
|
125 | """ | |
126 | Export a notebook object to Sphinx Manual LaTeX |
|
126 | Export a notebook object to Sphinx Manual LaTeX | |
127 | """ |
|
127 | """ | |
128 | return export(SphinxManualExporter, nb, **kw) |
|
128 | return export(SphinxManualExporter, nb, **kw) | |
129 |
|
129 | |||
130 |
|
130 | |||
131 | @DocDecorator |
|
131 | @DocDecorator | |
132 | def export_sphinx_howto(nb, **kw): |
|
132 | def export_sphinx_howto(nb, **kw): | |
133 | """ |
|
133 | """ | |
134 | Export a notebook object to Sphinx HowTo LaTeX |
|
134 | Export a notebook object to Sphinx HowTo LaTeX | |
135 | """ |
|
135 | """ | |
136 | return export(SphinxHowtoExporter, nb, **kw) |
|
136 | return export(SphinxHowtoExporter, nb, **kw) | |
137 |
|
137 | |||
138 |
|
138 | |||
139 | @DocDecorator |
|
139 | @DocDecorator | |
140 | def export_basic_html(nb, **kw): |
|
140 | def export_basic_html(nb, **kw): | |
141 | """ |
|
141 | """ | |
142 | Export a notebook object to Basic HTML |
|
142 | Export a notebook object to Basic HTML | |
143 | """ |
|
143 | """ | |
144 | return export(BasicHTMLExporter, nb, **kw) |
|
144 | return export(BasicHTMLExporter, nb, **kw) | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | @DocDecorator |
|
147 | @DocDecorator | |
148 | def export_full_html(nb, **kw): |
|
148 | def export_full_html(nb, **kw): | |
149 | """ |
|
149 | """ | |
150 | Export a notebook object to Full HTML |
|
150 | Export a notebook object to Full HTML | |
151 | """ |
|
151 | """ | |
152 | return export(FullHTMLExporter, nb, **kw) |
|
152 | return export(FullHTMLExporter, nb, **kw) | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | @DocDecorator |
|
155 | @DocDecorator | |
156 | def export_latex(nb, **kw): |
|
156 | def export_latex(nb, **kw): | |
157 | """ |
|
157 | """ | |
158 | Export a notebook object to LaTeX |
|
158 | Export a notebook object to LaTeX | |
159 | """ |
|
159 | """ | |
160 | return export(LatexExporter, nb, **kw) |
|
160 | return export(LatexExporter, nb, **kw) | |
161 |
|
161 | |||
162 |
|
162 | |||
163 | @DocDecorator |
|
163 | @DocDecorator | |
164 | def export_markdown(nb, **kw): |
|
164 | def export_markdown(nb, **kw): | |
165 | """ |
|
165 | """ | |
166 | Export a notebook object to Markdown |
|
166 | Export a notebook object to Markdown | |
167 | """ |
|
167 | """ | |
168 | return export(MarkdownExporter, nb, **kw) |
|
168 | return export(MarkdownExporter, nb, **kw) | |
169 |
|
169 | |||
170 |
|
170 | |||
171 | @DocDecorator |
|
171 | @DocDecorator | |
172 | def export_python(nb, **kw): |
|
172 | def export_python(nb, **kw): | |
173 | """ |
|
173 | """ | |
174 | Export a notebook object to Python |
|
174 | Export a notebook object to Python | |
175 | """ |
|
175 | """ | |
176 | return export(PythonExporter, nb, **kw) |
|
176 | return export(PythonExporter, nb, **kw) | |
177 |
|
177 | |||
178 |
|
178 | |||
179 | @DocDecorator |
|
179 | @DocDecorator | |
180 | def export_reveal(nb, **kw): |
|
180 | def export_reveal(nb, **kw): | |
181 | """ |
|
181 | """ | |
182 | Export a notebook object to Reveal |
|
182 | Export a notebook object to Reveal | |
183 | """ |
|
183 | """ | |
184 | return export(RevealExporter, nb, **kw) |
|
184 | return export(RevealExporter, nb, **kw) | |
185 |
|
185 | |||
186 |
|
186 | |||
187 | @DocDecorator |
|
187 | @DocDecorator | |
188 | def export_rst(nb, **kw): |
|
188 | def export_rst(nb, **kw): | |
189 | """ |
|
189 | """ | |
190 | Export a notebook object to RST |
|
190 | Export a notebook object to RST | |
191 | """ |
|
191 | """ | |
192 | return export(RstExporter, nb, **kw) |
|
192 | return export(RstExporter, nb, **kw) | |
193 |
|
193 | |||
194 |
|
194 | |||
195 | @DocDecorator |
|
195 | @DocDecorator | |
196 | def export_by_name(format_name, nb, **kw): |
|
196 | def export_by_name(format_name, nb, **kw): | |
197 | """ |
|
197 | """ | |
198 | Export a notebook object to a template type by its name. Reflection |
|
198 | Export a notebook object to a template type by its name. Reflection | |
199 | (Inspect) is used to find the template's corresponding explicit export |
|
199 | (Inspect) is used to find the template's corresponding explicit export | |
200 | method defined in this module. That method is then called directly. |
|
200 | method defined in this module. That method is then called directly. | |
201 |
|
201 | |||
202 | format_name : str |
|
202 | format_name : str | |
203 | Name of the template style to export to. |
|
203 | Name of the template style to export to. | |
204 | """ |
|
204 | """ | |
205 |
|
205 | |||
206 | function_name = "export_" + format_name.lower() |
|
206 | function_name = "export_" + format_name.lower() | |
207 |
|
207 | |||
208 | if function_name in globals(): |
|
208 | if function_name in globals(): | |
209 | return globals()[function_name](nb, **kw) |
|
209 | return globals()[function_name](nb, **kw) | |
210 | else: |
|
210 | else: | |
211 | raise ExporterNameError("template for `%s` not found" % function_name) |
|
211 | raise ExporterNameError("template for `%s` not found" % function_name) | |
212 |
|
212 | |||
213 | def get_export_names(): |
|
213 | def get_export_names(): | |
214 | "Return a list of the currently supported export targets" |
|
214 | "Return a list of the currently supported export targets" | |
215 | # grab everything after 'export_' |
|
215 | # grab everything after 'export_' | |
216 | l = [x[len('export_'):] for x in __all__ if x.startswith('export_')] |
|
216 | l = [x[len('export_'):] for x in __all__ if x.startswith('export_')] | |
217 |
|
217 | |||
218 |
|
|
218 | # filter out the one method that is not a template | |
219 | l = [x for x in l if 'by_name' not in x] |
|
219 | l = [x for x in l if 'by_name' not in x] | |
220 | return sorted(l) |
|
220 | return sorted(l) |
General Comments 0
You need to be logged in to leave comments.
Login now