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 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 | 276 |
277 path = "%s/%s" % (dirname, filename) | 277 path = "%s/%s" % (dirname, filename) |
278 code = self._params["source"].read_file(path) | 278 code = self._params["source"].read_file(path) |
279 module = imp.new_module(root.replace("/", ".")) | 279 module = imp.new_module(root.replace("/", ".")) |
280 exec code in module.__dict__ | 280 exec code in module.__dict__ |
281 | 281 |
282 name = os.path.basename(root) | 282 name = os.path.basename(root) |
283 if not hasattr(module, name): | 283 if not hasattr(module, name): |
284 raise Exception("Expected symbol %s not found in %s file %s" % (name,
dirname, filename)) | 284 raise Exception("Expected symbol %s not found in %s file %s" % (name,
dirname, filename)) |
285 dictionary[name] = getattr(module, name) | 285 dictionary[name] = getattr(module, name) |
| 286 |
| 287 # HACK: The module we created here can be garbage collected because it |
| 288 # isn't added to sys.modules. If a function is called and its module is |
| 289 # gone it might cause weird errors (imports and module variables |
| 290 # unavailable). We avoid this situation by explicitly referencing the |
| 291 # module from the function so they can only be garbage collected |
| 292 # together. |
286 if callable(dictionary[name]): | 293 if callable(dictionary[name]): |
287 dictionary[name].module_ref = module # Prevent garbage collection | 294 dictionary[name].module_ref = module |
288 | 295 |
289 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc
e"]), autoescape=True) | 296 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc
e"]), autoescape=True) |
290 self._env.filters.update(filters) | 297 self._env.filters.update(filters) |
291 self._env.globals.update(globals) | 298 self._env.globals.update(globals) |
292 | 299 |
293 def get_html(self, source): | 300 def get_html(self, source): |
294 template = self._env.from_string(source) | 301 template = self._env.from_string(source) |
295 module = template.make_module(self._params) | 302 module = template.make_module(self._params) |
296 for key, value in module.__dict__.iteritems(): | 303 for key, value in module.__dict__.iteritems(): |
297 if not key.startswith("_"): | 304 if not key.startswith("_"): |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 stack.pop() | 345 stack.pop() |
339 stack[-1]["subitems"].append(item) | 346 stack[-1]["subitems"].append(item) |
340 stack.append(item) | 347 stack.append(item) |
341 return structured | 348 return structured |
342 | 349 |
343 converters = { | 350 converters = { |
344 "html": RawConverter, | 351 "html": RawConverter, |
345 "md": MarkdownConverter, | 352 "md": MarkdownConverter, |
346 "tmpl": TemplateConverter, | 353 "tmpl": TemplateConverter, |
347 } | 354 } |
LEFT | RIGHT |