[Zpt] CVS: Packages/TAL - TALGenerator.py:1.10

guido@digicool.com guido@digicool.com
Fri, 16 Mar 2001 08:18:24 -0500 (EST)


Update of /cvs-repository/Packages/TAL
In directory korak:/tmp/cvs-serv21195

Modified Files:
	TALGenerator.py 
Log Message:
Add a simple optimizing pass.  This collapses consecutive rawtext
items and start and end tags without attributes into a single rawtext
item.

We could do more, by also changing start tags with only plain
(non-substituted) attributes into rawtext and collapsing.  After
breakfast.



--- Updated File TALGenerator.py in package Packages/TAL --
--- TALGenerator.py	2001/03/16 12:57:03	1.9
+++ TALGenerator.py	2001/03/16 13:18:23	1.10
@@ -114,7 +114,34 @@
         return self.optimize(self.program), self.macros
 
     def optimize(self, program):
-        return program # XXX later
+        output = []
+        collect = []
+        rawseen = cursor = 0
+        for cursor in xrange(len(program)+1):
+            try:
+                item = program[cursor]
+            except IndexError:
+                item = (None, None)
+            if item[0] == "rawtext":
+                collect.append(item[1])
+                continue
+            if item[0] == "endTag":
+                collect.append("</%s>" % item[1])
+                continue
+            if item[0] == "startTag" and not item[2]:
+                collect.append("<%s>" % item[1])
+                continue
+            if item[0] == "startEndTag" and not item[2]:
+                collect.append("<%s/>" % item[1])
+                continue
+            text = string.join(collect, "")
+            if text:
+                output.append(("rawtext", text))
+            if item[0] != None:
+                output.append(item)
+            rawseen = cursor+1
+            collect = []
+        return output
 
     def todoPush(self, todo):
         self.todoStack.append(todo)