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

Side by Side Diff: cms/converters.py

Issue 5521995252891648: Issue 2180 - [cms] Add support for custom Jinja2 functions (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « README.md ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 filename in self._params["source"].list_files("filters"): 271 for dirname, dictionary in [("filters", filters), ("globals", globals)]:
272 root, ext = os.path.splitext(filename) 272 for filename in self._params["source"].list_files(dirname):
273 if ext.lower() != ".py": 273 root, ext = os.path.splitext(filename)
274 continue 274 if ext.lower() != ".py":
275 continue
275 276
276 path = "%s/%s" % ("filters", filename) 277 path = "%s/%s" % (dirname, filename)
277 code = self._params["source"].read_file(path) 278 code = self._params["source"].read_file(path)
278 module = imp.new_module(root.replace("/", ".")) 279 module = imp.new_module(root.replace("/", "."))
279 exec code in module.__dict__ 280 exec code in module.__dict__
280 281
281 func = os.path.basename(root) 282 name = os.path.basename(root)
282 if not hasattr(module, func): 283 if not hasattr(module, name):
283 raise Exception("Expected function %s not found in filter file %s" % (fu nc, filename)) 284 raise Exception("Expected symbol %s not found in %s file %s" % (name, dirname, filename))
284 filters[func] = getattr(module, func) 285 dictionary[name] = getattr(module, name)
285 filters[func].module_ref = module # Prevent garbage collection 286
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
286 295
287 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)
288 self._env.filters.update(filters) 297 self._env.filters.update(filters)
289 self._env.globals.update(globals) 298 self._env.globals.update(globals)
290 299
291 def get_html(self, source): 300 def get_html(self, source):
292 template = self._env.from_string(source) 301 template = self._env.from_string(source)
293 module = template.make_module(self._params) 302 module = template.make_module(self._params)
294 for key, value in module.__dict__.iteritems(): 303 for key, value in module.__dict__.iteritems():
295 if not key.startswith("_"): 304 if not key.startswith("_"):
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
336 stack.pop() 345 stack.pop()
337 stack[-1]["subitems"].append(item) 346 stack[-1]["subitems"].append(item)
338 stack.append(item) 347 stack.append(item)
339 return structured 348 return structured
340 349
341 converters = { 350 converters = {
342 "html": RawConverter, 351 "html": RawConverter,
343 "md": MarkdownConverter, 352 "md": MarkdownConverter,
344 "tmpl": TemplateConverter, 353 "tmpl": TemplateConverter,
345 } 354 }
OLDNEW
« no previous file with comments | « README.md ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld