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