##// END OF EJS Templates
allow configurable filters
Matthias BUSSONNIER -
Show More
@@ -16,9 +16,55 b' from markdown import markdown'
16 16 from .utils import remove_ansi
17 17 from .utils import highlight, ansi2html
18 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 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 68 def rm_fake(strng):
23 69 return strng.replace('/files/', '')
24 70
@@ -19,7 +19,7 b' from __future__ import print_function, absolute_import'
19 19 import converters.transformers as trans
20 20 from converters.jinja_filters import (python_comment, indent,
21 21 rm_fake, remove_ansi, markdown, highlight,
22 ansi2html, markdown2latex, escape_tex)
22 ansi2html, markdown2latex, escape_tex, FilterDataType)
23 23
24 24 from converters.utils import markdown2rst
25 25
@@ -80,13 +80,17 b' def header_body():'
80 80 os.path.join(css, 'fbm.css'),
81 81 os.path.join(css, 'notebook.css'),
82 82 os.path.join(css, 'renderedhtml.css'),
83 os.path.join(css, 'style.min.css'),
83 84 # our overrides:
84 85 os.path.join(here, '..', 'css', 'static_html.css'),
85 86 ]:
86
87 try:
87 88 with io.open(sheet, encoding='utf-8') as f:
88 89 s = f.read()
89 90 header.append(s)
91 except IOError:
92 # new version of ipython with style.min.css, pass
93 pass
90 94
91 95 pygments_css = HtmlFormatter().get_style_defs('.highlight')
92 96 header.append(pygments_css)
@@ -103,10 +107,13 b" inlining['css'] = header_body()"
103 107
104 108 texenv.block_start_string = '((*'
105 109 texenv.block_end_string = '*))'
110
106 111 texenv.variable_start_string = '((('
107 112 texenv.variable_end_string = ')))'
113
108 114 texenv.comment_start_string = '((='
109 115 texenv.comment_end_string = '=))'
116
110 117 texenv.filters['escape_tex'] = escape_tex
111 118
112 119
@@ -119,15 +126,6 b' class ConverterTemplate(Configurable):'
119 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 129 pre_transformer_order = List(['haspyout_transformer'],
132 130 config=True,
133 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 137 tex_environement = Bool(False,
148 138 config=True,
149 139 help=""" is this a tex environment or not """)
@@ -155,27 +145,22 b' class ConverterTemplate(Configurable):'
155 145 # Instance-level attributes that are set in the constructor for this
156 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 150 preprocessors = []
172 151
173 152 def __init__(self, preprocessors={}, jinja_filters={}, config=None, **kw):
174 """
175 config: the Configurable confg object to pass around
153 """ Init a new converter.
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
178 to extract/inline file,
158 preprocessors: dict of **availlable** key/value function to run on
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 166 super(ConverterTemplate, self).__init__(config=config, **kw)
@@ -189,12 +174,11 b' class ConverterTemplate(Configurable):'
189 174 self.preprocessors.append(transformer)
190 175
191 176 ## for compat, remove later
192 if self.extract_figures:
193 177 self.preprocessors.append(trans.ExtractFigureTransformer(config=config))
194 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 182 self.env.filters['pycomment'] = python_comment
199 183 self.env.filters['indent'] = indent
200 184 self.env.filters['rm_fake'] = rm_fake
@@ -204,8 +188,11 b' class ConverterTemplate(Configurable):'
204 188 self.env.filters['ansi2html'] = ansi2html
205 189 self.env.filters['markdown2latex'] = markdown2latex
206 190 self.env.filters['markdown2rst'] = markdown2rst
207 for k, v in jinja_filters.iteritems():
208 self.env.filters[k] = v
191 for key, filtr in jinja_filters.iteritems():
192 if isinstance(filtr, MetaHasTraits):
193 self.env.filters[key] = filtr(config=config)
194 else :
195 self.env.filters[key] = filtr
209 196
210 197 self.template = self.env.get_template(self.template_file+self.ext)
211 198
@@ -240,3 +227,4 b' class ConverterTemplate(Configurable):'
240 227 with io.open(filename) as f:
241 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 7 from IPython.config.configurable import Configurable
8 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 12 """ A configurable transformer """
12 13
13 14 def __init__(self, config=None, **kw):
@@ -84,15 +85,6 b' class ExtractFigureTransformer(ActivatableTransformer):'
84 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 89 #to do change this to .format {} syntax
98 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 33 from converters.transformers import (ConfigurableTransformers,ExtractFigureTransformer)
34 34
35 from converters.jinja_filters import GlobalConfigurable
36
35 37
36 38 class NbconvertApp(Application):
37 39
@@ -58,6 +60,7 b' class NbconvertApp(Application):'
58 60 self.classes.insert(0,ConverterTemplate)
59 61 # register class here to have help with help all
60 62 self.classes.insert(0,ExtractFigureTransformer)
63 self.classes.insert(0,GlobalConfigurable)
61 64 # ensure those are registerd
62 65
63 66 def load_config_file(self, profile_name):
General Comments 0
You need to be logged in to leave comments. Login now