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