Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-present eyeo GmbH | 2 # Copyright (C) 2006-present 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 |
11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
12 # | 12 # |
13 # You should have received a copy of the GNU General Public License | 13 # You should have received a copy of the GNU General Public License |
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
15 | 15 |
16 import re | 16 import re |
17 | 17 |
18 __all__ = [ | 18 __all__ = [ |
19 'get_page_params', | 19 'get_page_params', |
20 'process_page', | 20 'process_page', |
21 'split_head_body', | 21 'split_head_body', |
22 'extract_page_metadata' | 22 'extract_page_metadata' |
23 ] | 23 ] |
24 | 24 |
25 | 25 |
26 def split_head_body(html): | 26 def split_head_body(html): |
Vasily Kuznetsov
2017/10/27 18:35:01
Maybe this function should be called `decapitate`.
mathias
2017/10/30 15:37:00
Funny, but no. The name is perfect, there's nothin
| |
27 """Split HTML page into head and remaining content. | 27 """Split HTML page into head and remaining content. |
28 | 28 |
29 This is used to pass head and body of the page to the template as two | 29 This is used to pass head and body of the page to the template as two |
30 separate variables. | 30 separate variables. |
31 | 31 |
32 Parameters | 32 Parameters |
33 ---------- | 33 ---------- |
34 html: str | 34 html: str |
35 Source HTML to split. | 35 Source HTML to split. |
36 | 36 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 value = value.strip() | 77 value = value.strip() |
78 if value.startswith('[') and value.endswith(']'): | 78 if value.startswith('[') and value.endswith(']'): |
79 value = [element.strip() for element in value[1:-1].split(',')] | 79 value = [element.strip() for element in value[1:-1].split(',')] |
80 lines[i] = '\n' | 80 lines[i] = '\n' |
81 metadata[name.strip()] = value | 81 metadata[name.strip()] = value |
82 return metadata, ''.join(lines) | 82 return metadata, ''.join(lines) |
83 | 83 |
84 | 84 |
85 def get_page_params(source, locale, page, format=None, site_url_override=None, | 85 def get_page_params(source, locale, page, format=None, site_url_override=None, |
86 localized_string_callback=None): | 86 localized_string_callback=None): |
87 from cms.converters import converters | 87 from cms.converters import converters |
Vasily Kuznetsov
2017/10/27 18:35:02
It makes more sense to do contrived imports on thi
mathias
2017/10/30 15:37:00
Acknowledged.
| |
88 | 88 |
89 # Guess page format if omitted, but default to Markdown for friendlier excep tions | 89 # Guess page format if omitted, but default to Markdown for friendlier excep tions |
90 if format is None: | 90 if format is None: |
91 for format in converters.iterkeys(): | 91 for format in converters.iterkeys(): |
92 if source.has_page(page, format): | 92 if source.has_page(page, format): |
93 break | 93 break |
94 else: | 94 else: |
95 format = 'md' | 95 format = 'md' |
96 | 96 |
97 params = { | 97 params = { |
98 'source': source, | 98 'source': source, |
99 'template': 'default', | 99 'template': 'default', |
100 'locale': locale, | 100 'locale': locale, |
101 'page': page, | 101 'page': page, |
102 'config': source.read_config(), | 102 'config': source.read_config(), |
103 'localized_string_callback': localized_string_callback, | 103 'localized_string_callback': localized_string_callback, |
104 } | 104 } |
105 | 105 |
106 localefile = page | 106 localefile = page |
107 if params['config'].has_option('locale_overrides', page): | 107 if params['config'].has_option('locale_overrides', page): |
108 localefile = params['config'].get('locale_overrides', page) | 108 localefile = params['config'].get('locale_overrides', page) |
109 params['localedata'] = source.read_locale(params['locale'], localefile) | 109 params['localedata'] = source.read_locale(params['locale'], localefile) |
110 | 110 |
111 if params['config'].has_option('general', 'siteurl'): | 111 if params['config'].has_option('general', 'siteurl'): |
112 if site_url_override: | 112 if site_url_override: |
113 params['site_url'] = site_url_override | 113 params['site_url'] = site_url_override |
114 else: | 114 else: |
115 params['site_url'] = params['config'].get('general', 'siteurl') | 115 params['site_url'] = params['config'].get('general', 'siteurl') |
116 | 116 |
117 data, filename = source.read_page(page, format) | 117 data, filename = source.read_page(page, format) |
Vasily Kuznetsov
2017/10/27 18:35:01
This was done in the constructor of `Converter` be
mathias
2017/10/30 15:37:00
Acknowledged.
| |
118 metadata, body = extract_page_metadata(data) | 118 metadata, body = extract_page_metadata(data) |
119 params['pagedata'] = body, filename | 119 params['pagedata'] = body, filename |
120 params.update(metadata) | 120 params.update(metadata) |
121 | 121 |
122 params['templatedata'] = source.read_template(params['template']) | 122 params['templatedata'] = source.read_template(params['template']) |
123 | 123 |
124 defaultlocale = params['config'].get('general', 'defaultlocale') | 124 defaultlocale = params['config'].get('general', 'defaultlocale') |
125 params['defaultlocale'] = defaultlocale | 125 params['defaultlocale'] = defaultlocale |
126 | 126 |
127 locales = [ | 127 locales = [ |
128 l | 128 l |
129 for l in source.list_locales() | 129 for l in source.list_locales() |
130 if source.has_locale(l, localefile) | 130 if source.has_locale(l, localefile) |
131 ] | 131 ] |
132 if defaultlocale not in locales: | 132 if defaultlocale not in locales: |
133 locales.append(defaultlocale) | 133 locales.append(defaultlocale) |
134 locales.sort() | 134 locales.sort() |
135 params['available_locales'] = locales | 135 params['available_locales'] = locales |
136 | 136 |
137 try: | 137 try: |
Vasily Kuznetsov
2017/10/27 18:35:02
Lines 137-143 should probably be in `converters.py
mathias
2017/10/30 15:37:00
Acknowledged.
| |
138 converter_class = converters[format] | 138 converter_class = converters[format] |
139 converter = converter_class(body, filename, params) | |
mathias
2017/10/30 15:37:00
This line should not be part of this try-block any
Vasily Kuznetsov
2017/11/07 17:08:29
Done.
| |
140 except KeyError: | 139 except KeyError: |
141 raise Exception('Page %s uses unknown format %s' % (page, format)) | 140 raise Exception('Page %s uses unknown format %s' % (page, format)) |
142 | 141 |
142 converter = converter_class(body, filename, params) | |
143 converted = converter() | 143 converted = converter() |
144 params['head'], params['body'] = split_head_body(converted) | 144 params['head'], params['body'] = split_head_body(converted) |
145 | 145 |
146 if converter.total_translations > 0: | 146 if converter.total_translations > 0: |
147 params['translation_ratio'] = ( | 147 params['translation_ratio'] = ( |
148 1 - float(converter.missing_translations) / converter.total_translat ions | 148 1 - float(converter.missing_translations) / converter.total_translat ions |
149 ) | 149 ) |
150 else: | 150 else: |
151 params['translation_ratio'] = 1 | 151 params['translation_ratio'] = 1 |
152 | 152 |
153 return params | 153 return params |
154 | 154 |
155 | 155 |
156 def process_page(source, locale, page, format=None, site_url_override=None, | 156 def process_page(source, locale, page, format=None, site_url_override=None, |
157 localized_string_callback=None): | 157 localized_string_callback=None): |
158 from cms.converters import TemplateConverter | 158 from cms.converters import TemplateConverter |
159 | 159 |
160 params = get_page_params(source, locale, page, format, site_url_override, | 160 params = get_page_params(source, locale, page, format, site_url_override, |
161 localized_string_callback) | 161 localized_string_callback) |
162 return TemplateConverter(*params['templatedata'], params=params)() | 162 return TemplateConverter(*params['templatedata'], params=params)() |
LEFT | RIGHT |