Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Convert ESIS events to SGML or XML markup. |
| 4 | |
| 5 | This is limited, but seems sufficient for the ESIS generated by the |
| 6 | latex2esis.py script when run over the Python documentation. |
| 7 | """ |
| 8 | __version__ = '$Revision$' |
| 9 | |
| 10 | import errno |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 11 | import esistools |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 12 | import re |
| 13 | import string |
| 14 | |
| 15 | |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 16 | def format_attrs(attrs): |
| 17 | attrs = attrs.items() |
| 18 | attrs.sort() |
| 19 | s = '' |
| 20 | for name, value in attrs: |
| 21 | s = '%s %s="%s"' % (s, name, value) |
| 22 | return s |
| 23 | |
| 24 | |
Fred Drake | 4abcffb | 1998-12-10 18:31:37 +0000 | [diff] [blame] | 25 | def do_convert(ifp, ofp, xml=0): |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 26 | attrs = {} |
| 27 | lastopened = None |
Fred Drake | 4abcffb | 1998-12-10 18:31:37 +0000 | [diff] [blame] | 28 | knownempties = [] |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 29 | knownempty = 0 |
| 30 | lastempty = 0 |
| 31 | while 1: |
| 32 | line = ifp.readline() |
| 33 | if not line: |
| 34 | break |
| 35 | |
| 36 | type = line[0] |
| 37 | data = line[1:] |
| 38 | if data and data[-1] == "\n": |
| 39 | data = data[:-1] |
| 40 | if type == "-": |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 41 | data = esistools.decode(data) |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 42 | ofp.write(data) |
| 43 | if "\n" in data: |
| 44 | lastopened = None |
| 45 | knownempty = 0 |
| 46 | lastempty = 0 |
| 47 | elif type == "(": |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 48 | if data == "COMMENT": |
| 49 | ofp.write("<!--") |
| 50 | continue |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 51 | if knownempty and xml: |
| 52 | ofp.write("<%s%s/>" % (data, format_attrs(attrs))) |
| 53 | else: |
| 54 | ofp.write("<%s%s>" % (data, format_attrs(attrs))) |
| 55 | if knownempty and data not in knownempties: |
| 56 | # accumulate knowledge! |
| 57 | knownempties.append(data) |
| 58 | attrs = {} |
| 59 | lastopened = data |
| 60 | lastempty = knownempty |
| 61 | knownempty = 0 |
| 62 | elif type == ")": |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 63 | if data == "COMMENT": |
| 64 | ofp.write("-->") |
| 65 | continue |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 66 | if xml: |
| 67 | if not lastempty: |
| 68 | ofp.write("</%s>" % data) |
| 69 | elif data not in knownempties: |
| 70 | if lastopened == data: |
| 71 | ofp.write("</>") |
| 72 | else: |
| 73 | ofp.write("</%s>" % data) |
| 74 | lastopened = None |
| 75 | lastempty = 0 |
| 76 | elif type == "A": |
| 77 | name, type, value = string.split(data, " ", 2) |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 78 | attrs[name] = esistools.decode(value) |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 79 | elif type == "e": |
| 80 | knownempty = 1 |
| 81 | |
| 82 | |
Fred Drake | 4abcffb | 1998-12-10 18:31:37 +0000 | [diff] [blame] | 83 | def sgml_convert(ifp, ofp): |
| 84 | return do_convert(ifp, ofp, xml=0) |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 85 | |
| 86 | |
Fred Drake | 4abcffb | 1998-12-10 18:31:37 +0000 | [diff] [blame] | 87 | def xml_convert(ifp, ofp): |
| 88 | return do_convert(ifp, ofp, xml=1) |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 89 | |
| 90 | |
| 91 | def main(): |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 92 | import getopt |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 93 | import sys |
| 94 | # |
| 95 | convert = sgml_convert |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 96 | xml = 0 |
| 97 | xmldecl = 0 |
| 98 | opts, args = getopt.getopt(sys.argv[1:], "dx", ["declare", "xml"]) |
| 99 | for opt, arg in opts: |
| 100 | if opt in ("-d", "--declare"): |
| 101 | xmldecl = 1 |
| 102 | elif opt in ("-x", "--xml"): |
| 103 | xml = 1 |
| 104 | convert = xml_convert |
| 105 | if len(args) == 0: |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 106 | ifp = sys.stdin |
| 107 | ofp = sys.stdout |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 108 | elif len(args) == 1: |
| 109 | ifp = open(args[0]) |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 110 | ofp = sys.stdout |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 111 | elif len(args) == 2: |
| 112 | ifp = open(args[0]) |
| 113 | ofp = open(args[1], "w") |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 114 | else: |
| 115 | usage() |
| 116 | sys.exit(2) |
| 117 | # knownempties is ignored in the XML version |
| 118 | try: |
Fred Drake | f077b9d | 1998-12-01 19:01:53 +0000 | [diff] [blame] | 119 | if xml and xmldecl: |
| 120 | opf.write('<?xml version="1.0" encoding="iso8859-1"?>\n') |
Fred Drake | 30a68c7 | 1998-11-23 16:59:39 +0000 | [diff] [blame] | 121 | convert(ifp, ofp) |
| 122 | except IOError, (err, msg): |
| 123 | if err != errno.EPIPE: |
| 124 | raise |
| 125 | |
| 126 | |
| 127 | if __name__ == "__main__": |
| 128 | main() |