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