cmlenz@1: # -*- coding: utf-8 -*- cmlenz@1: # cmlenz@66: # Copyright (C) 2006 Edgewall Software cmlenz@1: # All rights reserved. cmlenz@1: # cmlenz@1: # This software is licensed as described in the file COPYING, which cmlenz@1: # you should have received as part of this distribution. The terms cmlenz@66: # are also available at http://markup.edgewall.org/wiki/License. cmlenz@1: # cmlenz@1: # This software consists of voluntary contributions made by many cmlenz@1: # individuals. For the exact contribution history, see the revision cmlenz@66: # history and logs, available at http://markup.edgewall.org/log/. cmlenz@1: cmlenz@1: """This module provides different kinds of serialization methods for XML event cmlenz@1: streams. cmlenz@1: """ cmlenz@1: cmlenz@1: try: cmlenz@1: frozenset cmlenz@1: except NameError: cmlenz@1: from sets import ImmutableSet as frozenset cmlenz@85: from itertools import chain cmlenz@1: cmlenz@73: from markup.core import escape, Markup, Namespace, QName cmlenz@89: from markup.core import DOCTYPE, START, END, START_NS, END_NS, TEXT, COMMENT cmlenz@1: cmlenz@1: __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer'] cmlenz@1: cmlenz@1: cmlenz@1: class Serializer(object): cmlenz@1: """Base class for serializers.""" cmlenz@1: cmlenz@1: def serialize(self, stream): cmlenz@26: """Must be implemented by concrete subclasses to serialize the given cmlenz@26: stream. cmlenz@26: cmlenz@26: This method must be implemented as a generator, producing the cmlenz@26: serialized output incrementally as unicode strings. cmlenz@26: """ cmlenz@1: raise NotImplementedError cmlenz@1: cmlenz@1: cmlenz@85: class DocType(object): cmlenz@85: """Defines a number of commonly used DOCTYPE declarations as constants.""" cmlenz@85: cmlenz@85: HTML_STRICT = ('html', '-//W3C//DTD HTML 4.01//EN', cmlenz@85: 'http://www.w3.org/TR/html4/strict.dtd') cmlenz@85: HTML_TRANSITIONAL = ('html', '-//W3C//DTD HTML 4.01 Transitional//EN', cmlenz@85: 'http://www.w3.org/TR/html4/loose.dtd') cmlenz@85: HTML = HTML_STRICT cmlenz@85: cmlenz@85: XHTML_STRICT = ('html', '-//W3C//DTD XHTML 1.0 Strict//EN', cmlenz@85: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd') cmlenz@85: XHTML_TRANSITIONAL = ('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', cmlenz@85: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd') cmlenz@85: XHTML = XHTML_STRICT cmlenz@85: cmlenz@85: cmlenz@1: class XMLSerializer(Serializer): cmlenz@1: """Produces XML text from an event stream. cmlenz@1: cmlenz@1: >>> from markup.builder import tag cmlenz@20: >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) cmlenz@1: >>> print ''.join(XMLSerializer().serialize(elem.generate())) cmlenz@1:


cmlenz@1: """ cmlenz@85: def __init__(self, doctype=None): cmlenz@85: """Initialize the XML serializer. cmlenz@85: cmlenz@85: @param doctype: a `(name, pubid, sysid)` tuple that represents the cmlenz@85: DOCTYPE declaration that should be included at the top of the cmlenz@85: generated output cmlenz@85: """ cmlenz@85: self.preamble = [] cmlenz@85: if doctype: cmlenz@85: self.preamble.append((DOCTYPE, doctype, (None, -1, -1))) cmlenz@1: cmlenz@1: def serialize(self, stream): cmlenz@85: have_doctype = False cmlenz@1: ns_attrib = [] cmlenz@1: ns_mapping = {} cmlenz@1: cmlenz@85: stream = _PushbackIterator(chain(self.preamble, stream)) cmlenz@1: for kind, data, pos in stream: cmlenz@1: cmlenz@69: if kind is DOCTYPE: cmlenz@85: if not have_doctype: cmlenz@85: name, pubid, sysid = data cmlenz@85: buf = ['\n') cmlenz@85: yield Markup(''.join(buf), *filter(None, data)) cmlenz@85: have_doctype = True cmlenz@1: cmlenz@69: elif kind is START_NS: cmlenz@1: prefix, uri = data cmlenz@1: if uri not in ns_mapping: cmlenz@1: ns_mapping[uri] = prefix cmlenz@1: if not prefix: cmlenz@1: ns_attrib.append((QName('xmlns'), uri)) cmlenz@1: else: cmlenz@1: ns_attrib.append((QName('xmlns:%s' % prefix), uri)) cmlenz@1: cmlenz@69: elif kind is START: cmlenz@1: tag, attrib = data cmlenz@1: cmlenz@1: tagname = tag.localname cmlenz@1: if tag.namespace: cmlenz@1: try: cmlenz@1: prefix = ns_mapping[tag.namespace] cmlenz@1: if prefix: cmlenz@69: tagname = '%s:%s' % (prefix, tag.localname) cmlenz@1: except KeyError: cmlenz@1: ns_attrib.append((QName('xmlns'), tag.namespace)) cmlenz@69: buf = ['<%s' % tagname] cmlenz@1: cmlenz@1: if ns_attrib: cmlenz@1: attrib.extend(ns_attrib) cmlenz@1: ns_attrib = [] cmlenz@1: for attr, value in attrib: cmlenz@1: attrname = attr.localname cmlenz@1: if attr.namespace: cmlenz@26: prefix = ns_mapping.get(attr.namespace) cmlenz@1: if prefix: cmlenz@69: attrname = '%s:%s' % (prefix, attrname) cmlenz@73: buf.append(' %s="%s"' % (attrname, escape(value))) cmlenz@1: cmlenz@1: kind, data, pos = stream.next() cmlenz@69: if kind is END: cmlenz@1: buf.append('/>') cmlenz@1: else: cmlenz@1: buf.append('>') cmlenz@1: stream.pushback((kind, data, pos)) cmlenz@1: cmlenz@1: yield Markup(''.join(buf)) cmlenz@1: cmlenz@69: elif kind is END: cmlenz@1: tag = data cmlenz@1: tagname = tag.localname cmlenz@1: if tag.namespace: cmlenz@26: prefix = ns_mapping.get(tag.namespace) cmlenz@26: if prefix: cmlenz@69: tagname = '%s:%s' % (prefix, tag.localname) cmlenz@1: yield Markup('' % tagname) cmlenz@1: cmlenz@69: elif kind is TEXT: cmlenz@73: yield escape(data, quotes=False) cmlenz@1: cmlenz@89: elif kind is COMMENT: cmlenz@89: yield Markup('' % data) cmlenz@89: cmlenz@1: cmlenz@96: class XHTMLSerializer(XMLSerializer): cmlenz@96: """Produces XHTML text from an event stream. cmlenz@1: cmlenz@1: >>> from markup.builder import tag cmlenz@20: >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) cmlenz@96: >>> print ''.join(XHTMLSerializer().serialize(elem.generate())) cmlenz@96:


cmlenz@1: """ cmlenz@1: cmlenz@18: NAMESPACE = Namespace('http://www.w3.org/1999/xhtml') cmlenz@1: cmlenz@1: _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame', cmlenz@1: 'hr', 'img', 'input', 'isindex', 'link', 'meta', cmlenz@1: 'param']) cmlenz@1: _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare', cmlenz@1: 'defer', 'disabled', 'ismap', 'multiple', cmlenz@1: 'nohref', 'noresize', 'noshade', 'nowrap']) cmlenz@1: cmlenz@1: def serialize(self, stream): cmlenz@85: have_doctype = False cmlenz@1: ns_mapping = {} cmlenz@1: cmlenz@85: stream = _PushbackIterator(chain(self.preamble, stream)) cmlenz@1: for kind, data, pos in stream: cmlenz@1: cmlenz@69: if kind is DOCTYPE: cmlenz@85: if not have_doctype: cmlenz@85: name, pubid, sysid = data cmlenz@85: buf = ['\n') cmlenz@85: yield Markup(''.join(buf), *filter(None, data)) cmlenz@85: have_doctype = True cmlenz@1: cmlenz@69: elif kind is START_NS: cmlenz@1: prefix, uri = data cmlenz@1: if uri not in ns_mapping: cmlenz@1: ns_mapping[uri] = prefix cmlenz@1: cmlenz@69: elif kind is START: cmlenz@1: tag, attrib = data cmlenz@18: if tag.namespace and tag not in self.NAMESPACE: cmlenz@1: continue # not in the HTML namespace, so don't emit cmlenz@1: buf = ['<', tag.localname] cmlenz@96: cmlenz@96: for attr, value in attrib: cmlenz@96: if attr.namespace and attr not in self.NAMESPACE: cmlenz@96: continue # not in the HTML namespace, so don't emit cmlenz@96: if attr.localname in self._BOOLEAN_ATTRS: cmlenz@96: if value: cmlenz@96: buf.append(' %s="%s"' % (attr.localname, attr.localname)) cmlenz@96: else: cmlenz@96: buf.append(' %s="%s"' % (attr.localname, escape(value))) cmlenz@96: cmlenz@96: if tag.localname in self._EMPTY_ELEMS: cmlenz@96: kind, data, pos = stream.next() cmlenz@96: if kind is END: cmlenz@96: buf.append(' />') cmlenz@96: else: cmlenz@96: buf.append('>') cmlenz@96: stream.pushback((kind, data, pos)) cmlenz@96: else: cmlenz@96: buf.append('>') cmlenz@96: cmlenz@96: yield Markup(''.join(buf)) cmlenz@96: cmlenz@96: elif kind is END: cmlenz@96: tag = data cmlenz@96: if tag.namespace and tag not in self.NAMESPACE: cmlenz@96: continue # not in the HTML namespace, so don't emit cmlenz@96: yield Markup('' % tag.localname) cmlenz@96: cmlenz@96: elif kind is TEXT: cmlenz@96: yield escape(data, quotes=False) cmlenz@96: cmlenz@96: elif kind is COMMENT: cmlenz@96: yield Markup('' % data) cmlenz@96: cmlenz@96: cmlenz@96: class HTMLSerializer(XHTMLSerializer): cmlenz@96: """Produces HTML text from an event stream. cmlenz@96: cmlenz@96: >>> from markup.builder import tag cmlenz@96: >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True)) cmlenz@96: >>> print ''.join(HTMLSerializer().serialize(elem.generate())) cmlenz@96:


cmlenz@96: """ cmlenz@96: cmlenz@96: def serialize(self, stream): cmlenz@96: have_doctype = False cmlenz@96: ns_mapping = {} cmlenz@96: cmlenz@96: stream = _PushbackIterator(chain(self.preamble, stream)) cmlenz@96: for kind, data, pos in stream: cmlenz@96: cmlenz@96: if kind is DOCTYPE: cmlenz@96: if not have_doctype: cmlenz@96: name, pubid, sysid = data cmlenz@96: buf = ['\n') cmlenz@96: yield Markup(''.join(buf), *filter(None, data)) cmlenz@96: have_doctype = True cmlenz@96: cmlenz@96: elif kind is START_NS: cmlenz@96: prefix, uri = data cmlenz@96: if uri not in ns_mapping: cmlenz@96: ns_mapping[uri] = prefix cmlenz@96: cmlenz@96: elif kind is START: cmlenz@96: tag, attrib = data cmlenz@96: if tag.namespace and tag not in self.NAMESPACE: cmlenz@96: continue # not in the HTML namespace, so don't emit cmlenz@96: buf = ['<', tag.localname] cmlenz@96: cmlenz@1: for attr, value in attrib: cmlenz@18: if attr.namespace and attr not in self.NAMESPACE: cmlenz@1: continue # not in the HTML namespace, so don't emit cmlenz@1: if attr.localname in self._BOOLEAN_ATTRS: cmlenz@1: if value: cmlenz@1: buf.append(' %s' % attr.localname) cmlenz@1: else: cmlenz@73: buf.append(' %s="%s"' % (attr.localname, escape(value))) cmlenz@1: cmlenz@1: if tag.localname in self._EMPTY_ELEMS: cmlenz@1: kind, data, pos = stream.next() cmlenz@69: if kind is not END: cmlenz@1: stream.pushback((kind, data, pos)) cmlenz@1: cmlenz@1: yield Markup(''.join(buf + ['>'])) cmlenz@1: cmlenz@69: elif kind is END: cmlenz@1: tag = data cmlenz@18: if tag.namespace and tag not in self.NAMESPACE: cmlenz@1: continue # not in the HTML namespace, so don't emit cmlenz@1: yield Markup('' % tag.localname) cmlenz@1: cmlenz@69: elif kind is TEXT: cmlenz@73: yield escape(data, quotes=False) cmlenz@1: cmlenz@89: elif kind is COMMENT: cmlenz@89: yield Markup('' % data) cmlenz@89: cmlenz@1: cmlenz@26: class _PushbackIterator(object): cmlenz@1: """A simple wrapper for iterators that allows pushing items back on the cmlenz@1: queue via the `pushback()` method. cmlenz@1: cmlenz@1: That can effectively be used to peek at the next item.""" cmlenz@1: __slots__ = ['iterable', 'buf'] cmlenz@1: cmlenz@1: def __init__(self, iterable): cmlenz@1: self.iterable = iter(iterable) cmlenz@1: self.buf = [] cmlenz@1: cmlenz@1: def __iter__(self): cmlenz@1: return self cmlenz@1: cmlenz@1: def next(self): cmlenz@1: if self.buf: cmlenz@1: return self.buf.pop(0) cmlenz@1: return self.iterable.next() cmlenz@1: cmlenz@1: def pushback(self, item): cmlenz@1: self.buf.append(item)