Mercurial > genshi > mirror
diff genshi/builder.py @ 784:ea46fb523485 experimental-match-fastpaths
update to 0.5.x branch, up through r907
don't know how this fits in with SoC work, but I wanted to do due diligence and keep this branch working in case it someday gets considered for trunk
author | aflett |
---|---|
date | Mon, 21 Jul 2008 23:17:52 +0000 |
parents | 0ea38a6cf173 |
children |
line wrap: on
line diff
--- a/genshi/builder.py +++ b/genshi/builder.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# Copyright (C) 2006-2007 Edgewall Software +# Copyright (C) 2006-2008 Edgewall Software # All rights reserved. # # This software is licensed as described in the file COPYING, which @@ -68,7 +68,13 @@ Hello, <em>world</em>! """ -from genshi.core import Attrs, Namespace, QName, Stream, START, END, TEXT +try: + set +except NameError: + from sets import Set as set + +from genshi.core import Attrs, Markup, Namespace, QName, Stream, \ + START, END, TEXT __all__ = ['Fragment', 'Element', 'ElementFactory', 'tag'] __docformat__ = 'restructuredtext en' @@ -107,6 +113,9 @@ def __unicode__(self): return unicode(self.generate()) + def __html__(self): + return Markup(self.generate()) + def append(self, node): """Append an element or string as child node. @@ -146,14 +155,15 @@ return Stream(self._generate()) -def _value_to_unicode(value): - if isinstance(value, unicode): - return value - return unicode(value) - def _kwargs_to_attrs(kwargs): - return [(QName(k.rstrip('_').replace('_', '-')), _value_to_unicode(v)) - for k, v in kwargs.items() if v is not None] + attrs = [] + names = set() + for name, value in kwargs.items(): + name = name.rstrip('_').replace('_', '-') + if value is not None and name not in names: + attrs.append((QName(name), unicode(value))) + names.add(name) + return Attrs(attrs) class Element(Fragment): @@ -240,7 +250,7 @@ def __init__(self, tag_, **attrib): Fragment.__init__(self) self.tag = QName(tag_) - self.attrib = Attrs(_kwargs_to_attrs(attrib)) + self.attrib = _kwargs_to_attrs(attrib) def __call__(self, *args, **kwargs): """Append any positional arguments as child nodes, and keyword arguments @@ -250,7 +260,7 @@ :rtype: `Element` :see: `Fragment.append` """ - self.attrib |= Attrs(_kwargs_to_attrs(kwargs)) + self.attrib |= _kwargs_to_attrs(kwargs) Fragment.__call__(self, *args) return self