blob: f85b909ddabc752de0c9541f0fcee479af58a7cd [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]
12 module = input[:len(input)-(len(lineno)+1)]
13 return module, lineno
14 return
15
16
17def cmp_items((s1, line1), (s2, line2)):
18 rc = cmp(string.lower(s1), string.lower(s2))
19 if rc == 0:
20 # Make the lower-case version come first since the upper-case
21 # version is usually a helper module for constants and such.
22 rc = cmp(s2, s1)
23 return rc
24
25
26def 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")
40 ofp.write("\\begin{theindex}\n\n")
41 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])
57 ofp.write(" \\item {\\tt %s} %s\n" % (module, lineno))
58 ofp.write("\n\\end{theindex}\n")
59
60
61if __name__ == "__main__":
62 main()