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 | |
Fred Drake | a219e79 | 1998-02-17 23:13:19 +0000 | [diff] [blame] | 25 | |
Fred Drake | c1ce320 | 1998-01-02 03:00:27 +0000 | [diff] [blame] | 26 | def 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 Drake | a219e79 | 1998-02-17 23:13:19 +0000 | [diff] [blame] | 40 | ofp.write("\\begin{theindex}\n\label{modindex}\n\n") |
Fred Drake | c1ce320 | 1998-01-02 03:00:27 +0000 | [diff] [blame] | 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]) |
Fred Drake | cffaebb | 1998-02-13 05:11:05 +0000 | [diff] [blame] | 57 | ofp.write(" \\item {\\tt %s}, %s\n" % (module, lineno)) |
Fred Drake | c1ce320 | 1998-01-02 03:00:27 +0000 | [diff] [blame] | 58 | ofp.write("\n\\end{theindex}\n") |
| 59 | |
| 60 | |
| 61 | if __name__ == "__main__": |
| 62 | main() |