Ejemplo n.º 1
0
def convert(ifp, ofp, xml=0, autoclose=(), verbatims=()):
    if xml:
        autoclose = ()
    attrs = {}
    lastopened = None
    knownempties = []
    knownempty = 0
    lastempty = 0
    inverbatim = 0
    while 1:
        line = ifp.readline()
        if not line:
            break

        type = line[0]
        data = line[1:]
        if data and data[-1] == "\n":
            data = data[:-1]
        if type == "-":
            data = esistools.decode(data)
            data = escape(data)
            if not inverbatim:
                data = data.replace("---", "—")
            ofp.write(data)
            if "\n" in data:
                lastopened = None
            knownempty = 0
            lastempty = 0
        elif type == "(":
            if data == "COMMENT":
                ofp.write("<!--")
                continue
            data = map_gi(data, _elem_map)
            if knownempty and xml:
                ofp.write("<%s%s/>" % (data, format_attrs(attrs, xml)))
            else:
                ofp.write("<%s%s>" % (data, format_attrs(attrs, xml)))
            if knownempty and data not in knownempties:
                # accumulate knowledge!
                knownempties.append(data)
            attrs = {}
            lastopened = data
            lastempty = knownempty
            knownempty = 0
            inverbatim = data in verbatims
        elif type == ")":
            if data == "COMMENT":
                ofp.write("-->")
                continue
            data = map_gi(data, _elem_map)
            if xml:
                if not lastempty:
                    ofp.write("</%s>" % data)
            elif data not in knownempties:
                if data in autoclose:
                    pass
                elif lastopened == data:
                    ofp.write("</>")
                else:
                    ofp.write("</%s>" % data)
            lastopened = None
            lastempty = 0
            inverbatim = 0
        elif type == "A":
            name, type, value = data.split(" ", 2)
            name = map_gi(name, _attr_map)
            attrs[name] = esistools.decode(value)
        elif type == "e":
            knownempty = 1
        elif type == "&":
            ofp.write("&%s;" % data)
            knownempty = 0
        else:
            raise RuntimeError, "unrecognized ESIS event type: '%s'" % type

    if LIST_EMPTIES:
        dump_empty_element_names(knownempties)
Ejemplo n.º 2
0
def convert(ifp, ofp, xml=0, autoclose=(), verbatims=()):
    if xml:
        autoclose = ()
    attrs = {}
    lastopened = None
    knownempties = []
    knownempty = 0
    lastempty = 0
    inverbatim = 0
    while 1:
        line = ifp.readline()
        if not line:
            break

        type = line[0]
        data = line[1:]
        if data and data[-1] == "\n":
            data = data[:-1]
        if type == "-":
            data = esistools.decode(data)
            data = escape(data)
            if not inverbatim:
                data = data.replace("---", "&mdash;")
            ofp.write(data)
            if "\n" in data:
                lastopened = None
            knownempty = 0
            lastempty = 0
        elif type == "(":
            if data == "COMMENT":
                ofp.write("<!--")
                continue
            data = map_gi(data, _elem_map)
            if knownempty and xml:
                ofp.write("<%s%s/>" % (data, format_attrs(attrs, xml)))
            else:
                ofp.write("<%s%s>" % (data, format_attrs(attrs, xml)))
            if knownempty and data not in knownempties:
                # accumulate knowledge!
                knownempties.append(data)
            attrs = {}
            lastopened = data
            lastempty = knownempty
            knownempty = 0
            inverbatim = data in verbatims
        elif type == ")":
            if data == "COMMENT":
                ofp.write("-->")
                continue
            data = map_gi(data, _elem_map)
            if xml:
                if not lastempty:
                    ofp.write("</%s>" % data)
            elif data not in knownempties:
                if data in autoclose:
                    pass
                elif lastopened == data:
                    ofp.write("</>")
                else:
                    ofp.write("</%s>" % data)
            lastopened = None
            lastempty = 0
            inverbatim = 0
        elif type == "A":
            name, type, value = data.split(" ", 2)
            name = map_gi(name, _attr_map)
            attrs[name] = esistools.decode(value)
        elif type == "e":
            knownempty = 1
        elif type == "&":
            ofp.write("&%s;" % data)
            knownempty = 0
        else:
            raise RuntimeError, "unrecognized ESIS event type: '%s'" % type

    if LIST_EMPTIES:
        dump_empty_element_names(knownempties)