OLD | NEW |
1 # This file is part of the Adblock Plus web scripts, | 1 # This file is part of the Adblock Plus web scripts, |
2 # Copyright (C) 2006-2017 eyeo GmbH | 2 # Copyright (C) 2006-2017 eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 try: | 139 try: |
140 return email.encode('ascii') | 140 return email.encode('ascii') |
141 except UnicodeEncodeError: | 141 except UnicodeEncodeError: |
142 return '%s@%s' % (match.group(1).encode('ascii'), | 142 return '%s@%s' % (match.group(1).encode('ascii'), |
143 match.group(2).encode('idna')) | 143 match.group(2).encode('idna')) |
144 | 144 |
145 _template_cache = {} | 145 _template_cache = {} |
146 | 146 |
147 | 147 |
148 def get_template(template, autoescape=True, template_path=siteScriptsPath): | 148 def get_template(template, autoescape=True, template_path=siteScriptsPath): |
149 """Parses and returns a Jinja2 template""" | 149 """Load Jinja2 template. |
| 150 |
| 151 If `template` is a relative path, it's looked up inside `template_path`. |
| 152 If it's an absolute path, `template_path` is not used. |
| 153 |
| 154 Note: Each template will only be loaded once (when first requested). After |
| 155 that it will be cached and reused -- any changes on the filesystem will be |
| 156 ignored. |
| 157 """ |
| 158 if os.path.isabs(template): |
| 159 template_path, template = os.path.split(template) |
| 160 template_path = os.path.abspath(template_path) |
150 key = (template_path, template, autoescape) | 161 key = (template_path, template, autoescape) |
151 if not key in _template_cache: | 162 if key not in _template_cache: |
152 if autoescape: | 163 if autoescape: |
153 env = get_template_environment(template_path) | 164 env = get_template_environment(template_path) |
154 else: | 165 else: |
155 env = get_unescaped_template_environment(template_path) | 166 env = get_unescaped_template_environment(template_path) |
156 _template_cache[key] = env.get_template(template) | 167 _template_cache[key] = env.get_template(template) |
157 return _template_cache[key] | 168 return _template_cache[key] |
158 | 169 |
159 | 170 |
160 @cached(float('inf')) | 171 @cached(float('inf')) |
161 def get_template_environment(template_path): | 172 def get_template_environment(template_path): |
(...skipping 26 matching lines...) Expand all Loading... |
188 Returns a custom Jinja2 template environment with additional filters. | 199 Returns a custom Jinja2 template environment with additional filters. |
189 """ | 200 """ |
190 from sitescripts.templateFilters import filters | 201 from sitescripts.templateFilters import filters |
191 import jinja2 | 202 import jinja2 |
192 if not loader: | 203 if not loader: |
193 loader = jinja2.FileSystemLoader(siteScriptsPath) | 204 loader = jinja2.FileSystemLoader(siteScriptsPath) |
194 env = jinja2.Environment(loader=loader, autoescape=True) | 205 env = jinja2.Environment(loader=loader, autoescape=True) |
195 env.filters.update(filters) | 206 env.filters.update(filters) |
196 env.filters.update(additional_filters) | 207 env.filters.update(additional_filters) |
197 return env | 208 return env |
OLD | NEW |