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

Unified Diff: sitescripts/cms/converters.py

Issue 6439145228468224: Added custom template loader to CMS in order to enable {% include %} and {% import %} in jinja temp… (Closed)
Patch Set: Only allow inlcudes with escaped brackets in markdown Created Dec. 12, 2013, 1:48 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sitescripts/utils.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/cms/converters.py
===================================================================
--- a/sitescripts/cms/converters.py
+++ b/sitescripts/cms/converters.py
@@ -115,6 +115,9 @@
text = re.sub(r"(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")", process_link, text)
return text
+ include_start_regex = '<'
+ include_end_regex = '>'
+
def resolve_includes(self, text):
def resolve_include(match):
global converters
@@ -126,7 +129,14 @@
return converter()
raise Exception("Failed to resolve include %s in page %s" % (name, self._params["page"]))
- return re.sub(r'<\?\s*include\s+([^\s<>"]+)\s*\?>', resolve_include, text)
+ return re.sub(
+ r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % (
+ self.include_start_regex,
+ self.include_end_regex
+ ),
+ resolve_include,
+ text
+ )
def __call__(self):
result = self.get_html(self._params[self._key])
@@ -148,6 +158,15 @@
return result
class MarkdownConverter(Converter):
+ include_start_regex = r'(?:%s|%s)' % (
+ Converter.include_start_regex,
+ re.escape(jinja2.escape(Converter.include_start_regex))
+ )
+ include_end_regex = r'(?:%s|%s)' % (
+ Converter.include_end_regex,
+ re.escape(jinja2.escape(Converter.include_end_regex))
+ )
+
def get_html(self, source):
def remove_unnecessary_entities(match):
char = unichr(int(match.group(1)))
@@ -172,6 +191,16 @@
return result
class TemplateConverter(Converter):
+ class _SourceLoader(jinja2.BaseLoader):
+ def __init__(self, source):
+ self.source = source
+
+ def get_source(self, environment, template):
+ try:
+ return self.source.read_file(template + ".tmpl"), None, None
+ except Exception:
+ raise jinja2.TemplateNotFound(template)
+
def __init__(self, *args, **kwargs):
Converter.__init__(self, *args, **kwargs)
@@ -197,7 +226,7 @@
filters[func] = getattr(module, func)
filters[func].module_ref = module # Prevent garbage collection
- self._env = get_custom_template_environment(filters)
+ self._env = get_custom_template_environment(filters, self._SourceLoader(self._params["source"]))
def get_html(self, source):
template = self._env.from_string(source)
« no previous file with comments | « no previous file | sitescripts/utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld