def __call__(self, stream):
    boolean_attrs = self._BOOLEAN_ATTRS
    empty_elems = self._EMPTY_ELEMS
    noescape_elems = self._NOESCAPE_ELEMS
    have_doctype = False
    noescape = False
    (_emit, _get) = self._prepare_cache()
    for filter_ in self.filters:
        stream = filter_(stream)
    for (kind, data, _) in stream:
        if ((kind is TEXT) and isinstance(data, Markup)):
            (yield data)
            continue
        output = _get((kind, data))
        if (output is not None):
            (yield output)
            if (((kind is START) or (kind is EMPTY)) and (data[0] in noescape_elems)):
                noescape = True
            elif (kind is END):
                noescape = False
        elif ((kind is START) or (kind is EMPTY)):
            (tag, attrib) = data
            buf = ['<', tag]
            for (attr, value) in attrib:
                if (attr in boolean_attrs):
                    if value:
                        buf += [' ', attr]
                elif (':' in attr):
                    if ((attr == 'xml:lang') and ('lang' not in attrib)):
                        buf += [' lang="', escape(value), '"']
                elif (attr != 'xmlns'):
                    buf += [' ', attr, '="', escape(value), '"']
            buf.append('>')
            if (kind is EMPTY):
                if (tag not in empty_elems):
                    buf.append(('</%s>' % tag))
            (yield _emit(kind, data, Markup(''.join(buf))))
            if (tag in noescape_elems):
                noescape = True
        elif (kind is END):
            (yield _emit(kind, data, Markup(('</%s>' % data))))
            noescape = False
        elif (kind is TEXT):
            if noescape:
                (yield _emit(kind, data, data))
            else:
                (yield _emit(kind, data, escape(data, quotes=False)))
        elif (kind is COMMENT):
            (yield _emit(kind, data, Markup(('<!--%s-->' % data))))
        elif ((kind is DOCTYPE) and (not have_doctype)):
            (name, pubid, sysid) = data
            buf = ['<!DOCTYPE %s']
            if pubid:
                buf.append(' PUBLIC "%s"')
            elif sysid:
                buf.append(' SYSTEM')
            if sysid:
                buf.append(' "%s"')
            buf.append('>\n')
            (yield (Markup(''.join(buf)) % tuple([p for p in data if p])))
            have_doctype = True
        elif (kind is PI):
            (yield _emit(kind, data, Markup(('<?%s %s?>' % data))))
def __call__(self, stream):
    boolean_attrs = self._BOOLEAN_ATTRS
    empty_elems = self._EMPTY_ELEMS
    noescape_elems = self._NOESCAPE_ELEMS
    have_doctype = False
    noescape = False
    (_emit, _get) = self._prepare_cache()
    for filter_ in self.filters:
        stream = filter_(stream)
    for (kind, data, _) in stream:
        if ((kind is TEXT) and isinstance(data, Markup)):
            (yield data)
            continue
        output = _get((kind, data))
        if (output is not None):
            (yield output)
            if (((kind is START) or (kind is EMPTY)) and (data[0] in noescape_elems)):
                noescape = True
            elif (kind is END):
                noescape = False
        elif ((kind is START) or (kind is EMPTY)):
            (tag, attrib) = data
            buf = ['<', tag]
            for (attr, value) in attrib:
                if (attr in boolean_attrs):
                    if value:
                        buf += [' ', attr]
                elif (':' in attr):
                    if ((attr == 'xml:lang') and ('lang' not in attrib)):
                        buf += [' lang="', escape(value), '"']
                elif (attr != 'xmlns'):
                    buf += [' ', attr, '="', escape(value), '"']
            buf.append('>')
            if (kind is EMPTY):
                if (tag not in empty_elems):
                    buf.append(('</%s>' % tag))
            (yield _emit(kind, data, Markup(.join(buf))))
            if (tag in noescape_elems):
                noescape = True
        elif (kind is END):
            (yield _emit(kind, data, Markup(('</%s>' % data))))
            noescape = False
        elif (kind is TEXT):
            if noescape:
                (yield _emit(kind, data, data))
            else:
                (yield _emit(kind, data, escape(data, quotes=False)))
        elif (kind is COMMENT):
            (yield _emit(kind, data, Markup(('<!--%s-->' % data))))
        elif ((kind is DOCTYPE) and (not have_doctype)):
            (name, pubid, sysid) = data
            buf = ['<!DOCTYPE %s']
            if pubid:
                buf.append(' PUBLIC "%s"')
            elif sysid:
                buf.append(' SYSTEM')
            if sysid:
                buf.append(' "%s"')
            buf.append('>\n')
            (yield (Markup(.join(buf)) % tuple([p for p in data if p])))
            have_doctype = True
        elif (kind is PI):
            (yield _emit(kind, data, Markup(('<?%s %s?>' % data))))