LEFT | RIGHT |
(no file at all) | |
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 if length > 0: |
| 92 # Need to preserve line numbers for jinja2 tracebacks |
| 93 cutoff = m.end() if m else length |
| 94 source = '\n' * source.count('\n', 0, cutoff) + source[cutoff:] |
| 95 |
| 96 return metadata, source |
83 | 97 |
84 | 98 |
85 def get_page_params(source, locale, page, format=None, site_url_override=None, | 99 def get_page_params(source, locale, page, format=None, site_url_override=None, |
86 localized_string_callback=None): | 100 localized_string_callback=None): |
87 from cms.converters import converters | 101 from cms.converters import converters |
88 | 102 |
89 # Guess page format if omitted, but default to Markdown for friendlier excep
tions | 103 # Guess page format if omitted, but default to Markdown for friendlier excep
tions |
90 if format is None: | 104 if format is None: |
91 for format in converters.iterkeys(): | 105 for format in converters.iterkeys(): |
92 if source.has_page(page, format): | 106 if source.has_page(page, format): |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 return params | 167 return params |
154 | 168 |
155 | 169 |
156 def process_page(source, locale, page, format=None, site_url_override=None, | 170 def process_page(source, locale, page, format=None, site_url_override=None, |
157 localized_string_callback=None): | 171 localized_string_callback=None): |
158 from cms.converters import TemplateConverter | 172 from cms.converters import TemplateConverter |
159 | 173 |
160 params = get_page_params(source, locale, page, format, site_url_override, | 174 params = get_page_params(source, locale, page, format, site_url_override, |
161 localized_string_callback) | 175 localized_string_callback) |
162 return TemplateConverter(*params['templatedata'], params=params)() | 176 return TemplateConverter(*params['templatedata'], params=params)() |
LEFT | RIGHT |