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

Side by Side Diff: cms/converters.py

Issue 5703725486178304: Issue 2563 - [cms] Cache filters and globals during page generation (Closed)
Patch Set: Created May 20, 2015, 3:51 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
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,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import os 18 import os
19 import HTMLParser 19 import HTMLParser
20 import imp
21 import re 20 import re
22 21
23 import jinja2 22 import jinja2
24 import markdown 23 import markdown
25 24
26 25
27 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs are 26 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs are
28 # inserted into the <head> tag 27 # inserted into the <head> tag
29 orig_isBlockLevel = markdown.util.isBlockLevel 28 orig_isBlockLevel = markdown.util.isBlockLevel
30 def isBlockLevel(tag): 29 def isBlockLevel(tag):
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 "translate": self.translate, 319 "translate": self.translate,
321 "linkify": self.linkify, 320 "linkify": self.linkify,
322 "toclist": self.toclist, 321 "toclist": self.toclist,
323 } 322 }
324 323
325 globals = { 324 globals = {
326 "get_string": self.get_string, 325 "get_string": self.get_string,
327 "get_page_content": self.get_page_content, 326 "get_page_content": self.get_page_content,
328 } 327 }
329 328
330 self._module_refs = []
331 for dirname, dictionary in [("filters", filters), ("globals", globals)]: 329 for dirname, dictionary in [("filters", filters), ("globals", globals)]:
332 for filename in self._params["source"].list_files(dirname): 330 for filename in self._params["source"].list_files(dirname):
333 root, ext = os.path.splitext(filename) 331 root, ext = os.path.splitext(filename)
334 if ext.lower() != ".py": 332 if ext.lower() != ".py":
335 continue 333 continue
336 334
337 path = "%s/%s" % (dirname, filename) 335 path = "%s/%s" % (dirname, filename)
338 code = self._params["source"].read_file(path)
339 module = imp.new_module(root.replace("/", "."))
340 exec code in module.__dict__
341
342 name = os.path.basename(root) 336 name = os.path.basename(root)
343 if not hasattr(module, name): 337 dictionary[name] = self._params["source"].import_symbol(path, name)
344 raise Exception("Expected symbol %s not found in %s file %s" % (name, dirname, filename))
345 dictionary[name] = getattr(module, name)
346
347 # HACK: The module we created here can be garbage collected because it
348 # isn't added to sys.modules. If a function is called and its module is
349 # gone it might cause weird errors (imports and module variables
350 # unavailable). We avoid this situation by keeping a reference.
351 self._module_refs.append(module)
352 338
353 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) 339 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True)
354 self._env.filters.update(filters) 340 self._env.filters.update(filters)
355 self._env.globals.update(globals) 341 self._env.globals.update(globals)
356 342
357 def get_html(self, source): 343 def get_html(self, source):
358 template = self._env.from_string(source) 344 template = self._env.from_string(source)
359 module = template.make_module(self._params) 345 module = template.make_module(self._params)
360 for key, value in module.__dict__.iteritems(): 346 for key, value in module.__dict__.iteritems():
361 if not key.startswith("_"): 347 if not key.startswith("_"):
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 stack.pop() 398 stack.pop()
413 stack[-1]["subitems"].append(item) 399 stack[-1]["subitems"].append(item)
414 stack.append(item) 400 stack.append(item)
415 return structured 401 return structured
416 402
417 converters = { 403 converters = {
418 "html": RawConverter, 404 "html": RawConverter,
419 "md": MarkdownConverter, 405 "md": MarkdownConverter,
420 "tmpl": TemplateConverter, 406 "tmpl": TemplateConverter,
421 } 407 }
OLDNEW
« no previous file with comments | « cms/bin/generate_static_pages.py ('k') | cms/sources.py » ('j') | cms/sources.py » ('J')

Powered by Google App Engine
This is Rietveld