| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 def handle_entityref(self, name): | 105 def handle_entityref(self, name): |
| 106 self._append_text(self.unescape("&%s;" % name)) | 106 self._append_text(self.unescape("&%s;" % name)) |
| 107 | 107 |
| 108 def handle_charref(self, name): | 108 def handle_charref(self, name): |
| 109 self._append_text(self.unescape("&#%s;" % name)) | 109 self._append_text(self.unescape("&#%s;" % name)) |
| 110 | 110 |
| 111 class Converter: | 111 class Converter: |
| 112 whitelist = {"a", "em", "strong", "code", "span"} | 112 whitelist = {"a", "em", "strong", "code", "span"} |
| 113 missing_translations = 0 | 113 missing_translations = 0 |
| 114 total_translations = 0 | 114 total_translations = 0 |
| 115 removed_line = "" | |
| 116 | 115 |
| 117 def __init__(self, params, key="pagedata"): | 116 def __init__(self, params, key="pagedata"): |
| 118 self._params = params | 117 self._params = params |
| 119 self._key = key | 118 self._key = key |
| 120 self._attribute_parser = AttributeParser(self.whitelist) | 119 self._attribute_parser = AttributeParser(self.whitelist) |
| 121 self._seen_defaults = {} | 120 self._seen_defaults = {} |
| 122 | 121 |
| 123 # Read in any parameters specified at the beginning of the file | 122 # Read in any parameters specified at the beginning of the file |
| 124 data, filename = params[key] | 123 data, filename = params[key] |
| 125 lines = data.splitlines(True) | 124 lines = data.splitlines(True) |
| 126 for i, line in enumerate(lines): | 125 for i, line in enumerate(lines): |
| 127 if not re.search(r"^\s*[\w\-]+\s*=", line): | 126 if not re.search(r"^\s*[\w\-]+\s*=", line): |
| 128 break | 127 break |
| 129 name, value = line.split("=", 1) | 128 name, value = line.split("=", 1) |
| 130 params[name.strip()] = value.strip() | 129 params[name.strip()] = value.strip() |
| 131 lines[i] = self.removed_line | 130 lines[i] = "\n" |
|
Wladimir Palant
2015/09/16 17:35:52
Why not just assign "\n" here? I don't think that
Sebastian Noack
2015/09/16 19:04:04
Done.
| |
| 132 params[key] = ("".join(lines), filename) | 131 params[key] = ("".join(lines), filename) |
| 133 | 132 |
| 134 def localize_string(self, page, name, default, comment, localedata, escapes): | 133 def localize_string(self, page, name, default, comment, localedata, escapes): |
| 135 def escape(s): | 134 def escape(s): |
| 136 return re.sub(r".", | 135 return re.sub(r".", |
| 137 lambda match: escapes.get(match.group(0), match.group(0)), | 136 lambda match: escapes.get(match.group(0), match.group(0)), |
| 138 s, flags=re.S) | 137 s, flags=re.S) |
| 139 def re_escape(s): | 138 def re_escape(s): |
| 140 return re.escape(escape(s)) | 139 return re.escape(escape(s)) |
| 141 | 140 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 return re.sub( | 261 return re.sub( |
| 263 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( | 262 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( |
| 264 self.include_start_regex, | 263 self.include_start_regex, |
| 265 self.include_end_regex | 264 self.include_end_regex |
| 266 ), | 265 ), |
| 267 resolve_include, | 266 resolve_include, |
| 268 text | 267 text |
| 269 ) | 268 ) |
| 270 | 269 |
| 271 def __call__(self): | 270 def __call__(self): |
| 272 result = self.get_html(self._params[self._key]) | 271 result = self.get_html(*self._params[self._key]) |
| 273 result = self.resolve_includes(result) | 272 result = self.resolve_includes(result) |
| 274 if self._key == "pagedata": | 273 if self._key == "pagedata": |
| 275 head = [] | 274 head = [] |
| 276 def add_to_head(match): | 275 def add_to_head(match): |
| 277 head.append(match.group(1)) | 276 head.append(match.group(1)) |
| 278 return "" | 277 return "" |
| 279 body = re.sub(r"<head>(.*?)</head>", add_to_head, result, flags=re.S) | 278 body = re.sub(r"<head>(.*?)</head>", add_to_head, result, flags=re.S) |
| 280 return "".join(head), body | 279 return "".join(head), body |
| 281 else: | 280 else: |
| 282 return result | 281 return result |
| 283 | 282 |
| 284 class RawConverter(Converter): | 283 class RawConverter(Converter): |
| 285 def get_html(self, (source, filename)): | 284 def get_html(self, source, filename): |
| 286 result = self.insert_localized_strings(source, html_escapes) | 285 result = self.insert_localized_strings(source, html_escapes) |
| 287 result = self.process_links(result) | 286 result = self.process_links(result) |
| 288 return result | 287 return result |
| 289 | 288 |
| 290 class MarkdownConverter(Converter): | 289 class MarkdownConverter(Converter): |
| 291 include_start_regex = r'(?:%s|%s)' % ( | 290 include_start_regex = r'(?:%s|%s)' % ( |
| 292 Converter.include_start_regex, | 291 Converter.include_start_regex, |
| 293 re.escape(jinja2.escape(Converter.include_start_regex)) | 292 re.escape(jinja2.escape(Converter.include_start_regex)) |
| 294 ) | 293 ) |
| 295 include_end_regex = r'(?:%s|%s)' % ( | 294 include_end_regex = r'(?:%s|%s)' % ( |
| 296 Converter.include_end_regex, | 295 Converter.include_end_regex, |
| 297 re.escape(jinja2.escape(Converter.include_end_regex)) | 296 re.escape(jinja2.escape(Converter.include_end_regex)) |
| 298 ) | 297 ) |
| 299 | 298 |
| 300 def get_html(self, (source, filename)): | 299 def get_html(self, source, filename): |
| 301 def remove_unnecessary_entities(match): | 300 def remove_unnecessary_entities(match): |
| 302 char = unichr(int(match.group(1))) | 301 char = unichr(int(match.group(1))) |
| 303 if char in html_escapes: | 302 if char in html_escapes: |
| 304 return match.group(0) | 303 return match.group(0) |
| 305 else: | 304 else: |
| 306 return char | 305 return char |
| 307 | 306 |
| 308 escapes = {} | 307 escapes = {} |
| 309 for char in markdown.Markdown.ESCAPED_CHARS: | 308 for char in markdown.Markdown.ESCAPED_CHARS: |
| 310 escapes[char] = "&#" + str(ord(char)) + ";" | 309 escapes[char] = "&#" + str(ord(char)) + ";" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 325 | 324 |
| 326 class SourceTemplateLoader(jinja2.BaseLoader): | 325 class SourceTemplateLoader(jinja2.BaseLoader): |
| 327 def __init__(self, source): | 326 def __init__(self, source): |
| 328 self.source = source | 327 self.source = source |
| 329 | 328 |
| 330 def get_source(self, environment, template): | 329 def get_source(self, environment, template): |
| 331 try: | 330 try: |
| 332 result = self.source.read_file(template + ".tmpl") | 331 result = self.source.read_file(template + ".tmpl") |
| 333 except Exception: | 332 except Exception: |
| 334 raise jinja2.TemplateNotFound(template) | 333 raise jinja2.TemplateNotFound(template) |
| 335 return result + (None,) | 334 return result + (None,) |
|
Wladimir Palant
2015/09/16 17:35:53
I'm not really happy with unrelated changes being
Sebastian Noack
2015/09/16 19:04:04
The unrelated change wasn't intentional. As you ca
| |
| 336 | 335 |
| 337 class TemplateConverter(Converter): | 336 class TemplateConverter(Converter): |
| 338 removed_line = "{#\n#}" | |
| 339 | |
| 340 def __init__(self, *args, **kwargs): | 337 def __init__(self, *args, **kwargs): |
| 341 Converter.__init__(self, *args, **kwargs) | 338 Converter.__init__(self, *args, **kwargs) |
| 342 | 339 |
| 343 filters = { | 340 filters = { |
| 344 "translate": self.translate, | 341 "translate": self.translate, |
| 345 "linkify": self.linkify, | 342 "linkify": self.linkify, |
| 346 "toclist": self.toclist, | 343 "toclist": self.toclist, |
| 347 } | 344 } |
| 348 | 345 |
| 349 globals = { | 346 globals = { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 363 name = os.path.basename(root) | 360 name = os.path.basename(root) |
| 364 try: | 361 try: |
| 365 dictionary[name] = namespace[name] | 362 dictionary[name] = namespace[name] |
| 366 except KeyError: | 363 except KeyError: |
| 367 raise Exception("Expected symbol %r not found in %r" % (name, path)) | 364 raise Exception("Expected symbol %r not found in %r" % (name, path)) |
| 368 | 365 |
| 369 self._env = jinja2.Environment(loader=SourceTemplateLoader(self._params["sou rce"]), autoescape=True) | 366 self._env = jinja2.Environment(loader=SourceTemplateLoader(self._params["sou rce"]), autoescape=True) |
| 370 self._env.filters.update(filters) | 367 self._env.filters.update(filters) |
| 371 self._env.globals.update(globals) | 368 self._env.globals.update(globals) |
| 372 | 369 |
| 373 def get_html(self, (source, filename)): | 370 def get_html(self, source, filename): |
| 374 env = self._env | 371 env = self._env |
| 375 code = env.compile(source, None, filename) | 372 code = env.compile(source, None, filename) |
| 376 template = env.template_class.from_code(env, code, env.globals) | 373 template = jinja2.Template.from_code(env, code, env.globals) |
|
Wladimir Palant
2015/09/16 17:35:52
From what I can tell, Environment.template_class i
Sebastian Noack
2015/09/16 19:04:04
Done.
| |
| 377 | 374 |
| 378 try: | 375 try: |
| 379 module = template.make_module(self._params) | 376 module = template.make_module(self._params) |
| 380 except Exception: | 377 except Exception: |
| 381 env.handle_exception() | 378 env.handle_exception() |
| 382 | 379 |
| 383 for key, value in module.__dict__.iteritems(): | 380 for key, value in module.__dict__.iteritems(): |
| 384 if not key.startswith("_"): | 381 if not key.startswith("_"): |
| 385 self._params[key] = value | 382 self._params[key] = value |
| 386 | 383 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 stack.pop() | 437 stack.pop() |
| 441 stack[-1]["subitems"].append(item) | 438 stack[-1]["subitems"].append(item) |
| 442 stack.append(item) | 439 stack.append(item) |
| 443 return structured | 440 return structured |
| 444 | 441 |
| 445 converters = { | 442 converters = { |
| 446 "html": RawConverter, | 443 "html": RawConverter, |
| 447 "md": MarkdownConverter, | 444 "md": MarkdownConverter, |
| 448 "tmpl": TemplateConverter, | 445 "tmpl": TemplateConverter, |
| 449 } | 446 } |
| LEFT | RIGHT |