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@123: from itertools import chain cmlenz@1: try: cmlenz@1: frozenset cmlenz@1: except NameError: cmlenz@1: from sets import ImmutableSet as frozenset cmlenz@123: import re cmlenz@1: cmlenz@145: from markup.core import escape, Markup, Namespace, QName cmlenz@145: from markup.core import DOCTYPE, START, END, START_NS, TEXT, START_CDATA, \ cmlenz@145: END_CDATA, PI, COMMENT, XML_NAMESPACE cmlenz@1: cmlenz@1: __all__ = ['Serializer', 'XMLSerializer', 'HTMLSerializer'] 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@123: class XMLSerializer(object): 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@123: >>> print ''.join(XMLSerializer()(elem.generate())) cmlenz@1:


cmlenz@1: """ cmlenz@123: cmlenz@123: _PRESERVE_SPACE = frozenset() cmlenz@123: cmlenz@123: def __init__(self, doctype=None, strip_whitespace=True): 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@123: @param strip_whitespace: whether extraneous whitespace should be cmlenz@123: stripped from the output cmlenz@85: """ cmlenz@85: self.preamble = [] cmlenz@85: if doctype: cmlenz@85: self.preamble.append((DOCTYPE, doctype, (None, -1, -1))) cmlenz@123: self.filters = [] cmlenz@123: if strip_whitespace: cmlenz@123: self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE)) cmlenz@1: cmlenz@123: def __call__(self, stream): cmlenz@1: ns_attrib = [] cmlenz@141: ns_mapping = {XML_NAMESPACE.uri: 'xml'} cmlenz@143: have_doctype = False cmlenz@143: in_cdata = False cmlenz@1: cmlenz@123: stream = chain(self.preamble, stream) cmlenz@123: for filter_ in self.filters: cmlenz@123: stream = filter_(stream) cmlenz@123: stream = _PushbackIterator(stream) cmlenz@136: pushback = stream.pushback cmlenz@1: for kind, data, pos in stream: cmlenz@1: cmlenz@109: if kind is START: cmlenz@1: tag, attrib = data cmlenz@1: cmlenz@1: tagname = tag.localname cmlenz@123: namespace = tag.namespace cmlenz@123: if namespace: cmlenz@123: if namespace in ns_mapping: cmlenz@123: prefix = ns_mapping[namespace] cmlenz@1: if prefix: cmlenz@123: tagname = '%s:%s' % (prefix, tagname) cmlenz@123: else: cmlenz@123: ns_attrib.append((QName('xmlns'), namespace)) cmlenz@136: buf = ['<', tagname] cmlenz@1: cmlenz@123: for attr, value in attrib + ns_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@136: buf += [' ', attrname, '="', escape(value), '"'] cmlenz@123: ns_attrib = [] cmlenz@1: cmlenz@1: kind, data, pos = stream.next() cmlenz@69: if kind is END: cmlenz@136: buf += ['/>'] cmlenz@1: else: cmlenz@136: buf += ['>'] cmlenz@136: 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@143: if in_cdata: cmlenz@143: yield data cmlenz@143: else: cmlenz@143: yield escape(data, quotes=False) cmlenz@1: cmlenz@89: elif kind is COMMENT: cmlenz@89: yield Markup('' % data) cmlenz@89: cmlenz@136: elif kind is DOCTYPE and not have_doctype: cmlenz@136: name, pubid, sysid = data cmlenz@136: buf = ['\n'] cmlenz@136: yield Markup(''.join(buf), *filter(None, data)) cmlenz@136: have_doctype = True cmlenz@109: cmlenz@109: elif kind is START_NS: cmlenz@109: prefix, uri = data cmlenz@109: if uri not in ns_mapping: cmlenz@109: ns_mapping[uri] = prefix cmlenz@109: if not prefix: cmlenz@109: ns_attrib.append((QName('xmlns'), uri)) cmlenz@109: else: cmlenz@109: ns_attrib.append((QName('xmlns:%s' % prefix), uri)) cmlenz@109: cmlenz@143: elif kind is START_CDATA: cmlenz@143: yield Markup('') cmlenz@143: in_cdata = False cmlenz@143: cmlenz@105: elif kind is PI: cmlenz@105: yield Markup('' % data) cmlenz@105: 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@123: >>> print ''.join(XHTMLSerializer()(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@123: _PRESERVE_SPACE = frozenset([QName('pre'), QName('textarea')]) cmlenz@1: cmlenz@123: def __call__(self, stream): cmlenz@136: namespace = self.NAMESPACE cmlenz@141: ns_attrib = [] cmlenz@141: ns_mapping = {XML_NAMESPACE.uri: 'xml'} cmlenz@136: boolean_attrs = self._BOOLEAN_ATTRS cmlenz@136: empty_elems = self._EMPTY_ELEMS cmlenz@85: have_doctype = False cmlenz@143: in_cdata = False cmlenz@1: cmlenz@123: stream = chain(self.preamble, stream) cmlenz@123: for filter_ in self.filters: cmlenz@123: stream = filter_(stream) cmlenz@123: stream = _PushbackIterator(stream) cmlenz@136: pushback = stream.pushback cmlenz@1: for kind, data, pos in stream: cmlenz@1: cmlenz@109: if kind is START: cmlenz@1: tag, attrib = data cmlenz@96: cmlenz@141: tagname = tag.localname cmlenz@141: namespace = tag.namespace cmlenz@141: if namespace: cmlenz@141: if namespace in ns_mapping: cmlenz@141: prefix = ns_mapping[namespace] cmlenz@141: if prefix: cmlenz@141: tagname = '%s:%s' % (prefix, tagname) cmlenz@141: else: cmlenz@141: ns_attrib.append((QName('xmlns'), namespace)) cmlenz@141: buf = ['<', tagname] cmlenz@136: cmlenz@141: for attr, value in attrib + ns_attrib: cmlenz@141: attrname = attr.localname cmlenz@141: if attr.namespace: cmlenz@141: prefix = ns_mapping.get(attr.namespace) cmlenz@141: if prefix: cmlenz@141: attrname = '%s:%s' % (prefix, attrname) cmlenz@141: if attrname in boolean_attrs: cmlenz@141: if value: cmlenz@141: buf += [' ', attrname, '="', attrname, '"'] cmlenz@141: else: cmlenz@141: buf += [' ', attrname, '="', escape(value), '"'] cmlenz@141: ns_attrib = [] cmlenz@141: cmlenz@141: if (not tag.namespace or tag in namespace) and \ cmlenz@141: tagname in empty_elems: cmlenz@141: kind, data, pos = stream.next() cmlenz@141: if kind is END: cmlenz@141: buf += [' />'] cmlenz@96: else: cmlenz@136: buf += ['>'] cmlenz@141: pushback((kind, data, pos)) cmlenz@141: else: cmlenz@141: buf += ['>'] cmlenz@96: cmlenz@141: yield Markup(''.join(buf)) cmlenz@96: cmlenz@96: elif kind is END: cmlenz@96: tag = data cmlenz@141: tagname = tag.localname cmlenz@141: if tag.namespace: cmlenz@141: prefix = ns_mapping.get(tag.namespace) cmlenz@141: if prefix: cmlenz@141: tagname = '%s:%s' % (prefix, tag.localname) cmlenz@141: yield Markup('' % tagname) cmlenz@96: cmlenz@96: elif kind is TEXT: cmlenz@143: if in_cdata: cmlenz@143: yield data cmlenz@143: else: cmlenz@143: yield escape(data, quotes=False) cmlenz@96: cmlenz@96: elif kind is COMMENT: cmlenz@96: yield Markup('' % data) cmlenz@96: cmlenz@136: elif kind is DOCTYPE and not have_doctype: cmlenz@136: name, pubid, sysid = data cmlenz@136: buf = ['\n'] cmlenz@136: yield Markup(''.join(buf), *filter(None, data)) cmlenz@136: have_doctype = True cmlenz@109: cmlenz@141: elif kind is START_NS: cmlenz@141: prefix, uri = data cmlenz@141: if uri not in ns_mapping: cmlenz@141: ns_mapping[uri] = prefix cmlenz@141: if not prefix: cmlenz@141: ns_attrib.append((QName('xmlns'), uri)) cmlenz@141: else: cmlenz@141: ns_attrib.append((QName('xmlns:%s' % prefix), uri)) cmlenz@109: cmlenz@143: elif kind is START_CDATA: cmlenz@143: yield Markup('') cmlenz@143: in_cdata = False cmlenz@143: cmlenz@105: elif kind is PI: cmlenz@105: yield Markup('' % data) cmlenz@105: 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@123: >>> print ''.join(HTMLSerializer()(elem.generate())) cmlenz@96:


cmlenz@96: """ cmlenz@96: cmlenz@141: _NOESCAPE_ELEMS = frozenset([QName('script'), QName('style')]) cmlenz@141: cmlenz@141: def __init__(self, doctype=None, strip_whitespace=True): cmlenz@141: """Initialize the HTML serializer. cmlenz@141: cmlenz@141: @param doctype: a `(name, pubid, sysid)` tuple that represents the cmlenz@141: DOCTYPE declaration that should be included at the top of the cmlenz@141: generated output cmlenz@141: @param strip_whitespace: whether extraneous whitespace should be cmlenz@141: stripped from the output cmlenz@141: """ cmlenz@141: super(HTMLSerializer, self).__init__(doctype, False) cmlenz@141: if strip_whitespace: cmlenz@141: self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE, cmlenz@143: self._NOESCAPE_ELEMS, True)) cmlenz@141: cmlenz@123: def __call__(self, stream): cmlenz@136: namespace = self.NAMESPACE cmlenz@136: ns_mapping = {} cmlenz@136: boolean_attrs = self._BOOLEAN_ATTRS cmlenz@136: empty_elems = self._EMPTY_ELEMS cmlenz@141: noescape_elems = self._NOESCAPE_ELEMS cmlenz@96: have_doctype = False cmlenz@141: noescape = False cmlenz@96: cmlenz@123: stream = chain(self.preamble, stream) cmlenz@123: for filter_ in self.filters: cmlenz@123: stream = filter_(stream) cmlenz@123: stream = _PushbackIterator(stream) cmlenz@141: pushback = stream.pushback cmlenz@96: for kind, data, pos in stream: cmlenz@96: cmlenz@109: if kind is START: cmlenz@96: tag, attrib = data cmlenz@136: if not tag.namespace or tag in namespace: cmlenz@136: tagname = tag.localname cmlenz@136: buf = ['<', tagname] cmlenz@96: cmlenz@136: for attr, value in attrib: cmlenz@136: attrname = attr.localname cmlenz@141: if not attr.namespace or attr in namespace: cmlenz@136: if attrname in boolean_attrs: cmlenz@136: if value: cmlenz@136: buf += [' ', attrname] cmlenz@136: else: cmlenz@136: buf += [' ', attrname, '="', escape(value), '"'] cmlenz@1: cmlenz@136: if tagname in empty_elems: cmlenz@136: kind, data, pos = stream.next() cmlenz@136: if kind is not END: cmlenz@141: pushback((kind, data, pos)) cmlenz@1: cmlenz@140: buf += ['>'] cmlenz@140: yield Markup(''.join(buf)) cmlenz@1: cmlenz@141: if tagname in noescape_elems: cmlenz@141: noescape = True cmlenz@141: cmlenz@69: elif kind is END: cmlenz@1: tag = data cmlenz@136: if not tag.namespace or tag in namespace: cmlenz@136: yield Markup('' % tag.localname) cmlenz@1: cmlenz@141: noescape = False cmlenz@141: cmlenz@69: elif kind is TEXT: cmlenz@141: if noescape: cmlenz@141: yield data cmlenz@141: else: cmlenz@141: yield escape(data, quotes=False) cmlenz@1: cmlenz@89: elif kind is COMMENT: cmlenz@89: yield Markup('' % data) cmlenz@89: cmlenz@136: elif kind is DOCTYPE and not have_doctype: cmlenz@136: name, pubid, sysid = data cmlenz@136: buf = ['\n'] cmlenz@136: yield Markup(''.join(buf), *filter(None, data)) cmlenz@136: have_doctype = True cmlenz@109: cmlenz@136: elif kind is START_NS and data[1] not in ns_mapping: cmlenz@136: ns_mapping[data[1]] = data[0] cmlenz@109: cmlenz@105: elif kind is PI: cmlenz@105: yield Markup('' % data) cmlenz@105: cmlenz@1: cmlenz@123: class WhitespaceFilter(object): cmlenz@123: """A filter that removes extraneous ignorable white space from the cmlenz@123: stream.""" cmlenz@123: cmlenz@123: _TRAILING_SPACE = re.compile('[ \t]+(?=\n)') cmlenz@123: _LINE_COLLAPSE = re.compile('\n{2,}') cmlenz@141: _XML_SPACE = XML_NAMESPACE['space'] cmlenz@123: cmlenz@143: def __init__(self, preserve=None, noescape=None, escape_cdata=False): cmlenz@123: """Initialize the filter. cmlenz@123: cmlenz@141: @param preserve: a set or sequence of tag names for which white-space cmlenz@141: should be ignored. cmlenz@141: @param noescape: a set or sequence of tag names for which text content cmlenz@141: should not be escaped cmlenz@141: cmlenz@141: Both the `preserve` and `noescape` sets are expected to refer to cmlenz@141: elements that cannot contain further child elements. cmlenz@123: """ cmlenz@123: if preserve is None: cmlenz@123: preserve = [] cmlenz@123: self.preserve = frozenset(preserve) cmlenz@141: if noescape is None: cmlenz@141: noescape = [] cmlenz@141: self.noescape = frozenset(noescape) cmlenz@143: self.escape_cdata = escape_cdata cmlenz@123: cmlenz@123: def __call__(self, stream, ctxt=None): cmlenz@123: trim_trailing_space = self._TRAILING_SPACE.sub cmlenz@123: collapse_lines = self._LINE_COLLAPSE.sub cmlenz@141: xml_space = self._XML_SPACE cmlenz@123: mjoin = Markup('').join cmlenz@141: preserve_elems = self.preserve cmlenz@141: preserve = False cmlenz@141: noescape_elems = self.noescape cmlenz@141: noescape = False cmlenz@143: escape_cdata = self.escape_cdata cmlenz@123: cmlenz@123: textbuf = [] cmlenz@141: push_text = textbuf.append cmlenz@136: pop_text = textbuf.pop cmlenz@123: for kind, data, pos in chain(stream, [(None, None, None)]): cmlenz@123: if kind is TEXT: cmlenz@141: if noescape: cmlenz@141: data = Markup(data) cmlenz@141: push_text(data) cmlenz@123: else: cmlenz@123: if textbuf: cmlenz@123: if len(textbuf) > 1: cmlenz@123: text = mjoin(textbuf, escape_quotes=False) cmlenz@123: del textbuf[:] cmlenz@123: else: cmlenz@136: text = escape(pop_text(), quotes=False) cmlenz@141: if not preserve: cmlenz@123: text = collapse_lines('\n', trim_trailing_space('', text)) cmlenz@123: yield TEXT, Markup(text), pos cmlenz@141: cmlenz@141: if kind is START: cmlenz@141: tag, attrib = data cmlenz@141: if tag.localname in preserve_elems or \ cmlenz@141: data[1].get(xml_space) == 'preserve': cmlenz@141: preserve = True cmlenz@141: cmlenz@141: if tag.localname in noescape_elems: cmlenz@141: noescape = True cmlenz@141: cmlenz@141: elif kind is END: cmlenz@141: preserve = noescape = False cmlenz@141: cmlenz@143: elif kind is START_CDATA and not escape_cdata: cmlenz@143: noescape = True cmlenz@143: cmlenz@143: elif kind is END_CDATA and not escape_cdata: cmlenz@143: noescape = False cmlenz@143: cmlenz@136: if kind: cmlenz@123: yield kind, data, pos cmlenz@123: cmlenz@123: 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)