 Issue 5148261828526080:
  Issue 2119 - Add get_page_content template function.  (Closed)
    
  
    Issue 5148261828526080:
  Issue 2119 - Add get_page_content template function.  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| 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 from .converters import converters, TemplateConverter | 18 from .converters import converters, TemplateConverter | 
| 19 | 19 | 
| 20 def process_page(source, locale, page, format, site_url_override=None): | 20 def get_page_params(source, locale, page, format=None, site_url_override=None): | 
| 21 # Guess page format if omitted, but default to Markdown for friendlier excepti ons | |
| 22 if format is None: | |
| 23 format = "md" | |
| 24 for fmt in converters.keys(): | |
| 25 if source.has_page(page, fmt): | |
| 26 format = fmt | |
| 
Sebastian Noack
2015/04/08 08:38:57
I'd prefer to break when we found a match. Then we
 
kzar
2015/04/08 08:50:20
Cool, I've never seen an `else` clause for a `for`
 | |
| 27 | |
| 21 params = { | 28 params = { | 
| 22 "source": source, | 29 "source": source, | 
| 23 "template": "default", | 30 "template": "default", | 
| 24 "locale": locale, | 31 "locale": locale, | 
| 25 "page": page, | 32 "page": page, | 
| 26 "pagedata": source.read_page(page, format), | 33 "pagedata": source.read_page(page, format), | 
| 27 "config": source.read_config(), | 34 "config": source.read_config(), | 
| 28 } | 35 } | 
| 29 | 36 | 
| 30 localefile = page | 37 localefile = page | 
| 31 if params["config"].has_option("locale_overrides", page): | 38 if params["config"].has_option("locale_overrides", page): | 
| 32 localefile = params["config"].get("locale_overrides", page) | 39 localefile = params["config"].get("locale_overrides", page) | 
| 33 params["localedata"] = source.read_locale(params["locale"], localefile) | 40 params["localedata"] = source.read_locale(params["locale"], localefile) | 
| 34 | 41 | 
| 35 if params["config"].has_option("general", "siteurl"): | 42 if params["config"].has_option("general", "siteurl"): | 
| 36 if site_url_override: | 43 if site_url_override: | 
| 37 params["site_url"] = site_url_override | 44 params["site_url"] = site_url_override | 
| 38 else: | 45 else: | 
| 39 params["site_url"] = params["config"].get("general", "siteurl") | 46 params["site_url"] = params["config"].get("general", "siteurl") | 
| 40 | 47 | 
| 41 try: | 48 try: | 
| 42 converter = converters[format](params) | 49 converter = converters[format](params) | 
| 43 except KeyError: | 50 except KeyError: | 
| 44 raise Exception("Page %s uses unknown format %s" % (page, format)) | 51 raise Exception("Page %s uses unknown format %s" % (page, format)) | 
| 45 | 52 | 
| 46 # Note: The converter might change some parameters so we can only read in | 53 # Note: The converter might change some parameters so we can only read in | 
| 47 # template data here. | 54 # template data here. | 
| 48 params["templatedata"] = source.read_template(params["template"]) | 55 params["templatedata"] = source.read_template(params["template"]) | 
| 49 template_converter = TemplateConverter(params, key="templatedata") | |
| 50 | 56 | 
| 51 defaultlocale = params["config"].get("general", "defaultlocale") | 57 defaultlocale = params["config"].get("general", "defaultlocale") | 
| 52 params["defaultlocale"] = defaultlocale | 58 params["defaultlocale"] = defaultlocale | 
| 53 | 59 | 
| 54 locales = [ | 60 locales = [ | 
| 55 locale | 61 locale | 
| 56 for locale in source.list_locales() | 62 for locale in source.list_locales() | 
| 57 if source.has_locale(locale, localefile) | 63 if source.has_locale(locale, localefile) | 
| 58 ] | 64 ] | 
| 59 if defaultlocale not in locales: | 65 if defaultlocale not in locales: | 
| 60 locales.append(defaultlocale) | 66 locales.append(defaultlocale) | 
| 61 locales.sort() | 67 locales.sort() | 
| 62 params["available_locales"] = locales | 68 params["available_locales"] = locales | 
| 63 | 69 | 
| 64 params["head"], params["body"] = converter() | 70 params["head"], params["body"] = converter() | 
| 65 return template_converter() | 71 return params | 
| 72 | |
| 73 def process_page(source, locale, page, format, site_url_override=None): | |
| 74 return TemplateConverter( | |
| 75 get_page_params(source, locale, page, format, site_url_override), | |
| 76 key="templatedata" | |
| 77 )() | |
| OLD | NEW |