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

Side by Side Diff: sitescripts/cms/sources.py

Issue 6439145228468224: Added custom template loader to CMS in order to enable {% include %} and {% import %} in jinja temp… (Closed)
Patch Set: Created Dec. 11, 2013, 6:31 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 | « sitescripts/cms/converters.py ('k') | sitescripts/utils.py » ('j') | 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-2013 Eyeo GmbH 4 # Copyright (C) 2006-2013 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 sys, os, subprocess, zipfile, json, urlparse, codecs 18 import sys, os, subprocess, zipfile, json, urlparse, codecs
19 from StringIO import StringIO 19 from StringIO import StringIO
20 from ConfigParser import SafeConfigParser 20 from ConfigParser import SafeConfigParser
21 import jinja2
21 22
22 class Source: 23 class Source:
23 def resolve_link(self, url, locale): 24 def resolve_link(self, url, locale):
24 parsed = urlparse.urlparse(url) 25 parsed = urlparse.urlparse(url)
25 page = parsed.path 26 page = parsed.path
26 if parsed.scheme != "" or page.startswith("/") or page.startswith("."): 27 if parsed.scheme != "" or page.startswith("/") or page.startswith("."):
27 # Not a page link 28 # Not a page link
28 return None, None 29 return None, None
29 30
30 if parsed.path == "" and url != "": 31 if parsed.path == "" and url != "":
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 # Template helpers 160 # Template helpers
160 # 161 #
161 162
162 @staticmethod 163 @staticmethod
163 def template_filename(template): 164 def template_filename(template):
164 return "templates/%s.tmpl" % template 165 return "templates/%s.tmpl" % template
165 166
166 def read_template(self, template): 167 def read_template(self, template):
167 return self.read_file(self.template_filename(template)) 168 return self.read_file(self.template_filename(template))
168 169
170 def get_template_loader(self):
171 return SourceLoader(self)
172
169 # 173 #
170 # Include helpers 174 # Include helpers
171 # 175 #
172 176
173 @staticmethod 177 @staticmethod
174 def include_filename(include, format): 178 def include_filename(include, format):
175 return "includes/%s.%s" % (include, format) 179 return "includes/%s.%s" % (include, format)
176 180
177 def has_include(self, include, format): 181 def has_include(self, include, format):
178 return self.has_file(self.include_filename(include, format)) 182 return self.has_file(self.include_filename(include, format))
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 return 256 return
253 257
254 for filename in files: 258 for filename in files:
255 path = os.path.join(dir, filename) 259 path = os.path.join(dir, filename)
256 if os.path.isfile(path): 260 if os.path.isfile(path):
257 result.append(relpath + filename) 261 result.append(relpath + filename)
258 elif os.path.isdir(path): 262 elif os.path.isdir(path):
259 do_list(path, relpath + filename + "/") 263 do_list(path, relpath + filename + "/")
260 do_list(self.get_path(subdir), "") 264 do_list(self.get_path(subdir), "")
261 return result 265 return result
266
267 class SourceLoader(jinja2.BaseLoader):
Wladimir Palant 2013/12/11 18:46:02 I don't think that Jinja-specific functionality be
268 def __init__(self, source):
269 self.source = source
270
271 def get_source(self, environment, template):
272 try:
273 return self.source.read_file(template + '.tmpl'), None, None
Wladimir Palant 2013/12/11 18:46:02 I think this should be self.source.read_include(te
Sebastian Noack 2013/12/11 22:20:14 I've already addressed that, but I would like to u
Wladimir Palant 2013/12/12 07:16:54 Yes, you are right. We might want to extend the de
274 except Exception:
275 raise jinja2.TemplateNotFound(template)
OLDNEW
« no previous file with comments | « sitescripts/cms/converters.py ('k') | sitescripts/utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld