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