Show More
@@ -57,7 +57,7 b' from IPython.utils.traitlets import ( Unicode, Any, List, Bool)' | |||
|
57 | 57 | class ConversionException(Exception): |
|
58 | 58 | pass |
|
59 | 59 | |
|
60 | ||
|
60 | #todo move this out | |
|
61 | 61 | def header_body(): |
|
62 | 62 | """Return the body of the header as a list of strings.""" |
|
63 | 63 | |
@@ -109,7 +109,13 b" texenv.filters['escape_tex'] = escape_tex" | |||
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | class ConverterTemplate(Configurable): |
|
112 |
""" A Jinja2 base converter templates |
|
|
112 | """ A Jinja2 base converter templates | |
|
113 | ||
|
114 | Preprocess the ipynb files, feed it throug jinja templates, | |
|
115 | and spit an converted files and a data object with other data | |
|
116 | ||
|
117 | shoudl be mostly configurable | |
|
118 | """ | |
|
113 | 119 | |
|
114 | 120 | display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'], |
|
115 | 121 | config=True, |
@@ -120,7 +126,7 b' class ConverterTemplate(Configurable):' | |||
|
120 | 126 | """ |
|
121 | 127 | ) |
|
122 | 128 | |
|
123 |
pre_transformer_order = List(['haspyout_transformer' |
|
|
129 | pre_transformer_order = List(['haspyout_transformer'], | |
|
124 | 130 | config=True, |
|
125 | 131 | help= """ |
|
126 | 132 | An ordered list of pre transformer to apply to the ipynb |
@@ -142,7 +148,7 b' class ConverterTemplate(Configurable):' | |||
|
142 | 148 | |
|
143 | 149 | template_file = Unicode('', |
|
144 | 150 | config=True, |
|
145 |
help=""" |
|
|
151 | help=""" Name of the template file to use """ ) | |
|
146 | 152 | #------------------------------------------------------------------------- |
|
147 | 153 | # Instance-level attributes that are set in the constructor for this |
|
148 | 154 | # class. |
@@ -152,33 +158,38 b' class ConverterTemplate(Configurable):' | |||
|
152 | 158 | |
|
153 | 159 | infile_dir = Unicode() |
|
154 | 160 | |
|
161 | #todo: move to filter | |
|
155 | 162 | def filter_data_type(self, output): |
|
163 | """ return the first availlable format in priority """ | |
|
156 | 164 | for fmt in self.display_data_priority: |
|
157 | 165 | if fmt in output: |
|
158 | 166 | return [fmt] |
|
159 | 167 | |
|
160 | def __init__(self, preprocessors=[], config=None, **kw): | |
|
168 | preprocessors = [] | |
|
169 | ||
|
170 | def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw): | |
|
161 | 171 | """ |
|
162 | 172 | config: the Configurable confg object to pass around |
|
163 | 173 | |
|
164 |
preprocessors: |
|
|
174 | preprocessors: dict of **availlable** key/value function to run on ipynb json data before conversion | |
|
165 | 175 | to extract/inline file, |
|
166 | 176 | |
|
167 | 177 | """ |
|
168 | 178 | super(ConverterTemplate, self).__init__(config=config, **kw) |
|
169 | 179 | self.env = texenv if self.tex_environement else env |
|
170 | 180 | self.ext = '.tplx' if self.tex_environement else '.tpl' |
|
171 | self.nb = None | |
|
172 | self.preprocessors = preprocessors | |
|
173 | 181 | |
|
174 | 182 | for name in self.pre_transformer_order: |
|
175 | tr = getattr(trans, name) | |
|
176 | if isinstance(tr, MetaHasTraits): | |
|
177 | tr = tr(config=config) | |
|
178 | self.preprocessors.append(tr) | |
|
183 | transformer = getattr(preprocessors, name, getattr(trans, name, None)) | |
|
184 | if isinstance(transformer, MetaHasTraits): | |
|
185 | transformer = transformer(config=config) | |
|
186 | self.preprocessors.append(transformer) | |
|
187 | ||
|
188 | ## for compat, remove later | |
|
179 | 189 | if self.extract_figures: |
|
180 | 190 | self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) |
|
181 | 191 | |
|
192 | ## | |
|
182 | 193 | self.env.filters['filter_data_type'] = self.filter_data_type |
|
183 | 194 | self.env.filters['pycomment'] = python_comment |
|
184 | 195 | self.env.filters['indent'] = indent |
@@ -188,16 +199,17 b' class ConverterTemplate(Configurable):' | |||
|
188 | 199 | self.env.filters['highlight'] = highlight |
|
189 | 200 | self.env.filters['ansi2html'] = ansi2html |
|
190 | 201 | self.env.filters['markdown2latex'] = markdown2latex |
|
202 | for k, v in jinja_filters.iteritems(): | |
|
203 | self.env.filters[k] = v | |
|
191 | 204 | |
|
192 | 205 | self.template = self.env.get_template(self.template_file+self.ext) |
|
193 | 206 | |
|
194 | 207 | |
|
195 | def process(self): | |
|
208 | def process(self, nb): | |
|
196 | 209 | """ |
|
197 | 210 | preprocess the notebook json for easier use with the templates. |
|
198 | 211 | will call all the `preprocessor`s in order before returning it. |
|
199 | 212 | """ |
|
200 | nb = self.nb | |
|
201 | 213 | |
|
202 | 214 | # dict of 'resources' that could be made by the preprocessors |
|
203 | 215 | # like key/value data to extract files from ipynb like in latex conversion |
@@ -208,20 +220,18 b' class ConverterTemplate(Configurable):' | |||
|
208 | 220 | |
|
209 | 221 | return nb, resources |
|
210 | 222 | |
|
211 | def convert(self): | |
|
223 | def convert(self, nb): | |
|
212 | 224 | """ convert the ipynb file |
|
213 | 225 | |
|
214 | 226 | return both the converted ipynb file and a dict containing potential |
|
215 | 227 | other resources |
|
216 | 228 | """ |
|
217 | nb, resources = self.process() | |
|
229 | nb, resources = self.process(nb) | |
|
218 | 230 | return self.template.render(nb=nb, inlining=inlining), resources |
|
219 | 231 | |
|
220 | 232 | |
|
221 |
def |
|
|
233 | def from_filename(self, filename): | |
|
222 | 234 | "read and parse notebook into NotebookNode called self.nb" |
|
223 | 235 | with io.open(filename) as f: |
|
224 |
self. |
|
|
225 | ||
|
226 | ||
|
236 | return self.convert(nbformat.read(f, 'json')) | |
|
227 | 237 |
@@ -90,9 +90,8 b' class NbconvertApp(Application):' | |||
|
90 | 90 | template_file = sys.argv[1] |
|
91 | 91 | |
|
92 | 92 | C = ConverterTemplate(config=self.config) |
|
93 | C.read(ipynb_file) | |
|
94 | 93 | |
|
95 |
output,resources = C. |
|
|
94 | output,resources = C.from_filename(ipynb_file) | |
|
96 | 95 | if self.stdout : |
|
97 | 96 | print(output.encode('utf-8')) |
|
98 | 97 |
@@ -24,7 +24,6 b' def check_null_profile(profile):' | |||
|
24 | 24 | loader = PyFileConfigLoader(profile, path=[os.path.join(os.getcwdu(),'profile/test')]) |
|
25 | 25 | config = loader.load_config() |
|
26 | 26 | C = ConverterTemplate(config=config) |
|
27 |
C. |
|
|
28 | result,_ = C.convert() | |
|
27 | result,_ = C.from_filename('tests/ipynbref/IntroNumPy.orig.ipynb') | |
|
29 | 28 | nt.assert_equal(result,'') |
|
30 | 29 |
General Comments 0
You need to be logged in to leave comments.
Login now