it's now possible to register extensions after an environment
was created.
--HG--
branch : trunk
diff --git a/jinja2/compiler.py b/jinja2/compiler.py
index 1b01ed9..8660f8c 100644
--- a/jinja2/compiler.py
+++ b/jinja2/compiler.py
@@ -1390,7 +1390,11 @@
self.write(repr(val))
def visit_TemplateData(self, node, frame):
- self.write(repr(node.as_const(frame.eval_ctx)))
+ try:
+ self.write(repr(node.as_const(frame.eval_ctx)))
+ except nodes.Impossible:
+ self.write('(context.eval_ctx.autoescape and Markup or identity)(%r)'
+ % node.data)
def visit_Tuple(self, node, frame):
self.write('(')
diff --git a/jinja2/ext.py b/jinja2/ext.py
index 4220657..48f04fc 100644
--- a/jinja2/ext.py
+++ b/jinja2/ext.py
@@ -130,7 +130,7 @@
def _make_new_gettext(func):
@contextfunction
def gettext(__context, __string, **variables):
- rv = __context.call(func, __string)
+ rv = __context.call(func, __string)
if __context.eval_ctx.autoescape:
rv = Markup(rv)
return rv % variables
@@ -275,18 +275,13 @@
if var not in variables:
variables[var] = nodes.Name(var, 'load')
- # no variables referenced? no need to escape
- if not referenced:
- singular = singular.replace('%%', '%')
- if plural:
- plural = plural.replace('%%', '%')
-
if not have_plural:
plural_expr = None
elif plural_expr is None:
parser.fail('pluralize without variables', lineno)
- node = self._make_node(singular, plural, variables, plural_expr)
+ node = self._make_node(singular, plural, variables, plural_expr,
+ bool(referenced))
node.set_lineno(lineno)
return node
@@ -322,8 +317,16 @@
return referenced, concat(buf)
- def _make_node(self, singular, plural, variables, plural_expr):
+ def _make_node(self, singular, plural, variables, plural_expr,
+ vars_referenced):
"""Generates a useful node from the data provided."""
+ # no variables referenced? no need to escape for old style
+ # gettext invocations
+ if not vars_referenced and not self.environment.newstyle_gettext:
+ singular = singular.replace('%%', '%')
+ if plural:
+ plural = plural.replace('%%', '%')
+
# singular only:
if plural_expr is None:
gettext = nodes.Name('gettext', 'load')
@@ -343,6 +346,7 @@
# enough to handle the variable expansion and autoescape
# handling itself
if self.environment.newstyle_gettext:
+ print 'HERE'
for key, value in variables.iteritems():
node.kwargs.append(nodes.Keyword(key, value))
diff --git a/jinja2/nodes.py b/jinja2/nodes.py
index 8b5f89a..6446c70 100644
--- a/jinja2/nodes.py
+++ b/jinja2/nodes.py
@@ -442,7 +442,10 @@
fields = ('data',)
def as_const(self, eval_ctx=None):
- if get_eval_context(self, eval_ctx).autoescape:
+ eval_ctx = get_eval_context(self, eval_ctx)
+ if eval_ctx.volatile:
+ raise Impossible()
+ if eval_ctx.autoescape:
return Markup(self.data)
return self.data
@@ -839,6 +842,8 @@
def as_const(self, eval_ctx=None):
eval_ctx = get_eval_context(self, eval_ctx)
+ if eval_ctx.volatile:
+ raise Impossible()
expr = self.expr.as_const(eval_ctx)
if eval_ctx.autoescape:
return Markup(expr)
diff --git a/jinja2/testsuite/ext.py b/jinja2/testsuite/ext.py
index 1306973..98e4161 100644
--- a/jinja2/testsuite/ext.py
+++ b/jinja2/testsuite/ext.py
@@ -311,6 +311,16 @@
assert tmpl.render(LANGUAGE='de', apples=1) == '1 Apfel'
assert tmpl.render(LANGUAGE='de', apples=5) == u'5 Äpfel'
+ def test_autoescape_support(self):
+ env = Environment(extensions=['jinja2.ext.autoescape',
+ 'jinja2.ext.i18n'])
+ env.install_gettext_callables(lambda x: u'<strong>Wert: %(name)s</strong>',
+ lambda s, p, n: s, newstyle=True)
+ t = env.from_string('{% autoescape ae %}{{ gettext("foo", name='
+ '"<test>") }}{% endautoescape %}')
+ assert t.render(ae=True) == '<strong>Wert: <test></strong>'
+ assert t.render(ae=False) == '<strong>Wert: <test></strong>'
+
class AutoEscapeTestCase(JinjaTestCase):