summaryrefslogtreecommitdiffstats
path: root/Doc/tools/sgmlconv/esis2sgml.py
blob: 172e71202c4a41bdebfdf0b78abd2f4abe83780f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#! /usr/bin/env python

"""Convert ESIS events to SGML or XML markup.

This is limited, but seems sufficient for the ESIS generated by the
latex2esis.py script when run over the Python documentation.
"""
__version__ = '$Revision$'

import errno
import esistools
import re
import string


def format_attrs(attrs):
    attrs = attrs.items()
    attrs.sort()
    s = ''
    for name, value in attrs:
        s = '%s %s="%s"' % (s, name, value)
    return s


def do_convert(ifp, ofp, xml=0):
    attrs = {}
    lastopened = None
    knownempties = []
    knownempty = 0
    lastempty = 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)
            ofp.write(data)
            if "\n" in data:
                lastopened = None
            knownempty = 0
            lastempty = 0
        elif type == "(":
            if data == "COMMENT":
                ofp.write("<!--")
                continue
            if knownempty and xml:
                ofp.write("<%s%s/>" % (data, format_attrs(attrs)))
            else:
                ofp.write("<%s%s>" % (data, format_attrs(attrs)))
            if knownempty and data not in knownempties:
                # accumulate knowledge!
                knownempties.append(data)
            attrs = {}
            lastopened = data
            lastempty = knownempty
            knownempty = 0
        elif type == ")":
            if data == "COMMENT":
                ofp.write("-->")
                continue
            if xml:
                if not lastempty:
                    ofp.write("</%s>" % data)
            elif data not in knownempties:
                if lastopened == data:
                    ofp.write("</>")
                else:
                    ofp.write("</%s>" % data)
            lastopened = None
            lastempty = 0
        elif type == "A":
            name, type, value = string.split(data, " ", 2)
            attrs[name] = esistools.decode(value)
        elif type == "e":
            knownempty = 1


def sgml_convert(ifp, ofp):
    return do_convert(ifp, ofp, xml=0)


def xml_convert(ifp, ofp):
    return do_convert(ifp, ofp, xml=1)


def main():
    import getopt
    import sys
    #
    convert = sgml_convert
    xml = 0
    xmldecl = 0
    opts, args = getopt.getopt(sys.argv[1:], "dx", ["declare", "xml"])
    for opt, arg in opts:
        if opt in ("-d", "--declare"):
            xmldecl = 1
        elif opt in ("-x", "--xml"):
            xml = 1
            convert = xml_convert
    if len(args) == 0:
        ifp = sys.stdin
        ofp = sys.stdout
    elif len(args) == 1:
        ifp = open(args[0])
        ofp = sys.stdout
    elif len(args) == 2:
        ifp = open(args[0])
        ofp = open(args[1], "w")
    else:
        usage()
        sys.exit(2)
    # knownempties is ignored in the XML version
    try:
        if xml and xmldecl:
            opf.write('<?xml version="1.0" encoding="iso8859-1"?>\n')
        convert(ifp, ofp)
    except IOError, (err, msg):
        if err != errno.EPIPE:
            raise


if __name__ == "__main__":
    main()