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