@@ -76,60 +76,72 @@ def concatenateCharacterTokens(tokens):
7676yield {"type" :"Characters" ,"data" :"" .join (pendingCharacters )}
7777
7878
79- def pprint (tokens ):
79+ def pprint (walker ):
80+ """Pretty printer for tree walkers"""
8081output = []
8182indent = 0
82- for token in concatenateCharacterTokens (tokens ):
83+ for token in concatenateCharacterTokens (walker ):
8384type = token ["type" ]
8485if type in ("StartTag" ,"EmptyTag" ):
85- if ( token [ "namespace" ] and
86- token ["namespace" ]!= constants .namespaces ["html" ]) :
86+ # tag name
87+ if token [ "namespace" ] and token ["namespace" ]!= constants .namespaces ["html" ]:
8788if token ["namespace" ]in constants .prefixes :
88- name = constants .prefixes [token ["namespace" ]]
89+ ns = constants .prefixes [token ["namespace" ]]
8990else :
90- name = token ["namespace" ]
91- name + =" " + token ["name" ]
91+ ns = token ["namespace" ]
92+ name = "%s %s" % ( ns , token ["name" ])
9293else :
9394name = token ["name" ]
9495output .append ("%s<%s>" % (" " * indent ,name ))
9596indent += 2
97+ # attributes (sorted for consistent ordering)
9698attrs = token ["data" ]
97- if attrs :
98- # TODO: Remove this if statement, attrs should always exist
99- for (namespace ,name ),value in sorted (attrs .items ()):
100- if namespace :
101- if namespace in constants .prefixes :
102- outputname = constants .prefixes [namespace ]
103- else :
104- outputname = namespace
105- outputname += " " + name
99+ for (namespace ,localname ),value in sorted (attrs .items ()):
100+ if namespace :
101+ if namespace in constants .prefixes :
102+ ns = constants .prefixes [namespace ]
106103else :
107- outputname = name
108- output .append ("%s%s=\" %s\" " % (" " * indent ,outputname ,value ))
104+ ns = namespace
105+ name = "%s %s" % (ns ,localname )
106+ else :
107+ name = localname
108+ output .append ("%s%s=\" %s\" " % (" " * indent ,name ,value ))
109+ # self-closing
109110if type == "EmptyTag" :
110111indent -= 2
112+
111113elif type == "EndTag" :
112114indent -= 2
115+
113116elif type == "Comment" :
114117output .append ("%s<!-- %s -->" % (" " * indent ,token ["data" ]))
118+
115119elif type == "Doctype" :
116120if token ["name" ]:
117121if token ["publicId" ]:
118122output .append ("""%s<!DOCTYPE %s "%s" "%s">""" %
119- (" " * indent ,token ["name" ],
123+ (" " * indent ,
124+ token ["name" ],
120125token ["publicId" ],
121- token ["systemId" ]and token ["systemId" ]or "" ))
126+ token ["systemId" ]if token ["systemId" ]else "" ))
122127elif token ["systemId" ]:
123128output .append ("""%s<!DOCTYPE %s "" "%s">""" %
124- (" " * indent ,token ["name" ],
129+ (" " * indent ,
130+ token ["name" ],
125131token ["systemId" ]))
126132else :
127133output .append ("%s<!DOCTYPE %s>" % (" " * indent ,
128134token ["name" ]))
129135else :
130136output .append ("%s<!DOCTYPE >" % (" " * indent ,))
131- elif type in ("Characters" ,"SpaceCharacters" ):
137+
138+ elif type == "Characters" :
132139output .append ("%s\" %s\" " % (" " * indent ,token ["data" ]))
140+
141+ elif type == "SpaceCharacters" :
142+ assert False ,"concatenateCharacterTokens should have got rid of all Space tokens"
143+
133144else :
134- pass # TODO: what to do with errors?
145+ raise ValueError ("Unknown token type, %s" % type )
146+
135147return "\n " .join (output )