Left: | ||
Right: |
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-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 import json | |
17 | 18 |
18 __all__ = [ | 19 __all__ = [ |
19 'get_page_params', | 20 'get_page_params', |
20 'process_page', | 21 'process_page', |
21 'split_head_body', | 22 'split_head_body', |
22 'extract_page_metadata' | 23 'extract_page_metadata' |
23 ] | 24 ] |
24 | 25 |
25 | 26 |
26 def split_head_body(html): | 27 def split_head_body(html): |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 ---------- | 59 ---------- |
59 source: str | 60 source: str |
60 Source text of the page. | 61 Source text of the page. |
61 | 62 |
62 Returns | 63 Returns |
63 ------- | 64 ------- |
64 (dict, str) | 65 (dict, str) |
65 Metadata of the page, remaining source text without metadata. | 66 Metadata of the page, remaining source text without metadata. |
66 | 67 |
67 """ | 68 """ |
68 metadata = {} | 69 m = re.search(r'^\s*<!--\s*(.*?)-->', source, re.S) |
69 lines = source.splitlines(True) | 70 text = m.group(1) if m else source |
70 for i, line in enumerate(lines): | 71 |
71 if line.strip() in {'<!--', '-->'}: | 72 decoder = json.JSONDecoder() |
72 lines[i] = '' | 73 try: |
73 continue | 74 metadata, length = decoder.raw_decode(text) |
74 if not re.search(r'^\s*[\w\-]+\s*=', line): | 75 except ValueError: |
75 break | 76 metadata = None |
76 name, value = line.split('=', 1) | 77 |
77 value = value.strip() | 78 if not isinstance(metadata, dict): |
78 if value.startswith('[') and value.endswith(']'): | 79 metadata = {} |
79 value = [element.strip() for element in value[1:-1].split(',')] | 80 length = 0 |
80 lines[i] = '\n' | 81 for line in text.splitlines(True): |
81 metadata[name.strip()] = value | 82 if not re.search(r'^\s*[\w\-]+\s*=', line): |
82 return metadata, ''.join(lines) | 83 break |
84 name, value = line.split('=', 1) | |
85 value = value.strip() | |
86 if value.startswith('[') and value.endswith(']'): | |
87 value = [element.strip() for element in value[1:-1].split(',')] | |
88 metadata[name.strip()] = value | |
89 length += len(line) | |
90 | |
91 # Need to preserve line numbers for jinja2 tracebacks | |
92 cutoff = m.end() if m else length | |
93 return metadata, '\n' * source.count('\n', 0, cutoff) + source[cutoff:] | |
Sebastian Noack
2018/03/26 02:57:48
I just noticed one more edge case, if there is an
Vasily Kuznetsov
2018/03/26 09:24:28
Good catch, Sebastian! It probably makes sense to
Jon Sonesen
2018/03/28 03:01:36
Perhaps a test case should be added as well to cov
rosie
2018/03/31 21:09:28
I've implemented Sebastian's suggestion and it wor
Sebastian Noack
2018/03/31 22:07:35
So default.tmpl is empty, and empty.tmpl is not. T
Vasily Kuznetsov
2018/04/03 14:29:19
I suppose we can say that it ended up being so for
rosie
2018/04/18 02:35:32
Acknowledged.
| |
83 | 94 |
84 | 95 |
85 def get_page_params(source, locale, page, format=None, site_url_override=None, | 96 def get_page_params(source, locale, page, format=None, site_url_override=None, |
86 localized_string_callback=None): | 97 localized_string_callback=None): |
87 from cms.converters import converters | 98 from cms.converters import converters |
88 | 99 |
89 # Guess page format if omitted, but default to Markdown for friendlier excep tions | 100 # Guess page format if omitted, but default to Markdown for friendlier excep tions |
90 if format is None: | 101 if format is None: |
91 for format in converters.iterkeys(): | 102 for format in converters.iterkeys(): |
92 if source.has_page(page, format): | 103 if source.has_page(page, format): |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
153 return params | 164 return params |
154 | 165 |
155 | 166 |
156 def process_page(source, locale, page, format=None, site_url_override=None, | 167 def process_page(source, locale, page, format=None, site_url_override=None, |
157 localized_string_callback=None): | 168 localized_string_callback=None): |
158 from cms.converters import TemplateConverter | 169 from cms.converters import TemplateConverter |
159 | 170 |
160 params = get_page_params(source, locale, page, format, site_url_override, | 171 params = get_page_params(source, locale, page, format, site_url_override, |
161 localized_string_callback) | 172 localized_string_callback) |
162 return TemplateConverter(*params['templatedata'], params=params)() | 173 return TemplateConverter(*params['templatedata'], params=params)() |
OLD | NEW |