--- html.py.orig1 2003-11-24 20:48:58.000000000 -0500 +++ html.py 2003-11-30 10:38:22.000000000 -0500 @@ -69,12 +69,54 @@ ValuelessAttr = ["valueless_attr"] # magic singleton object -def htmltag (tag, xml_end=0, css_class=None, **attrs): - """Create a HTML tag. +def htmlattrs (**attrs): + """ + Return a dictionary suitable for passing as the keyword argument + dictionary to htmltag(). Trailing underscores are stripped + to allow use of Python keywords (specifically, 'class') as + arguments (hence HTML tags). Also, all attribute names are + forced to lowercase. + + For example, + htmlattrs(href="http://google.com", + title="A nice search engine", + class_="link", + onClick='alert("ouch!")') + returns the dictionary + { 'href': 'http://google.com', + 'title': 'A nice search engine', + 'class': 'link', + 'onclick': 'alert("ouch!")' } + """ + d = {} + for attr in attrs: + d[attr.rstrip('_').lower()] = attrs[attr] + return d + +def htmltag (tag, xml_end=0, **attrs): + """ + Return a string with an HTML tag and various attributes. Does + nothing about the corresponding end tag, unless 'xml_end' is true, + in which case a valid XML-style "" tag is produced. + + Keyword arguments to this function become HTML tag attributes, eg. + htmltag("img", src="foo.gif", alt="Picture of a foo") + returns + 'Picture of a foo' + Note that keyword args are transformed into tag attributes in hash + order (ie. unpredictable and subject to change across Python + versions). """ r = ["<%s" % tag] - if css_class is not None: - attrs['class'] = css_class + + # This is for backwards compatibility with code that predates + # the htmlattrs() function above. (However, special treatment + # of 'css_class' only appeared in Quixote 0.7a1, so it could + # probably just be removed without great harm.) + if 'css_class' in attrs and 'class' not in attrs: + attrs['class'] = attrs['css_class'] + del attrs['css_class'] + for (attr, val) in attrs.items(): if val is ValuelessAttr: val = attr