1 | #! /usr/bin/env python
|
---|
2 |
|
---|
3 | """Combine similar index entries into an entry and subentries.
|
---|
4 |
|
---|
5 | For example:
|
---|
6 |
|
---|
7 | \item {foobar} (in module flotz), 23
|
---|
8 | \item {foobar} (in module whackit), 4323
|
---|
9 |
|
---|
10 | becomes
|
---|
11 |
|
---|
12 | \item {foobar}
|
---|
13 | \subitem in module flotz, 23
|
---|
14 | \subitem in module whackit, 4323
|
---|
15 |
|
---|
16 | Note that an item which matches the format of a collapsable item but which
|
---|
17 | isn't part of a group of similar items is not modified.
|
---|
18 | """
|
---|
19 | __version__ = '$Revision: 29268 $'
|
---|
20 |
|
---|
21 | import re
|
---|
22 | import StringIO
|
---|
23 | import sys
|
---|
24 |
|
---|
25 |
|
---|
26 | def cmp_entries(e1, e2):
|
---|
27 | return cmp(e1[1].lower(), e2[1].lower()) or cmp(e1, e2)
|
---|
28 |
|
---|
29 |
|
---|
30 | def dump_entries(write, entries):
|
---|
31 | if len(entries) == 1:
|
---|
32 | write(" \\item %s (%s)%s\n" % entries[0])
|
---|
33 | return
|
---|
34 | write(" \item %s\n" % entries[0][0])
|
---|
35 | # now sort these in a case insensitive manner:
|
---|
36 | if len(entries) > 0:
|
---|
37 | entries.sort(cmp_entries)
|
---|
38 | for xxx, subitem, pages in entries:
|
---|
39 | write(" \subitem %s%s\n" % (subitem, pages))
|
---|
40 |
|
---|
41 |
|
---|
42 | breakable_re = re.compile(
|
---|
43 | r" \\item (.*) [(](.*)[)]((?:(?:, \d+)|(?:, \\[a-z]*\{\d+\}))+)")
|
---|
44 |
|
---|
45 |
|
---|
46 | def process(ifn, ofn=None):
|
---|
47 | if ifn == "-":
|
---|
48 | ifp = sys.stdin
|
---|
49 | else:
|
---|
50 | ifp = open(ifn)
|
---|
51 | if ofn is None:
|
---|
52 | ofn = ifn
|
---|
53 | ofp = StringIO.StringIO()
|
---|
54 | entries = []
|
---|
55 | match = breakable_re.match
|
---|
56 | write = ofp.write
|
---|
57 | while 1:
|
---|
58 | line = ifp.readline()
|
---|
59 | if not line:
|
---|
60 | break
|
---|
61 | m = match(line)
|
---|
62 | if m:
|
---|
63 | entry = m.group(1, 2, 3)
|
---|
64 | if entries and entries[-1][0] != entry[0]:
|
---|
65 | dump_entries(write, entries)
|
---|
66 | entries = []
|
---|
67 | entries.append(entry)
|
---|
68 | elif entries:
|
---|
69 | dump_entries(write, entries)
|
---|
70 | entries = []
|
---|
71 | write(line)
|
---|
72 | else:
|
---|
73 | write(line)
|
---|
74 | del write
|
---|
75 | del match
|
---|
76 | ifp.close()
|
---|
77 | data = ofp.getvalue()
|
---|
78 | ofp.close()
|
---|
79 | if ofn == "-":
|
---|
80 | ofp = sys.stdout
|
---|
81 | else:
|
---|
82 | ofp = open(ofn, "w")
|
---|
83 | ofp.write(data)
|
---|
84 | ofp.close()
|
---|
85 |
|
---|
86 |
|
---|
87 | def main():
|
---|
88 | import getopt
|
---|
89 | outfile = None
|
---|
90 | opts, args = getopt.getopt(sys.argv[1:], "o:")
|
---|
91 | for opt, val in opts:
|
---|
92 | if opt in ("-o", "--output"):
|
---|
93 | outfile = val
|
---|
94 | filename = args[0]
|
---|
95 | outfile = outfile or filename
|
---|
96 | process(filename, outfile)
|
---|
97 |
|
---|
98 |
|
---|
99 | if __name__ == "__main__":
|
---|
100 | main()
|
---|