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