cmlenz@1: # -*- coding: utf-8 -*- cmlenz@1: # cmlenz@408: # Copyright (C) 2006-2007 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@230: # are also available at http://genshi.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@230: # history and logs, available at http://genshi.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@410: from genshi.core import escape, Attrs, Markup, Namespace, QName, StreamEventKind cmlenz@460: from genshi.core import START, END, TEXT, XML_DECL, DOCTYPE, START_NS, END_NS, \ cmlenz@402: START_CDATA, END_CDATA, PI, COMMENT, XML_NAMESPACE cmlenz@1: cmlenz@462: __all__ = ['encode', 'get_serializer', 'DocType', 'XMLSerializer', cmlenz@462: 'XHTMLSerializer', 'HTMLSerializer', 'TextSerializer'] cmlenz@425: __docformat__ = 'restructuredtext en' cmlenz@1: cmlenz@462: def encode(iterator, method='xml', encoding='utf-8'): cmlenz@462: """Encode serializer output into a string. cmlenz@462: cmlenz@462: :param iterator: the iterator returned from serializing a stream (basically cmlenz@462: any iterator that yields unicode objects) cmlenz@462: :param method: the serialization method; determines how characters not cmlenz@462: representable in the specified encoding are treated cmlenz@462: :param encoding: how the output string should be encoded; if set to `None`, cmlenz@462: this method returns a `unicode` object cmlenz@462: :return: a string or unicode object (depending on the `encoding` parameter) cmlenz@462: :since: version 0.4.1 cmlenz@462: """ cmlenz@462: output = u''.join(list(iterator)) cmlenz@462: if encoding is not None: cmlenz@462: errors = 'replace' cmlenz@462: if method != 'text' and not isinstance(method, TextSerializer): cmlenz@462: errors = 'xmlcharrefreplace' cmlenz@462: return output.encode(encoding, errors) cmlenz@462: return output cmlenz@462: cmlenz@462: def get_serializer(method='xml', **kwargs): cmlenz@462: """Return a serializer object for the given method. cmlenz@462: cmlenz@462: :param method: the serialization method; can be either "xml", "xhtml", cmlenz@462: "html", "text", or a custom serializer class cmlenz@462: cmlenz@462: Any additional keyword arguments are passed to the serializer, and thus cmlenz@462: depend on the `method` parameter value. cmlenz@462: cmlenz@462: :see: `XMLSerializer`, `XHTMLSerializer`, `HTMLSerializer`, `TextSerializer` cmlenz@462: :since: version 0.4.1 cmlenz@462: """ cmlenz@462: if isinstance(method, basestring): cmlenz@462: method = {'xml': XMLSerializer, cmlenz@462: 'xhtml': XHTMLSerializer, cmlenz@462: 'html': HTMLSerializer, cmlenz@462: 'text': TextSerializer}[method.lower()] cmlenz@462: return method(**kwargs) cmlenz@462: cmlenz@1: cmlenz@85: class DocType(object): cmlenz@85: """Defines a number of commonly used DOCTYPE declarations as constants.""" cmlenz@85: cmlenz@410: HTML_STRICT = ( cmlenz@410: 'html', '-//W3C//DTD HTML 4.01//EN', cmlenz@410: 'http://www.w3.org/TR/html4/strict.dtd' cmlenz@410: ) cmlenz@410: HTML_TRANSITIONAL = ( cmlenz@410: 'html', '-//W3C//DTD HTML 4.01 Transitional//EN', cmlenz@410: 'http://www.w3.org/TR/html4/loose.dtd' cmlenz@410: ) cmlenz@464: HTML_FRAMESET = ( cmlenz@464: 'html', '-//W3C//DTD HTML 4.01 Frameset//EN', cmlenz@464: 'http://www.w3.org/TR/html4/frameset.dtd' cmlenz@464: ) cmlenz@85: HTML = HTML_STRICT cmlenz@85: cmlenz@464: HTML5 = ('html', None, None) cmlenz@464: cmlenz@410: XHTML_STRICT = ( cmlenz@410: 'html', '-//W3C//DTD XHTML 1.0 Strict//EN', cmlenz@410: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd' cmlenz@410: ) cmlenz@410: XHTML_TRANSITIONAL = ( cmlenz@410: 'html', '-//W3C//DTD XHTML 1.0 Transitional//EN', cmlenz@410: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd' cmlenz@410: ) cmlenz@464: XHTML_FRAMESET = ( cmlenz@464: 'html', '-//W3C//DTD XHTML 1.0 Frameset//EN', cmlenz@464: 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd' cmlenz@464: ) cmlenz@85: XHTML = XHTML_STRICT cmlenz@85: cmlenz@464: def get(cls, name): cmlenz@464: """Return the ``(name, pubid, sysid)`` tuple of the ``DOCTYPE`` cmlenz@464: declaration for the specified name. cmlenz@464: cmlenz@464: The following names are recognized in this version: cmlenz@464: * "html" or "html-strict" for the HTML 4.01 strict DTD cmlenz@464: * "html-transitional" for the HTML 4.01 transitional DTD cmlenz@464: * "html-transitional" for the HTML 4.01 frameset DTD cmlenz@464: * "html5" for the ``DOCTYPE`` proposed for HTML5 cmlenz@464: * "xhtml" or "xhtml-strict" for the XHTML 1.0 strict DTD cmlenz@464: * "xhtml-transitional" for the XHTML 1.0 transitional DTD cmlenz@464: * "xhtml-frameset" for the XHTML 1.0 frameset DTD cmlenz@464: cmlenz@464: :param name: the name of the ``DOCTYPE`` cmlenz@464: :return: the ``(name, pubid, sysid)`` tuple for the requested cmlenz@464: ``DOCTYPE``, or ``None`` if the name is not recognized cmlenz@464: :since: version 0.4.1 cmlenz@464: """ cmlenz@464: return { cmlenz@464: 'html': cls.HTML, 'html-strict': cls.HTML_STRICT, cmlenz@464: 'html-transitional': DocType.HTML_TRANSITIONAL, cmlenz@464: 'html-frameset': DocType.HTML_FRAMESET, cmlenz@464: 'html5': cls.HTML5, cmlenz@464: 'xhtml': cls.XHTML, 'xhtml-strict': cls.XHTML_STRICT, cmlenz@464: 'xhtml-transitional': cls.XHTML_TRANSITIONAL, cmlenz@464: 'xhtml-frameset': cls.XHTML_FRAMESET, cmlenz@464: }.get(name.lower()) cmlenz@464: get = classmethod(get) cmlenz@448: cmlenz@85: cmlenz@123: class XMLSerializer(object): cmlenz@1: """Produces XML text from an event stream. cmlenz@1: cmlenz@230: >>> from genshi.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@410: def __init__(self, doctype=None, strip_whitespace=True, cmlenz@410: namespace_prefixes=None): cmlenz@85: """Initialize the XML serializer. cmlenz@85: cmlenz@425: :param doctype: a ``(name, pubid, sysid)`` tuple that represents the cmlenz@425: DOCTYPE declaration that should be included at the top cmlenz@494: of the generated output, or the name of a DOCTYPE as cmlenz@494: defined in `DocType.get` cmlenz@425: :param strip_whitespace: whether extraneous whitespace should be cmlenz@425: stripped from the output cmlenz@494: :note: Changed in 0.4.2: The `doctype` parameter can now be a string. cmlenz@85: """ cmlenz@85: self.preamble = [] cmlenz@85: if doctype: cmlenz@494: if isinstance(doctype, basestring): cmlenz@494: doctype = DocType.get(doctype) cmlenz@85: self.preamble.append((DOCTYPE, doctype, (None, -1, -1))) cmlenz@212: self.filters = [EmptyTagFilter()] cmlenz@123: if strip_whitespace: cmlenz@123: self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE)) cmlenz@410: self.filters.append(NamespaceFlattener(prefixes=namespace_prefixes)) cmlenz@1: cmlenz@123: def __call__(self, stream): cmlenz@460: have_decl = 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@1: for kind, data, pos in stream: cmlenz@1: cmlenz@212: if kind is START or kind is EMPTY: cmlenz@1: tag, attrib = data cmlenz@410: buf = ['<', tag] cmlenz@397: for attr, value in attrib: cmlenz@410: buf += [' ', attr, '="', escape(value), '"'] cmlenz@397: buf.append(kind is EMPTY and '/>' or '>') cmlenz@397: yield Markup(u''.join(buf)) cmlenz@1: cmlenz@69: elif kind is END: cmlenz@410: yield Markup('' % data) 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@460: elif kind is XML_DECL and not have_decl: cmlenz@460: version, encoding, standalone = data cmlenz@460: buf = ['\n') cmlenz@460: yield Markup(u''.join(buf)) cmlenz@460: have_decl = True cmlenz@460: cmlenz@136: elif kind is DOCTYPE and not have_doctype: cmlenz@136: name, pubid, sysid = data cmlenz@136: buf = ['\n') cmlenz@397: yield Markup(u''.join(buf), *filter(None, data)) cmlenz@136: have_doctype = True 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@230: >>> from genshi.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@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@346: _PRESERVE_SPACE = frozenset([ cmlenz@346: QName('pre'), QName('http://www.w3.org/1999/xhtml}pre'), cmlenz@346: QName('textarea'), QName('http://www.w3.org/1999/xhtml}textarea') cmlenz@346: ]) cmlenz@1: cmlenz@410: def __init__(self, doctype=None, strip_whitespace=True, cmlenz@410: namespace_prefixes=None): cmlenz@410: super(XHTMLSerializer, self).__init__(doctype, False) cmlenz@410: self.filters = [EmptyTagFilter()] cmlenz@410: if strip_whitespace: cmlenz@410: self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE)) cmlenz@410: namespace_prefixes = namespace_prefixes or {} cmlenz@410: namespace_prefixes['http://www.w3.org/1999/xhtml'] = '' cmlenz@410: self.filters.append(NamespaceFlattener(prefixes=namespace_prefixes)) cmlenz@410: cmlenz@123: def __call__(self, stream): 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@1: for kind, data, pos in stream: cmlenz@1: cmlenz@212: if kind is START or kind is EMPTY: cmlenz@1: tag, attrib = data cmlenz@410: buf = ['<', tag] cmlenz@397: for attr, value in attrib: cmlenz@410: if attr in boolean_attrs: cmlenz@410: value = attr cmlenz@524: elif attr == u'xml:lang' and u'lang' not in attrib: cmlenz@524: buf += [' lang="', escape(value), '"'] cmlenz@410: buf += [' ', attr, '="', escape(value), '"'] cmlenz@212: if kind is EMPTY: cmlenz@410: if tag in empty_elems: cmlenz@397: buf.append(' />') cmlenz@96: else: cmlenz@410: buf.append('>' % tag) cmlenz@141: else: cmlenz@397: buf.append('>') cmlenz@397: yield Markup(u''.join(buf)) cmlenz@96: cmlenz@96: elif kind is END: cmlenz@410: yield Markup('' % data) 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@397: yield Markup(u''.join(buf), *filter(None, data)) cmlenz@136: have_doctype = True 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@230: >>> from genshi.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@410: _NOESCAPE_ELEMS = frozenset([ cmlenz@410: QName('script'), QName('http://www.w3.org/1999/xhtml}script'), cmlenz@410: QName('style'), QName('http://www.w3.org/1999/xhtml}style') cmlenz@410: ]) cmlenz@141: cmlenz@141: def __init__(self, doctype=None, strip_whitespace=True): cmlenz@141: """Initialize the HTML serializer. cmlenz@141: cmlenz@425: :param doctype: a ``(name, pubid, sysid)`` tuple that represents the cmlenz@425: DOCTYPE declaration that should be included at the top cmlenz@425: of the generated output cmlenz@425: :param strip_whitespace: whether extraneous whitespace should be cmlenz@425: stripped from the output cmlenz@141: """ cmlenz@141: super(HTMLSerializer, self).__init__(doctype, False) cmlenz@410: self.filters = [EmptyTagFilter()] cmlenz@141: if strip_whitespace: cmlenz@141: self.filters.append(WhitespaceFilter(self._PRESERVE_SPACE, cmlenz@305: self._NOESCAPE_ELEMS)) cmlenz@524: self.filters.append(NamespaceFlattener(prefixes={ cmlenz@524: 'http://www.w3.org/1999/xhtml': '' cmlenz@524: })) cmlenz@141: cmlenz@123: def __call__(self, stream): 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@96: for kind, data, pos in stream: cmlenz@96: cmlenz@212: if kind is START or kind is EMPTY: cmlenz@96: tag, attrib = data cmlenz@410: buf = ['<', tag] cmlenz@410: for attr, value in attrib: cmlenz@410: if attr in boolean_attrs: cmlenz@410: if value: cmlenz@410: buf += [' ', attr] cmlenz@524: elif ':' in attr: cmlenz@524: if attr == 'xml:lang' and u'lang' not in attrib: cmlenz@524: buf += [' lang="', escape(value), '"'] cmlenz@524: elif attr != 'xmlns': cmlenz@410: buf += [' ', attr, '="', escape(value), '"'] cmlenz@410: buf.append('>') cmlenz@410: if kind is EMPTY: cmlenz@410: if tag not in empty_elems: cmlenz@410: buf.append('' % tag) cmlenz@410: yield Markup(u''.join(buf)) cmlenz@410: if tag in noescape_elems: cmlenz@410: noescape = True cmlenz@141: cmlenz@69: elif kind is END: cmlenz@410: yield Markup('' % data) 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@397: yield Markup(u''.join(buf), *filter(None, data)) cmlenz@136: have_doctype = True cmlenz@109: cmlenz@105: elif kind is PI: cmlenz@105: yield Markup('' % data) cmlenz@105: cmlenz@1: cmlenz@200: class TextSerializer(object): cmlenz@200: """Produces plain text from an event stream. cmlenz@200: cmlenz@200: Only text events are included in the output. Unlike the other serializer, cmlenz@200: special XML characters are not escaped: cmlenz@200: cmlenz@230: >>> from genshi.builder import tag cmlenz@200: >>> elem = tag.div(tag.a('', href='foo'), tag.br) cmlenz@200: >>> print elem cmlenz@200:
<Hello!>
cmlenz@200: >>> print ''.join(TextSerializer()(elem.generate())) cmlenz@200: cmlenz@200: cmlenz@200: If text events contain literal markup (instances of the `Markup` class), cmlenz@200: tags or entities are stripped from the output: cmlenz@200: cmlenz@200: >>> elem = tag.div(Markup('Hello!
')) cmlenz@200: >>> print elem cmlenz@200:
Hello!
cmlenz@200: >>> print ''.join(TextSerializer()(elem.generate())) cmlenz@200: Hello! cmlenz@200: """ cmlenz@200: cmlenz@200: def __call__(self, stream): cmlenz@410: for event in stream: cmlenz@410: if event[0] is TEXT: cmlenz@410: data = event[1] cmlenz@200: if type(data) is Markup: cmlenz@200: data = data.striptags().stripentities() cmlenz@201: yield unicode(data) cmlenz@200: cmlenz@200: cmlenz@212: class EmptyTagFilter(object): cmlenz@212: """Combines `START` and `STOP` events into `EMPTY` events for elements that cmlenz@212: have no contents. cmlenz@212: """ cmlenz@212: cmlenz@212: EMPTY = StreamEventKind('EMPTY') cmlenz@212: cmlenz@212: def __call__(self, stream): cmlenz@212: prev = (None, None, None) cmlenz@410: for ev in stream: cmlenz@212: if prev[0] is START: cmlenz@410: if ev[0] is END: cmlenz@212: prev = EMPTY, prev[1], prev[2] cmlenz@212: yield prev cmlenz@212: continue cmlenz@212: else: cmlenz@212: yield prev cmlenz@410: if ev[0] is not START: cmlenz@410: yield ev cmlenz@410: prev = ev cmlenz@212: cmlenz@212: cmlenz@212: EMPTY = EmptyTagFilter.EMPTY cmlenz@212: cmlenz@212: cmlenz@410: class NamespaceFlattener(object): cmlenz@410: r"""Output stream filter that removes namespace information from the stream, cmlenz@410: instead adding namespace attributes and prefixes as needed. cmlenz@410: cmlenz@425: :param prefixes: optional mapping of namespace URIs to prefixes cmlenz@410: cmlenz@410: >>> from genshi.input import XML cmlenz@410: >>> xml = XML(''' cmlenz@410: ... cmlenz@410: ... ''') cmlenz@410: >>> for kind, data, pos in NamespaceFlattener()(xml): cmlenz@410: ... print kind, repr(data) cmlenz@410: START (u'doc', Attrs([(u'xmlns', u'NS1'), (u'xmlns:two', u'NS2')])) cmlenz@410: TEXT u'\n ' cmlenz@410: START (u'two:item', Attrs()) cmlenz@410: END u'two:item' cmlenz@410: TEXT u'\n' cmlenz@410: END u'doc' cmlenz@410: """ cmlenz@410: cmlenz@410: def __init__(self, prefixes=None): cmlenz@410: self.prefixes = {XML_NAMESPACE.uri: 'xml'} cmlenz@410: if prefixes is not None: cmlenz@410: self.prefixes.update(prefixes) cmlenz@410: cmlenz@410: def __call__(self, stream): cmlenz@410: prefixes = dict([(v, [k]) for k, v in self.prefixes.items()]) cmlenz@410: namespaces = {XML_NAMESPACE.uri: ['xml']} cmlenz@410: def _push_ns(prefix, uri): cmlenz@410: namespaces.setdefault(uri, []).append(prefix) cmlenz@410: prefixes.setdefault(prefix, []).append(uri) cmlenz@410: cmlenz@410: ns_attrs = [] cmlenz@410: _push_ns_attr = ns_attrs.append cmlenz@437: def _make_ns_attr(prefix, uri): cmlenz@437: return u'xmlns%s' % (prefix and ':%s' % prefix or ''), uri cmlenz@410: cmlenz@410: def _gen_prefix(): cmlenz@410: val = 0 cmlenz@410: while 1: cmlenz@410: val += 1 cmlenz@410: yield 'ns%d' % val cmlenz@410: _gen_prefix = _gen_prefix().next cmlenz@410: cmlenz@410: for kind, data, pos in stream: cmlenz@410: cmlenz@410: if kind is START or kind is EMPTY: cmlenz@410: tag, attrs = data cmlenz@410: cmlenz@410: tagname = tag.localname cmlenz@410: tagns = tag.namespace cmlenz@410: if tagns: cmlenz@410: if tagns in namespaces: cmlenz@410: prefix = namespaces[tagns][-1] cmlenz@410: if prefix: cmlenz@410: tagname = u'%s:%s' % (prefix, tagname) cmlenz@410: else: cmlenz@410: _push_ns_attr((u'xmlns', tagns)) cmlenz@410: _push_ns('', tagns) cmlenz@410: cmlenz@410: new_attrs = [] cmlenz@410: for attr, value in attrs: cmlenz@410: attrname = attr.localname cmlenz@410: attrns = attr.namespace cmlenz@410: if attrns: cmlenz@410: if attrns not in namespaces: cmlenz@410: prefix = _gen_prefix() cmlenz@410: _push_ns(prefix, attrns) cmlenz@412: _push_ns_attr(('xmlns:%s' % prefix, attrns)) cmlenz@410: else: cmlenz@410: prefix = namespaces[attrns][-1] cmlenz@410: if prefix: cmlenz@410: attrname = u'%s:%s' % (prefix, attrname) cmlenz@410: new_attrs.append((attrname, value)) cmlenz@410: cmlenz@410: yield kind, (tagname, Attrs(ns_attrs + new_attrs)), pos cmlenz@410: del ns_attrs[:] cmlenz@410: cmlenz@410: elif kind is END: cmlenz@410: tagname = data.localname cmlenz@410: tagns = data.namespace cmlenz@410: if tagns: cmlenz@410: prefix = namespaces[tagns][-1] cmlenz@410: if prefix: cmlenz@410: tagname = u'%s:%s' % (prefix, tagname) cmlenz@410: yield kind, tagname, pos cmlenz@410: cmlenz@410: elif kind is START_NS: cmlenz@410: prefix, uri = data cmlenz@410: if uri not in namespaces: cmlenz@410: prefix = prefixes.get(uri, [prefix])[-1] cmlenz@437: _push_ns_attr(_make_ns_attr(prefix, uri)) cmlenz@410: _push_ns(prefix, uri) cmlenz@410: cmlenz@410: elif kind is END_NS: cmlenz@410: if data in prefixes: cmlenz@410: uris = prefixes.get(data) cmlenz@410: uri = uris.pop() cmlenz@410: if not uris: cmlenz@410: del prefixes[data] cmlenz@410: if uri not in uris or uri != uris[-1]: cmlenz@410: uri_prefixes = namespaces[uri] cmlenz@410: uri_prefixes.pop() cmlenz@410: if not uri_prefixes: cmlenz@410: del namespaces[uri] cmlenz@437: if ns_attrs: cmlenz@437: attr = _make_ns_attr(data, uri) cmlenz@437: if attr in ns_attrs: cmlenz@437: ns_attrs.remove(attr) cmlenz@410: cmlenz@410: else: cmlenz@410: yield kind, data, pos cmlenz@410: cmlenz@410: cmlenz@123: class WhitespaceFilter(object): cmlenz@123: """A filter that removes extraneous ignorable white space from the cmlenz@410: stream. cmlenz@410: """ cmlenz@123: cmlenz@305: def __init__(self, preserve=None, noescape=None): cmlenz@123: """Initialize the filter. cmlenz@123: cmlenz@425: :param preserve: a set or sequence of tag names for which white-space cmlenz@425: should be preserved cmlenz@425: :param noescape: a set or sequence of tag names for which text content cmlenz@425: should not be escaped cmlenz@141: cmlenz@346: The `noescape` set is expected to refer to elements that cannot contain cmlenz@425: further child elements (such as ``