1998-11-23 12:59:39 -04:00
|
|
|
#! /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
|
1998-12-01 15:01:53 -04:00
|
|
|
import esistools
|
1999-01-19 19:03:04 -04:00
|
|
|
import os
|
1998-11-23 12:59:39 -04:00
|
|
|
import re
|
|
|
|
import string
|
|
|
|
|
1999-01-14 13:06:09 -04:00
|
|
|
from xml.utils import escape
|
|
|
|
|
1998-11-23 12:59:39 -04:00
|
|
|
|
1999-01-19 19:03:04 -04:00
|
|
|
EMPTIES_FILENAME = "../sgml/empties.dat"
|
|
|
|
LIST_EMPTIES = 0
|
|
|
|
|
|
|
|
|
1999-01-19 13:10:31 -04:00
|
|
|
def format_attrs(attrs, xml=0):
|
1998-11-23 12:59:39 -04:00
|
|
|
attrs = attrs.items()
|
|
|
|
attrs.sort()
|
|
|
|
s = ''
|
|
|
|
for name, value in attrs:
|
1999-01-19 13:10:31 -04:00
|
|
|
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):
|
1999-01-29 17:35:50 -04:00
|
|
|
if value == "no" + name:
|
|
|
|
s = "%s %s" % (s, value)
|
|
|
|
else:
|
|
|
|
s = "%s %s=%s" % (s, name, value)
|
1999-01-19 13:10:31 -04:00
|
|
|
else:
|
|
|
|
s = '%s %s="%s"' % (s, name, escape(value))
|
1998-11-23 12:59:39 -04:00
|
|
|
return s
|
|
|
|
|
|
|
|
|
1999-01-19 19:03:04 -04:00
|
|
|
_nmtoken_rx = re.compile("[a-z][-._a-z0-9]*$", re.IGNORECASE)
|
1999-01-19 13:10:31 -04:00
|
|
|
def isnmtoken(s):
|
|
|
|
return _nmtoken_rx.match(s) is not None
|
|
|
|
|
1999-01-19 19:03:04 -04:00
|
|
|
_token_rx = re.compile("[a-z0-9][-._a-z0-9]*$", re.IGNORECASE)
|
1999-01-19 13:10:31 -04:00
|
|
|
def istoken(s):
|
|
|
|
return _token_rx.match(s) is not None
|
|
|
|
|
|
|
|
|
1999-01-20 16:35:05 -04:00
|
|
|
def do_convert(ifp, ofp, xml=0, autoclose=()):
|
|
|
|
if xml:
|
|
|
|
autoclose = ()
|
1998-11-23 12:59:39 -04:00
|
|
|
attrs = {}
|
|
|
|
lastopened = None
|
1998-12-10 14:31:37 -04:00
|
|
|
knownempties = []
|
1998-11-23 12:59:39 -04:00
|
|
|
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 == "-":
|
1998-12-01 15:01:53 -04:00
|
|
|
data = esistools.decode(data)
|
1999-01-14 13:06:09 -04:00
|
|
|
ofp.write(escape(data))
|
1998-11-23 12:59:39 -04:00
|
|
|
if "\n" in data:
|
|
|
|
lastopened = None
|
|
|
|
knownempty = 0
|
|
|
|
lastempty = 0
|
|
|
|
elif type == "(":
|
1998-12-01 15:01:53 -04:00
|
|
|
if data == "COMMENT":
|
|
|
|
ofp.write("<!--")
|
|
|
|
continue
|
1998-11-23 12:59:39 -04:00
|
|
|
if knownempty and xml:
|
1999-01-19 13:10:31 -04:00
|
|
|
ofp.write("<%s%s/>" % (data, format_attrs(attrs, xml)))
|
1998-11-23 12:59:39 -04:00
|
|
|
else:
|
1999-01-19 13:10:31 -04:00
|
|
|
ofp.write("<%s%s>" % (data, format_attrs(attrs, xml)))
|
1998-11-23 12:59:39 -04:00
|
|
|
if knownempty and data not in knownempties:
|
|
|
|
# accumulate knowledge!
|
|
|
|
knownempties.append(data)
|
|
|
|
attrs = {}
|
|
|
|
lastopened = data
|
|
|
|
lastempty = knownempty
|
|
|
|
knownempty = 0
|
|
|
|
elif type == ")":
|
1998-12-01 15:01:53 -04:00
|
|
|
if data == "COMMENT":
|
|
|
|
ofp.write("-->")
|
|
|
|
continue
|
1998-11-23 12:59:39 -04:00
|
|
|
if xml:
|
|
|
|
if not lastempty:
|
|
|
|
ofp.write("</%s>" % data)
|
|
|
|
elif data not in knownempties:
|
1999-01-20 16:35:05 -04:00
|
|
|
if data in autoclose:
|
|
|
|
pass
|
|
|
|
elif lastopened == data:
|
1998-11-23 12:59:39 -04:00
|
|
|
ofp.write("</>")
|
|
|
|
else:
|
|
|
|
ofp.write("</%s>" % data)
|
|
|
|
lastopened = None
|
|
|
|
lastempty = 0
|
|
|
|
elif type == "A":
|
|
|
|
name, type, value = string.split(data, " ", 2)
|
1998-12-01 15:01:53 -04:00
|
|
|
attrs[name] = esistools.decode(value)
|
1998-11-23 12:59:39 -04:00
|
|
|
elif type == "e":
|
|
|
|
knownempty = 1
|
|
|
|
|
1999-01-19 19:03:04 -04:00
|
|
|
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()
|
|
|
|
|
1998-11-23 12:59:39 -04:00
|
|
|
|
1999-01-20 16:35:05 -04:00
|
|
|
def sgml_convert(ifp, ofp, autoclose):
|
|
|
|
return do_convert(ifp, ofp, xml=0, autoclose=autoclose)
|
1998-11-23 12:59:39 -04:00
|
|
|
|
|
|
|
|
1999-01-20 16:35:05 -04:00
|
|
|
def xml_convert(ifp, ofp, autoclose):
|
|
|
|
return do_convert(ifp, ofp, xml=1, autoclose=autoclose)
|
|
|
|
|
|
|
|
|
1999-01-22 18:48:24 -04:00
|
|
|
AUTOCLOSE = ("para", "term",)
|
1998-11-23 12:59:39 -04:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
1998-12-01 15:01:53 -04:00
|
|
|
import getopt
|
1998-11-23 12:59:39 -04:00
|
|
|
import sys
|
|
|
|
#
|
1999-01-20 16:35:05 -04:00
|
|
|
autoclose = AUTOCLOSE
|
1998-11-23 12:59:39 -04:00
|
|
|
convert = sgml_convert
|
1998-12-01 15:01:53 -04:00
|
|
|
xml = 0
|
|
|
|
xmldecl = 0
|
1999-01-20 16:35:05 -04:00
|
|
|
opts, args = getopt.getopt(sys.argv[1:], "adx",
|
|
|
|
["autoclose", "declare", "xml"])
|
1998-12-01 15:01:53 -04:00
|
|
|
for opt, arg in opts:
|
|
|
|
if opt in ("-d", "--declare"):
|
|
|
|
xmldecl = 1
|
|
|
|
elif opt in ("-x", "--xml"):
|
|
|
|
xml = 1
|
|
|
|
convert = xml_convert
|
1999-01-20 16:35:05 -04:00
|
|
|
elif opt in ("-a", "--autoclose"):
|
|
|
|
autoclose = string.split(arg, ",")
|
1998-12-01 15:01:53 -04:00
|
|
|
if len(args) == 0:
|
1998-11-23 12:59:39 -04:00
|
|
|
ifp = sys.stdin
|
|
|
|
ofp = sys.stdout
|
1998-12-01 15:01:53 -04:00
|
|
|
elif len(args) == 1:
|
|
|
|
ifp = open(args[0])
|
1998-11-23 12:59:39 -04:00
|
|
|
ofp = sys.stdout
|
1998-12-01 15:01:53 -04:00
|
|
|
elif len(args) == 2:
|
|
|
|
ifp = open(args[0])
|
|
|
|
ofp = open(args[1], "w")
|
1998-11-23 12:59:39 -04:00
|
|
|
else:
|
|
|
|
usage()
|
|
|
|
sys.exit(2)
|
|
|
|
# knownempties is ignored in the XML version
|
|
|
|
try:
|
1998-12-01 15:01:53 -04:00
|
|
|
if xml and xmldecl:
|
|
|
|
opf.write('<?xml version="1.0" encoding="iso8859-1"?>\n')
|
1999-01-20 16:35:05 -04:00
|
|
|
convert(ifp, ofp, autoclose)
|
1998-11-23 12:59:39 -04:00
|
|
|
except IOError, (err, msg):
|
|
|
|
if err != errno.EPIPE:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|