mirror of
https://github.com/python/cpython.git
synced 2025-11-10 22:36:18 +00:00
format_attrs(): Attempt a bit more minimization for SGML output.
This commit is contained in:
parent
9bbdce5945
commit
f82e4ab617
1 changed files with 22 additions and 4 deletions
|
|
@ -15,15 +15,33 @@ import string
|
||||||
from xml.utils import escape
|
from xml.utils import escape
|
||||||
|
|
||||||
|
|
||||||
def format_attrs(attrs):
|
def format_attrs(attrs, xml=0):
|
||||||
attrs = attrs.items()
|
attrs = attrs.items()
|
||||||
attrs.sort()
|
attrs.sort()
|
||||||
s = ''
|
s = ''
|
||||||
for name, value in attrs:
|
for name, value in attrs:
|
||||||
|
if xml:
|
||||||
|
s = '%s %s="%s"' % (s, name, escape(value))
|
||||||
|
else:
|
||||||
|
# this is a little bogus, but should do for now
|
||||||
|
if name == value and isnmtoken(value):
|
||||||
|
s = "%s %s" % (s, value)
|
||||||
|
elif istoken(value):
|
||||||
|
s = "%s %s=%s" % (s, name, value)
|
||||||
|
else:
|
||||||
s = '%s %s="%s"' % (s, name, escape(value))
|
s = '%s %s="%s"' % (s, name, escape(value))
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
|
||||||
|
_nmtoken_rx = re.compile("[a-z][-._a-z0-9]*", re.IGNORECASE)
|
||||||
|
def isnmtoken(s):
|
||||||
|
return _nmtoken_rx.match(s) is not None
|
||||||
|
|
||||||
|
_token_rx = re.compile("[a-z0-9][-._a-z0-9]*", re.IGNORECASE)
|
||||||
|
def istoken(s):
|
||||||
|
return _token_rx.match(s) is not None
|
||||||
|
|
||||||
|
|
||||||
def do_convert(ifp, ofp, xml=0):
|
def do_convert(ifp, ofp, xml=0):
|
||||||
attrs = {}
|
attrs = {}
|
||||||
lastopened = None
|
lastopened = None
|
||||||
|
|
@ -51,9 +69,9 @@ def do_convert(ifp, ofp, xml=0):
|
||||||
ofp.write("<!--")
|
ofp.write("<!--")
|
||||||
continue
|
continue
|
||||||
if knownempty and xml:
|
if knownempty and xml:
|
||||||
ofp.write("<%s%s/>" % (data, format_attrs(attrs)))
|
ofp.write("<%s%s/>" % (data, format_attrs(attrs, xml)))
|
||||||
else:
|
else:
|
||||||
ofp.write("<%s%s>" % (data, format_attrs(attrs)))
|
ofp.write("<%s%s>" % (data, format_attrs(attrs, xml)))
|
||||||
if knownempty and data not in knownempties:
|
if knownempty and data not in knownempties:
|
||||||
# accumulate knowledge!
|
# accumulate knowledge!
|
||||||
knownempties.append(data)
|
knownempties.append(data)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue