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

Side by Side Diff: cms/utils.py

Issue 29328184: Issue 3092 - Support use of Jinja2 template inheritance (Closed)
Patch Set: Created Sept. 18, 2015, 11:06 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« cms/converters.py ('K') | « cms/converters.py ('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,
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 re
19
18 from cms.converters import converters, TemplateConverter 20 from cms.converters import converters, TemplateConverter
19 21
20 def get_page_params(source, locale, page, format=None, site_url_override=None, 22 def get_page_params(source, locale, page, format=None, site_url_override=None,
21 localized_string_callback=None): 23 localized_string_callback=None):
22 # Guess page format if omitted, but default to Markdown for friendlier excepti ons 24 # Guess page format if omitted, but default to Markdown for friendlier excepti ons
23 if format is None: 25 if format is None:
24 for format in converters.iterkeys(): 26 for format in converters.iterkeys():
25 if source.has_page(page, format): 27 if source.has_page(page, format):
26 break 28 break
27 else: 29 else:
28 format = "md" 30 format = "md"
29 31
30 params = { 32 params = {
31 "source": source, 33 "source": source,
32 "template": "default", 34 "template": "default",
33 "locale": locale, 35 "locale": locale,
34 "page": page, 36 "page": page,
35 "pagedata": source.read_page(page, format), 37 "pagedata": source.read_page(page, format),
36 "config": source.read_config(), 38 "config": source.read_config(),
37 "localized_string_callback": localized_string_callback, 39 "localized_string_callback": localized_string_callback,
40 "jinja2_inheritence": False
38 } 41 }
39 42
40 localefile = page 43 localefile = page
41 if params["config"].has_option("locale_overrides", page): 44 if params["config"].has_option("locale_overrides", page):
42 localefile = params["config"].get("locale_overrides", page) 45 localefile = params["config"].get("locale_overrides", page)
43 params["localedata"] = source.read_locale(params["locale"], localefile) 46 params["localedata"] = source.read_locale(params["locale"], localefile)
44 47
45 if params["config"].has_option("general", "siteurl"): 48 if params["config"].has_option("general", "siteurl"):
46 if site_url_override: 49 if site_url_override:
47 params["site_url"] = site_url_override 50 params["site_url"] = site_url_override
48 else: 51 else:
49 params["site_url"] = params["config"].get("general", "siteurl") 52 params["site_url"] = params["config"].get("general", "siteurl")
50 53
51 try: 54 try:
52 converter = converters[format](params) 55 converter = converters[format](params)
53 except KeyError: 56 except KeyError:
54 raise Exception("Page %s uses unknown format %s" % (page, format)) 57 raise Exception("Page %s uses unknown format %s" % (page, format))
55 58
56 # Note: The converter might change some parameters so we can only read in 59 if format == "tmpl" and re.search(r"{%\s*extends.+?%}", params["pagedata"][0]) :
57 # template data here. 60 params["jinja2_inheritence"] = True
58 params["templatedata"] = source.read_template(params["template"]) 61 else:
62 # Note: The converter might change some parameters so we can only read in
63 # template data here.
64 params["templatedata"] = source.read_template(params["template"])
59 65
60 defaultlocale = params["config"].get("general", "defaultlocale") 66 defaultlocale = params["config"].get("general", "defaultlocale")
61 params["defaultlocale"] = defaultlocale 67 params["defaultlocale"] = defaultlocale
62 68
63 locales = [ 69 locales = [
64 l 70 l
65 for l in source.list_locales() 71 for l in source.list_locales()
66 if source.has_locale(l, localefile) 72 if source.has_locale(l, localefile)
67 ] 73 ]
68 if defaultlocale not in locales: 74 if defaultlocale not in locales:
69 locales.append(defaultlocale) 75 locales.append(defaultlocale)
70 locales.sort() 76 locales.sort()
71 params["available_locales"] = locales 77 params["available_locales"] = locales
72 78
73 params["head"], params["body"] = converter() 79 if params["jinja2_inheritence"]:
80 params["body"] = converter()
81 else:
82 params["head"], params["body"] = converter()
83
74 if converter.total_translations > 0: 84 if converter.total_translations > 0:
75 params["translation_ratio"] = (1 - 85 params["translation_ratio"] = (1 -
76 float(converter.missing_translations) / converter.total_translations) 86 float(converter.missing_translations) / converter.total_translations)
77 else: 87 else:
78 params["translation_ratio"] = 1 88 params["translation_ratio"] = 1
79 89
80 return params 90 return params
81 91
82 def process_page(source, locale, page, format=None, site_url_override=None, 92 def process_page(source, locale, page, format=None, site_url_override=None,
83 localized_string_callback=None): 93 localized_string_callback=None):
84 return TemplateConverter( 94 page_params = get_page_params(source, locale, page, format,
85 get_page_params(source, locale, page, format, 95 site_url_override, localized_string_callback)
86 site_url_override, localized_string_callback), 96
87 key="templatedata" 97 if page_params["jinja2_inheritence"]:
88 )() 98 return page_params["body"]
99 else:
100 return TemplateConverter(
101 page_params,
102 key="templatedata"
103 )()
OLDNEW
« cms/converters.py ('K') | « cms/converters.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld