diff genshi/output.py @ 397:31742fe6d47e trunk

* Moved some utility functions from `genshi.core` to `genshi.util` (backwards compatibility preserved via imports) * Minor performance tweaks for the serializers
author cmlenz
date Tue, 02 Jan 2007 17:48:06 +0000
parents 96882a191686
children c199e9b95884
line wrap: on
line diff
--- a/genshi/output.py
+++ b/genshi/output.py
@@ -98,21 +98,21 @@
                         ns_attrib.append((QName('xmlns'), namespace))
                 buf = ['<', tagname]
 
-                for attr, value in attrib + tuple(ns_attrib):
+                if ns_attrib:
+                    attrib += tuple(ns_attrib)
+                for attr, value in attrib:
                     attrname = attr.localname
-                    if attr.namespace:
-                        prefix = ns_mapping.get(attr.namespace)
+                    attrns = attr.namespace
+                    if attrns:
+                        prefix = ns_mapping.get(attrns)
                         if prefix:
                             attrname = '%s:%s' % (prefix, attrname)
                     buf += [' ', attrname, '="', escape(value), '"']
                 ns_attrib = []
 
-                if kind is EMPTY:
-                    buf += ['/>']
-                else:
-                    buf += ['>']
+                buf.append(kind is EMPTY and '/>' or '>')
 
-                yield Markup(''.join(buf))
+                yield Markup(u''.join(buf))
 
             elif kind is END:
                 tag = data
@@ -136,13 +136,13 @@
                 name, pubid, sysid = data
                 buf = ['<!DOCTYPE %s']
                 if pubid:
-                    buf += [' PUBLIC "%s"']
+                    buf.append(' PUBLIC "%s"')
                 elif sysid:
-                    buf += [' SYSTEM']
+                    buf.append(' SYSTEM')
                 if sysid:
-                    buf += [' "%s"']
-                buf += ['>\n']
-                yield Markup(''.join(buf), *filter(None, data))
+                    buf.append(' "%s"')
+                buf.append('>\n')
+                yield Markup(u''.join(buf), *filter(None, data))
                 have_doctype = True
 
             elif kind is START_NS:
@@ -216,10 +216,13 @@
                         ns_attrib.append((QName('xmlns'), tagns))
                 buf = ['<', tagname]
 
-                for attr, value in chain(attrib, ns_attrib):
+                if ns_attrib:
+                    attrib += tuple(ns_attrib)
+                for attr, value in attrib:
                     attrname = attr.localname
-                    if attr.namespace:
-                        prefix = ns_mapping.get(attr.namespace)
+                    attrns = attr.namespace
+                    if attrns:
+                        prefix = ns_mapping.get(attrns)
                         if prefix:
                             attrname = '%s:%s' % (prefix, attrname)
                     if attrname in boolean_attrs:
@@ -231,14 +234,14 @@
 
                 if kind is EMPTY:
                     if (tagns and tagns != namespace.uri) \
-                            or tag.localname in empty_elems:
-                        buf += [' />']
+                            or tagname in empty_elems:
+                        buf.append(' />')
                     else:
-                        buf += ['></%s>' % tagname]
+                        buf.append('></%s>' % tagname)
                 else:
-                    buf += ['>']
+                    buf.append('>')
 
-                yield Markup(''.join(buf))
+                yield Markup(u''.join(buf))
 
             elif kind is END:
                 tag = data
@@ -262,13 +265,13 @@
                 name, pubid, sysid = data
                 buf = ['<!DOCTYPE %s']
                 if pubid:
-                    buf += [' PUBLIC "%s"']
+                    buf.append(' PUBLIC "%s"')
                 elif sysid:
-                    buf += [' SYSTEM']
+                    buf.append(' SYSTEM')
                 if sysid:
-                    buf += [' "%s"']
-                buf += ['>\n']
-                yield Markup(''.join(buf), *filter(None, data))
+                    buf.append(' "%s"')
+                buf.append('>\n')
+                yield Markup(u''.join(buf), *filter(None, data))
                 have_doctype = True
 
             elif kind is START_NS:
@@ -349,13 +352,13 @@
                             else:
                                 buf += [' ', attrname, '="', escape(value), '"']
 
-                    buf += ['>']
+                    buf.append('>')
 
                     if kind is EMPTY:
                         if tagname not in empty_elems:
-                            buf += ['</%s>' % tagname]
+                            buf.append('</%s>' % tagname)
 
-                    yield Markup(''.join(buf))
+                    yield Markup(u''.join(buf))
 
                     if tagname in noescape_elems:
                         noescape = True
@@ -380,13 +383,13 @@
                 name, pubid, sysid = data
                 buf = ['<!DOCTYPE %s']
                 if pubid:
-                    buf += [' PUBLIC "%s"']
+                    buf.append(' PUBLIC "%s"')
                 elif sysid:
-                    buf += [' SYSTEM']
+                    buf.append(' SYSTEM')
                 if sysid:
-                    buf += [' "%s"']
-                buf += ['>\n']
-                yield Markup(''.join(buf), *filter(None, data))
+                    buf.append(' "%s"')
+                buf.append('>\n')
+                yield Markup(u''.join(buf), *filter(None, data))
                 have_doctype = True
 
             elif kind is START_NS and data[1] not in ns_mapping:
@@ -460,7 +463,7 @@
         """Initialize the filter.
         
         @param preserve: a set or sequence of tag names for which white-space
-            should be ignored.
+            should be preserved
         @param noescape: a set or sequence of tag names for which text content
             should not be escaped
         
Copyright (C) 2012-2017 Edgewall Software