def __call__(self, stream):
    have_decl = have_doctype = False
    in_cdata = False
    (_emit, _get) = self._prepare_cache()
    for filter_ in self.filters:
        stream = filter_(stream)
    for (kind, data, pos) in stream:
        if ((kind is TEXT) and isinstance(data, Markup)):
            (yield data)
            continue
        cached = _get((kind, data))
        if (cached is not None):
            (yield cached)
        elif ((kind is START) or (kind is EMPTY)):
            (tag, attrib) = data
            buf = ['<', tag]
            for (attr, value) in attrib:
                buf += [' ', attr, '="', escape(value), '"']
            buf.append((((kind is EMPTY) and '/>') or '>'))
            (yield _emit(kind, data, Markup(''.join(buf))))
        elif (kind is END):
            (yield _emit(kind, data, Markup(('</%s>' % data))))
        elif (kind is TEXT):
            if in_cdata:
                (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 XML_DECL) and (not have_decl)):
            (version, encoding, standalone) = data
            buf = [('<?xml version="%s"' % version)]
            if encoding:
                buf.append((' encoding="%s"' % encoding))
            if (standalone != (- 1)):
                standalone = ((standalone and 'yes') or 'no')
                buf.append((' standalone="%s"' % standalone))
            buf.append('?>\n')
            (yield Markup(''.join(buf)))
            have_decl = True
        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 START_CDATA):
            (yield Markup('<![CDATA['))
            in_cdata = True
        elif (kind is END_CDATA):
            (yield Markup(']]>'))
            in_cdata = False
        elif (kind is PI):
            (yield _emit(kind, data, Markup(('<?%s %s?>' % data))))
def __call__(self, stream):
    have_decl = have_doctype = False
    in_cdata = False
    (_emit, _get) = self._prepare_cache()
    for filter_ in self.filters:
        stream = filter_(stream)
    for (kind, data, pos) in stream:
        if ((kind is TEXT) and isinstance(data, Markup)):
            (yield data)
            continue
        cached = _get((kind, data))
        if (cached is not None):
            (yield cached)
        elif ((kind is START) or (kind is EMPTY)):
            (tag, attrib) = data
            buf = ['<', tag]
            for (attr, value) in attrib:
                buf += [' ', attr, '="', escape(value), '"']
            buf.append((((kind is EMPTY) and '/>') or '>'))
            (yield _emit(kind, data, Markup(.join(buf))))
        elif (kind is END):
            (yield _emit(kind, data, Markup(('</%s>' % data))))
        elif (kind is TEXT):
            if in_cdata:
                (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 XML_DECL) and (not have_decl)):
            (version, encoding, standalone) = data
            buf = [('<?xml version="%s"' % version)]
            if encoding:
                buf.append((' encoding="%s"' % encoding))
            if (standalone != (- 1)):
                standalone = ((standalone and 'yes') or 'no')
                buf.append((' standalone="%s"' % standalone))
            buf.append('?>\n')
            (yield Markup(.join(buf)))
            have_decl = True
        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 START_CDATA):
            (yield Markup('<![CDATA['))
            in_cdata = True
        elif (kind is END_CDATA):
            (yield Markup(']]>'))
            in_cdata = False
        elif (kind is PI):
            (yield _emit(kind, data, Markup(('<?%s %s?>' % data))))