Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Doc / tools / buildindex.py
blob81bd311d0307f7afbf427c96bc2d6f4f91b0b0af
1 #! /usr/bin/env python
3 __version__ = '$Revision$'
5 import os
6 import re
7 import string
8 import sys
11 class Node:
12 __rmjunk = re.compile("<#\d+#>")
14 def __init__(self, link, str, seqno):
15 self.links = [link]
16 self.seqno = seqno
17 # remove <#\d+#> left in by moving the data out of LaTeX2HTML
18 str = self.__rmjunk.sub('', str)
19 # build up the text
20 self.text = split_entry_text(str)
21 self.key = split_entry_key(str)
23 def __cmp__(self, other):
24 """Comparison operator includes sequence number, for use with
25 list.sort()."""
26 return self.cmp_entry(other) or cmp(self.seqno, other.seqno)
28 def cmp_entry(self, other):
29 """Comparison 'operator' that ignores sequence number."""
30 c = 0
31 for i in range(min(len(self.key), len(other.key))):
32 c = (cmp_part(self.key[i], other.key[i])
33 or cmp_part(self.text[i], other.text[i]))
34 if c:
35 break
36 return c or cmp(self.key, other.key) or cmp(self.text, other.text)
38 def __repr__(self):
39 return "<Node for %s (%s)>" % (string.join(self.text, '!'), self.seqno)
41 def __str__(self):
42 return string.join(self.key, '!')
44 def dump(self):
45 return "%s\1%s###%s\n" \
46 % (string.join(self.links, "\1"),
47 string.join(self.text, '!'),
48 self.seqno)
51 def cmp_part(s1, s2):
52 result = cmp(s1, s2)
53 if result == 0:
54 return 0
55 l1 = string.lower(s1)
56 l2 = string.lower(s2)
57 minlen = min(len(s1), len(s2))
58 if len(s1) < len(s2) and l1 == l2[:len(s1)]:
59 result = -1
60 elif len(s2) < len(s1) and l2 == l1[:len(s2)]:
61 result = 1
62 else:
63 result = cmp(l1, l2) or cmp(s1, s2)
64 return result
67 def split_entry(str, which):
68 stuff = []
69 parts = string.split(str, '!')
70 parts = map(string.split, parts, ['@'] * len(parts))
71 for entry in parts:
72 if len(entry) != 1:
73 key = entry[which]
74 else:
75 key = entry[0]
76 stuff.append(key)
77 return stuff
80 _rmtt = re.compile(r"(.*)<tt(?: class=[a-z0-9]+)?>(.*)</tt>(.*)$",
81 re.IGNORECASE)
82 _rmparens = re.compile(r"\(\)")
84 def split_entry_key(str):
85 parts = split_entry(str, 1)
86 for i in range(len(parts)):
87 m = _rmtt.match(parts[i])
88 if m:
89 parts[i] = string.join(m.group(1, 2, 3), '')
90 else:
91 parts[i] = string.lower(parts[i])
92 # remove '()' from the key:
93 parts[i] = _rmparens.sub('', parts[i])
94 return map(trim_ignored_letters, parts)
97 def split_entry_text(str):
98 if '<' in str:
99 m = _rmtt.match(str)
100 if m:
101 str = string.join(m.group(1, 2, 3), '')
102 return split_entry(str, 1)
105 def load(fp):
106 nodes = []
107 rx = re.compile("(.*)\1(.*)###(.*)$")
108 while 1:
109 line = fp.readline()
110 if not line:
111 break
112 m = rx.match(line)
113 if m:
114 link, str, seqno = m.group(1, 2, 3)
115 nodes.append(Node(link, str, seqno))
116 return nodes
119 def trim_ignored_letters(s):
120 # ignore $ to keep environment variables with the
121 # leading letter from the name
122 s = string.lower(s)
123 if s[0] == "$":
124 return s[1:]
125 else:
126 return s
128 def get_first_letter(s):
129 return string.lower(trim_ignored_letters(s)[0])
132 def split_letters(nodes):
133 letter_groups = []
134 if nodes:
135 group = []
136 append = group.append
137 letter = get_first_letter(nodes[0].text[0])
138 letter_groups.append((letter, group))
139 for node in nodes:
140 nletter = get_first_letter(node.text[0])
141 if letter != nletter:
142 letter = nletter
143 group = []
144 letter_groups.append((letter, group))
145 append = group.append
146 append(node)
147 return letter_groups
150 # need a function to separate the nodes into columns...
151 def split_columns(nodes, columns=1):
152 if columns <= 1:
153 return [nodes]
154 # This is a rough height; we may have to increase to avoid breaks before
155 # a subitem.
156 colheight = len(nodes) / columns
157 numlong = len(nodes) % columns
158 if numlong:
159 colheight = colheight + 1
160 else:
161 numlong = columns
162 cols = []
163 for i in range(numlong):
164 start = i * colheight
165 end = start + colheight
166 cols.append(nodes[start:end])
167 del nodes[:end]
168 colheight = colheight - 1
169 try:
170 numshort = len(nodes) / colheight
171 except ZeroDivisionError:
172 cols = cols + (columns - len(cols)) * [[]]
173 else:
174 for i in range(numshort):
175 start = i * colheight
176 end = start + colheight
177 cols.append(nodes[start:end])
178 return cols
181 DL_LEVEL_INDENT = " "
183 def format_column(nodes):
184 strings = ["<dl compact>"]
185 append = strings.append
186 level = 0
187 previous = []
188 for node in nodes:
189 current = node.text
190 count = 0
191 for i in range(min(len(current), len(previous))):
192 if previous[i] != current[i]:
193 break
194 count = i + 1
195 if count > level:
196 append("<dl compact>" * (count - level) + "\n")
197 level = count
198 elif level > count:
199 append("\n")
200 append(level * DL_LEVEL_INDENT)
201 append("</dl>" * (level - count))
202 level = count
203 # else: level == count
204 for i in range(count, len(current) - 1):
205 term = node.text[i]
206 level = level + 1
207 append("\n<dt>%s\n<dd>\n%s<dl compact>"
208 % (term, level * DL_LEVEL_INDENT))
209 append("\n%s<dt>%s%s</a>"
210 % (level * DL_LEVEL_INDENT, node.links[0], node.text[-1]))
211 for link in node.links[1:]:
212 append(",\n%s %s[Link]</a>" % (level * DL_LEVEL_INDENT, link))
213 previous = current
214 append("\n")
215 append("</dl>" * (level + 1))
216 return string.join(strings, '')
219 def format_nodes(nodes, columns=1):
220 strings = []
221 append = strings.append
222 if columns > 1:
223 colnos = range(columns)
224 colheight = len(nodes) / columns
225 if len(nodes) % columns:
226 colheight = colheight + 1
227 colwidth = 100 / columns
228 append('<table width="100%"><tr valign="top">')
229 for col in split_columns(nodes, columns):
230 append('<td width="%d%%">\n' % colwidth)
231 append(format_column(col))
232 append("\n</td>")
233 append("\n</tr></table>")
234 else:
235 append(format_column(nodes))
236 append("\n<p>\n")
237 return string.join(strings, '')
240 def format_letter(letter):
241 if letter == '.':
242 lettername = ". (dot)"
243 elif letter == '_':
244 lettername = "_ (underscore)"
245 else:
246 lettername = string.upper(letter)
247 return "\n<hr>\n<h2><a name=\"letter-%s\">%s</a></h2>\n\n" \
248 % (letter, lettername)
251 def format_html_letters(nodes, columns=1):
252 letter_groups = split_letters(nodes)
253 items = []
254 for letter, nodes in letter_groups:
255 s = "<b><a href=\"#letter-%s\">%s</a></b>" % (letter, letter)
256 items.append(s)
257 s = ["<hr><center>\n%s</center>\n" % string.join(items, " |\n")]
258 for letter, nodes in letter_groups:
259 s.append(format_letter(letter))
260 s.append(format_nodes(nodes, columns))
261 return string.join(s, '')
263 def format_html(nodes, columns):
264 return format_nodes(nodes, columns)
267 def collapse(nodes):
268 """Collapse sequences of nodes with matching keys into a single node.
269 Destructive."""
270 if len(nodes) < 2:
271 return
272 prev = nodes[0]
273 i = 1
274 while i < len(nodes):
275 node = nodes[i]
276 if not node.cmp_entry(prev):
277 prev.links.append(node.links[0])
278 del nodes[i]
279 else:
280 i = i + 1
281 prev = node
284 def dump(nodes, fp):
285 for node in nodes:
286 fp.write(node.dump())
289 def process_nodes(nodes, columns, letters):
290 nodes.sort()
291 collapse(nodes)
292 if letters:
293 return format_html_letters(nodes, columns)
294 else:
295 return format_html(nodes, columns)
298 def main():
299 import getopt
300 ifn = "-"
301 ofn = "-"
302 columns = 1
303 letters = 0
304 opts, args = getopt.getopt(sys.argv[1:], "c:lo:",
305 ["columns=", "letters", "output="])
306 for opt, val in opts:
307 if opt in ("-o", "--output"):
308 ofn = val
309 elif opt in ("-c", "--columns"):
310 columns = string.atoi(val)
311 elif opt in ("-l", "--letters"):
312 letters = 1
313 if not args:
314 args = [ifn]
315 nodes = []
316 for fn in args:
317 nodes = nodes + load(open(fn))
318 num_nodes = len(nodes)
319 html = process_nodes(nodes, columns, letters)
320 program = os.path.basename(sys.argv[0])
321 if ofn == "-":
322 sys.stdout.write(html)
323 sys.stderr.write("\n%s: %d index nodes" % (program, num_nodes))
324 else:
325 open(ofn, "w").write(html)
326 print
327 print "%s: %d index nodes" % (program, num_nodes)
330 if __name__ == "__main__":
331 main()