Commit c7326263 authored by Guido van Rossum's avatar Guido van Rossum

Move compile() into the DummyEngine() class (deleting our

DummyCompiler class), and make sure to call it everywhere it is
needed (in particular for attribute replacements).

Emit a new opcode, "mode", with an argument "html" or "xml", depending
on the value of the xml argument to the constructor.  (This requires a
matching checkin of TALInterpreter.)  (This comment was mistakenly
added to the previous checkin of DummyCompiler.py.)

Don't append to self.program directly; always call self.emit().
parent b9f0ea81
...@@ -91,17 +91,13 @@ import re ...@@ -91,17 +91,13 @@ import re
import cgi import cgi
from TALDefs import * from TALDefs import *
from DummyEngine import DummyEngine
class DummyCompiler:
def compile(self, expr):
return expr
class TALGenerator: class TALGenerator:
def __init__(self, expressionCompiler=None, xml=1): def __init__(self, expressionCompiler=None, xml=1):
if not expressionCompiler: if not expressionCompiler:
expressionCompiler = DummyCompiler() expressionCompiler = DummyEngine()
self.expressionCompiler = expressionCompiler self.expressionCompiler = expressionCompiler
self.program = [] self.program = []
self.stack = [] self.stack = []
...@@ -111,6 +107,7 @@ class TALGenerator: ...@@ -111,6 +107,7 @@ class TALGenerator:
self.slotStack = [] self.slotStack = []
self.xml = xml self.xml = xml
self.emit("version", TAL_VERSION) self.emit("version", TAL_VERSION)
self.emit("mode", xml and "xml" or "html")
def getCode(self): def getCode(self):
return self.optimize(self.program), self.macros return self.optimize(self.program), self.macros
...@@ -196,17 +193,17 @@ class TALGenerator: ...@@ -196,17 +193,17 @@ class TALGenerator:
opcode = "startEndTag" opcode = "startEndTag"
else: else:
opcode = "startTag" opcode = "startTag"
self.program.append((opcode, name, attrlist)) self.emit(opcode, name, attrlist)
def emitEndTag(self, name): def emitEndTag(self, name):
if self.xml and self.program and self.program[-1][0] == "startTag": if self.xml and self.program and self.program[-1][0] == "startTag":
# Minimize empty element # Minimize empty element
self.program[-1] = ("startEndTag",) + self.program[-1][1:] self.program[-1] = ("startEndTag",) + self.program[-1][1:]
else: else:
self.program.append(("endTag", name)) self.emit("endTag", name)
def emitRawText(self, text): def emitRawText(self, text):
self.program.append(("rawtext", text)) self.emit("rawtext", text)
def emitText(self, text): def emitText(self, text):
self.emitRawText(cgi.escape(text)) self.emitRawText(cgi.escape(text))
...@@ -329,7 +326,7 @@ class TALGenerator: ...@@ -329,7 +326,7 @@ class TALGenerator:
rest = rest + string.join(collect, "") rest = rest + string.join(collect, "")
del self.program[i:] del self.program[i:]
if text: if text:
self.program.append(("rawtext", text)) self.emit("rawtext", text)
return rest return rest
return None return None
...@@ -350,7 +347,7 @@ class TALGenerator: ...@@ -350,7 +347,7 @@ class TALGenerator:
def emitStartElement(self, name, attrlist, taldict, metaldict, def emitStartElement(self, name, attrlist, taldict, metaldict,
position=(None, None), isend=0): position=(None, None), isend=0):
if len(taldict) == len(metaldict) == 0: if not taldict and not metaldict:
# Handle the simple, common case # Handle the simple, common case
self.emitStartTag(name, attrlist, isend) self.emitStartTag(name, attrlist, isend)
self.todoPush({}) self.todoPush({})
...@@ -433,6 +430,8 @@ class TALGenerator: ...@@ -433,6 +430,8 @@ class TALGenerator:
self.emitText(repeatWhitespace) self.emitText(repeatWhitespace)
if attrsubst: if attrsubst:
repldict = parseAttributeReplacements(attrsubst) repldict = parseAttributeReplacements(attrsubst)
for key, value in repldict.items():
repldict[key] = self.expressionCompiler.compile(value)
else: else:
repldict = {} repldict = {}
if replace: if replace:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment