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: Generalized to all globals Created March 20, 2015, 4:01 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 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 name = os.path.basename(root) 282 name = os.path.basename(root)
283 if not hasattr(module, name): 283 if not hasattr(module, name):
284 raise Exception("Expected symbol %s not found in %s file %s" % (name, dirname, filename)) 284 raise Exception("Expected symbol %s not found in %s file %s" % (name, dirname, filename))
285 dictionary[name] = getattr(module, name) 285 dictionary[name] = getattr(module, name)
286 if hasattr(dictionary[name], "__call__"): 286
Sebastian Noack 2015/03/20 16:10:54 You might want to use the callable() built-in func
Wladimir Palant 2015/03/20 16:21:12 A function doesn't usually have a reference to its
Sebastian Noack 2015/03/20 16:26:54 Yeah, this actually make sense, since we create th
Wladimir Palant 2015/03/20 16:42:50 Done.
287 dictionary[name].module_ref = module # Prevent garbage collection 287 # HACK: The module we created here can be garbage collected because it
288 # isn't added to sys.modules. If a function is called and its module is
289 # gone it might cause weird errors (imports and module variables
290 # unavailable). We avoid this situation by explicitly referencing the
291 # module from the function so they can only be garbage collected
292 # together.
293 if callable(dictionary[name]):
294 dictionary[name].module_ref = module
288 295
289 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) 296 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True)
290 self._env.filters.update(filters) 297 self._env.filters.update(filters)
291 self._env.globals.update(globals) 298 self._env.globals.update(globals)
292 299
293 def get_html(self, source): 300 def get_html(self, source):
294 template = self._env.from_string(source) 301 template = self._env.from_string(source)
295 module = template.make_module(self._params) 302 module = template.make_module(self._params)
296 for key, value in module.__dict__.iteritems(): 303 for key, value in module.__dict__.iteritems():
297 if not key.startswith("_"): 304 if not key.startswith("_"):
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 stack.pop() 345 stack.pop()
339 stack[-1]["subitems"].append(item) 346 stack[-1]["subitems"].append(item)
340 stack.append(item) 347 stack.append(item)
341 return structured 348 return structured
342 349
343 converters = { 350 converters = {
344 "html": RawConverter, 351 "html": RawConverter,
345 "md": MarkdownConverter, 352 "md": MarkdownConverter,
346 "tmpl": TemplateConverter, 353 "tmpl": TemplateConverter,
347 } 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