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