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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
#! /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 os
import re
import string
from xml.utils import escape
EMPTIES_FILENAME = "../sgml/empties.dat"
LIST_EMPTIES = 0
def format_attrs(attrs, xml=0):
attrs = attrs.items()
attrs.sort()
s = ''
for name, value in attrs:
if xml:
s = '%s %s="%s"' % (s, name, escape(value))
else:
# this is a little bogus, but should do for now
if name == value and isnmtoken(value):
s = "%s %s" % (s, value)
elif istoken(value):
s = "%s %s=%s" % (s, name, value)
else:
s = '%s %s="%s"' % (s, name, escape(value))
return s
_nmtoken_rx = re.compile("[a-z][-._a-z0-9]*$", re.IGNORECASE)
def isnmtoken(s):
return _nmtoken_rx.match(s) is not None
_token_rx = re.compile("[a-z0-9][-._a-z0-9]*$", re.IGNORECASE)
def istoken(s):
return _token_rx.match(s) is not None
def do_convert(ifp, ofp, xml=0, autoclose=()):
if xml:
autoclose = ()
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(escape(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, 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
elif type == ")":
if data == "COMMENT":
ofp.write("-->")
continue
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
elif type == "A":
name, type, value = string.split(data, " ", 2)
attrs[name] = esistools.decode(value)
elif type == "e":
knownempty = 1
if LIST_EMPTIES:
knownempties.append("")
if os.path.isfile(EMPTIES_FILENAME):
mode = "a"
else:
mode = "w"
fp = open(EMPTIES_FILENAME, mode)
fp.write(string.join(knownempties, "\n"))
fp.close()
def sgml_convert(ifp, ofp, autoclose):
return do_convert(ifp, ofp, xml=0, autoclose=autoclose)
def xml_convert(ifp, ofp, autoclose):
return do_convert(ifp, ofp, xml=1, autoclose=autoclose)
AUTOCLOSE = ("para", "term",)
def main():
import getopt
import sys
#
autoclose = AUTOCLOSE
convert = sgml_convert
xml = 0
xmldecl = 0
opts, args = getopt.getopt(sys.argv[1:], "adx",
["autoclose", "declare", "xml"])
for opt, arg in opts:
if opt in ("-d", "--declare"):
xmldecl = 1
elif opt in ("-x", "--xml"):
xml = 1
convert = xml_convert
elif opt in ("-a", "--autoclose"):
autoclose = string.split(arg, ",")
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, autoclose)
except IOError, (err, msg):
if err != errno.EPIPE:
raise
if __name__ == "__main__":
main()
|