blob: 0209a1a3f621c979cbe0ad72312e736ab396ab57 [file] [log] [blame]
Fred Drakec1ce3201998-01-02 03:00:27 +00001#! /usr/bin/env python
2
3"""Script to convert raw module index data to module index."""
4
5import os
6import string
7import sys
8
9
10def parse_line(input):
11 lineno = string.split(input)[-1]
Fred Drakecffaebb1998-02-13 05:11:05 +000012 module = string.strip(input[:len(input)-(len(lineno)+1)])
Fred Drakec1ce3201998-01-02 03:00:27 +000013 return module, lineno
Fred Drakec1ce3201998-01-02 03:00:27 +000014
15
16def cmp_items((s1, line1), (s2, line2)):
17 rc = cmp(string.lower(s1), string.lower(s2))
18 if rc == 0:
19 # Make the lower-case version come first since the upper-case
20 # version is usually a helper module for constants and such.
21 rc = cmp(s2, s1)
22 return rc
23
24
Fred Drakea219e791998-02-17 23:13:19 +000025
Fred Drakec1ce3201998-01-02 03:00:27 +000026def main():
27 if sys.argv[1:]:
28 infile = sys.argv[1]
29 else:
30 infile = "-"
31 if infile == "-":
32 ifp = sys.stdin
33 ofp = sys.stdout
34 sys.stdout = sys.stderr
35 else:
36 ifp = open(infile)
37 base, ext = os.path.splitext(infile)
38 outfile = base + ".ind"
39 ofp = open(outfile, "w")
Fred Drakea219e791998-02-17 23:13:19 +000040 ofp.write("\\begin{theindex}\n\label{modindex}\n\n")
Fred Drakec1ce3201998-01-02 03:00:27 +000041 lines = ifp.readlines()
42 for i in range(len(lines)):
43 if lines[i][0] == '\\':
44 lines[i] = '\1' + lines[i]
45 lines = map(parse_line, lines)
46 lines.sort(cmp_items)
47 prev_letter = lines[0][0][0]
48 if prev_letter == '\1':
49 prev_letter = lines[0][0][1]
50 prev_letter = string.lower(prev_letter)
51 for module, lineno in lines:
52 if module[0] == '\1':
53 module = module[1:]
54 if string.lower(module[0]) != prev_letter:
55 ofp.write("\n \\indexspace\n\n")
56 prev_letter = string.lower(module[0])
Fred Drakecffaebb1998-02-13 05:11:05 +000057 ofp.write(" \\item {\\tt %s}, %s\n" % (module, lineno))
Fred Drakec1ce3201998-01-02 03:00:27 +000058 ofp.write("\n\\end{theindex}\n")
59
60
61if __name__ == "__main__":
62 main()