##// END OF EJS Templates
Use globals dict to find right export function instead of...
Jonathan Frederic -
Show More
@@ -1,487 +1,483
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
19 19 from IPython.nbformat.v3.nbbase import NotebookNode
20 20
21 21 from .exporters.exporter import Exporter
22 22 from .exporters.basichtml import BasicHtmlExporter
23 23 from .exporters.fullhtml import FullHtmlExporter
24 24 from .exporters.latex import LatexExporter
25 25 from .exporters.markdown import MarkdownExporter
26 26 from .exporters.python import PythonExporter
27 27 from .exporters.python_armor import PythonArmorExporter
28 28 from .exporters.reveal import RevealExporter
29 29 from .exporters.rst import RstExporter
30 30 from .exporters.sphinx_howto import SphinxHowtoExporter
31 31 from .exporters.sphinx_manual import SphinxManualExporter
32 32
33 33
34 34 #-----------------------------------------------------------------------------
35 35 # Functions
36 36 #-----------------------------------------------------------------------------
37 37
38 38 def export(nb, config=None, transformers=None, filters=None, exporter_type=Exporter):
39 39 """
40 40 Export a notebook object using specific exporter class.
41 41
42 42 Parameters
43 43 ----------
44 44 config : config
45 45 User configuration instance.
46 46 transformers : list[of transformer]
47 47 Custom transformers to apply to the notebook prior to engaging
48 48 the Jinja template engine. Any transformers specified here
49 49 will override existing transformers if a naming conflict
50 50 occurs.
51 51 filters : list[of filter]
52 52 Custom filters to make accessible to the Jinja templates. Any
53 53 filters specified here will override existing filters if a
54 54 naming conflict occurs.
55 55 exporter_type:
56 56 Class type of the exporter that should be used. This method
57 57 will initialize it's own instance of the class. It is
58 58 ASSUMED that the class type provided exposes a
59 59 constructor (__init__) with the same signature as the
60 60 base Exporter class.
61 61
62 62 Returns
63 63 ----------
64 64 tuple- output, resources, exporter_instance
65 65 output : str
66 66 Jinja 2 output. This is the resulting converted notebook.
67 67 resources : dictionary
68 68 Dictionary of resources used prior to and during the conversion
69 69 process.
70 70 exporter_instance : Exporter
71 71 Instance of the Exporter class used to export the document. Useful
72 72 to caller because it provides a 'file_extension' property which
73 73 specifies what extension the output should be saved as.
74 74 """
75 75
76 76 #Check arguments
77 77 if exporter_type is None:
78 78 raise TypeError("Exporter is None")
79 79 elif not issubclass(exporter_type, Exporter):
80 80 raise TypeError("Exporter type does not inherit from Exporter (base)")
81 81
82 82 if nb is None:
83 83 raise TypeError("nb is None")
84 84
85 85 #Create the exporter
86 86 exporter_instance = exporter_type(preprocessors=transformers,
87 87 jinja_filters=filters, config=config)
88 88
89 89 #Try to convert the notebook using the appropriate conversion function.
90 90 if isinstance(nb, NotebookNode):
91 91 output, resources = exporter_instance.from_notebook_node(nb)
92 92 elif isinstance(nb, basestring):
93 93 output, resources = exporter_instance.from_filename(nb)
94 94 else:
95 95 output, resources = exporter_instance.from_file(nb)
96 96 return output, resources, exporter_instance
97 97
98 98
99 99 def export_sphinx_manual(nb, config=None, transformers=None, filters=None):
100 100 """
101 101 Export a notebook object to Sphinx Manual LaTeX
102 102
103 103 Parameters
104 104 ----------
105 105 config : config
106 106 User configuration instance.
107 107 transformers : list[of transformer]
108 108 Custom transformers to apply to the notebook prior to engaging
109 109 the Jinja template engine. Any transformers specified here
110 110 will override existing transformers if a naming conflict
111 111 occurs.
112 112 filters : list[of filter]
113 113 Custom filters to make accessible to the Jinja templates. Any
114 114 filters specified here will override existing filters if a
115 115 naming conflict occurs.
116 116
117 117 Returns
118 118 ----------
119 119 tuple- output, resources, exporter_instance
120 120 output : str
121 121 Jinja 2 output. This is the resulting converted notebook.
122 122 resources : dictionary
123 123 Dictionary of resources used prior to and during the conversion
124 124 process.
125 125 exporter_instance : Exporter
126 126 Instance of the Exporter class used to export the document. Useful
127 127 to caller because it provides a 'file_extension' property which
128 128 specifies what extension the output should be saved as.
129 129 """
130 130 return export(nb, config, transformers, filters, SphinxManualExporter)
131 131
132 132
133 133 def export_sphinx_howto(nb, config=None, transformers=None, filters=None):
134 134 """
135 135 Export a notebook object to Sphinx HowTo LaTeX
136 136
137 137 Parameters
138 138 ----------
139 139 config : config
140 140 User configuration instance.
141 141 transformers : list[of transformer]
142 142 Custom transformers to apply to the notebook prior to engaging
143 143 the Jinja template engine. Any transformers specified here
144 144 will override existing transformers if a naming conflict
145 145 occurs.
146 146 filters : list[of filter]
147 147 Custom filters to make accessible to the Jinja templates. Any
148 148 filters specified here will override existing filters if a
149 149 naming conflict occurs.
150 150
151 151 Returns
152 152 ----------
153 153 tuple- output, resources, exporter_instance
154 154 output : str
155 155 Jinja 2 output. This is the resulting converted notebook.
156 156 resources : dictionary
157 157 Dictionary of resources used prior to and during the conversion
158 158 process.
159 159 exporter_instance : Exporter
160 160 Instance of the Exporter class used to export the document. Useful
161 161 to caller because it provides a 'file_extension' property which
162 162 specifies what extension the output should be saved as.
163 163 """
164 164 return export(nb, config, transformers, filters, SphinxHowtoExporter)
165 165
166 166
167 167 def export_basic_html(nb, config=None, transformers=None, filters=None):
168 168 """
169 169 Export a notebook object to Basic HTML
170 170
171 171 Parameters
172 172 ----------
173 173 config : config
174 174 User configuration instance.
175 175 transformers : list[of transformer]
176 176 Custom transformers to apply to the notebook prior to engaging
177 177 the Jinja template engine. Any transformers specified here
178 178 will override existing transformers if a naming conflict
179 179 occurs.
180 180 filters : list[of filter]
181 181 Custom filters to make accessible to the Jinja templates. Any
182 182 filters specified here will override existing filters if a
183 183 naming conflict occurs.
184 184
185 185 Returns
186 186 ----------
187 187 tuple- output, resources, exporter_instance
188 188 output : str
189 189 Jinja 2 output. This is the resulting converted notebook.
190 190 resources : dictionary
191 191 Dictionary of resources used prior to and during the conversion
192 192 process.
193 193 exporter_instance : Exporter
194 194 Instance of the Exporter class used to export the document. Useful
195 195 to caller because it provides a 'file_extension' property which
196 196 specifies what extension the output should be saved as.
197 197 """
198 198 return export(nb, config, transformers, filters, BasicHtmlExporter)
199 199
200 200
201 201 def export_full_html(nb, config=None, transformers=None, filters=None):
202 202 """
203 203 Export a notebook object to Full HTML
204 204
205 205 Parameters
206 206 ----------
207 207 config : config
208 208 User configuration instance.
209 209 transformers : list[of transformer]
210 210 Custom transformers to apply to the notebook prior to engaging
211 211 the Jinja template engine. Any transformers specified here
212 212 will override existing transformers if a naming conflict
213 213 occurs.
214 214 filters : list[of filter]
215 215 Custom filters to make accessible to the Jinja templates. Any
216 216 filters specified here will override existing filters if a
217 217 naming conflict occurs.
218 218
219 219 Returns
220 220 ----------
221 221 tuple- output, resources, exporter_instance
222 222 output : str
223 223 Jinja 2 output. This is the resulting converted notebook.
224 224 resources : dictionary
225 225 Dictionary of resources used prior to and during the conversion
226 226 process.
227 227 exporter_instance : Exporter
228 228 Instance of the Exporter class used to export the document. Useful
229 229 to caller because it provides a 'file_extension' property which
230 230 specifies what extension the output should be saved as.
231 231 """
232 232 return export(nb, config, transformers, filters, FullHtmlExporter)
233 233
234 234
235 235 def export_latex(nb, config=None, transformers=None, filters=None):
236 236 """
237 237 Export a notebook object to LaTeX
238 238
239 239 Parameters
240 240 ----------
241 241 config : config
242 242 User configuration instance.
243 243 transformers : list[of transformer]
244 244 Custom transformers to apply to the notebook prior to engaging
245 245 the Jinja template engine. Any transformers specified here
246 246 will override existing transformers if a naming conflict
247 247 occurs.
248 248 filters : list[of filter]
249 249 Custom filters to make accessible to the Jinja templates. Any
250 250 filters specified here will override existing filters if a
251 251 naming conflict occurs.
252 252
253 253 Returns
254 254 ----------
255 255 tuple- output, resources, exporter_instance
256 256 output : str
257 257 Jinja 2 output. This is the resulting converted notebook.
258 258 resources : dictionary
259 259 Dictionary of resources used prior to and during the conversion
260 260 process.
261 261 exporter_instance : Exporter
262 262 Instance of the Exporter class used to export the document. Useful
263 263 to caller because it provides a 'file_extension' property which
264 264 specifies what extension the output should be saved as.
265 265 """
266 266 return export(nb, config, transformers, filters, LatexExporter)
267 267
268 268
269 269 def export_markdown(nb, config=None, transformers=None, filters=None):
270 270 """
271 271 Export a notebook object to Markdown
272 272
273 273 Parameters
274 274 ----------
275 275 config : config
276 276 User configuration instance.
277 277 transformers : list[of transformer]
278 278 Custom transformers to apply to the notebook prior to engaging
279 279 the Jinja template engine. Any transformers specified here
280 280 will override existing transformers if a naming conflict
281 281 occurs.
282 282 filters : list[of filter]
283 283 Custom filters to make accessible to the Jinja templates. Any
284 284 filters specified here will override existing filters if a
285 285 naming conflict occurs.
286 286
287 287 Returns
288 288 ----------
289 289 tuple- output, resources, exporter_instance
290 290 output : str
291 291 Jinja 2 output. This is the resulting converted notebook.
292 292 resources : dictionary
293 293 Dictionary of resources used prior to and during the conversion
294 294 process.
295 295 exporter_instance : Exporter
296 296 Instance of the Exporter class used to export the document. Useful
297 297 to caller because it provides a 'file_extension' property which
298 298 specifies what extension the output should be saved as.
299 299 """
300 300 return export(nb, config, transformers, filters, MarkdownExporter)
301 301
302 302
303 303 def export_python(nb, config=None, transformers=None, filters=None):
304 304 """
305 305 Export a notebook object to Python
306 306
307 307 Parameters
308 308 ----------
309 309 config : config
310 310 User configuration instance.
311 311 transformers : list[of transformer]
312 312 Custom transformers to apply to the notebook prior to engaging
313 313 the Jinja template engine. Any transformers specified here
314 314 will override existing transformers if a naming conflict
315 315 occurs.
316 316 filters : list[of filter]
317 317 Custom filters to make accessible to the Jinja templates. Any
318 318 filters specified here will override existing filters if a
319 319 naming conflict occurs.
320 320
321 321 Returns
322 322 ----------
323 323 tuple- output, resources, exporter_instance
324 324 output : str
325 325 Jinja 2 output. This is the resulting converted notebook.
326 326 resources : dictionary
327 327 Dictionary of resources used prior to and during the conversion
328 328 process.
329 329 exporter_instance : Exporter
330 330 Instance of the Exporter class used to export the document. Useful
331 331 to caller because it provides a 'file_extension' property which
332 332 specifies what extension the output should be saved as.
333 333 """
334 334 return export(nb, config, transformers, filters, PythonExporter)
335 335
336 336
337 337 def export_python_armor(nb, config=None, transformers=None, filters=None):
338 338 """
339 339 Export a notebook object to Python (Armor)
340 340
341 341 Parameters
342 342 ----------
343 343 config : config
344 344 User configuration instance.
345 345 transformers : list[of transformer]
346 346 Custom transformers to apply to the notebook prior to engaging
347 347 the Jinja template engine. Any transformers specified here
348 348 will override existing transformers if a naming conflict
349 349 occurs.
350 350 filters : list[of filter]
351 351 Custom filters to make accessible to the Jinja templates. Any
352 352 filters specified here will override existing filters if a
353 353 naming conflict occurs.
354 354
355 355 Returns
356 356 ----------
357 357 tuple- output, resources, exporter_instance
358 358 output : str
359 359 Jinja 2 output. This is the resulting converted notebook.
360 360 resources : dictionary
361 361 Dictionary of resources used prior to and during the conversion
362 362 process.
363 363 exporter_instance : Exporter
364 364 Instance of the Exporter class used to export the document. Useful
365 365 to caller because it provides a 'file_extension' property which
366 366 specifies what extension the output should be saved as.
367 367 """
368 368 return export(nb, config, transformers, filters, PythonArmorExporter)
369 369
370 370
371 371 def export_reveal(nb, config=None, transformers=None, filters=None):
372 372 """
373 373 Export a notebook object to Reveal
374 374
375 375 Parameters
376 376 ----------
377 377 config : config
378 378 User configuration instance.
379 379 transformers : list[of transformer]
380 380 Custom transformers to apply to the notebook prior to engaging
381 381 the Jinja template engine. Any transformers specified here
382 382 will override existing transformers if a naming conflict
383 383 occurs.
384 384 filters : list[of filter]
385 385 Custom filters to make accessible to the Jinja templates. Any
386 386 filters specified here will override existing filters if a
387 387 naming conflict occurs.
388 388
389 389 Returns
390 390 ----------
391 391 tuple- output, resources, exporter_instance
392 392 output : str
393 393 Jinja 2 output. This is the resulting converted notebook.
394 394 resources : dictionary
395 395 Dictionary of resources used prior to and during the conversion
396 396 process.
397 397 exporter_instance : Exporter
398 398 Instance of the Exporter class used to export the document. Useful
399 399 to caller because it provides a 'file_extension' property which
400 400 specifies what extension the output should be saved as.
401 401 """
402 402 return export(nb, config, transformers, filters, RevealExporter)
403 403
404 404
405 405 def export_rst(nb, config=None, transformers=None, filters=None):
406 406 """
407 407 Export a notebook object to RST
408 408
409 409 Parameters
410 410 ----------
411 411 config : config
412 412 User configuration instance.
413 413 transformers : list[of transformer]
414 414 Custom transformers to apply to the notebook prior to engaging
415 415 the Jinja template engine. Any transformers specified here
416 416 will override existing transformers if a naming conflict
417 417 occurs.
418 418 filters : list[of filter]
419 419 Custom filters to make accessible to the Jinja templates. Any
420 420 filters specified here will override existing filters if a
421 421 naming conflict occurs.
422 422
423 423 Returns
424 424 ----------
425 425 tuple- output, resources, exporter_instance
426 426 output : str
427 427 Jinja 2 output. This is the resulting converted notebook.
428 428 resources : dictionary
429 429 Dictionary of resources used prior to and during the conversion
430 430 process.
431 431 exporter_instance : Exporter
432 432 Instance of the Exporter class used to export the document. Useful
433 433 to caller because it provides a 'file_extension' property which
434 434 specifies what extension the output should be saved as.
435 435 """
436 436 return export(nb, config, transformers, filters, RstExporter)
437 437
438 438
439 439 def export_by_name(nb, template_name, config=None, transformers=None, filters=None):
440 440 """
441 441 Export a notebook object to a template type by its name. Reflection
442 442 (Inspect) is used to find the template's corresponding explicit export
443 443 method defined in this module. That method is then called directly.
444 444
445 445 Parameters
446 446 ----------
447 447 template_name : str
448 448 Name of the template style to export to.
449 449 config : config
450 450 User configuration instance.
451 451 transformers : list[of transformer]
452 452 Custom transformers to apply to the notebook prior to engaging
453 453 the Jinja template engine. Any transformers specified here
454 454 will override existing transformers if a naming conflict
455 455 occurs.
456 456 filters : list[of filter]
457 457 Custom filters to make accessible to the Jinja templates. Any
458 458 filters specified here will override existing filters if a
459 459 naming conflict occurs.
460 460
461 461 Returns
462 462 ----------
463 463 tuple- (output, resources, exporter_instance)
464 464 None- if template not found
465 465
466 466 output : str
467 467 Jinja 2 output. This is the resulting converted notebook.
468 468 resources : dictionary
469 469 Dictionary of resources used prior to and during the conversion
470 470 process.
471 471 exporter_instance : Exporter
472 472 Instance of the Exporter class used to export the document. Useful
473 473 to caller because it provides a 'file_extension' property which
474 474 specifies what extension the output should be saved as.
475 475 """
476 476
477 #Use reflection to get functions defined in this module.
478 cls_functions = inspect.getmembers(sys.modules[__name__], inspect.isfunction)
479
480 #Check if the characters following "export_" (7 char) equals the template name.
481 for (function_name, function_handle) in cls_functions:
482 function_name = function_name.lower()
483 if (len(function_name) > 7 and function_name[7:] == template_name.lower()):
484 return function_handle(nb, config, transformers, filters)
477 function_name = "export_" + template_name.lower()
485 478
479 if function_name in globals():
480 return globals()[function_name](nb, config, transformers, filters)
481 else:
486 482 return None
487 483 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now