Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Python -*- |
| 3 | |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 4 | """usage: %(program)s [options] file... |
| 5 | |
| 6 | Supported options: |
| 7 | |
| 8 | --address addr |
| 9 | -a addr Set the address text to include at the end of the generated |
| 10 | HTML; this should be used for contact information. |
| 11 | --columns cols |
| 12 | -c cols Set the number of columns each index section should be |
| 13 | displayed in. The default is 1. |
| 14 | --help |
| 15 | -h Display this help message. |
| 16 | --letters |
| 17 | -l Split the output into sections by letter. |
| 18 | --output file |
| 19 | -o file Write output to 'file' instead of standard out. |
| 20 | --iconserver is Use 'is' as the directory containing icons for the |
| 21 | navigation bar. The default is 'icons'. |
| 22 | --title str Set the page title to 'str'. The default is 'Global |
| 23 | Module Index'. |
| 24 | --uplink url Set the upward link URL. The default is './'. |
| 25 | --uptitle str Set the upward link title. The default is 'Python |
| 26 | Documentation Index'. |
| 27 | """ |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 28 | import buildindex |
| 29 | import getopt |
| 30 | import os |
| 31 | import re |
| 32 | import string |
| 33 | import sys |
| 34 | |
| 35 | |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 36 | def usage(): |
| 37 | program = os.path.basename(sys.argv[0]) |
| 38 | print __doc__ % {"program": program} |
| 39 | |
| 40 | |
| 41 | def error(msg, rc=2): |
| 42 | sys.stdout = sys.stderr |
| 43 | print msg |
| 44 | print |
| 45 | usage() |
| 46 | sys.exit(rc) |
| 47 | |
| 48 | |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 49 | _rx = re.compile( |
Fred Drake | ba82878 | 2000-04-03 04:19:14 +0000 | [diff] [blame^] | 50 | "<dt><a href='(module-.*\.html)#l2h-\d+'><tt class='module'>" |
| 51 | "([a-zA-Z_][a-zA-Z0-9_.]*</tt>(\s*<em>" |
| 52 | "\(<span class='platform'>.*</span>\)</em>)?)</a>") |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 53 | |
| 54 | def main(): |
| 55 | outputfile = "-" |
| 56 | columns = 1 |
| 57 | letters = 0 |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 58 | uplink = "./" |
| 59 | uptitle = "Python Documentation Index" |
| 60 | variables = {"address": "", |
| 61 | "iconserver": "icons", |
| 62 | "imgtype": "gif", |
| 63 | "title": "Global Module Index", |
| 64 | "uplinkalt": "up", |
| 65 | "uplinkicon": "up", |
| 66 | } |
| 67 | try: |
| 68 | opts, args = getopt.getopt(sys.argv[1:], "a:c:hlo:", |
| 69 | [# script controls: |
| 70 | "columns=", "help", "letters", "output=", |
| 71 | # content components: |
| 72 | "address=", "iconserver=", |
| 73 | "title=", "uplink=", "uptitle="]) |
| 74 | except getopt.error, msg: |
| 75 | error(msg) |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 76 | for opt, val in opts: |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 77 | if opt in ("-a", "--address"): |
| 78 | val = string.strip(val) |
| 79 | variables["address"] = val and "<address>\n%s\n</address>\n" % val |
| 80 | elif opt in ("-h", "--help"): |
| 81 | usage() |
| 82 | sys.exit() |
| 83 | elif opt in ("-o", "--output"): |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 84 | outputfile = val |
| 85 | elif opt in ("-c", "--columns"): |
| 86 | columns = string.atoi(val) |
| 87 | elif opt in ("-l", "--letters"): |
| 88 | letters = 1 |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 89 | elif opt == "--title": |
| 90 | variables["title"] = string.strip(val) |
| 91 | elif opt == "--uplink": |
| 92 | uplink = string.strip(val) |
| 93 | elif opt == "--uptitle": |
| 94 | uptitle = string.strip(val) |
| 95 | elif opt == "--iconserver": |
| 96 | variables["iconserver"] = string.strip(val) or "." |
| 97 | if uplink and uptitle: |
| 98 | variables["uplinkalt"] = "up" |
| 99 | variables["uplinkicon"] = "up" |
| 100 | else: |
| 101 | variables["uplinkalt"] = "" |
| 102 | variables["uplinkicon"] = "blank" |
| 103 | variables["uplink"] = uplink |
| 104 | variables["uptitle"] = uptitle |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 105 | if not args: |
| 106 | args = ["-"] |
| 107 | # |
| 108 | # Collect the input data: |
| 109 | # |
| 110 | nodes = [] |
| 111 | seqno = 0 |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 112 | has_plat_flag = 0 |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 113 | for ifn in args: |
| 114 | if ifn == "-": |
| 115 | ifp = sys.stdin |
| 116 | dirname = '' |
| 117 | else: |
| 118 | ifp = open(ifn) |
| 119 | dirname = os.path.dirname(ifn) |
| 120 | while 1: |
| 121 | line = ifp.readline() |
| 122 | if not line: |
| 123 | break |
| 124 | m = _rx.match(line) |
| 125 | if m: |
| 126 | # This line specifies a module! |
| 127 | basename, modname = m.group(1, 2) |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 128 | has_plat_flag = has_plat_flag or m.group(3) |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 129 | linkfile = os.path.join(dirname, basename) |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 130 | nodes.append(buildindex.Node( |
| 131 | '<a href="%s">' % linkfile, |
| 132 | "<tt class=module>%s</tt>" % modname, |
| 133 | seqno)) |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 134 | seqno = seqno + 1 |
| 135 | ifp.close() |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 136 | # |
| 137 | # Generate all output: |
| 138 | # |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 139 | num_nodes = len(nodes) |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 140 | # Here's the HTML generation: |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 141 | parts = [HEAD % variables, |
| 142 | buildindex.process_nodes(nodes, columns, letters), |
| 143 | TAIL % variables, |
| 144 | ] |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 145 | if has_plat_flag: |
| 146 | parts.insert(1, PLAT_DISCUSS) |
| 147 | html = string.join(parts, '') |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 148 | program = os.path.basename(sys.argv[0]) |
| 149 | if outputfile == "-": |
| 150 | sys.stdout.write(html) |
| 151 | sys.stderr.write("%s: %d index nodes\n" % (program, num_nodes)) |
| 152 | else: |
| 153 | open(outputfile, "w").write(html) |
| 154 | print |
| 155 | print "%s: %d index nodes" % (program, num_nodes) |
| 156 | |
| 157 | |
Fred Drake | 7f492ad | 1999-03-02 16:22:56 +0000 | [diff] [blame] | 158 | PLAT_DISCUSS = """ |
| 159 | <p> Some module names are followed by an annotation indicating what |
| 160 | platform they are available on.</p> |
| 161 | |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 162 | """ |
| 163 | |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 164 | NAVIGATION = """\ |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 165 | <div class=navigation> |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 166 | <table width="100%%" cellpadding=0 cellspacing=2> |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 167 | <tr> |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 168 | <td><img width=32 height=32 align=bottom border=0 alt="" |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 169 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
Fred Drake | 1acb874 | 1999-03-04 21:33:55 +0000 | [diff] [blame] | 170 | <td><a href="%(uplink)s" |
| 171 | title="%(uptitle)s"><img width=32 height=32 align=bottom border=0 |
| 172 | alt="%(uplinkalt)s" |
| 173 | src="%(iconserver)s/%(uplinkicon)s.%(imgtype)s"></a></td> |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 174 | <td><img width=32 height=32 align=bottom border=0 alt="" |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 175 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 176 | <td align=center bgcolor="#99CCFF" width="100%%"> |
| 177 | <b class=title>%(title)s</b></td> |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 178 | <td><img width=32 height=32 align=bottom border=0 alt="" |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 179 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 180 | <td><img width=32 height=32 align=bottom border=0 alt="" |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 181 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
| 182 | <td><img width=32 height=32 align=bottom border=0 alt="" |
| 183 | src="%(iconserver)s/blank.%(imgtype)s"></td> |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 184 | </tr></table> |
Fred Drake | 1acb874 | 1999-03-04 21:33:55 +0000 | [diff] [blame] | 185 | <b class=navlabel>Up:</b> <span class=sectref><a href="%(uplink)s" |
| 186 | title="%(uptitle)s">%(uptitle)s</A></span> |
| 187 | <br></div> |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 188 | """ |
| 189 | |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 190 | HEAD = """\ |
| 191 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 192 | <html> |
| 193 | <head> |
| 194 | <title>Global Module Index</title> |
| 195 | <meta name="description" content="%(title)s"> |
| 196 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
| 197 | <link rel="STYLESHEET" href="lib/lib.css"> |
| 198 | </head> |
| 199 | <body bgcolor=white> |
| 200 | """ + NAVIGATION + """\ |
Fred Drake | 1acb874 | 1999-03-04 21:33:55 +0000 | [diff] [blame] | 201 | <hr> |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 202 | |
| 203 | <h2>%(title)s</h2> |
| 204 | |
| 205 | """ |
| 206 | |
Fred Drake | 1acb874 | 1999-03-04 21:33:55 +0000 | [diff] [blame] | 207 | TAIL = "<hr>\n" + NAVIGATION + """\ |
Fred Drake | 5f7832d | 1999-03-04 21:19:57 +0000 | [diff] [blame] | 208 | %(address)s</body> |
| 209 | </html> |
| 210 | """ |
Fred Drake | 2ef38a7 | 1999-02-24 17:33:07 +0000 | [diff] [blame] | 211 | |
| 212 | if __name__ == "__main__": |
| 213 | main() |