blob: 7d5c682022c283b045837b9951fa40bf760f2654 [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 os
29import re
Fred Drake2ef38a71999-02-24 17:33:07 +000030import sys
31
Fred Drake06912b72002-10-16 15:29:07 +000032import buildindex
33import support
34
Fred Drake2ef38a71999-02-24 17:33:07 +000035
Fred Drake01a110b2000-10-05 05:14:26 +000036class IndexOptions(support.Options):
Fred Drakee03e1fe2002-04-05 17:34:50 +000037 aesop_type = "links"
38
Fred Drake01a110b2000-10-05 05:14:26 +000039 def __init__(self):
40 support.Options.__init__(self)
41 self.add_args("l", ["letters"])
42 self.letters = 0
Fred Drake5f7832d1999-03-04 21:19:57 +000043
Fred Drake01a110b2000-10-05 05:14:26 +000044 def handle_option(self, opt, val):
45 if opt in ("-l", "--letters"):
46 self.letters = 1
Fred Drake5f7832d1999-03-04 21:19:57 +000047
Fred Drake01a110b2000-10-05 05:14:26 +000048 def usage(self):
49 program = os.path.basename(sys.argv[0])
50 print __doc__ % {"program": program}
Fred Drake5f7832d1999-03-04 21:19:57 +000051
52
Fred Drake0a0ef862000-11-28 16:20:50 +000053class Node(buildindex.Node):
Fred Drake9ca78ac2001-06-22 17:11:30 +000054 def __init__(self, link, str, seqno, platinfo):
55 self.annotation = platinfo or None
56 if str[0][-5:] == "</tt>":
57 str = str[:-5]
58 self.modname = str
Fred Drake0a0ef862000-11-28 16:20:50 +000059 buildindex.Node.__init__(self, link, self.modname, seqno)
Fred Drake9ca78ac2001-06-22 17:11:30 +000060 if platinfo:
61 s = '<tt class="module">%s</tt> %s' \
62 % (self.modname, self.annotation)
63 else:
64 s = '<tt class="module">%s</tt>' % str
65 self.text = [s]
Fred Drake0a0ef862000-11-28 16:20:50 +000066
67 def __str__(self):
Fred Drake9ca78ac2001-06-22 17:11:30 +000068 if self.annotation:
69 return '<tt class="module">%s</tt> %s' \
70 % (self.modname, self.annotation)
71 else:
72 return '<tt class="module">%s</tt>' % self.modname
Fred Drake0a0ef862000-11-28 16:20:50 +000073
Fred Drake2ef38a71999-02-24 17:33:07 +000074_rx = re.compile(
Fred Drake9ca78ac2001-06-22 17:11:30 +000075 "<dt><a href=['\"](module-.*\.html)(?:#l2h-\d+)?['\"]>"
76 "<tt class=['\"]module['\"]>([a-zA-Z_][a-zA-Z0-9_.]*)</tt>\s*(<em>"
77 "\(<span class=['\"]platform['\"]>.*</span>\)</em>)?</a>")
Fred Drake2ef38a71999-02-24 17:33:07 +000078
79def main():
Fred Drake01a110b2000-10-05 05:14:26 +000080 options = IndexOptions()
81 options.variables["title"] = "Global Module Index"
82 options.parse(sys.argv[1:])
83 args = options.args
Fred Drake2ef38a71999-02-24 17:33:07 +000084 if not args:
85 args = ["-"]
86 #
87 # Collect the input data:
88 #
89 nodes = []
Fred Drake7f492ad1999-03-02 16:22:56 +000090 has_plat_flag = 0
Fred Drake2ef38a71999-02-24 17:33:07 +000091 for ifn in args:
92 if ifn == "-":
93 ifp = sys.stdin
94 dirname = ''
95 else:
96 ifp = open(ifn)
97 dirname = os.path.dirname(ifn)
98 while 1:
99 line = ifp.readline()
100 if not line:
101 break
102 m = _rx.match(line)
103 if m:
104 # This line specifies a module!
Fred Drake9ca78ac2001-06-22 17:11:30 +0000105 basename, modname, platinfo = m.group(1, 2, 3)
106 has_plat_flag = has_plat_flag or platinfo
Fred Drake2ef38a71999-02-24 17:33:07 +0000107 linkfile = os.path.join(dirname, basename)
Fred Drake9ca78ac2001-06-22 17:11:30 +0000108 nodes.append(Node('<a href="%s">' % linkfile, modname,
109 len(nodes), platinfo))
Fred Drake2ef38a71999-02-24 17:33:07 +0000110 ifp.close()
Fred Drake7f492ad1999-03-02 16:22:56 +0000111 #
112 # Generate all output:
113 #
Fred Drake2ef38a71999-02-24 17:33:07 +0000114 num_nodes = len(nodes)
Fred Drake7f492ad1999-03-02 16:22:56 +0000115 # Here's the HTML generation:
Fred Drake01a110b2000-10-05 05:14:26 +0000116 parts = [options.get_header(),
117 buildindex.process_nodes(nodes, options.columns, options.letters),
118 options.get_footer(),
Fred Drake5f7832d1999-03-04 21:19:57 +0000119 ]
Fred Drake7f492ad1999-03-02 16:22:56 +0000120 if has_plat_flag:
121 parts.insert(1, PLAT_DISCUSS)
Fred Drake06912b72002-10-16 15:29:07 +0000122 html = ''.join(parts)
Fred Drake2ef38a71999-02-24 17:33:07 +0000123 program = os.path.basename(sys.argv[0])
Fred Drake01a110b2000-10-05 05:14:26 +0000124 fp = options.get_output_file()
Fred Drake06912b72002-10-16 15:29:07 +0000125 fp.write(html.rstrip() + "\n")
Fred Drake01a110b2000-10-05 05:14:26 +0000126 if options.outputfile == "-":
Fred Drakeb258bed2001-02-12 15:30:22 +0000127 sys.stderr.write("%s: %d index nodes\n" % (program, num_nodes))
Fred Drake2ef38a71999-02-24 17:33:07 +0000128 else:
Fred Drake2ef38a71999-02-24 17:33:07 +0000129 print
130 print "%s: %d index nodes" % (program, num_nodes)
131
132
Fred Drake7f492ad1999-03-02 16:22:56 +0000133PLAT_DISCUSS = """
134<p> Some module names are followed by an annotation indicating what
135platform they are available on.</p>
136
Fred Drake2ef38a71999-02-24 17:33:07 +0000137"""
138
Fred Drake2ef38a71999-02-24 17:33:07 +0000139
140if __name__ == "__main__":
141 main()