From b40b8803aec7fc0312b010b390a1ccbc4b2a65d3 Mon Sep 17 00:00:00 2001 From: Christoph Hack Date: Tue, 8 Apr 2008 23:01:58 +0200 Subject: [PATCH] used the new nodetransformer to make mitsuhiko happy --HG-- branch : trunk --- jinja2/optimizer.py | 23 ++--------------------- test_optimizer.py | 4 ++-- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/jinja2/optimizer.py b/jinja2/optimizer.py index b12b1f4..c9787f6 100644 --- a/jinja2/optimizer.py +++ b/jinja2/optimizer.py @@ -8,7 +8,7 @@ * eliminating constant nodes * evaluating filters and macros on constant nodes * unroll loops on constant values - * replace variables which are already known (because the doesn't + * replace variables which are already known (because they doesn't change often and you want to prerender a template) with constants After the optimation you will get a new, simplier template which can @@ -24,27 +24,12 @@ from jinja2 import nodes from jinja2.visitor import NodeVisitor, NodeTransformer -class Optimizer(NodeVisitor): +class Optimizer(NodeTransformer): def __init__(self, environment, context={}): self.environment = environment self.context = context - def visit_Output(self, node): - node.nodes = [self.visit(n) for n in node.nodes] - return node - - def visit_Template(self, node): - body = [] - for n in node.body: - x = self.visit(n) - if isinstance(x, list): - body.extend(x) - else: - body.append(x) - node.body = body - return node - def visit_Filter(self, node): """Try to evaluate filters if possible.""" try: @@ -77,10 +62,6 @@ class Optimizer(NodeVisitor): return node return nodes.Const(self.context[node.name]) - def generic_visit(self, node, *args, **kwargs): - NodeVisitor.generic_visit(self, node, *args, **kwargs) - return node - def optimize(node, environment): optimizer = Optimizer(environment) diff --git a/test_optimizer.py b/test_optimizer.py index 7320243..e23d862 100644 --- a/test_optimizer.py +++ b/test_optimizer.py @@ -8,8 +8,8 @@ ast = env.parse(""" Hi {{ ""|e }}, how are you? - {% for item in ('foo', 'bar', 'blub') %} - {{ item }} + {% for item in ('foo', 'bar', 'blub', '<42>') %} + {{ item|e }} {% endfor %} """) print ast -- 2.26.2