Fred Drake | c1ce320 | 1998-01-02 03:00:27 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Script to convert raw module index data to module index.""" |
| 4 | |
| 5 | import os |
| 6 | import string |
| 7 | import sys |
| 8 | |
| 9 | |
| 10 | def parse_line(input): |
| 11 | lineno = string.split(input)[-1] |
Fred Drake | cffaebb | 1998-02-13 05:11:05 +0000 | [diff] [blame^] | 12 | module = string.strip(input[:len(input)-(len(lineno)+1)]) |
Fred Drake | c1ce320 | 1998-01-02 03:00:27 +0000 | [diff] [blame] | 13 | return module, lineno |
Fred Drake | c1ce320 | 1998-01-02 03:00:27 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def 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 | |
| 25 | def 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 Drake | cffaebb | 1998-02-13 05:11:05 +0000 | [diff] [blame^] | 56 | ofp.write(" \\item {\\tt %s}, %s\n" % (module, lineno)) |
Fred Drake | c1ce320 | 1998-01-02 03:00:27 +0000 | [diff] [blame] | 57 | ofp.write("\n\\end{theindex}\n") |
| 58 | |
| 59 | |
| 60 | if __name__ == "__main__": |
| 61 | main() |