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 lines = params[key].splitlines(True) |
124 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): | 125 for i, line in enumerate(lines): |
Sebastian Noack
2015/09/15 17:48:36
In order to prevent line numbers from being off we
| |
125 name, value = lines.pop(0).split("=", 1) | 126 if not re.search(r"^\s*[\w\-]+\s*=", line): |
127 break | |
128 name, value = line.split("=", 1) | |
126 params[name.strip()] = value.strip() | 129 params[name.strip()] = value.strip() |
130 lines[i] = self.removed_line | |
127 params[key] = "".join(lines) | 131 params[key] = "".join(lines) |
128 | 132 |
129 def localize_string(self, page, name, default, comment, localedata, escapes): | 133 def localize_string(self, page, name, default, comment, localedata, escapes): |
130 def escape(s): | 134 def escape(s): |
131 return re.sub(r".", | 135 return re.sub(r".", |
132 lambda match: escapes.get(match.group(0), match.group(0)), | 136 lambda match: escapes.get(match.group(0), match.group(0)), |
133 s, flags=re.S) | 137 s, flags=re.S) |
134 def re_escape(s): | 138 def re_escape(s): |
135 return re.escape(escape(s)) | 139 return re.escape(escape(s)) |
136 | 140 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 | 243 |
240 include_start_regex = '<' | 244 include_start_regex = '<' |
241 include_end_regex = '>' | 245 include_end_regex = '>' |
242 | 246 |
243 def resolve_includes(self, text): | 247 def resolve_includes(self, text): |
244 def resolve_include(match): | 248 def resolve_include(match): |
245 global converters | 249 global converters |
246 name = match.group(1) | 250 name = match.group(1) |
247 for format, converter_class in converters.iteritems(): | 251 for format, converter_class in converters.iteritems(): |
248 if self._params["source"].has_include(name, format): | 252 if self._params["source"].has_include(name, format): |
253 self._params["include"] = name | |
Sebastian Noack
2015/09/15 17:48:36
There seems to be no way to get the filename for i
| |
249 self._params["includedata"] = self._params["source"].read_include(name , format) | 254 self._params["includedata"] = self._params["source"].read_include(name , format) |
250 converter = converter_class(self._params, key="includedata") | 255 converter = converter_class(self._params, key="includedata") |
251 result = converter() | 256 result = converter() |
252 self.missing_translations += converter.missing_translations | 257 self.missing_translations += converter.missing_translations |
253 self.total_translations += converter.total_translations | 258 self.total_translations += converter.total_translations |
254 return result | 259 return result |
255 raise Exception("Failed to resolve include %s on page %s" % (name, self._p arams["page"])) | 260 raise Exception("Failed to resolve include %s on page %s" % (name, self._p arams["page"])) |
256 | 261 |
257 return re.sub( | 262 return re.sub( |
258 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( | 263 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 |
321 class TemplateConverter(Converter): | 326 class TemplateConverter(Converter): |
322 class _SourceLoader(jinja2.BaseLoader): | 327 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 | 328 |
332 def __init__(self, *args, **kwargs): | 329 def __init__(self, *args, **kwargs): |
333 Converter.__init__(self, *args, **kwargs) | 330 Converter.__init__(self, *args, **kwargs) |
334 | 331 |
335 filters = { | 332 filters = { |
336 "translate": self.translate, | 333 "translate": self.translate, |
337 "linkify": self.linkify, | 334 "linkify": self.linkify, |
338 "toclist": self.toclist, | 335 "toclist": self.toclist, |
339 } | 336 } |
340 | 337 |
341 globals = { | 338 globals = { |
342 "get_string": self.get_string, | 339 "get_string": self.get_string, |
343 "get_page_content": self.get_page_content, | 340 "get_page_content": self.get_page_content, |
344 } | 341 } |
345 | 342 |
346 for dirname, dictionary in [("filters", filters), ("globals", globals)]: | 343 for dirname, dictionary in [("filters", filters), ("globals", globals)]: |
347 for filename in self._params["source"].list_files(dirname): | 344 for filename in self._params["source"].list_files(dirname): |
348 root, ext = os.path.splitext(filename) | 345 root, ext = os.path.splitext(filename) |
349 if ext.lower() != ".py": | 346 if ext.lower() != ".py": |
350 continue | 347 continue |
351 | 348 |
352 path = "%s/%s" % (dirname, filename) | 349 path = "%s/%s" % (dirname, filename) |
353 name = os.path.basename(root) | 350 name = os.path.basename(root) |
354 dictionary[name] = self._params["source"].import_symbol(path, name) | 351 dictionary[name] = self._params["source"].import_symbol(path, name) |
355 | 352 |
356 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) | 353 self._env = jinja2.Environment(loader=self._params["source"].get_template_lo ader(), autoescape=True) |
357 self._env.filters.update(filters) | 354 self._env.filters.update(filters) |
358 self._env.globals.update(globals) | 355 self._env.globals.update(globals) |
359 | 356 |
357 def get_template_filename(self): | |
358 source = self._params["source"] | |
Sebastian Noack
2015/09/15 17:48:36
And here is where the problematic part starts. I d
| |
359 if hasattr(source, "get_path"): | |
360 if self._key == "pagedata": | |
361 return source.get_path(source.page_filename(self._params["page"], "tmpl" )) | |
362 if self._key == "includedata": | |
363 return source.get_path(source.include_filename(self._params["include"], "tmpl")) | |
364 if self._key == "templatedata": | |
365 return source.get_path(source.template_filename(self._params["template"] )) | |
366 | |
360 def get_html(self, source): | 367 def get_html(self, source): |
361 template = self._env.from_string(source) | 368 env = self._env |
362 module = template.make_module(self._params) | 369 code = env.compile(source, None, self.get_template_filename()) |
Sebastian Noack
2015/09/15 17:48:36
There are two ways to make the template aware of t
| |
370 template = env.template_class.from_code(env, code, env.globals) | |
371 | |
372 try: | |
373 module = template.make_module(self._params) | |
374 except Exception: | |
375 env.handle_exception() | |
Sebastian Noack
2015/09/15 17:48:36
Note that this is necessary because as opposed to
| |
376 | |
363 for key, value in module.__dict__.iteritems(): | 377 for key, value in module.__dict__.iteritems(): |
364 if not key.startswith("_"): | 378 if not key.startswith("_"): |
365 self._params[key] = value | 379 self._params[key] = value |
366 | 380 |
367 result = unicode(module) | 381 result = unicode(module) |
368 result = self.process_links(result) | 382 result = self.process_links(result) |
369 return result | 383 return result |
370 | 384 |
371 def translate(self, default, name, comment=None): | 385 def translate(self, default, name, comment=None): |
372 return jinja2.Markup(self.localize_string( | 386 return jinja2.Markup(self.localize_string( |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 stack.pop() | 434 stack.pop() |
421 stack[-1]["subitems"].append(item) | 435 stack[-1]["subitems"].append(item) |
422 stack.append(item) | 436 stack.append(item) |
423 return structured | 437 return structured |
424 | 438 |
425 converters = { | 439 converters = { |
426 "html": RawConverter, | 440 "html": RawConverter, |
427 "md": MarkdownConverter, | 441 "md": MarkdownConverter, |
428 "tmpl": TemplateConverter, | 442 "tmpl": TemplateConverter, |
429 } | 443 } |
OLD | NEW |