Show More
@@ -1,224 +1,223 b'' | |||
|
1 | 1 | """Base classes for the notebook conversion pipeline. |
|
2 | 2 | |
|
3 | 3 | This module defines ConverterTemplate, a highly configurable converter |
|
4 | 4 | that uses Jinja2 to convert notebook files into different format. |
|
5 | 5 | |
|
6 | 6 | You can register both pre-transformers that will act on the notebook format |
|
7 | 7 | befor conversion and jinja filter that would then be availlable in the templates |
|
8 | 8 | """ |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | # Copyright (c) 2013, the IPython Development Team. |
|
11 | 11 | # |
|
12 | 12 | # Distributed under the terms of the Modified BSD License. |
|
13 | 13 | # |
|
14 | 14 | # The full license is in the file COPYING.txt, distributed with this software. |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | # Imports |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | 21 | from __future__ import print_function, absolute_import |
|
22 | 22 | |
|
23 | 23 | # Stdlib imports |
|
24 | 24 | import io |
|
25 | 25 | |
|
26 | 26 | # IPython imports |
|
27 | 27 | from IPython.utils.traitlets import MetaHasTraits |
|
28 | 28 | from IPython.utils.traitlets import (Unicode, List, Bool) |
|
29 | 29 | from IPython.config.configurable import Configurable |
|
30 | 30 | from IPython.nbformat import current as nbformat |
|
31 | 31 | |
|
32 | 32 | # other libs/dependencies |
|
33 | 33 | from jinja2 import Environment, FileSystemLoader |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | # local import (pre-transformers) |
|
37 | 37 | import converters.transformers as trans |
|
38 | 38 | |
|
39 | 39 | # some jinja filters |
|
40 | 40 | from converters.jinja_filters import (python_comment, indent, |
|
41 | 41 | rm_fake, remove_ansi, markdown, highlight, |
|
42 | 42 | ansi2html, markdown2latex, escape_tex, FilterDataType) |
|
43 | 43 | |
|
44 | 44 | from converters.utils import markdown2rst |
|
45 | from converters.config import GlobalConfigurable | |
|
46 | 45 | |
|
47 | 46 | |
|
48 | 47 | |
|
49 | 48 | # define differents environemnt with different |
|
50 | 49 | # delimiters not to conflict with languages inside |
|
51 | 50 | |
|
52 | 51 | env = Environment( |
|
53 | 52 | loader=FileSystemLoader([ |
|
54 | 53 | './templates/', |
|
55 | 54 | './templates/skeleton/', |
|
56 | 55 | ]), |
|
57 | 56 | extensions=['jinja2.ext.loopcontrols'] |
|
58 | 57 | ) |
|
59 | 58 | |
|
60 | 59 | texenv = Environment( |
|
61 | 60 | loader=FileSystemLoader([ |
|
62 | 61 | './templates/tex/', |
|
63 | 62 | './templates/skeleton/tex/', |
|
64 | 63 | ]), |
|
65 | 64 | extensions=['jinja2.ext.loopcontrols'] |
|
66 | 65 | ) |
|
67 | 66 | |
|
68 | 67 | |
|
69 | 68 | texenv.block_start_string = '((*' |
|
70 | 69 | texenv.block_end_string = '*))' |
|
71 | 70 | |
|
72 | 71 | texenv.variable_start_string = '(((' |
|
73 | 72 | texenv.variable_end_string = ')))' |
|
74 | 73 | |
|
75 | 74 | texenv.comment_start_string = '((=' |
|
76 | 75 | texenv.comment_end_string = '=))' |
|
77 | 76 | |
|
78 | 77 | texenv.filters['escape_tex'] = escape_tex |
|
79 | 78 | |
|
80 | 79 | #----------------------------------------------------------------------------- |
|
81 | 80 | # Class declarations |
|
82 | 81 | #----------------------------------------------------------------------------- |
|
83 | 82 | class ConversionException(Exception): |
|
84 | 83 | pass |
|
85 | 84 | |
|
86 | 85 | class ConverterTemplate(Configurable): |
|
87 | 86 | """ A Jinja2 base converter templates |
|
88 | 87 | |
|
89 | 88 | Preprocess the ipynb files, feed it throug jinja templates, |
|
90 | 89 | and spit an converted files and a data object with other data |
|
91 | 90 | |
|
92 | 91 | shoudl be mostly configurable |
|
93 | 92 | """ |
|
94 | 93 | |
|
95 | 94 | pre_transformer_order = List(['haspyout_transformer'], |
|
96 | 95 | config=True, |
|
97 | 96 | help= """ |
|
98 | 97 | An ordered list of pre transformer to apply to the ipynb |
|
99 | 98 | file befor running through templates |
|
100 | 99 | """ |
|
101 | 100 | ) |
|
102 | 101 | |
|
103 | 102 | tex_environement = Bool(False, |
|
104 | 103 | config=True, |
|
105 | 104 | help=""" is this a tex environment or not """) |
|
106 | 105 | |
|
107 | 106 | template_file = Unicode('', |
|
108 | 107 | config=True, |
|
109 | 108 | help=""" Name of the template file to use """ ) |
|
110 | 109 | #------------------------------------------------------------------------- |
|
111 | 110 | # Instance-level attributes that are set in the constructor for this |
|
112 | 111 | # class. |
|
113 | 112 | #------------------------------------------------------------------------- |
|
114 | 113 | |
|
115 | 114 | |
|
116 | 115 | preprocessors = [] |
|
117 | 116 | |
|
118 | 117 | def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw): |
|
119 | 118 | """ Init a new converter. |
|
120 | 119 | |
|
121 | 120 | config: the Configurable config object to pass around. |
|
122 | 121 | |
|
123 | 122 | preprocessors: dict of **availlable** key/value function to run on |
|
124 | 123 | ipynb json data before conversion to extract/inline file. |
|
125 | 124 | See `transformer.py` and `ConfigurableTransformers` |
|
126 | 125 | |
|
127 | 126 | set the order in which the transformers should apply |
|
128 | 127 | with the `pre_transformer_order` trait of this class |
|
129 | 128 | |
|
130 | 129 | transformers registerd by this key will take precedence on |
|
131 | 130 | default one. |
|
132 | 131 | |
|
133 | 132 | |
|
134 | 133 | jinja_filters: dict of supplementary jinja filter that should be made |
|
135 | 134 | availlable in template. If those are of Configurable Class type, |
|
136 | 135 | they will be instanciated with the config object as argument. |
|
137 | 136 | |
|
138 | 137 | user defined filter will overwrite the one availlable by default. |
|
139 | 138 | """ |
|
140 | 139 | super(ConverterTemplate, self).__init__(config=config, **kw) |
|
141 | 140 | |
|
142 | 141 | # variable parameters depending on the pype of jinja environement |
|
143 | 142 | self.env = texenv if self.tex_environement else env |
|
144 | 143 | self.ext = '.tplx' if self.tex_environement else '.tpl' |
|
145 | 144 | |
|
146 | 145 | for name in self.pre_transformer_order: |
|
147 | 146 | # get the user-defined transformer first |
|
148 | 147 | transformer = getattr(preprocessors, name, getattr(trans, name, None)) |
|
149 | 148 | if isinstance(transformer, MetaHasTraits): |
|
150 | 149 | transformer = transformer(config=config) |
|
151 | 150 | self.preprocessors.append(transformer) |
|
152 | 151 | |
|
153 | 152 | ## for compat, remove later |
|
154 | 153 | self.preprocessors.append(trans.coalesce_streams) |
|
155 | 154 | self.preprocessors.append(trans.ExtractFigureTransformer(config=config)) |
|
156 | 155 | self.preprocessors.append(trans.RevealHelpTransformer(config=config)) |
|
157 | 156 | self.preprocessors.append(trans.CSSHtmlHeaderTransformer(config=config)) |
|
158 | 157 | |
|
159 | 158 | ## |
|
160 | 159 | self.env.filters['filter_data_type'] = FilterDataType(config=config) |
|
161 | 160 | self.env.filters['pycomment'] = python_comment |
|
162 | 161 | self.env.filters['indent'] = indent |
|
163 | 162 | self.env.filters['rm_fake'] = rm_fake |
|
164 | 163 | self.env.filters['rm_ansi'] = remove_ansi |
|
165 | 164 | self.env.filters['markdown'] = markdown |
|
166 | 165 | self.env.filters['highlight'] = highlight |
|
167 | 166 | self.env.filters['ansi2html'] = ansi2html |
|
168 | 167 | self.env.filters['markdown2latex'] = markdown2latex |
|
169 | 168 | self.env.filters['markdown2rst'] = markdown2rst |
|
170 | 169 | |
|
171 | 170 | ## user filter will overwrite |
|
172 | 171 | for key, filtr in jinja_filters.iteritems(): |
|
173 | 172 | if isinstance(filtr, MetaHasTraits): |
|
174 | 173 | self.env.filters[key] = filtr(config=config) |
|
175 | 174 | else : |
|
176 | 175 | self.env.filters[key] = filtr |
|
177 | 176 | |
|
178 | 177 | self.template = self.env.get_template(self.template_file+self.ext) |
|
179 | 178 | |
|
180 | 179 | |
|
181 | 180 | def process(self, nb): |
|
182 | 181 | """ |
|
183 | 182 | preprocess the notebook json for easier use with the templates. |
|
184 | 183 | will call all the `preprocessor`s in order before returning it. |
|
185 | 184 | """ |
|
186 | 185 | |
|
187 | 186 | # dict of 'resources' that could be made by the preprocessors |
|
188 | 187 | # like key/value data to extract files from ipynb like in latex conversion |
|
189 | 188 | resources = {} |
|
190 | 189 | |
|
191 | 190 | for preprocessor in self.preprocessors: |
|
192 | 191 | nb, resources = preprocessor(nb, resources) |
|
193 | 192 | |
|
194 | 193 | return nb, resources |
|
195 | 194 | |
|
196 | 195 | def convert(self, nb): |
|
197 | 196 | """ convert the ipynb file |
|
198 | 197 | |
|
199 | 198 | return both the converted ipynb file and a dict containing potential |
|
200 | 199 | other resources |
|
201 | 200 | """ |
|
202 | 201 | nb, resources = self.process(nb) |
|
203 | 202 | return self.template.render(nb=nb, resources=resources), resources |
|
204 | 203 | |
|
205 | 204 | |
|
206 | 205 | def from_filename(self, filename): |
|
207 | 206 | """read and convert a notebook from a file name""" |
|
208 | 207 | with io.open(filename) as f: |
|
209 | 208 | return self.convert(nbformat.read(f, 'json')) |
|
210 | 209 | |
|
211 | 210 | def from_file(self, filelike): |
|
212 | 211 | """read and convert a notebook from a filelike object |
|
213 | 212 | |
|
214 | 213 | filelike object will just be "read" and should be json format.. |
|
215 | 214 | """ |
|
216 | 215 | return self.convert(nbformat.read(filelike, 'json')) |
|
217 | 216 | |
|
218 | 217 | def from_json(self, json): |
|
219 | 218 | """ not implemented |
|
220 | 219 | |
|
221 | 220 | Should convert from a json object |
|
222 | 221 | """ |
|
223 | 222 | raise NotImplementedError('not implemented (yet?)') |
|
224 | 223 |
|
1 | NO CONTENT: modified file |
General Comments 0
You need to be logged in to leave comments.
Login now