Show More
@@ -107,10 +107,10 def export(exporter_type, nb, **kw): | |||
|
107 | 107 | raise TypeError("nb is None") |
|
108 | 108 | |
|
109 | 109 | #Create the exporter |
|
110 | exporter_instance = exporter_type(config=kw.get('config', Config())) | |
|
110 | resources = kw.pop('resources', None) | |
|
111 | exporter_instance = exporter_type(**kw) | |
|
111 | 112 | |
|
112 | 113 | #Try to convert the notebook using the appropriate conversion function. |
|
113 | resources = kw.get('resources', {}) | |
|
114 | 114 | if isinstance(nb, NotebookNode): |
|
115 | 115 | output, resources = exporter_instance.from_notebook_node(nb, resources) |
|
116 | 116 | elif isinstance(nb, basestring): |
@@ -152,7 +152,7 class Exporter(Configurable): | |||
|
152 | 152 | #Load user transformers. Enabled by default. |
|
153 | 153 | if self.transformers: |
|
154 | 154 | for transformer in self.transformers: |
|
155 | self.register_transformer(transformer, True) | |
|
155 | self.register_transformer(transformer, enabled=True) | |
|
156 | 156 | |
|
157 | 157 | #Load user filters. Overwrite existing filters if need be. |
|
158 | 158 | if self.filters: |
@@ -178,19 +178,20 class Exporter(Configurable): | |||
|
178 | 178 | """ |
|
179 | 179 | nb_copy = deepcopy(nb) |
|
180 | 180 | |
|
181 | #Set output extension in resources dict | |
|
182 | #TODO: init_resources | |
|
183 | resources['output_extension'] = self.file_extension | |
|
184 | ||
|
181 | 185 | #Preprocess |
|
182 |
nb_copy, resources = self._ |
|
|
186 | nb_copy, resources = self._transform(nb_copy, resources) | |
|
183 | 187 | |
|
184 | 188 | #Convert |
|
185 | 189 | self.template = self.environment.get_template(self.template_file + self.template_extension) |
|
186 | 190 | output = self.template.render(nb=nb_copy, resources=resources) |
|
187 | ||
|
188 | #Set output extension in resources dict | |
|
189 | resources['output_extension'] = self.file_extension | |
|
190 | 191 | return output, resources |
|
191 | 192 | |
|
192 | 193 | |
|
193 | def from_filename(self, filename, resources=None **kw): | |
|
194 | def from_filename(self, filename, resources=None, **kw): | |
|
194 | 195 | """ |
|
195 | 196 | Convert a notebook from a notebook file. |
|
196 | 197 | |
@@ -244,11 +245,9 class Exporter(Configurable): | |||
|
244 | 245 | elif isinstance(transformer, MetaHasTraits): |
|
245 | 246 | #Transformer is configurable. Make sure to pass in new default for |
|
246 | 247 | #the enabled flag if one was specified. |
|
247 | c = Config() | |
|
248 |
if |
|
|
249 |
c = |
|
|
250 | c.merge(self.config) | |
|
251 | transformer_instance = transformer(config=c) | |
|
248 | transformer_instance = transformer(parent=self) | |
|
249 | if enabled is not None: | |
|
250 | transformer_instance.enabled = True | |
|
252 | 251 | |
|
253 | 252 | else: |
|
254 | 253 | #Transformer is not configurable, construct it |
@@ -335,7 +334,7 class Exporter(Configurable): | |||
|
335 | 334 | self.environment.comment_end_string = self.jinja_comment_block_end |
|
336 | 335 | |
|
337 | 336 | |
|
338 |
def _ |
|
|
337 | def _transform(self, nb, resources): | |
|
339 | 338 | """ |
|
340 | 339 | Preprocess the notebook before passing it into the Jinja engine. |
|
341 | 340 | To preprocess the notebook is to apply all of the |
@@ -99,7 +99,7 class LatexExporter(Exporter): | |||
|
99 | 99 | def default_config(self): |
|
100 | 100 | c = Config({ |
|
101 | 101 | 'GlobalConfigurable': { |
|
102 | 'display_data_priority' : ['latex', 'png', 'jpg', 'jpeg', 'text'] | |
|
102 | 'display_data_priority' : ['latex', 'png', 'jpg', 'pdf', 'jpeg', 'text'] | |
|
103 | 103 | }, |
|
104 | 104 | 'ExtractFigureTransformer': { |
|
105 | 105 | 'enabled':True |
@@ -143,6 +143,7 class NbConvertApp(BaseIPythonApplication): | |||
|
143 | 143 | super(NbConvertApp, self).start() |
|
144 | 144 | |
|
145 | 145 | #Export each notebook |
|
146 | #TODO: Empty check | |
|
146 | 147 | for notebook_filename in self.notebooks: |
|
147 | 148 | |
|
148 | 149 | #Get a unique key for the notebook and set it in the resources object. |
@@ -153,7 +154,7 class NbConvertApp(BaseIPythonApplication): | |||
|
153 | 154 | |
|
154 | 155 | #Try to export |
|
155 | 156 | try: |
|
156 |
|
|
|
157 | output, resources = export_by_name(self.export_format, | |
|
157 | 158 | notebook_filename, |
|
158 | 159 | resources=resources, |
|
159 | 160 | config=self.config) |
@@ -164,8 +165,9 class NbConvertApp(BaseIPythonApplication): | |||
|
164 | 165 | "\n\t" + "\n\t".join(get_export_names()), |
|
165 | 166 | file=sys.stderr) |
|
166 | 167 | sys.exit(-1) |
|
167 |
e |
|
|
168 | output, resources = return_value | |
|
168 | except Exception as e: | |
|
169 | print("Error: could no export '%s'" % notebook_filename, file=sys.stderr) | |
|
170 | print(e, file=sys.stderr) | |
|
169 | 171 | |
|
170 | 172 | #Write |
|
171 | 173 | self.writer.write(output, resources, notebook_name=notebook_name) |
@@ -176,4 +178,3 class NbConvertApp(BaseIPythonApplication): | |||
|
176 | 178 | #----------------------------------------------------------------------------- |
|
177 | 179 | |
|
178 | 180 | launch_new_instance = NbConvertApp.launch_instance |
|
179 |
@@ -9,6 +9,8 one format to another. | |||
|
9 | 9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | #TODO: Come after extract | |
|
13 | ||
|
12 | 14 | #----------------------------------------------------------------------------- |
|
13 | 15 | # Imports |
|
14 | 16 | #----------------------------------------------------------------------------- |
@@ -42,6 +44,7 class ConvertFiguresTransformer(ActivatableTransformer): | |||
|
42 | 44 | """ |
|
43 | 45 | super(ConvertFiguresTransformer, self).__init__(**kw) |
|
44 | 46 | |
|
47 | #TODO: Configurable, singular | |
|
45 | 48 | self._from_formats = from_formats |
|
46 | 49 | self._to_format = to_format |
|
47 | 50 |
@@ -69,3 +69,4 class ConvertSvgTransformer(ConvertFiguresTransformer): | |||
|
69 | 69 | #data type, so base64 encode. |
|
70 | 70 | else: |
|
71 | 71 | return TypeError("Inkscape svg to png conversion failed") |
|
72 | #TODO: ERROR: which notebook crashed exporter |
@@ -18,15 +18,6 from IPython.utils.traitlets import Dict, Unicode | |||
|
18 | 18 | from .activatable import ActivatableTransformer |
|
19 | 19 | |
|
20 | 20 | #----------------------------------------------------------------------------- |
|
21 | # Constants | |
|
22 | #----------------------------------------------------------------------------- | |
|
23 | ||
|
24 | # win64 is win32 for backwards compatability, for now. See | |
|
25 | # http://mail.python.org/pipermail/patches/2000-May/000648.html | |
|
26 | # for the original patch that this decision was made. | |
|
27 | WINDOWS_PLATFORMS = ['win32'] | |
|
28 | ||
|
29 | #----------------------------------------------------------------------------- | |
|
30 | 21 | # Classes |
|
31 | 22 | #----------------------------------------------------------------------------- |
|
32 | 23 | |
@@ -75,7 +66,7 class ExtractFigureTransformer(ActivatableTransformer): | |||
|
75 | 66 | #Binary files are base64-encoded, SVG is already XML |
|
76 | 67 | if out_type in ('png', 'jpg', 'pdf'): |
|
77 | 68 | data = data.decode('base64') |
|
78 |
elif sys.platform |
|
|
69 | elif sys.platform == 'win32': | |
|
79 | 70 | data = data.replace('\n', '\r\n').encode("UTF-8") |
|
80 | 71 | else: |
|
81 | 72 | data = data.encode("UTF-8") |
@@ -129,7 +129,7 class SphinxTransformer(ActivatableTransformer): | |||
|
129 | 129 | # '_draft' to signify that this needs to change. |
|
130 | 130 | if not "_draft" in nb.metadata: |
|
131 | 131 | nb.metadata._draft = {} |
|
132 | ||
|
132 | #TODO: Remove draft, and nb | |
|
133 | 133 | if not "sphinx" in resources: |
|
134 | 134 | resources["sphinx"] = {} |
|
135 | 135 |
General Comments 0
You need to be logged in to leave comments.
Login now