##// END OF EJS Templates
allow configurable filters
Matthias BUSSONNIER -
Show More
@@ -16,9 +16,55 b' from markdown import markdown'
16 from .utils import remove_ansi
16 from .utils import remove_ansi
17 from .utils import highlight, ansi2html
17 from .utils import highlight, ansi2html
18 from .utils import markdown2latex
18 from .utils import markdown2latex
19
20 from IPython.config.configurable import Configurable
21 from IPython.utils.traitlets import Unicode, Bool, Dict, List
22
19 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
20 # Class declarations
24 # Class declarations
21 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
27 class GlobalConfigurable(Configurable):
28 """Global configurable class for shared config
29
30 Usefull for display data priority that might be use by many trasnformers
31 """
32
33 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
34 config=True,
35 help= """
36 An ordered list of prefered output type, the first
37 encounterd will usually be used when converting discarding
38 the others.
39 """
40 )
41
42 def __init__(self, config=None, **kw):
43 super(GlobalConfigurable, self).__init__( config=config, **kw)
44
45 class ConfigurableFilter(GlobalConfigurable):
46 """Configurable Jinja Filter"""
47
48 def __init__(self, config=None, **kw):
49 super(ConfigurableFilter, self).__init__(config=config, **kw)
50
51 def __call__(self, *args, **kwargs):
52 raise NotImplementedError('should be implemented by subclass')
53
54
55 class FilterDataType(ConfigurableFilter):
56 """ return the preferd displayed format
57 """
58
59 def __call__(self, output):
60 """ return the first availlable format in priority """
61 for fmt in self.display_data_priority:
62 if fmt in output:
63 return [fmt]
64 raise Exception("did not found any format I can extract in output, shoudl at lest have one")
65
66
67
22 def rm_fake(strng):
68 def rm_fake(strng):
23 return strng.replace('/files/', '')
69 return strng.replace('/files/', '')
24
70
@@ -19,7 +19,7 b' from __future__ import print_function, absolute_import'
19 import converters.transformers as trans
19 import converters.transformers as trans
20 from converters.jinja_filters import (python_comment, indent,
20 from converters.jinja_filters import (python_comment, indent,
21 rm_fake, remove_ansi, markdown, highlight,
21 rm_fake, remove_ansi, markdown, highlight,
22 ansi2html, markdown2latex, escape_tex)
22 ansi2html, markdown2latex, escape_tex, FilterDataType)
23
23
24 from converters.utils import markdown2rst
24 from converters.utils import markdown2rst
25
25
@@ -80,13 +80,17 b' def header_body():'
80 os.path.join(css, 'fbm.css'),
80 os.path.join(css, 'fbm.css'),
81 os.path.join(css, 'notebook.css'),
81 os.path.join(css, 'notebook.css'),
82 os.path.join(css, 'renderedhtml.css'),
82 os.path.join(css, 'renderedhtml.css'),
83 os.path.join(css, 'style.min.css'),
83 # our overrides:
84 # our overrides:
84 os.path.join(here, '..', 'css', 'static_html.css'),
85 os.path.join(here, '..', 'css', 'static_html.css'),
85 ]:
86 ]:
86
87 try:
87 with io.open(sheet, encoding='utf-8') as f:
88 with io.open(sheet, encoding='utf-8') as f:
88 s = f.read()
89 s = f.read()
89 header.append(s)
90 header.append(s)
91 except IOError:
92 # new version of ipython with style.min.css, pass
93 pass
90
94
91 pygments_css = HtmlFormatter().get_style_defs('.highlight')
95 pygments_css = HtmlFormatter().get_style_defs('.highlight')
92 header.append(pygments_css)
96 header.append(pygments_css)
@@ -103,10 +107,13 b" inlining['css'] = header_body()"
103
107
104 texenv.block_start_string = '((*'
108 texenv.block_start_string = '((*'
105 texenv.block_end_string = '*))'
109 texenv.block_end_string = '*))'
110
106 texenv.variable_start_string = '((('
111 texenv.variable_start_string = '((('
107 texenv.variable_end_string = ')))'
112 texenv.variable_end_string = ')))'
113
108 texenv.comment_start_string = '((='
114 texenv.comment_start_string = '((='
109 texenv.comment_end_string = '=))'
115 texenv.comment_end_string = '=))'
116
110 texenv.filters['escape_tex'] = escape_tex
117 texenv.filters['escape_tex'] = escape_tex
111
118
112
119
@@ -119,15 +126,6 b' class ConverterTemplate(Configurable):'
119 shoudl be mostly configurable
126 shoudl be mostly configurable
120 """
127 """
121
128
122 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
123 config=True,
124 help= """
125 An ordered list of prefered output type, the first
126 encounterd will usually be used when converting discarding
127 the others.
128 """
129 )
130
131 pre_transformer_order = List(['haspyout_transformer'],
129 pre_transformer_order = List(['haspyout_transformer'],
132 config=True,
130 config=True,
133 help= """
131 help= """
@@ -136,14 +134,6 b' class ConverterTemplate(Configurable):'
136 """
134 """
137 )
135 )
138
136
139 extract_figures = Bool(False,
140 config=True,
141 help= """
142 wether to remove figure data from ipynb and store them in auxiliary
143 dictionnary
144 """
145 )
146
147 tex_environement = Bool(False,
137 tex_environement = Bool(False,
148 config=True,
138 config=True,
149 help=""" is this a tex environment or not """)
139 help=""" is this a tex environment or not """)
@@ -155,27 +145,22 b' class ConverterTemplate(Configurable):'
155 # Instance-level attributes that are set in the constructor for this
145 # Instance-level attributes that are set in the constructor for this
156 # class.
146 # class.
157 #-------------------------------------------------------------------------
147 #-------------------------------------------------------------------------
158 infile = Any()
159
160
161 infile_dir = Unicode()
162
148
163 #todo: move to filter
164 def filter_data_type(self, output):
165 """ return the first availlable format in priority """
166 for fmt in self.display_data_priority:
167 if fmt in output:
168 return [fmt]
169 raise Exception("did not found any format I can extract in output, shoudl at lest have one")
170
149
171 preprocessors = []
150 preprocessors = []
172
151
173 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
152 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
174 """
153 """ Init a new converter.
175 config: the Configurable confg object to pass around
154
155
156 config: the Configurable confgg object to pass around
176
157
177 preprocessors: dict of **availlable** key/value function to run on ipynb json data before conversion
158 preprocessors: dict of **availlable** key/value function to run on
178 to extract/inline file,
159 ipynb json data before conversion to extract/inline file,
160
161 jinja_filter : dict of supplementary jinja filter that should be made
162 availlable in template. If those are of Configurable Class type, they
163 will be instanciated with the config object as argument.
179
164
180 """
165 """
181 super(ConverterTemplate, self).__init__(config=config, **kw)
166 super(ConverterTemplate, self).__init__(config=config, **kw)
@@ -189,12 +174,11 b' class ConverterTemplate(Configurable):'
189 self.preprocessors.append(transformer)
174 self.preprocessors.append(transformer)
190
175
191 ## for compat, remove later
176 ## for compat, remove later
192 if self.extract_figures:
177 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
193 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
194 self.preprocessors.append(trans.RevealHelpTransformer(config=config))
178 self.preprocessors.append(trans.RevealHelpTransformer(config=config))
195
179
196 ##
180 ##
197 self.env.filters['filter_data_type'] = self.filter_data_type
181 self.env.filters['filter_data_type'] = FilterDataType(config=config)
198 self.env.filters['pycomment'] = python_comment
182 self.env.filters['pycomment'] = python_comment
199 self.env.filters['indent'] = indent
183 self.env.filters['indent'] = indent
200 self.env.filters['rm_fake'] = rm_fake
184 self.env.filters['rm_fake'] = rm_fake
@@ -204,8 +188,11 b' class ConverterTemplate(Configurable):'
204 self.env.filters['ansi2html'] = ansi2html
188 self.env.filters['ansi2html'] = ansi2html
205 self.env.filters['markdown2latex'] = markdown2latex
189 self.env.filters['markdown2latex'] = markdown2latex
206 self.env.filters['markdown2rst'] = markdown2rst
190 self.env.filters['markdown2rst'] = markdown2rst
207 for k, v in jinja_filters.iteritems():
191 for key, filtr in jinja_filters.iteritems():
208 self.env.filters[k] = v
192 if isinstance(filtr, MetaHasTraits):
193 self.env.filters[key] = filtr(config=config)
194 else :
195 self.env.filters[key] = filtr
209
196
210 self.template = self.env.get_template(self.template_file+self.ext)
197 self.template = self.env.get_template(self.template_file+self.ext)
211
198
@@ -240,3 +227,4 b' class ConverterTemplate(Configurable):'
240 with io.open(filename) as f:
227 with io.open(filename) as f:
241 return self.convert(nbformat.read(f, 'json'))
228 return self.convert(nbformat.read(f, 'json'))
242
229
230
@@ -2,12 +2,13 b''
2
2
3 """
3 """
4
4
5 from __future__ import print_function
5 from __future__ import print_function, absolute_import
6
6
7 from IPython.config.configurable import Configurable
7 from IPython.config.configurable import Configurable
8 from IPython.utils.traitlets import Unicode, Bool, Dict, List
8 from IPython.utils.traitlets import Unicode, Bool, Dict, List
9 from .jinja_filters import GlobalConfigurable
9
10
10 class ConfigurableTransformers(Configurable):
11 class ConfigurableTransformers(GlobalConfigurable):
11 """ A configurable transformer """
12 """ A configurable transformer """
12
13
13 def __init__(self, config=None, **kw):
14 def __init__(self, config=None, **kw):
@@ -84,15 +85,6 b' class ExtractFigureTransformer(ActivatableTransformer):'
84 Usefull for latex where svg will be converted to pdf before inclusion
85 Usefull for latex where svg will be converted to pdf before inclusion
85 """
86 """
86 )
87 )
87 display_data_priority = List(['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text'],
88 config=True,
89 help= """
90 An ordered list of prefered output type, the first
91 encounterd will usually be used when converting discarding
92 the others.
93 """
94 )
95
96
88
97 #to do change this to .format {} syntax
89 #to do change this to .format {} syntax
98 key_tpl = Unicode('_fig_%02i.%s', config=True)
90 key_tpl = Unicode('_fig_%02i.%s', config=True)
@@ -32,6 +32,8 b' from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStr'
32
32
33 from converters.transformers import (ConfigurableTransformers,ExtractFigureTransformer)
33 from converters.transformers import (ConfigurableTransformers,ExtractFigureTransformer)
34
34
35 from converters.jinja_filters import GlobalConfigurable
36
35
37
36 class NbconvertApp(Application):
38 class NbconvertApp(Application):
37
39
@@ -58,6 +60,7 b' class NbconvertApp(Application):'
58 self.classes.insert(0,ConverterTemplate)
60 self.classes.insert(0,ConverterTemplate)
59 # register class here to have help with help all
61 # register class here to have help with help all
60 self.classes.insert(0,ExtractFigureTransformer)
62 self.classes.insert(0,ExtractFigureTransformer)
63 self.classes.insert(0,GlobalConfigurable)
61 # ensure those are registerd
64 # ensure those are registerd
62
65
63 def load_config_file(self, profile_name):
66 def load_config_file(self, profile_name):
General Comments 0
You need to be logged in to leave comments. Login now