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