Commit 257571b0 authored by Guido van Rossum's avatar Guido van Rossum

Changes to suppress TAL attributes (and METAL attributes, and selected

XMLNS attributes) in rendered output, with an option to keep the old
behavior.

HTMLTALParser.py, TALParser.py:
    Mark TAL, METAL and TAL/METAL-related XMLNS attributes in the
    attribute list; the old macroHack marking is subsumed in this.
    The marking is a third value in the list item describing the
    attribute, saying "tal", "metal" or "xmlns".  All TAL and METAL
    attributes are marked; only XMLNS attributes whose namespace URI
    value is the special marker for TAL or METAL are marked.

TALInterpreter.py:
    - Get rid of the non-functional 'html' keyword argument.
    - Add new 'showtal' keyword argument to constructor; this defaults
      to -1, which means that the value should default to 0 if TAL
      expansion is requested (through the 'tal' keyword argument) and
      1 if TAL expansion is not requested.
    - In do_startEndTag(), don't special-case empty HTML tags; this is
      already taken care of by TALGenerator.
    - Implement the showtal behavior: when this flag is false, don't
      output TAL, METAL and XMLNS attributes (that are marked as such
      by the parser).
    - Fix a bug: <img src="foo" tal:attributes="src nothing"> should
      render as <img>, not as <img src>.

TALDefs.py:
    Bumped TAL_VERSION to "1.0.1"

driver.py:
    New command line option -t to show tal in output

README.txt:
    Update TO DO list.
parent 1bb7d3b7
...@@ -317,15 +317,16 @@ class HTMLTALParser(HTMLParser): ...@@ -317,15 +317,16 @@ class HTMLTALParser(HTMLParser):
if metaldict.has_key(suffix): if metaldict.has_key(suffix):
raise METALError("duplicate METAL attribute " + raise METALError("duplicate METAL attribute " +
`suffix`, self.getpos()) `suffix`, self.getpos())
item = (key, value) item = (key, value, "metal")
metaldict[suffix] = value metaldict[suffix] = value
if suffix == "define-macro":
item = (key,value,"macroHack")
elif nsuri == ZOPE_TAL_NS: elif nsuri == ZOPE_TAL_NS:
if taldict.has_key(suffix): if taldict.has_key(suffix):
raise TALError("duplicate TAL attribute " + raise TALError("duplicate TAL attribute " +
`suffix`, self.getpos()) `suffix`, self.getpos())
item = (key, value) item = (key, value, "tal")
taldict[suffix] = value taldict[suffix] = value
elif (prefix == "xmlns" and
value in (ZOPE_METAL_NS, ZOPE_TAL_NS)):
item = (key, value, "xmlns")
attrlist.append(item) attrlist.append(item)
return attrlist, taldict, metaldict return attrlist, taldict, metaldict
...@@ -91,6 +91,3 @@ TO DO ...@@ -91,6 +91,3 @@ TO DO
tal:replace and tal:attributes should not be required to do tal:replace and tal:attributes should not be required to do
attribute replacement on the inserted text -- this would require a attribute replacement on the inserted text -- this would require a
change to the TAL spec though. change to the TAL spec though.
- In rendered HTML or XML, the TAL attributes should be suppressed.
(Effectively, when TALInterpreter is invoked with tal=1.)
...@@ -86,7 +86,7 @@ ...@@ -86,7 +86,7 @@
Common definitions used by TAL and METAL compilation an transformation. Common definitions used by TAL and METAL compilation an transformation.
""" """
TAL_VERSION = "1.0" TAL_VERSION = "1.0.1"
XML_NS = "http://www.w3.org/XML/1998/namespace" # URI for XML namespace XML_NS = "http://www.w3.org/XML/1998/namespace" # URI for XML namespace
XMLNS_NS = "http://www.w3.org/2000/xmlns/" # URI for XML NS declarations XMLNS_NS = "http://www.w3.org/2000/xmlns/" # URI for XML NS declarations
......
...@@ -150,7 +150,7 @@ class AltTALGenerator(TALGenerator): ...@@ -150,7 +150,7 @@ class AltTALGenerator(TALGenerator):
class TALInterpreter: class TALInterpreter:
def __init__(self, program, macros, engine, stream=None, def __init__(self, program, macros, engine, stream=None,
debug=0, wrap=60, metal=1, tal=1, html=0): debug=0, wrap=60, metal=1, tal=1, showtal=-1):
self.program = program self.program = program
self.macros = macros self.macros = macros
self.engine = engine self.engine = engine
...@@ -160,7 +160,11 @@ class TALInterpreter: ...@@ -160,7 +160,11 @@ class TALInterpreter:
self.wrap = wrap self.wrap = wrap
self.metal = metal self.metal = metal
self.tal = tal self.tal = tal
self.html = html assert showtal in (-1, 0, 1)
if showtal == -1:
showtal = (not tal)
self.showtal = showtal
self.html = 0
self.slots = {} self.slots = {}
self.currentMacro = None self.currentMacro = None
self.position = None, None # (lineno, offset) self.position = None, None # (lineno, offset)
...@@ -225,18 +229,14 @@ class TALInterpreter: ...@@ -225,18 +229,14 @@ class TALInterpreter:
assert version == TAL_VERSION assert version == TAL_VERSION
def do_mode(self, mode): def do_mode(self, mode):
assert mode in ["html", "xml"] assert mode in ("html", "xml")
self.html = (mode == "html") self.html = (mode == "html")
def do_setPosition(self, position): def do_setPosition(self, position):
self.position = position self.position = position
def do_startEndTag(self, name, attrList): def do_startEndTag(self, name, attrList):
if self.html and string.lower(name) not in EMPTY_HTML_TAGS: self.startTagCommon(name, attrList, self.endsep)
self.do_startTag(name, attrList)
self.do_endTag(name)
else:
self.startTagCommon(name, attrList, self.endsep)
def do_startTag(self, name, attrList): def do_startTag(self, name, attrList):
self.startTagCommon(name, attrList, ">") self.startTagCommon(name, attrList, ">")
...@@ -251,6 +251,8 @@ class TALInterpreter: ...@@ -251,6 +251,8 @@ class TALInterpreter:
name, value = item[:2] name, value = item[:2]
if len(item) > 2: if len(item) > 2:
action = item[2] action = item[2]
if not self.showtal and action in ("tal", "metal", "xmlns"):
continue
if action == "replace" and len(item) > 3 and self.tal: if action == "replace" and len(item) > 3 and self.tal:
if self.html and string.lower(name) in BOOLEAN_HTML_ATTRS: if self.html and string.lower(name) in BOOLEAN_HTML_ATTRS:
ok = self.engine.evaluateBoolean(item[3]) ok = self.engine.evaluateBoolean(item[3])
...@@ -260,7 +262,9 @@ class TALInterpreter: ...@@ -260,7 +262,9 @@ class TALInterpreter:
value = None value = None
else: else:
value = self.engine.evaluateText(item[3]) value = self.engine.evaluateText(item[3])
elif (action == "macroHack" and self.currentMacro and if value is None:
continue
elif (action == "metal" and self.currentMacro and
name[-13:] == ":define-macro" and self.metal): name[-13:] == ":define-macro" and self.metal):
name = name[:-13] + ":use-macro" name = name[:-13] + ":use-macro"
value = self.currentMacro value = self.currentMacro
......
...@@ -144,10 +144,10 @@ class TALParser(XMLParser): ...@@ -144,10 +144,10 @@ class TALParser(XMLParser):
item = self.fixname(key), value item = self.fixname(key), value
if key[:nmetal] == metalprefix: if key[:nmetal] == metalprefix:
metaldict[key[nmetal:]] = value metaldict[key[nmetal:]] = value
if key[nmetal:] == "define-macro": item = item + ("metal",)
item = item + ("macroHack",)
elif key[:ntal] == talprefix: elif key[:ntal] == talprefix:
taldict[key[ntal:]] = value taldict[key[ntal:]] = value
item = item + ("tal",)
fixedattrlist.append(item) fixedattrlist.append(item)
return fixedattrlist, taldict, metaldict return fixedattrlist, taldict, metaldict
...@@ -155,9 +155,14 @@ class TALParser(XMLParser): ...@@ -155,9 +155,14 @@ class TALParser(XMLParser):
newlist = [] newlist = []
for prefix, uri in self.nsNew: for prefix, uri in self.nsNew:
if prefix: if prefix:
newlist.append(("xmlns:" + prefix, uri)) key = "xmlns:" + prefix
else: else:
newlist.append(("xmlns", uri)) key = "xmlns"
if uri in (ZOPE_METAL_NS, ZOPE_TAL_NS):
item = (key, uri, "xmlns")
else:
item = (key, uri)
newlist.append(item)
self.nsNew = [] self.nsNew = []
return newlist return newlist
......
...@@ -109,16 +109,18 @@ def main(): ...@@ -109,16 +109,18 @@ def main():
macros = 0 macros = 0
mode = None mode = None
showcode = 0 showcode = 0
showtal = -1
try: try:
opts, args = getopt.getopt(sys.argv[1:], "hxmns") opts, args = getopt.getopt(sys.argv[1:], "hxmnst")
except getopt.error, msg: except getopt.error, msg:
sys.stderr.write("\n%s\n" % str(msg)) sys.stderr.write("\n%s\n" % str(msg))
sys.stderr.write( sys.stderr.write(
"usage: driver.py [-h|-x] [-m] [-n] [s] [file]\n") "usage: driver.py [-h|-x] [-m] [-n] [-s] [-t] [file]\n")
sys.stderr.write("-h/-x -- HTML/XML input (default auto)\n") sys.stderr.write("-h/-x -- HTML/XML input (default auto)\n")
sys.stderr.write("-m -- macro expansion only\n") sys.stderr.write("-m -- macro expansion only\n")
sys.stderr.write("-n -- turn off the Python 1.5.2 test\n") sys.stderr.write("-n -- turn off the Python 1.5.2 test\n")
sys.stderr.write("-s -- print intermediate code\n") sys.stderr.write("-s -- print intermediate code\n")
sys.stderr.write("-t -- leave tal/metal attributes in output\n")
sys.exit(2) sys.exit(2)
for o, a in opts: for o, a in opts:
if o == '-h': if o == '-h':
...@@ -131,6 +133,8 @@ def main(): ...@@ -131,6 +133,8 @@ def main():
mode = "xml" mode = "xml"
if o == '-s': if o == '-s':
showcode = 1 showcode = 1
if o == '-t':
showtal = 1
if not versionTest: if not versionTest:
if sys.version[:5] != "1.5.2": if sys.version[:5] != "1.5.2":
sys.stderr.write( sys.stderr.write(
...@@ -142,14 +146,15 @@ def main(): ...@@ -142,14 +146,15 @@ def main():
file = FILE file = FILE
it = compilefile(file, mode) it = compilefile(file, mode)
if showcode: showit(it) if showcode: showit(it)
else: interpretit(it, tal=(not macros)) else: interpretit(it, tal=(not macros), showtal=showtal)
def interpretit(it, engine=None, stream=None, tal=1): def interpretit(it, engine=None, stream=None, tal=1, showtal=-1):
from TALInterpreter import TALInterpreter from TALInterpreter import TALInterpreter
program, macros = it program, macros = it
if engine is None: if engine is None:
engine = DummyEngine(macros) engine = DummyEngine(macros)
TALInterpreter(program, macros, engine, stream, wrap=0, tal=tal)() TALInterpreter(program, macros, engine, stream, wrap=0,
tal=tal, showtal=showtal)()
def compilefile(file, mode=None): def compilefile(file, mode=None):
assert mode in ("html", "xml", None) assert mode in ("html", "xml", None)
......
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