Automated merge with ssh://team@pocoo.org/jinja2-main

--HG--
branch : trunk
diff --git a/jinja2/optimizer.py b/jinja2/optimizer.py
index b12b1f4..e7f6ac7 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
@@ -22,29 +22,15 @@
 from copy import deepcopy
 from jinja2 import nodes
 from jinja2.visitor import NodeVisitor, NodeTransformer
+from jinja2.runtime import subscribe
 
 
-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,11 +63,16 @@
             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 visit_Subscript(self, node):
+        try:
+            item = self.visit(node.node).as_const()
+            arg = self.visit(node.arg).as_const()
+        except nodes.Impossible:
+            return node
+        # XXX: what does the 3rd parameter mean?
+        return nodes.Const(subscribe(item, arg, None))
 
 
-def optimize(node, environment):
-    optimizer = Optimizer(environment)
+def optimize(node, environment, context={}):
+    optimizer = Optimizer(environment, context=context)
     return optimizer.visit(node)
diff --git a/test_optimizer.py b/test_optimizer.py
index 7320243..06ee440 100644
--- a/test_optimizer.py
+++ b/test_optimizer.py
@@ -4,19 +4,24 @@
 
 
 env = Environment()
+forums = [
+    {'id': 1, 'name': u'Example'},
+    {'id': 2, 'name': u'Foobar'},
+    {'id': 3, 'name': u'<42>'}
+]
 ast = env.parse("""
     Hi {{ "<blub>"|e }},
     how are you?
 
-    {% for item in ('foo', 'bar', 'blub') %}
-        {{ item }}
+    {% for forum in forums %}
+        {{ readstatus(forum.id) }} {{ forum.id|e }} {{ forum.name|e }}
     {% endfor %}
 """)
 print ast
 print
 print generate(ast, env, "foo.html")
 print
-ast = optimize(ast, env)
+ast = optimize(ast, env, context={'forums': forums})
 print ast
 print
 print generate(ast, env, "foo.html")