blob: 5f2da0ebc168b5b7e20b23744683f5c74cffd79f [file] [log] [blame]
Fred Drake2ef38a71999-02-24 17:33:07 +00001#! /usr/bin/env python
2# -*- Python -*-
3
Fred Drake5f7832d1999-03-04 21:19:57 +00004"""usage: %(program)s [options] file...
5
6Supported 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 Drake2ef38a71999-02-24 17:33:07 +000028import buildindex
Fred Drake2ef38a71999-02-24 17:33:07 +000029import os
30import re
31import string
Fred Drake01a110b2000-10-05 05:14:26 +000032import support
Fred Drake2ef38a71999-02-24 17:33:07 +000033import sys
34
35
Fred Drake01a110b2000-10-05 05:14:26 +000036class IndexOptions(support.Options):
37 def __init__(self):
38 support.Options.__init__(self)
39 self.add_args("l", ["letters"])
40 self.letters = 0
Fred Drake5f7832d1999-03-04 21:19:57 +000041
Fred Drake01a110b2000-10-05 05:14:26 +000042 def handle_option(self, opt, val):
43 if opt in ("-l", "--letters"):
44 self.letters = 1
Fred Drake5f7832d1999-03-04 21:19:57 +000045
Fred Drake01a110b2000-10-05 05:14:26 +000046 def usage(self):
47 program = os.path.basename(sys.argv[0])
48 print __doc__ % {"program": program}
Fred Drake5f7832d1999-03-04 21:19:57 +000049
50
Fred Drake0a0ef862000-11-28 16:20:50 +000051class Node(buildindex.Node):
52 annotation = ""
53
54 def __init__(self, link, str, seqno):
Fred Drake498cb152001-02-12 19:12:55 +000055 parts = string.split(str, None, 1)
56 if parts[0][-5:] == "</tt>":
Fred Drake0a0ef862000-11-28 16:20:50 +000057 self.modname = parts[0][:-5]
58 else:
59 self.modname = parts[0]
60 if len(parts) == 2:
61 self.annotation = parts[1]
62 buildindex.Node.__init__(self, link, self.modname, seqno)
63
64 def __str__(self):
65 return '<tt class="module">%s</tt> %s' \
66 % (self.modname, self.annotation)
67
Fred Drake2ef38a71999-02-24 17:33:07 +000068_rx = re.compile(
Fred Drakeba828782000-04-03 04:19:14 +000069 "<dt><a href='(module-.*\.html)#l2h-\d+'><tt class='module'>"
70 "([a-zA-Z_][a-zA-Z0-9_.]*</tt>(\s*<em>"
71 "\(<span class='platform'>.*</span>\)</em>)?)</a>")
Fred Drake2ef38a71999-02-24 17:33:07 +000072
73def main():
Fred Drake01a110b2000-10-05 05:14:26 +000074 options = IndexOptions()
75 options.variables["title"] = "Global Module Index"
76 options.parse(sys.argv[1:])
77 args = options.args
Fred Drake2ef38a71999-02-24 17:33:07 +000078 if not args:
79 args = ["-"]
80 #
81 # Collect the input data:
82 #
83 nodes = []
84 seqno = 0
Fred Drake7f492ad1999-03-02 16:22:56 +000085 has_plat_flag = 0
Fred Drake2ef38a71999-02-24 17:33:07 +000086 for ifn in args:
87 if ifn == "-":
88 ifp = sys.stdin
89 dirname = ''
90 else:
91 ifp = open(ifn)
92 dirname = os.path.dirname(ifn)
93 while 1:
94 line = ifp.readline()
95 if not line:
96 break
97 m = _rx.match(line)
98 if m:
99 # This line specifies a module!
100 basename, modname = m.group(1, 2)
Fred Drake7f492ad1999-03-02 16:22:56 +0000101 has_plat_flag = has_plat_flag or m.group(3)
Fred Drake2ef38a71999-02-24 17:33:07 +0000102 linkfile = os.path.join(dirname, basename)
Fred Drake0a0ef862000-11-28 16:20:50 +0000103 nodes.append(Node('<a href="%s">' % linkfile, modname, seqno))
Fred Drake2ef38a71999-02-24 17:33:07 +0000104 seqno = seqno + 1
105 ifp.close()
Fred Drake7f492ad1999-03-02 16:22:56 +0000106 #
107 # Generate all output:
108 #
Fred Drake2ef38a71999-02-24 17:33:07 +0000109 num_nodes = len(nodes)
Fred Drake7f492ad1999-03-02 16:22:56 +0000110 # Here's the HTML generation:
Fred Drake01a110b2000-10-05 05:14:26 +0000111 parts = [options.get_header(),
112 buildindex.process_nodes(nodes, options.columns, options.letters),
113 options.get_footer(),
Fred Drake5f7832d1999-03-04 21:19:57 +0000114 ]
Fred Drake7f492ad1999-03-02 16:22:56 +0000115 if has_plat_flag:
116 parts.insert(1, PLAT_DISCUSS)
117 html = string.join(parts, '')
Fred Drake2ef38a71999-02-24 17:33:07 +0000118 program = os.path.basename(sys.argv[0])
Fred Drake01a110b2000-10-05 05:14:26 +0000119 fp = options.get_output_file()
Fred Drakeb258bed2001-02-12 15:30:22 +0000120 fp.write(string.rstrip(html) + "\n")
Fred Drake01a110b2000-10-05 05:14:26 +0000121 if options.outputfile == "-":
Fred Drakeb258bed2001-02-12 15:30:22 +0000122 sys.stderr.write("%s: %d index nodes\n" % (program, num_nodes))
Fred Drake2ef38a71999-02-24 17:33:07 +0000123 else:
Fred Drake2ef38a71999-02-24 17:33:07 +0000124 print
125 print "%s: %d index nodes" % (program, num_nodes)
126
127
Fred Drake7f492ad1999-03-02 16:22:56 +0000128PLAT_DISCUSS = """
129<p> Some module names are followed by an annotation indicating what
130platform they are available on.</p>
131
Fred Drake2ef38a71999-02-24 17:33:07 +0000132"""
133
Fred Drake2ef38a71999-02-24 17:33:07 +0000134
135if __name__ == "__main__":
136 main()