Show More
@@ -37,7 +37,7 b' texenv = Environment(' | |||
|
37 | 37 | # IPython imports |
|
38 | 38 | from IPython.nbformat import current as nbformat |
|
39 | 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 | 42 | # Our own imports |
|
43 | 43 | from IPython.utils.text import indent |
@@ -112,21 +112,6 b' def _new_figure(data, fmt, count):' | |||
|
112 | 112 | inlining = {} |
|
113 | 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 | 115 | LATEX_SUBS = ( |
|
131 | 116 | (re.compile(r'\\'), r'\\textbackslash'), |
|
132 | 117 | (re.compile(r'([{}_#%&$])'), r'\\\1'), |
@@ -150,15 +135,6 b" texenv.comment_start_string = '((='" | |||
|
150 | 135 | texenv.comment_end_string = '=))' |
|
151 | 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 | 138 | def cell_preprocessor(function): |
|
163 | 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 | 173 | @cell_preprocessor |
|
198 |
def |
|
|
174 | def extract_figure_transformer(cell,other,count): | |
|
199 | 175 | for i,out in enumerate(cell.get('outputs', [])): |
|
200 | 176 | for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']: |
|
201 | 177 | if out.hasattr(type): |
@@ -204,7 +180,7 b' def outline_figure_transformer(cell,other,count):' | |||
|
204 | 180 | out['key_'+type] = figname |
|
205 | 181 | other[figname] = data |
|
206 | 182 | count = count+1 |
|
207 |
return |
|
|
183 | return cell,other | |
|
208 | 184 | |
|
209 | 185 | |
|
210 | 186 | class ConverterTemplate(Configurable): |
@@ -249,14 +225,25 b' class ConverterTemplate(Configurable):' | |||
|
249 | 225 | to extract/inline file, |
|
250 | 226 | |
|
251 | 227 | """ |
|
228 | super(ConverterTemplate, self).__init__(config=config, **kw) | |
|
252 | 229 | self.env = texenv if tex_environement else env |
|
253 | 230 | self.ext = '.tplx' if tex_environement else '.tpl' |
|
254 | 231 | self.nb = None |
|
255 | 232 | self.preprocessors = preprocessors |
|
256 | 233 | self.preprocessors.append(haspyout_transformer) |
|
257 | self.preprocessors.append(outline_figure_transformer) | |
|
258 | super(ConverterTemplate, self).__init__(config=config, **kw) | |
|
234 | if self.extract_figures: | |
|
235 | self.preprocessors.append(extract_figure_transformer) | |
|
236 | ||
|
259 | 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 | 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 | 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 | 262 | for preprocessor in self.preprocessors: |
|
272 |
nb, |
|
|
263 | nb,resources = preprocessor(nb,resources) | |
|
273 | 264 | |
|
274 | return nb | |
|
265 | return nb, resources | |
|
275 | 266 | |
|
276 | 267 | def convert(self): |
|
277 | 268 | """ convert the ipynb file |
@@ -279,7 +270,8 b' class ConverterTemplate(Configurable):' | |||
|
279 | 270 | return both the converted ipynb file and a dict containing potential |
|
280 | 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 | 277 | def read(self, filename): |
@@ -44,13 +44,28 b' class NbconvertApp(Application):' | |||
|
44 | 44 | else: |
|
45 | 45 | tex_environement=False |
|
46 | 46 | |
|
47 |
C = ConverterTemplate(tplfile=sys.argv[1], |
|
|
47 | C = ConverterTemplate(tplfile=sys.argv[1], | |
|
48 | tex_environement=tex_environement, | |
|
49 | config=self.config) | |
|
48 | 50 | C.read(ipynb_file) |
|
49 | 51 | |
|
50 |
output,res |
|
|
52 | output,resources = C.convert() | |
|
51 | 53 | |
|
52 | 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 | 69 | def main(): |
|
55 | 70 | """Convert a notebook to html in one step""" |
|
56 | 71 | app = NbconvertApp.instance() |
General Comments 0
You need to be logged in to leave comments.
Login now