| 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 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 261 return re.sub( | 261 return re.sub( |
| 262 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( | 262 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( |
| 263 self.include_start_regex, | 263 self.include_start_regex, |
| 264 self.include_end_regex | 264 self.include_end_regex |
| 265 ), | 265 ), |
| 266 resolve_include, | 266 resolve_include, |
| 267 text | 267 text |
| 268 ) | 268 ) |
| 269 | 269 |
| 270 def __call__(self): | 270 def __call__(self): |
| 271 result = self.get_html(self._params[self._key]) | 271 result = self.get_html(*self._params[self._key]) |
| 272 result = self.resolve_includes(result) | 272 result = self.resolve_includes(result) |
| 273 if self._key == "pagedata": | 273 if self._key == "pagedata": |
| 274 head = [] | 274 head = [] |
| 275 def add_to_head(match): | 275 def add_to_head(match): |
| 276 head.append(match.group(1)) | 276 head.append(match.group(1)) |
| 277 return "" | 277 return "" |
| 278 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) |
| 279 return "".join(head), body | 279 return "".join(head), body |
| 280 else: | 280 else: |
| 281 return result | 281 return result |
| 282 | 282 |
| 283 class RawConverter(Converter): | 283 class RawConverter(Converter): |
| 284 def get_html(self, (source, filename)): | 284 def get_html(self, source, filename): |
|
kzar
2015/09/17 08:41:17
Any reason we don't just have source and filename
Sebastian Noack
2015/09/17 08:49:59
Indeed, we could do the unpacking in the calling c
Sebastian Noack
2015/09/17 10:01:23
This bothered me. So I uploaded another patch set.
| |
| 285 result = self.insert_localized_strings(source, html_escapes) | 285 result = self.insert_localized_strings(source, html_escapes) |
| 286 result = self.process_links(result) | 286 result = self.process_links(result) |
| 287 return result | 287 return result |
| 288 | 288 |
| 289 class MarkdownConverter(Converter): | 289 class MarkdownConverter(Converter): |
| 290 include_start_regex = r'(?:%s|%s)' % ( | 290 include_start_regex = r'(?:%s|%s)' % ( |
| 291 Converter.include_start_regex, | 291 Converter.include_start_regex, |
| 292 re.escape(jinja2.escape(Converter.include_start_regex)) | 292 re.escape(jinja2.escape(Converter.include_start_regex)) |
| 293 ) | 293 ) |
| 294 include_end_regex = r'(?:%s|%s)' % ( | 294 include_end_regex = r'(?:%s|%s)' % ( |
| 295 Converter.include_end_regex, | 295 Converter.include_end_regex, |
| 296 re.escape(jinja2.escape(Converter.include_end_regex)) | 296 re.escape(jinja2.escape(Converter.include_end_regex)) |
| 297 ) | 297 ) |
| 298 | 298 |
| 299 def get_html(self, (source, filename)): | 299 def get_html(self, source, filename): |
| 300 def remove_unnecessary_entities(match): | 300 def remove_unnecessary_entities(match): |
| 301 char = unichr(int(match.group(1))) | 301 char = unichr(int(match.group(1))) |
| 302 if char in html_escapes: | 302 if char in html_escapes: |
| 303 return match.group(0) | 303 return match.group(0) |
| 304 else: | 304 else: |
| 305 return char | 305 return char |
| 306 | 306 |
| 307 escapes = {} | 307 escapes = {} |
| 308 for char in markdown.Markdown.ESCAPED_CHARS: | 308 for char in markdown.Markdown.ESCAPED_CHARS: |
| 309 escapes[char] = "&#" + str(ord(char)) + ";" | 309 escapes[char] = "&#" + str(ord(char)) + ";" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 360 name = os.path.basename(root) | 360 name = os.path.basename(root) |
| 361 try: | 361 try: |
| 362 dictionary[name] = namespace[name] | 362 dictionary[name] = namespace[name] |
| 363 except KeyError: | 363 except KeyError: |
| 364 raise Exception("Expected symbol %r not found in %r" % (name, path)) | 364 raise Exception("Expected symbol %r not found in %r" % (name, path)) |
| 365 | 365 |
| 366 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) |
| 367 self._env.filters.update(filters) | 367 self._env.filters.update(filters) |
| 368 self._env.globals.update(globals) | 368 self._env.globals.update(globals) |
| 369 | 369 |
| 370 def get_html(self, (source, filename)): | 370 def get_html(self, source, filename): |
| 371 env = self._env | 371 env = self._env |
| 372 code = env.compile(source, None, filename) | 372 code = env.compile(source, None, filename) |
| 373 template = jinja2.Template.from_code(env, code, env.globals) | 373 template = jinja2.Template.from_code(env, code, env.globals) |
|
kzar
2015/09/17 08:41:17
I was trying to look up jinja2.Template.from_code
Sebastian Noack
2015/09/17 08:49:59
Indeed, it's not documented, at least not in the o
Wladimir Palant
2015/09/17 21:30:14
It's documented under http://code.nabla.net/doc/ji
| |
| 374 | 374 |
| 375 try: | 375 try: |
| 376 module = template.make_module(self._params) | 376 module = template.make_module(self._params) |
| 377 except Exception: | 377 except Exception: |
| 378 env.handle_exception() | 378 env.handle_exception() |
| 379 | 379 |
| 380 for key, value in module.__dict__.iteritems(): | 380 for key, value in module.__dict__.iteritems(): |
| 381 if not key.startswith("_"): | 381 if not key.startswith("_"): |
| 382 self._params[key] = value | 382 self._params[key] = value |
| 383 | 383 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 437 stack.pop() | 437 stack.pop() |
| 438 stack[-1]["subitems"].append(item) | 438 stack[-1]["subitems"].append(item) |
| 439 stack.append(item) | 439 stack.append(item) |
| 440 return structured | 440 return structured |
| 441 | 441 |
| 442 converters = { | 442 converters = { |
| 443 "html": RawConverter, | 443 "html": RawConverter, |
| 444 "md": MarkdownConverter, | 444 "md": MarkdownConverter, |
| 445 "tmpl": TemplateConverter, | 445 "tmpl": TemplateConverter, |
| 446 } | 446 } |
| LEFT | RIGHT |