convert(): Added parameter "autoclose", which should be a sequence of

general identifiers for which closing tags will be omitted
	when SGML is generated.  This can be used to tell the markup
	generator to drop stuff like </para>.  Note that it needs to
	be possible for the closing tag to *always* be omitted for it
	to be included in "autoclose".

main():  Added command-line option "-a" / "--autoclose" to set the
	list of general identifiers passed to the convert() function
	as the "autoclose" parameter.  The list may only be specified
	once (not additive) and GIs should be comma-separated.  The
	default list includes only "para".
This commit is contained in:
Fred Drake 1999-01-20 20:35:05 +00:00
parent 0a5b8de5e2
commit 43278f01dc
1 changed files with 19 additions and 8 deletions

View File

@ -47,7 +47,9 @@ def istoken(s):
return _token_rx.match(s) is not None return _token_rx.match(s) is not None
def do_convert(ifp, ofp, xml=0): def do_convert(ifp, ofp, xml=0, autoclose=()):
if xml:
autoclose = ()
attrs = {} attrs = {}
lastopened = None lastopened = None
knownempties = [] knownempties = []
@ -92,7 +94,9 @@ def do_convert(ifp, ofp, xml=0):
if not lastempty: if not lastempty:
ofp.write("</%s>" % data) ofp.write("</%s>" % data)
elif data not in knownempties: elif data not in knownempties:
if lastopened == data: if data in autoclose:
pass
elif lastopened == data:
ofp.write("</>") ofp.write("</>")
else: else:
ofp.write("</%s>" % data) ofp.write("</%s>" % data)
@ -115,28 +119,35 @@ def do_convert(ifp, ofp, xml=0):
fp.close() fp.close()
def sgml_convert(ifp, ofp): def sgml_convert(ifp, ofp, autoclose):
return do_convert(ifp, ofp, xml=0) return do_convert(ifp, ofp, xml=0, autoclose=autoclose)
def xml_convert(ifp, ofp): def xml_convert(ifp, ofp, autoclose):
return do_convert(ifp, ofp, xml=1) return do_convert(ifp, ofp, xml=1, autoclose=autoclose)
AUTOCLOSE = ("para",)
def main(): def main():
import getopt import getopt
import sys import sys
# #
autoclose = AUTOCLOSE
convert = sgml_convert convert = sgml_convert
xml = 0 xml = 0
xmldecl = 0 xmldecl = 0
opts, args = getopt.getopt(sys.argv[1:], "dx", ["declare", "xml"]) opts, args = getopt.getopt(sys.argv[1:], "adx",
["autoclose", "declare", "xml"])
for opt, arg in opts: for opt, arg in opts:
if opt in ("-d", "--declare"): if opt in ("-d", "--declare"):
xmldecl = 1 xmldecl = 1
elif opt in ("-x", "--xml"): elif opt in ("-x", "--xml"):
xml = 1 xml = 1
convert = xml_convert convert = xml_convert
elif opt in ("-a", "--autoclose"):
autoclose = string.split(arg, ",")
if len(args) == 0: if len(args) == 0:
ifp = sys.stdin ifp = sys.stdin
ofp = sys.stdout ofp = sys.stdout
@ -153,7 +164,7 @@ def main():
try: try:
if xml and xmldecl: if xml and xmldecl:
opf.write('<?xml version="1.0" encoding="iso8859-1"?>\n') opf.write('<?xml version="1.0" encoding="iso8859-1"?>\n')
convert(ifp, ofp) convert(ifp, ofp, autoclose)
except IOError, (err, msg): except IOError, (err, msg):
if err != errno.EPIPE: if err != errno.EPIPE:
raise raise