blob: 84c106608fa11288ed5cb190427578f4399bb9ed [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
25def main():
26 if sys.argv[1:]:
27 infile = sys.argv[1]
28 else:
29 infile = "-"
30 if infile == "-":
31 ifp = sys.stdin
32 ofp = sys.stdout
33 sys.stdout = sys.stderr
34 else:
35 ifp = open(infile)
36 base, ext = os.path.splitext(infile)
37 outfile = base + ".ind"
38 ofp = open(outfile, "w")
39 ofp.write("\\begin{theindex}\n\n")
40 lines = ifp.readlines()
41 for i in range(len(lines)):
42 if lines[i][0] == '\\':
43 lines[i] = '\1' + lines[i]
44 lines = map(parse_line, lines)
45 lines.sort(cmp_items)
46 prev_letter = lines[0][0][0]
47 if prev_letter == '\1':
48 prev_letter = lines[0][0][1]
49 prev_letter = string.lower(prev_letter)
50 for module, lineno in lines:
51 if module[0] == '\1':
52 module = module[1:]
53 if string.lower(module[0]) != prev_letter:
54 ofp.write("\n \\indexspace\n\n")
55 prev_letter = string.lower(module[0])
Fred Drakecffaebb1998-02-13 05:11:05 +000056 ofp.write(" \\item {\\tt %s}, %s\n" % (module, lineno))
Fred Drakec1ce3201998-01-02 03:00:27 +000057 ofp.write("\n\\end{theindex}\n")
58
59
60if __name__ == "__main__":
61 main()