blob: 23a200e7eb33af0025ed1b58636bbc34a482c422 [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):
Fred Drake9ca78ac2001-06-22 17:11:30 +000052 def __init__(self, link, str, seqno, platinfo):
53 self.annotation = platinfo or None
54 if str[0][-5:] == "</tt>":
55 str = str[:-5]
56 self.modname = str
Fred Drake0a0ef862000-11-28 16:20:50 +000057 buildindex.Node.__init__(self, link, self.modname, seqno)
Fred Drake9ca78ac2001-06-22 17:11:30 +000058 if platinfo:
59 s = '<tt class="module">%s</tt> %s' \
60 % (self.modname, self.annotation)
61 else:
62 s = '<tt class="module">%s</tt>' % str
63 self.text = [s]
Fred Drake0a0ef862000-11-28 16:20:50 +000064
65 def __str__(self):
Fred Drake9ca78ac2001-06-22 17:11:30 +000066 if self.annotation:
67 return '<tt class="module">%s</tt> %s' \
68 % (self.modname, self.annotation)
69 else:
70 return '<tt class="module">%s</tt>' % self.modname
Fred Drake0a0ef862000-11-28 16:20:50 +000071
Fred Drake2ef38a71999-02-24 17:33:07 +000072_rx = re.compile(
Fred Drake9ca78ac2001-06-22 17:11:30 +000073 "<dt><a href=['\"](module-.*\.html)(?:#l2h-\d+)?['\"]>"
74 "<tt class=['\"]module['\"]>([a-zA-Z_][a-zA-Z0-9_.]*)</tt>\s*(<em>"
75 "\(<span class=['\"]platform['\"]>.*</span>\)</em>)?</a>")
Fred Drake2ef38a71999-02-24 17:33:07 +000076
77def main():
Fred Drake01a110b2000-10-05 05:14:26 +000078 options = IndexOptions()
79 options.variables["title"] = "Global Module Index"
80 options.parse(sys.argv[1:])
81 args = options.args
Fred Drake2ef38a71999-02-24 17:33:07 +000082 if not args:
83 args = ["-"]
84 #
85 # Collect the input data:
86 #
87 nodes = []
Fred Drake7f492ad1999-03-02 16:22:56 +000088 has_plat_flag = 0
Fred Drake2ef38a71999-02-24 17:33:07 +000089 for ifn in args:
90 if ifn == "-":
91 ifp = sys.stdin
92 dirname = ''
93 else:
94 ifp = open(ifn)
95 dirname = os.path.dirname(ifn)
96 while 1:
97 line = ifp.readline()
98 if not line:
99 break
100 m = _rx.match(line)
101 if m:
102 # This line specifies a module!
Fred Drake9ca78ac2001-06-22 17:11:30 +0000103 basename, modname, platinfo = m.group(1, 2, 3)
104 has_plat_flag = has_plat_flag or platinfo
Fred Drake2ef38a71999-02-24 17:33:07 +0000105 linkfile = os.path.join(dirname, basename)
Fred Drake9ca78ac2001-06-22 17:11:30 +0000106 nodes.append(Node('<a href="%s">' % linkfile, modname,
107 len(nodes), platinfo))
Fred Drake2ef38a71999-02-24 17:33:07 +0000108 ifp.close()
Fred Drake7f492ad1999-03-02 16:22:56 +0000109 #
110 # Generate all output:
111 #
Fred Drake2ef38a71999-02-24 17:33:07 +0000112 num_nodes = len(nodes)
Fred Drake7f492ad1999-03-02 16:22:56 +0000113 # Here's the HTML generation:
Fred Drake01a110b2000-10-05 05:14:26 +0000114 parts = [options.get_header(),
115 buildindex.process_nodes(nodes, options.columns, options.letters),
116 options.get_footer(),
Fred Drake5f7832d1999-03-04 21:19:57 +0000117 ]
Fred Drake7f492ad1999-03-02 16:22:56 +0000118 if has_plat_flag:
119 parts.insert(1, PLAT_DISCUSS)
120 html = string.join(parts, '')
Fred Drake2ef38a71999-02-24 17:33:07 +0000121 program = os.path.basename(sys.argv[0])
Fred Drake01a110b2000-10-05 05:14:26 +0000122 fp = options.get_output_file()
Fred Drakeb258bed2001-02-12 15:30:22 +0000123 fp.write(string.rstrip(html) + "\n")
Fred Drake01a110b2000-10-05 05:14:26 +0000124 if options.outputfile == "-":
Fred Drakeb258bed2001-02-12 15:30:22 +0000125 sys.stderr.write("%s: %d index nodes\n" % (program, num_nodes))
Fred Drake2ef38a71999-02-24 17:33:07 +0000126 else:
Fred Drake2ef38a71999-02-24 17:33:07 +0000127 print
128 print "%s: %d index nodes" % (program, num_nodes)
129
130
Fred Drake7f492ad1999-03-02 16:22:56 +0000131PLAT_DISCUSS = """
132<p> Some module names are followed by an annotation indicating what
133platform they are available on.</p>
134
Fred Drake2ef38a71999-02-24 17:33:07 +0000135"""
136
Fred Drake2ef38a71999-02-24 17:33:07 +0000137
138if __name__ == "__main__":
139 main()