##// END OF EJS Templates
flag for extracting figure
Matthias BUSSONNIER -
Show More
@@ -37,7 +37,7 b' texenv = Environment('
37 # IPython imports
37 # IPython imports
38 from IPython.nbformat import current as nbformat
38 from IPython.nbformat import current as nbformat
39 from IPython.config.configurable import Configurable
39 from IPython.config.configurable import Configurable
40 from IPython.utils.traitlets import ( Unicode, Any, List)
40 from IPython.utils.traitlets import ( Unicode, Any, List, Bool)
41
41
42 # Our own imports
42 # Our own imports
43 from IPython.utils.text import indent
43 from IPython.utils.text import indent
@@ -112,21 +112,6 b' def _new_figure(data, fmt, count):'
112 inlining = {}
112 inlining = {}
113 inlining['css'] = header_body()
113 inlining['css'] = header_body()
114
114
115
116
117
118
119
120 env.filters['pycomment'] = python_comment
121 env.filters['indent'] = indent
122 env.filters['rm_fake'] = rm_fake
123 env.filters['rm_ansi'] = remove_ansi
124 env.filters['markdown'] = markdown
125 env.filters['highlight'] = highlight
126 env.filters['ansi2html'] = ansi2html
127
128
129
130 LATEX_SUBS = (
115 LATEX_SUBS = (
131 (re.compile(r'\\'), r'\\textbackslash'),
116 (re.compile(r'\\'), r'\\textbackslash'),
132 (re.compile(r'([{}_#%&$])'), r'\\\1'),
117 (re.compile(r'([{}_#%&$])'), r'\\\1'),
@@ -150,15 +135,6 b" texenv.comment_start_string = '((='"
150 texenv.comment_end_string = '=))'
135 texenv.comment_end_string = '=))'
151 texenv.filters['escape_tex'] = escape_tex
136 texenv.filters['escape_tex'] = escape_tex
152
137
153 texenv.filters['pycomment'] = python_comment
154 texenv.filters['indent'] = indent
155 texenv.filters['rm_fake'] = rm_fake
156 texenv.filters['rm_ansi'] = remove_ansi
157 texenv.filters['markdown'] = markdown
158 texenv.filters['highlight'] = highlight
159 texenv.filters['ansi2html'] = ansi2html
160 texenv.filters['markdown2latex'] = markdown2latex
161
162 def cell_preprocessor(function):
138 def cell_preprocessor(function):
163 """ wrap a function to be executed on all cells of a notebook
139 """ wrap a function to be executed on all cells of a notebook
164
140
@@ -195,7 +171,7 b' def haspyout_transformer(cell, other, count):'
195
171
196
172
197 @cell_preprocessor
173 @cell_preprocessor
198 def outline_figure_transformer(cell,other,count):
174 def extract_figure_transformer(cell,other,count):
199 for i,out in enumerate(cell.get('outputs', [])):
175 for i,out in enumerate(cell.get('outputs', [])):
200 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
176 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
201 if out.hasattr(type):
177 if out.hasattr(type):
@@ -204,7 +180,7 b' def outline_figure_transformer(cell,other,count):'
204 out['key_'+type] = figname
180 out['key_'+type] = figname
205 other[figname] = data
181 other[figname] = data
206 count = count+1
182 count = count+1
207 return nb,other
183 return cell,other
208
184
209
185
210 class ConverterTemplate(Configurable):
186 class ConverterTemplate(Configurable):
@@ -249,14 +225,25 b' class ConverterTemplate(Configurable):'
249 to extract/inline file,
225 to extract/inline file,
250
226
251 """
227 """
228 super(ConverterTemplate, self).__init__(config=config, **kw)
252 self.env = texenv if tex_environement else env
229 self.env = texenv if tex_environement else env
253 self.ext = '.tplx' if tex_environement else '.tpl'
230 self.ext = '.tplx' if tex_environement else '.tpl'
254 self.nb = None
231 self.nb = None
255 self.preprocessors = preprocessors
232 self.preprocessors = preprocessors
256 self.preprocessors.append(haspyout_transformer)
233 self.preprocessors.append(haspyout_transformer)
257 self.preprocessors.append(outline_figure_transformer)
234 if self.extract_figures:
258 super(ConverterTemplate, self).__init__(config=config, **kw)
235 self.preprocessors.append(extract_figure_transformer)
236
259 self.env.filters['filter_data_type'] = self.filter_data_type
237 self.env.filters['filter_data_type'] = self.filter_data_type
238 self.env.filters['pycomment'] = python_comment
239 self.env.filters['indent'] = indent
240 self.env.filters['rm_fake'] = rm_fake
241 self.env.filters['rm_ansi'] = remove_ansi
242 self.env.filters['markdown'] = markdown
243 self.env.filters['highlight'] = highlight
244 self.env.filters['ansi2html'] = ansi2html
245 self.env.filters['markdown2latex'] = markdown2latex
246
260 self.template = self.env.get_template(tplfile+self.ext)
247 self.template = self.env.get_template(tplfile+self.ext)
261
248
262
249
@@ -268,10 +255,14 b' class ConverterTemplate(Configurable):'
268 """
255 """
269 nb = self.nb
256 nb = self.nb
270
257
258 # dict of 'resources' that could be made by the preprocessors
259 # like key/value data to extract files from ipynb like in latex conversion
260 resources = {}
261
271 for preprocessor in self.preprocessors:
262 for preprocessor in self.preprocessors:
272 nb,others = preprocessor(nb,{})
263 nb,resources = preprocessor(nb,resources)
273
264
274 return nb
265 return nb, resources
275
266
276 def convert(self):
267 def convert(self):
277 """ convert the ipynb file
268 """ convert the ipynb file
@@ -279,7 +270,8 b' class ConverterTemplate(Configurable):'
279 return both the converted ipynb file and a dict containing potential
270 return both the converted ipynb file and a dict containing potential
280 other resources
271 other resources
281 """
272 """
282 return self.template.render(nb=self.process(), inlining=inlining), {}
273 nb,resources = self.process()
274 return self.template.render(nb=nb, inlining=inlining), resources
283
275
284
276
285 def read(self, filename):
277 def read(self, filename):
@@ -44,13 +44,28 b' class NbconvertApp(Application):'
44 else:
44 else:
45 tex_environement=False
45 tex_environement=False
46
46
47 C = ConverterTemplate(tplfile=sys.argv[1], tex_environement=tex_environement, config=self.config)
47 C = ConverterTemplate(tplfile=sys.argv[1],
48 tex_environement=tex_environement,
49 config=self.config)
48 C.read(ipynb_file)
50 C.read(ipynb_file)
49
51
50 output,rest = C.convert()
52 output,resources = C.convert()
51
53
52 print(output.encode('utf-8'))
54 print(output.encode('utf-8'))
53
55
56 keys = resources.keys()
57 if keys :
58 print('''
59 ====================== Keys in Resources ==================================
60 ''')
61 print(resources.keys())
62 print("""
63 ===========================================================================
64 you are responsible from writing those data do a file in the right place if
65 they need to be.
66 ===========================================================================
67 """)
68
54 def main():
69 def main():
55 """Convert a notebook to html in one step"""
70 """Convert a notebook to html in one step"""
56 app = NbconvertApp.instance()
71 app = NbconvertApp.instance()
General Comments 0
You need to be logged in to leave comments. Login now