diff markup/output.py @ 26:039fc5b87405

* Split out the XPath tests into a separate `unittest`-based file. * Added many more docstrings. * Cleaned up the implementation of the XML/HTML parsers a bit. * The HTML parser now correctly handles minimized attributes. * Added `COPYING` and `README` files.
author cmlenz
date Wed, 28 Jun 2006 08:55:04 +0000
parents e3d3c1d8c98a
children b8456279c444
line wrap: on
line diff
--- a/markup/output.py
+++ b/markup/output.py
@@ -21,7 +21,6 @@
     from sets import ImmutableSet as frozenset
 
 from markup.core import Markup, Namespace, QName, Stream
-from markup.filters import WhitespaceFilter
 
 __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer']
 
@@ -30,6 +29,12 @@
     """Base class for serializers."""
 
     def serialize(self, stream):
+        """Must be implemented by concrete subclasses to serialize the given
+        stream.
+        
+        This method must be implemented as a generator, producing the
+        serialized output incrementally as unicode strings.
+        """
         raise NotImplementedError
 
 
@@ -46,7 +51,7 @@
         ns_attrib = []
         ns_mapping = {}
 
-        stream = PushbackIterator(stream)
+        stream = _PushbackIterator(stream)
         for kind, data, pos in stream:
 
             if kind is Stream.DOCTYPE:
@@ -81,11 +86,7 @@
                 for attr, value in attrib:
                     attrname = attr.localname
                     if attr.namespace:
-                        try:
-                            prefix = ns_mapping[attr.namespace]
-                        except KeyError:
-                            # FIXME: synthesize a prefix for the attribute?
-                            prefix = ''
+                        prefix = ns_mapping.get(attr.namespace)
                         if prefix:
                             attrname = prefix + ':' + attrname
                     buf.append(' %s="%s"' % (attrname, Markup.escape(value)))
@@ -103,12 +104,9 @@
                 tag = data
                 tagname = tag.localname
                 if tag.namespace:
-                    try:
-                        prefix = ns_mapping[tag.namespace]
-                        if prefix:
-                            tagname = prefix + ':' + tag.localname
-                    except KeyError:
-                        pass
+                    prefix = ns_mapping.get(tag.namespace)
+                    if prefix:
+                        tagname = prefix + ':' + tag.localname
                 yield Markup('</%s>' % tagname)
 
             elif kind is Stream.TEXT:
@@ -136,7 +134,7 @@
     def serialize(self, stream):
         ns_mapping = {}
 
-        stream = PushbackIterator(stream)
+        stream = _PushbackIterator(stream)
         for kind, data, pos in stream:
 
             if kind is Stream.DOCTYPE:
@@ -179,7 +177,7 @@
                 yield Markup.escape(data, quotes=False)
 
 
-class PushbackIterator(object):
+class _PushbackIterator(object):
     """A simple wrapper for iterators that allows pushing items back on the
     queue via the `pushback()` method.
     
Copyright (C) 2012-2017 Edgewall Software