Bump version to 0.9.1.
[python/dscho.git] / Doc / tools / mkmodindex
blob14568d129ea1b42417f0fc910103dfbce91ad5a0
1 #! /usr/bin/env python
2 # -*- Python -*-
4 """usage: %(program)s [options] file...
6 Supported options:
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 """
28 import buildindex
29 import getopt
30 import os
31 import re
32 import string
33 import sys
36 def usage():
37 program = os.path.basename(sys.argv[0])
38 print __doc__ % {"program": program}
41 def error(msg, rc=2):
42 sys.stdout = sys.stderr
43 print msg
44 print
45 usage()
46 sys.exit(rc)
49 _rx = re.compile(
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>")
54 def main():
55 outputfile = "-"
56 columns = 1
57 letters = 0
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",
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)
76 for opt, val in opts:
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"):
84 outputfile = val
85 elif opt in ("-c", "--columns"):
86 columns = string.atoi(val)
87 elif opt in ("-l", "--letters"):
88 letters = 1
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
105 if not args:
106 args = ["-"]
108 # Collect the input data:
110 nodes = []
111 seqno = 0
112 has_plat_flag = 0
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)
128 has_plat_flag = has_plat_flag or m.group(3)
129 linkfile = os.path.join(dirname, basename)
130 nodes.append(buildindex.Node(
131 '<a href="%s">' % linkfile,
132 "<tt class=module>%s</tt>" % modname,
133 seqno))
134 seqno = seqno + 1
135 ifp.close()
137 # Generate all output:
139 num_nodes = len(nodes)
140 # Here's the HTML generation:
141 parts = [HEAD % variables,
142 buildindex.process_nodes(nodes, columns, letters),
143 TAIL % variables,
145 if has_plat_flag:
146 parts.insert(1, PLAT_DISCUSS)
147 html = string.join(parts, '')
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)
158 PLAT_DISCUSS = """
159 <p> Some module names are followed by an annotation indicating what
160 platform they are available on.</p>
164 NAVIGATION = """\
165 <div class=navigation>
166 <table width="100%%" cellpadding=0 cellspacing=2>
167 <tr>
168 <td><img width=32 height=32 align=bottom border=0 alt=""
169 src="%(iconserver)s/blank.%(imgtype)s"></td>
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>
174 <td><img width=32 height=32 align=bottom border=0 alt=""
175 src="%(iconserver)s/blank.%(imgtype)s"></td>
176 <td align=center bgcolor="#99CCFF" width="100%%">
177 <b class=title>%(title)s</b></td>
178 <td><img width=32 height=32 align=bottom border=0 alt=""
179 src="%(iconserver)s/blank.%(imgtype)s"></td>
180 <td><img width=32 height=32 align=bottom border=0 alt=""
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>
184 </tr></table>
185 <b class=navlabel>Up:</b> <span class=sectref><a href="%(uplink)s"
186 title="%(uptitle)s">%(uptitle)s</A></span>
187 <br></div>
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 + """\
201 <hr>
203 <h2>%(title)s</h2>
207 TAIL = "<hr>\n" + NAVIGATION + """\
208 %(address)s</body>
209 </html>
212 if __name__ == "__main__":
213 main()