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-2016 Eyeo GmbH | 2 # Copyright (C) 2006-2016 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 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 self._string = None | 73 self._string = None |
74 self._attrs = None | 74 self._attrs = None |
75 self._pagename = None | 75 self._pagename = None |
76 self._inside_fixed = False | 76 self._inside_fixed = False |
77 self._fixed_strings = None | 77 self._fixed_strings = None |
78 | 78 |
79 def handle_starttag(self, tag, attrs): | 79 def handle_starttag(self, tag, attrs): |
80 if self._inside_fixed: | 80 if self._inside_fixed: |
81 raise Exception("Unexpected HTML tag '{}' inside a fixed string" | 81 raise Exception("Unexpected HTML tag '{}' inside a fixed string" |
82 ' on page {}'.format(tag, self._pagename)) | 82 ' on page {}'.format(tag, self._pagename)) |
83 if tag == 'fix': | 83 elif tag == 'fix': |
Wladimir Palant
2017/03/12 16:02:22
For reference, I'm not a huge fan of removing ever
Vasily Kuznetsov
2017/03/13 12:25:29
I agree that applying strict "no unnecessary else'
| |
84 self._inside_fixed = True | 84 self._inside_fixed = True |
85 self._fixed_strings.append([]) | 85 self._fixed_strings.append([]) |
86 elif tag in self._whitelist: | 86 elif tag in self._whitelist: |
87 self._attrs.setdefault(tag, []).append(attrs) | 87 self._attrs.setdefault(tag, []).append(attrs) |
88 self._string.append('<{}>'.format(tag)) | 88 self._string.append('<{}>'.format(tag)) |
89 else: | 89 else: |
90 raise Exception("Unexpected HTML tag '{}' inside a fixed string" | 90 raise Exception("Unexpected HTML tag '{}' inside a fixed string" |
91 ' on page {}'.format(tag, self._pagename)) | 91 ' on page {}'.format(tag, self._pagename)) |
92 | 92 |
93 def handle_endtag(self, tag): | 93 def handle_endtag(self, tag): |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 '<(?:{}[^<>]*?|/{})>'.format(t, t) | 199 '<(?:{}[^<>]*?|/{})>'.format(t, t) |
200 for t in map(re.escape, self.whitelist - {tag}) | 200 for t in map(re.escape, self.whitelist - {tag}) |
201 ))) | 201 ))) |
202 saved = saved_attributes.get(tag, []) | 202 saved = saved_attributes.get(tag, []) |
203 for attrs in saved: | 203 for attrs in saved: |
204 attrs = map(stringify_attribute, attrs) | 204 attrs = map(stringify_attribute, attrs) |
205 result = re.sub( | 205 result = re.sub( |
206 r'{}({}*?){}'.format(re_escape('<{}>'.format(tag)), | 206 r'{}({}*?){}'.format(re_escape('<{}>'.format(tag)), |
207 allowed_contents, | 207 allowed_contents, |
208 re_escape('</{}>'.format(tag))), | 208 re_escape('</{}>'.format(tag))), |
209 lambda match: '<{}{}>{}</{}>'.format( | 209 lambda match: r'<{}{}>{}</{}>'.format( |
Wladimir Palant
2017/03/12 16:02:22
Like with my other comment, by not using raw strin
Vasily Kuznetsov
2017/03/13 12:25:29
I switched from raw strings to unicode strings to
| |
210 tag, | 210 tag, |
211 ' ' + ' '.join(attrs) if attrs else '', | 211 ' ' + ' '.join(attrs) if attrs else '', |
212 match.group(1), | 212 match.group(1), |
213 tag | 213 tag |
214 ), | 214 ), |
215 result, 1, flags=re.S | 215 result, 1, flags=re.S |
216 ) | 216 ) |
217 result = re.sub( | 217 result = re.sub( |
218 r'{}({}*?){}'.format(re_escape('<{}>'.format(tag)), | 218 r'{}({}*?){}'.format(re_escape('<{}>'.format(tag)), |
219 allowed_contents, | 219 allowed_contents, |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 stack.pop() | 479 stack.pop() |
480 stack[-1]['subitems'].append(item) | 480 stack[-1]['subitems'].append(item) |
481 stack.append(item) | 481 stack.append(item) |
482 return structured | 482 return structured |
483 | 483 |
484 converters = { | 484 converters = { |
485 'html': RawConverter, | 485 'html': RawConverter, |
486 'md': MarkdownConverter, | 486 'md': MarkdownConverter, |
487 'tmpl': TemplateConverter, | 487 'tmpl': TemplateConverter, |
488 } | 488 } |
LEFT | RIGHT |