Left: | ||
Right: |
OLD | NEW |
---|---|
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 = "" | |
115 | 116 |
116 def __init__(self, params, key="pagedata"): | 117 def __init__(self, params, key="pagedata"): |
117 self._params = params | 118 self._params = params |
118 self._key = key | 119 self._key = key |
119 self._attribute_parser = AttributeParser(self.whitelist) | 120 self._attribute_parser = AttributeParser(self.whitelist) |
120 self._seen_defaults = {} | 121 self._seen_defaults = {} |
121 | 122 |
122 # Read in any parameters specified at the beginning of the file | 123 # Read in any parameters specified at the beginning of the file |
123 lines = params[key].splitlines(True) | 124 data, filename = params[key] |
124 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): | 125 lines = data.splitlines(True) |
125 name, value = lines.pop(0).split("=", 1) | 126 for i, line in enumerate(lines): |
127 if not re.search(r"^\s*[\w\-]+\s*=", line): | |
128 break | |
129 name, value = line.split("=", 1) | |
126 params[name.strip()] = value.strip() | 130 params[name.strip()] = value.strip() |
127 params[key] = "".join(lines) | 131 lines[i] = self.removed_line |
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) | |
128 | 133 |
129 def localize_string(self, page, name, default, comment, localedata, escapes): | 134 def localize_string(self, page, name, default, comment, localedata, escapes): |
130 def escape(s): | 135 def escape(s): |
131 return re.sub(r".", | 136 return re.sub(r".", |
132 lambda match: escapes.get(match.group(0), match.group(0)), | 137 lambda match: escapes.get(match.group(0), match.group(0)), |
133 s, flags=re.S) | 138 s, flags=re.S) |
134 def re_escape(s): | 139 def re_escape(s): |
135 return re.escape(escape(s)) | 140 return re.escape(escape(s)) |
136 | 141 |
137 # Handle duplicated strings | 142 # Handle duplicated strings |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 head = [] | 275 head = [] |
271 def add_to_head(match): | 276 def add_to_head(match): |
272 head.append(match.group(1)) | 277 head.append(match.group(1)) |
273 return "" | 278 return "" |
274 body = re.sub(r"<head>(.*?)</head>", add_to_head, result, flags=re.S) | 279 body = re.sub(r"<head>(.*?)</head>", add_to_head, result, flags=re.S) |
275 return "".join(head), body | 280 return "".join(head), body |
276 else: | 281 else: |
277 return result | 282 return result |
278 | 283 |
279 class RawConverter(Converter): | 284 class RawConverter(Converter): |
280 def get_html(self, source): | 285 def get_html(self, (source, filename)): |
281 result = self.insert_localized_strings(source, html_escapes) | 286 result = self.insert_localized_strings(source, html_escapes) |
282 result = self.process_links(result) | 287 result = self.process_links(result) |
283 return result | 288 return result |
284 | 289 |
285 class MarkdownConverter(Converter): | 290 class MarkdownConverter(Converter): |
286 include_start_regex = r'(?:%s|%s)' % ( | 291 include_start_regex = r'(?:%s|%s)' % ( |
287 Converter.include_start_regex, | 292 Converter.include_start_regex, |
288 re.escape(jinja2.escape(Converter.include_start_regex)) | 293 re.escape(jinja2.escape(Converter.include_start_regex)) |
289 ) | 294 ) |
290 include_end_regex = r'(?:%s|%s)' % ( | 295 include_end_regex = r'(?:%s|%s)' % ( |
291 Converter.include_end_regex, | 296 Converter.include_end_regex, |
292 re.escape(jinja2.escape(Converter.include_end_regex)) | 297 re.escape(jinja2.escape(Converter.include_end_regex)) |
293 ) | 298 ) |
294 | 299 |
295 def get_html(self, source): | 300 def get_html(self, (source, filename)): |
296 def remove_unnecessary_entities(match): | 301 def remove_unnecessary_entities(match): |
297 char = unichr(int(match.group(1))) | 302 char = unichr(int(match.group(1))) |
298 if char in html_escapes: | 303 if char in html_escapes: |
299 return match.group(0) | 304 return match.group(0) |
300 else: | 305 else: |
301 return char | 306 return char |
302 | 307 |
303 escapes = {} | 308 escapes = {} |
304 for char in markdown.Markdown.ESCAPED_CHARS: | 309 for char in markdown.Markdown.ESCAPED_CHARS: |
305 escapes[char] = "&#" + str(ord(char)) + ";" | 310 escapes[char] = "&#" + str(ord(char)) + ";" |
306 for key, value in html_escapes.iteritems(): | 311 for key, value in html_escapes.iteritems(): |
307 escapes[key] = value | 312 escapes[key] = value |
308 | 313 |
309 md = markdown.Markdown(output="html5", extensions=["attr_list"]) | 314 md = markdown.Markdown(output="html5", extensions=["attr_list"]) |
310 md.preprocessors["html_block"].markdown_in_raw = True | 315 md.preprocessors["html_block"].markdown_in_raw = True |
311 | 316 |
312 def to_html(s): | 317 def to_html(s): |
313 return re.sub(r'</?p>', '', md.convert(s)) | 318 return re.sub(r'</?p>', '', md.convert(s)) |
314 | 319 |
315 result = self.insert_localized_strings(source, escapes, to_html) | 320 result = self.insert_localized_strings(source, escapes, to_html) |
316 result = md.convert(result) | 321 result = md.convert(result) |
317 result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result) | 322 result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result) |
318 result = self.process_links(result) | 323 result = self.process_links(result) |
319 return result | 324 return result |
320 | 325 |
326 class SourceTemplateLoader(jinja2.BaseLoader): | |
327 def __init__(self, source): | |
328 self.source = source | |
329 | |
330 def get_source(self, environment, template): | |
331 try: | |
332 result = self.source.read_file(template + ".tmpl") | |
333 except Exception: | |
334 raise jinja2.TemplateNotFound(template) | |
335 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 | |
321 class TemplateConverter(Converter): | 337 class TemplateConverter(Converter): |
322 class _SourceLoader(jinja2.BaseLoader): | 338 removed_line = "{#\n#}" |
323 def __init__(self, source): | |
324 self.source = source | |
325 | |
326 def get_source(self, environment, template): | |
327 try: | |
328 return self.source.read_file(template + ".tmpl"), None, None | |
329 except Exception: | |
330 raise jinja2.TemplateNotFound(template) | |
331 | 339 |
332 def __init__(self, *args, **kwargs): | 340 def __init__(self, *args, **kwargs): |
333 Converter.__init__(self, *args, **kwargs) | 341 Converter.__init__(self, *args, **kwargs) |
334 | 342 |
335 filters = { | 343 filters = { |
336 "translate": self.translate, | 344 "translate": self.translate, |
337 "linkify": self.linkify, | 345 "linkify": self.linkify, |
338 "toclist": self.toclist, | 346 "toclist": self.toclist, |
339 } | 347 } |
340 | 348 |
341 globals = { | 349 globals = { |
342 "get_string": self.get_string, | 350 "get_string": self.get_string, |
343 "get_page_content": self.get_page_content, | 351 "get_page_content": self.get_page_content, |
344 } | 352 } |
345 | 353 |
346 for dirname, dictionary in [("filters", filters), ("globals", globals)]: | 354 for dirname, dictionary in [("filters", filters), ("globals", globals)]: |
347 for filename in self._params["source"].list_files(dirname): | 355 for filename in self._params["source"].list_files(dirname): |
348 root, ext = os.path.splitext(filename) | 356 root, ext = os.path.splitext(filename) |
349 if ext.lower() != ".py": | 357 if ext.lower() != ".py": |
350 continue | 358 continue |
351 | 359 |
352 path = "%s/%s" % (dirname, filename) | 360 path = "%s/%s" % (dirname, filename) |
361 namespace = self._params["source"].exec_file(path) | |
362 | |
353 name = os.path.basename(root) | 363 name = os.path.basename(root) |
354 dictionary[name] = self._params["source"].import_symbol(path, name) | 364 try: |
365 dictionary[name] = namespace[name] | |
366 except KeyError: | |
367 raise Exception("Expected symbol %r not found in %r" % (name, path)) | |
355 | 368 |
356 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) | 369 self._env = jinja2.Environment(loader=SourceTemplateLoader(self._params["sou rce"]), autoescape=True) |
357 self._env.filters.update(filters) | 370 self._env.filters.update(filters) |
358 self._env.globals.update(globals) | 371 self._env.globals.update(globals) |
359 | 372 |
360 def get_html(self, source): | 373 def get_html(self, (source, filename)): |
361 template = self._env.from_string(source) | 374 env = self._env |
362 module = template.make_module(self._params) | 375 code = env.compile(source, None, filename) |
376 template = env.template_class.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 | |
378 try: | |
379 module = template.make_module(self._params) | |
380 except Exception: | |
381 env.handle_exception() | |
382 | |
363 for key, value in module.__dict__.iteritems(): | 383 for key, value in module.__dict__.iteritems(): |
364 if not key.startswith("_"): | 384 if not key.startswith("_"): |
365 self._params[key] = value | 385 self._params[key] = value |
366 | 386 |
367 result = unicode(module) | 387 result = unicode(module) |
368 result = self.process_links(result) | 388 result = self.process_links(result) |
369 return result | 389 return result |
370 | 390 |
371 def translate(self, default, name, comment=None): | 391 def translate(self, default, name, comment=None): |
372 return jinja2.Markup(self.localize_string( | 392 return jinja2.Markup(self.localize_string( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 stack.pop() | 440 stack.pop() |
421 stack[-1]["subitems"].append(item) | 441 stack[-1]["subitems"].append(item) |
422 stack.append(item) | 442 stack.append(item) |
423 return structured | 443 return structured |
424 | 444 |
425 converters = { | 445 converters = { |
426 "html": RawConverter, | 446 "html": RawConverter, |
427 "md": MarkdownConverter, | 447 "md": MarkdownConverter, |
428 "tmpl": TemplateConverter, | 448 "tmpl": TemplateConverter, |
429 } | 449 } |
OLD | NEW |