blob: aea99626ebda04593a3bd93f05f84fcd3fcba388 [file] [log] [blame]
Fred Drake30a68c71998-11-23 16:59:39 +00001#! /usr/bin/env python
2
3"""Convert ESIS events to SGML or XML markup.
4
5This is limited, but seems sufficient for the ESIS generated by the
6latex2esis.py script when run over the Python documentation.
7"""
Fred Drake607aed71999-02-18 16:30:16 +00008
9# This should have an explicit option to indicate whether the *INPUT* was
10# generated from an SGML or an XML application.
11
Fred Drake30a68c71998-11-23 16:59:39 +000012__version__ = '$Revision$'
13
14import errno
Fred Drakef077b9d1998-12-01 19:01:53 +000015import esistools
Fred Drake36dfe581999-01-19 23:03:04 +000016import os
Fred Drake30a68c71998-11-23 16:59:39 +000017import re
18import string
19
Fred Drake79ad1f11999-01-14 17:06:09 +000020from xml.utils import escape
21
Fred Drake30a68c71998-11-23 16:59:39 +000022
Fred Drake607aed71999-02-18 16:30:16 +000023AUTOCLOSE = ()
24
Fred Drake36dfe581999-01-19 23:03:04 +000025EMPTIES_FILENAME = "../sgml/empties.dat"
26LIST_EMPTIES = 0
27
28
Fred Drake607aed71999-02-18 16:30:16 +000029_elem_map = {}
30_attr_map = {}
31_token_map = {}
32
33_normalize_case = str
34
35def map_gi(sgmlgi, map):
36 uncased = _normalize_case(sgmlgi)
37 try:
38 return map[uncased]
39 except IndexError:
40 map[uncased] = sgmlgi
41 return sgmlgi
42
43def null_map_gi(sgmlgi, map):
44 return sgmlgi
45
46
Fred Drakef82e4ab1999-01-19 17:10:31 +000047def format_attrs(attrs, xml=0):
Fred Drake30a68c71998-11-23 16:59:39 +000048 attrs = attrs.items()
49 attrs.sort()
Fred Drake607aed71999-02-18 16:30:16 +000050 parts = []
51 append = parts.append
Fred Drake30a68c71998-11-23 16:59:39 +000052 for name, value in attrs:
Fred Drakef82e4ab1999-01-19 17:10:31 +000053 if xml:
Fred Drake607aed71999-02-18 16:30:16 +000054 append('%s="%s"' % (name, escape(value)))
Fred Drakef82e4ab1999-01-19 17:10:31 +000055 else:
56 # this is a little bogus, but should do for now
57 if name == value and isnmtoken(value):
Fred Drake607aed71999-02-18 16:30:16 +000058 append(value)
Fred Drakef82e4ab1999-01-19 17:10:31 +000059 elif istoken(value):
Fred Drake279ca751999-01-29 21:35:50 +000060 if value == "no" + name:
Fred Drake607aed71999-02-18 16:30:16 +000061 append(value)
Fred Drake279ca751999-01-29 21:35:50 +000062 else:
Fred Drake607aed71999-02-18 16:30:16 +000063 append("%s=%s" % (name, value))
Fred Drakef82e4ab1999-01-19 17:10:31 +000064 else:
Fred Drake607aed71999-02-18 16:30:16 +000065 append('%s="%s"' % (name, escape(value)))
66 if parts:
67 parts.insert(0, '')
68 return string.join(parts)
Fred Drake30a68c71998-11-23 16:59:39 +000069
70
Fred Drake36dfe581999-01-19 23:03:04 +000071_nmtoken_rx = re.compile("[a-z][-._a-z0-9]*$", re.IGNORECASE)
Fred Drakef82e4ab1999-01-19 17:10:31 +000072def isnmtoken(s):
73 return _nmtoken_rx.match(s) is not None
74
Fred Drake36dfe581999-01-19 23:03:04 +000075_token_rx = re.compile("[a-z0-9][-._a-z0-9]*$", re.IGNORECASE)
Fred Drakef82e4ab1999-01-19 17:10:31 +000076def istoken(s):
77 return _token_rx.match(s) is not None
78
79
Fred Drake43278f01999-01-20 20:35:05 +000080def do_convert(ifp, ofp, xml=0, autoclose=()):
81 if xml:
82 autoclose = ()
Fred Drake30a68c71998-11-23 16:59:39 +000083 attrs = {}
84 lastopened = None
Fred Drake4abcffb1998-12-10 18:31:37 +000085 knownempties = []
Fred Drake30a68c71998-11-23 16:59:39 +000086 knownempty = 0
87 lastempty = 0
88 while 1:
89 line = ifp.readline()
90 if not line:
91 break
92
93 type = line[0]
94 data = line[1:]
95 if data and data[-1] == "\n":
96 data = data[:-1]
97 if type == "-":
Fred Drakef077b9d1998-12-01 19:01:53 +000098 data = esistools.decode(data)
Fred Drake79ad1f11999-01-14 17:06:09 +000099 ofp.write(escape(data))
Fred Drake30a68c71998-11-23 16:59:39 +0000100 if "\n" in data:
101 lastopened = None
102 knownempty = 0
103 lastempty = 0
104 elif type == "(":
Fred Drakef077b9d1998-12-01 19:01:53 +0000105 if data == "COMMENT":
106 ofp.write("<!--")
107 continue
Fred Drake607aed71999-02-18 16:30:16 +0000108 data = map_gi(data, _elem_map)
Fred Drake30a68c71998-11-23 16:59:39 +0000109 if knownempty and xml:
Fred Drakef82e4ab1999-01-19 17:10:31 +0000110 ofp.write("<%s%s/>" % (data, format_attrs(attrs, xml)))
Fred Drake30a68c71998-11-23 16:59:39 +0000111 else:
Fred Drakef82e4ab1999-01-19 17:10:31 +0000112 ofp.write("<%s%s>" % (data, format_attrs(attrs, xml)))
Fred Drake30a68c71998-11-23 16:59:39 +0000113 if knownempty and data not in knownempties:
114 # accumulate knowledge!
115 knownempties.append(data)
116 attrs = {}
117 lastopened = data
118 lastempty = knownempty
119 knownempty = 0
120 elif type == ")":
Fred Drakef077b9d1998-12-01 19:01:53 +0000121 if data == "COMMENT":
122 ofp.write("-->")
123 continue
Fred Drake607aed71999-02-18 16:30:16 +0000124 data = map_gi(data, _elem_map)
Fred Drake30a68c71998-11-23 16:59:39 +0000125 if xml:
126 if not lastempty:
127 ofp.write("</%s>" % data)
128 elif data not in knownempties:
Fred Drake43278f01999-01-20 20:35:05 +0000129 if data in autoclose:
130 pass
131 elif lastopened == data:
Fred Drake30a68c71998-11-23 16:59:39 +0000132 ofp.write("</>")
133 else:
134 ofp.write("</%s>" % data)
135 lastopened = None
136 lastempty = 0
137 elif type == "A":
138 name, type, value = string.split(data, " ", 2)
Fred Drake607aed71999-02-18 16:30:16 +0000139 name = map_gi(name, _attr_map)
Fred Drakef077b9d1998-12-01 19:01:53 +0000140 attrs[name] = esistools.decode(value)
Fred Drake30a68c71998-11-23 16:59:39 +0000141 elif type == "e":
142 knownempty = 1
143
Fred Drake36dfe581999-01-19 23:03:04 +0000144 if LIST_EMPTIES:
Fred Drake607aed71999-02-18 16:30:16 +0000145 dump_empty_element_names(knownempties)
146
147
148def dump_empty_element_names(knownempties):
149 knownempties.append("")
150 if os.path.isfile(EMPTIES_FILENAME):
151 mode = "a"
152 else:
153 mode = "w"
154 fp = open(EMPTIES_FILENAME, mode)
155 fp.write(string.join(knownempties, "\n"))
156 fp.close()
Fred Drake36dfe581999-01-19 23:03:04 +0000157
Fred Drake30a68c71998-11-23 16:59:39 +0000158
Fred Drake43278f01999-01-20 20:35:05 +0000159def sgml_convert(ifp, ofp, autoclose):
160 return do_convert(ifp, ofp, xml=0, autoclose=autoclose)
Fred Drake30a68c71998-11-23 16:59:39 +0000161
162
Fred Drake43278f01999-01-20 20:35:05 +0000163def xml_convert(ifp, ofp, autoclose):
164 return do_convert(ifp, ofp, xml=1, autoclose=autoclose)
165
166
Fred Drake607aed71999-02-18 16:30:16 +0000167def update_gi_map(map, names, fromsgml=1):
168 for name in string.split(names, ","):
169 if fromsgml:
170 uncased = string.lower(name)
171 else:
172 uncased = name
173 map[uncased] = name
Fred Drake30a68c71998-11-23 16:59:39 +0000174
175
176def main():
Fred Drakef077b9d1998-12-01 19:01:53 +0000177 import getopt
Fred Drake30a68c71998-11-23 16:59:39 +0000178 import sys
179 #
Fred Drake43278f01999-01-20 20:35:05 +0000180 autoclose = AUTOCLOSE
Fred Drake607aed71999-02-18 16:30:16 +0000181 convert = xml_convert
182 xml = 1
Fred Drakef077b9d1998-12-01 19:01:53 +0000183 xmldecl = 0
Fred Drake607aed71999-02-18 16:30:16 +0000184 elem_names = ''
185 attr_names = ''
186 value_names = ''
187 opts, args = getopt.getopt(sys.argv[1:], "adesx",
188 ["autoclose=", "declare", "sgml", "xml",
189 "elements-map=", "attributes-map",
190 "values-map="])
Fred Drakef077b9d1998-12-01 19:01:53 +0000191 for opt, arg in opts:
192 if opt in ("-d", "--declare"):
193 xmldecl = 1
Fred Drake607aed71999-02-18 16:30:16 +0000194 elif opt == "-e":
195 global LIST_EMPTIES
196 LIST_EMPTIES = 1
197 elif opt in ("-s", "--sgml"):
198 xml = 0
199 convert = sgml_convert
Fred Drakef077b9d1998-12-01 19:01:53 +0000200 elif opt in ("-x", "--xml"):
201 xml = 1
202 convert = xml_convert
Fred Drake43278f01999-01-20 20:35:05 +0000203 elif opt in ("-a", "--autoclose"):
204 autoclose = string.split(arg, ",")
Fred Drake607aed71999-02-18 16:30:16 +0000205 elif opt == "--elements-map":
206 elem_names = ("%s,%s" % (elem_names, arg))[1:]
207 elif opt == "--attributes-map":
208 attr_names = ("%s,%s" % (attr_names, arg))[1:]
209 elif opt == "--values-map":
210 value_names = ("%s,%s" % (value_names, arg))[1:]
211 #
212 # open input streams:
213 #
Fred Drakef077b9d1998-12-01 19:01:53 +0000214 if len(args) == 0:
Fred Drake30a68c71998-11-23 16:59:39 +0000215 ifp = sys.stdin
216 ofp = sys.stdout
Fred Drakef077b9d1998-12-01 19:01:53 +0000217 elif len(args) == 1:
218 ifp = open(args[0])
Fred Drake30a68c71998-11-23 16:59:39 +0000219 ofp = sys.stdout
Fred Drakef077b9d1998-12-01 19:01:53 +0000220 elif len(args) == 2:
221 ifp = open(args[0])
222 ofp = open(args[1], "w")
Fred Drake30a68c71998-11-23 16:59:39 +0000223 else:
224 usage()
225 sys.exit(2)
Fred Drake607aed71999-02-18 16:30:16 +0000226 #
227 # setup the name maps:
228 #
229 if elem_names or attr_names or value_names:
230 # assume the origin was SGML; ignore case of the names from the ESIS
231 # stream but set up conversion tables to get the case right on output
232 global _normalize_case
233 _normalize_case = string.lower
234 update_gi_map(_elem_map, string.split(elem_names, ","))
235 update_gi_map(_attr_map, string.split(attr_names, ","))
236 update_gi_map(_values_map, string.split(value_names, ","))
237 else:
238 global map_gi
239 map_gi = null_map_gi
240 #
241 # run the conversion:
242 #
Fred Drake30a68c71998-11-23 16:59:39 +0000243 try:
Fred Drakef077b9d1998-12-01 19:01:53 +0000244 if xml and xmldecl:
245 opf.write('<?xml version="1.0" encoding="iso8859-1"?>\n')
Fred Drake43278f01999-01-20 20:35:05 +0000246 convert(ifp, ofp, autoclose)
Fred Drake30a68c71998-11-23 16:59:39 +0000247 except IOError, (err, msg):
248 if err != errno.EPIPE:
249 raise
250
251
252if __name__ == "__main__":
253 main()