fixed a bug in extension handling
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent d1ff858 · Browse files · 6 files·+58 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+58 added−7 removed
- other:+58 added−7 removed
Provenance
- Commit
27069d7- Tree
20364be- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
d1ff858
6 files changed+58 added−7 removed
modified:docs/cache_extension.py+2 added-4 removed
36 unmodified lines37 body = parser.parse_statements(['name:endcache'], drop_needle=True)38 39 # now return a `CallBlock` node that calls our _cache_support40 # helper method on this extension.41- return nodes.CallBlock(42- nodes.Call(self.attr('_cache_support'), args, [], None, None),43- [], [], body44- ).set_lineno(lineno)41+ return nodes.CallBlock(self.call_method('_cache_support', args),42+ [], [], body).set_lineno(lineno)43 44 def _cache_support(self, name, timeout, caller):45 """Helper callback."""46 key = self.environment.fragment_cache_prefix + name10 unmodified linesmodified:docs/extensions.rst+1 added-1 removed
132 unmodified lines133 134 Extensions always have to extend the :class:`jinja2.ext.Extension` class:135 136 .. autoclass:: Extension137- :members: parse, attr137+ :members: parse, attr, call_method138 139 .. attribute:: identifier140 141 The identifier of the extension. This is always the true import name70 unmodified linesmodified:jinja2/compiler.py+2 added-1 removed
717 unmodified lines718 719 # if this extends statement was in the root level we can take720 # advantage of that information and simplify the generated code721 # in the top level from this point onwards722+ if frame.rootlevel:722- self.has_known_extends = True723+ self.has_known_extends = True724 725 # and now we have one more726 self.extends_so_far += 1727 554 unmodified linesmodified:jinja2/environment.py+3 added-1 removed
13 unmodified lines14 from jinja2.parser import Parser15 from jinja2.optimizer import optimize16 from jinja2.compiler import generate17 from jinja2.runtime import Undefined, Context18-from jinja2.debug import translate_exception, translate_syntax_error18 from jinja2.exceptions import TemplateSyntaxError19 from jinja2.utils import import_string, LRUCache, Markup, missing, concat20 21 275 unmodified lines297 """298 try:299 return Parser(self, source, filename).parse()300 except TemplateSyntaxError, e:301+ from jinja2.debug import translate_syntax_error302 exc_type, exc_value, tb = translate_syntax_error(e)303 raise exc_type, exc_value, tb304 305 def lex(self, source, filename=None):179 unmodified lines485 """486 try:487 return concat(self._generate(*args, **kwargs))488 except:489+ from jinja2.debug import translate_exception490 exc_type, exc_value, tb = translate_exception(sys.exc_info())491 raise exc_type, exc_value, tb492 493 def stream(self, *args, **kwargs):13 unmodified lines507 try:508 for item in self._generate(*args, **kwargs):509 yield item510 except:511+ from jinja2.debug import translate_exception512 exc_type, exc_value, tb = translate_exception(sys.exc_info())513 raise exc_type, exc_value, tb514 515 def _generate(self, *args, **kwargs):177 unmodified linesmodified:jinja2/ext.py+11 added
71 unmodified lines72 parser as first argument. The token the parser stream is pointing at73 is the name token that matched. This method has to return one or a74 list of multiple nodes.75 """76+ raise NotImplementedError()77 78 def attr(self, name, lineno=None):79 """Return an attribute node for the current extension. This is useful80 to pass callbacks to template code::3 unmodified lines84 That would call `self._my_callback` when the template is evaluated.85 """86 return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)87 88+ def call_method(self, name, args=None, kwargs=None, dyn_args=None,89+ dyn_kwargs=None, lineno=None):90+ """Call a method of the extension."""91+ if args is None:92+ args = []93+ if kwargs is None:94+ kwargs = []95+ return nodes.Call(self.attr(name, lineno=lineno), args, kwargs,96+ dyn_args, dyn_kwargs, lineno=lineno)97 98+99 class InternationalizationExtension(Extension):100 """This extension adds gettext support to Jinja2."""101 tags = set(['trans'])102 261 unmodified linesadded:jinja2-debug.py+39 added
1+#!/usr/bin/env python2+# -*- coding: utf-8 -*-3+"""4+ Jinja2 Debug Interface5+ ~~~~~~~~~~~~~~~~~~~~~~6+7+ Helper script for internal Jinja2 debugging. Requires Werkzeug.8+9+ :copyright: Copyright 2008 by Armin Ronacher.10+ :license: BSD.11+"""12+import sys13+import jinja214+from werkzeug import script15+16+env = jinja2.Environment()17+18+def shell_init_func():19+ def _compile(x):20+ print env.compile(x, raw=True)21+ result = {22+ 'e': env,23+ 'c': _compile,24+ 't': env.from_string,25+ 'p': env.parse26+ }27+ for key in jinja2.__all__:28+ result[key] = getattr(jinja2, key)29+ return result30+31+32+def action_compile():33+ print env.compile(sys.stdin.read(), raw=True)34+35+action_shell = script.make_shell(shell_init_func)36+37+38+if __name__ == '__main__':39+ script.run()