Skip to content

Commit dee0321

Browse files
committed
Cleanup treewalkers.pprint somewhat.
1 parent 9b1096e commit dee0321

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

html5lib/treewalkers/__init__.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,60 +76,72 @@ def concatenateCharacterTokens(tokens):
7676
yield {"type": "Characters", "data": "".join(pendingCharacters)}
7777

7878

79-
def pprint(tokens):
79+
def pprint(walker):
80+
"""Pretty printer for tree walkers"""
8081
output = []
8182
indent = 0
82-
for token in concatenateCharacterTokens(tokens):
83+
for token in concatenateCharacterTokens(walker):
8384
type = token["type"]
8485
if 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"]:
8788
if token["namespace"] in constants.prefixes:
88-
name = constants.prefixes[token["namespace"]]
89+
ns = constants.prefixes[token["namespace"]]
8990
else:
90-
name = token["namespace"]
91-
name += " " + token["name"]
91+
ns = token["namespace"]
92+
name = "%s %s" % (ns, token["name"])
9293
else:
9394
name = token["name"]
9495
output.append("%s<%s>" % (" " * indent, name))
9596
indent += 2
97+
# attributes (sorted for consistent ordering)
9698
attrs = 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]
106103
else:
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
109110
if type == "EmptyTag":
110111
indent -= 2
112+
111113
elif type == "EndTag":
112114
indent -= 2
115+
113116
elif type == "Comment":
114117
output.append("%s<!-- %s -->" % (" " * indent, token["data"]))
118+
115119
elif type == "Doctype":
116120
if token["name"]:
117121
if token["publicId"]:
118122
output.append("""%s<!DOCTYPE %s "%s" "%s">""" %
119-
(" " * indent, token["name"],
123+
(" " * indent,
124+
token["name"],
120125
token["publicId"],
121-
token["systemId"] and token["systemId"] or ""))
126+
token["systemId"] if token["systemId"] else ""))
122127
elif token["systemId"]:
123128
output.append("""%s<!DOCTYPE %s "" "%s">""" %
124-
(" " * indent, token["name"],
129+
(" " * indent,
130+
token["name"],
125131
token["systemId"]))
126132
else:
127133
output.append("%s<!DOCTYPE %s>" % (" " * indent,
128134
token["name"]))
129135
else:
130136
output.append("%s<!DOCTYPE >" % (" " * indent,))
131-
elif type in ("Characters", "SpaceCharacters"):
137+
138+
elif type == "Characters":
132139
output.append("%s\"%s\"" % (" " * indent, token["data"]))
140+
141+
elif type == "SpaceCharacters":
142+
assert False, "concatenateCharacterTokens should have got rid of all Space tokens"
143+
133144
else:
134-
pass # TODO: what to do with errors?
145+
raise ValueError("Unknown token type, %s" % type)
146+
135147
return "\n".join(output)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy