Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: cms/converters.py

Issue 5521995252891648: Issue 2180 - [cms] Add support for custom Jinja2 functions (Closed)
Left Patch Set: Created March 19, 2015, 9:52 p.m.
Right Patch Set: Better comment Created March 20, 2015, 4:42 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « README.md ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus web scripts, 3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2015 Eyeo GmbH 4 # Copyright (C) 2006-2015 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 filters = { 261 filters = {
262 "translate": self.translate, 262 "translate": self.translate,
263 "linkify": self.linkify, 263 "linkify": self.linkify,
264 "toclist": self.toclist, 264 "toclist": self.toclist,
265 } 265 }
266 266
267 globals = { 267 globals = {
268 "get_string": self.get_string, 268 "get_string": self.get_string,
269 } 269 }
270 270
271 for dirname, dictionary in [("filters", filters), ("functions", globals)]: 271 for dirname, dictionary in [("filters", filters), ("globals", globals)]:
Sebastian Noack 2015/03/20 09:49:43 How about calling it "globals" as well to match th
Wladimir Palant 2015/03/20 16:02:24 Strictly speaking - yes, these don't need to be fu
Sebastian Noack 2015/03/20 16:10:54 Instances (with methods) would come to my mind.
272 for filename in self._params["source"].list_files(dirname): 272 for filename in self._params["source"].list_files(dirname):
273 root, ext = os.path.splitext(filename) 273 root, ext = os.path.splitext(filename)
274 if ext.lower() != ".py": 274 if ext.lower() != ".py":
275 continue 275 continue
276 276
277 path = "%s/%s" % (dirname, filename) 277 path = "%s/%s" % (dirname, filename)
278 code = self._params["source"].read_file(path) 278 code = self._params["source"].read_file(path)
279 module = imp.new_module(root.replace("/", ".")) 279 module = imp.new_module(root.replace("/", "."))
280 exec code in module.__dict__ 280 exec code in module.__dict__
281 281
282 func = os.path.basename(root) 282 name = os.path.basename(root)
283 if not hasattr(module, func): 283 if not hasattr(module, name):
284 raise Exception("Expected function %s not found in %s file %s" % (func , dirname, filename)) 284 raise Exception("Expected symbol %s not found in %s file %s" % (name, dirname, filename))
285 dictionary[func] = getattr(module, func) 285 dictionary[name] = getattr(module, name)
286 dictionary[func].module_ref = module # Prevent garbage collection 286
287 287 # HACK: The module we created here can be garbage collected because it
288 self._env = jinja2.Environment( 288 # isn't added to sys.modules. If a function is called and its module is
289 loader=self._SourceLoader(self._params["source"]), 289 # gone it might cause weird errors (imports and module variables
290 extensions=["jinja2.ext.do",], 290 # unavailable). We avoid this situation by explicitly referencing the
291 autoescape=True 291 # module from the function so they can only be garbage collected
292 ) 292 # together.
293 if callable(dictionary[name]):
294 dictionary[name].module_ref = module
295
296 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True)
293 self._env.filters.update(filters) 297 self._env.filters.update(filters)
294 self._env.globals.update(globals) 298 self._env.globals.update(globals)
295 299
296 def get_html(self, source): 300 def get_html(self, source):
297 template = self._env.from_string(source) 301 template = self._env.from_string(source)
298 return template.render(self._params, params=self._params) 302 module = template.make_module(self._params)
303 for key, value in module.__dict__.iteritems():
304 if not key.startswith("_"):
305 self._params[key] = value
306 return unicode(module)
299 307
300 def translate(self, default, name, comment=None): 308 def translate(self, default, name, comment=None):
301 # Note: We currently ignore the comment, it is only relevant when 309 # Note: We currently ignore the comment, it is only relevant when
302 # generating the master translation. 310 # generating the master translation.
303 localedata = self._params["localedata"] 311 localedata = self._params["localedata"]
304 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes)) 312 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes))
305 313
306 def get_string(self, name, page): 314 def get_string(self, name, page):
307 localedata = self._params["source"].read_locale(self._params["locale"], page ) 315 localedata = self._params["source"].read_locale(self._params["locale"], page )
308 default = localedata[name] 316 default = localedata[name]
(...skipping 28 matching lines...) Expand all
337 stack.pop() 345 stack.pop()
338 stack[-1]["subitems"].append(item) 346 stack[-1]["subitems"].append(item)
339 stack.append(item) 347 stack.append(item)
340 return structured 348 return structured
341 349
342 converters = { 350 converters = {
343 "html": RawConverter, 351 "html": RawConverter,
344 "md": MarkdownConverter, 352 "md": MarkdownConverter,
345 "tmpl": TemplateConverter, 353 "tmpl": TemplateConverter,
346 } 354 }
LEFTRIGHT
« README.md ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld