1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
#! /usr/bin/env python
"""
"""
__version__ = '$Revision$'
import re
import string
import sys
class Node:
__rmtt = re.compile(r"(.*)<tt>(.*)</tt>(.*)$", re.IGNORECASE)
__rmjunk = re.compile("<#\d+#>")
def __init__(self, link, str, seqno):
self.links = [link]
self.seqno = seqno
# remove <#\d+#> left in by moving the data out of LaTeX2HTML
str = self.__rmjunk.sub('', str)
# now remove <tt>...</tt> markup; contents remain.
if '<' in str:
m = self.__rmtt.match(str)
if m:
kstr = string.join(m.group(1, 2, 3), '')
else:
kstr = str
else:
kstr = str
kstr = string.lower(kstr)
# build up the text
self.text = []
parts = string.split(str, '!')
parts = map(string.split, parts, ['@'] * len(parts))
for entry in parts:
if len(entry) != 1:
key, text = entry
else:
text = entry[0]
self.text.append(text)
# Building the key must be separate since any <tt> has been stripped
# from the key, but can be avoided if both key and text sources are
# the same.
if kstr != str:
self.key = []
kparts = string.split(kstr, '!')
kparts = map(string.split, kparts, ['@'] * len(kparts))
for entry in kparts:
if len(entry) != 1:
key, text = entry
else:
key = entry[0]
self.key.append(key)
else:
self.key = self.text
def __cmp__(self, other):
"""Comparison operator includes sequence number, for use with
list.sort()."""
return self.cmp_entry(other) or cmp(self.seqno, other.seqno)
def cmp_entry(self, other):
"""Comparison 'operator' that ignores sequence number."""
for i in range(min(len(self.key), len(other.key))):
c = (cmp(self.key[i], other.key[i])
or cmp(self.text[i], other.text[i]))
if c:
return c
return cmp(self.key, other.key)
def __repr__(self):
return "<Node for %s (%s)>" % (string.join(self.text, '!'), self.seqno)
def __str__(self):
return string.join(self.key, '!')
def dump(self):
return "%s\0%s###%s\n" \
% (string.join(self.links, "\0"),
string.join(self.text, '!'),
self.seqno)
def load(fp):
nodes = []
rx = re.compile(r"(.*)\0(.*)###(.*)$")
while 1:
line = fp.readline()
if not line:
break
m = rx.match(line)
if m:
link, str, seqno = m.group(1, 2, 3)
nodes.append(Node(link, str, seqno))
return nodes
def split_letters(nodes):
letter_groups = []
group = []
append = group.append
if nodes:
letter = nodes[0].key[0][0]
letter_groups.append((letter, group))
for node in nodes:
nletter = node.key[0][0]
if letter != nletter:
letter = nletter
group = []
letter_groups.append((letter, group))
append = group.append
append(node)
return letter_groups
def format_nodes(nodes):
# Does not create multiple links to multiple targets for the same entry;
# uses a separate entry for each target. This is a bug.
level = 0
strings = ["<dl compact>"]
append = strings.append
prev = None
for node in nodes:
nlevel = len(node.key) - 1
if nlevel > level:
if prev is None or node.key[level] != prev.key[level]:
append("%s\n<dl compact>" % node.text[level])
else:
append("<dl compact>")
level = nlevel
elif nlevel < level:
append("</dl>" * (level - len(node.key) + 1))
level = nlevel
if prev is not None and node.key[level] != prev.key[level]:
append("</dl>")
else:
append("<dl compact>")
elif level:
if node.key[level-1] != prev.key[level-1]:
append("</dl>\n%s<dl compact>"
% node.text[level-1])
append("%s%s</a><br>" % (node.links[0], node.text[-1]))
for link in node.links[1:]:
strings[-1] = strings[-1][:-4] + ","
append(link + "[Link]</a><br>")
prev = node
append("</dl>" * (level + 1))
append("")
append("")
return string.join(strings, "\n")
def format_letter(letter):
if letter == '.':
lettername = ". (dot)"
elif letter == '_':
lettername = "_ (underscore)"
else:
lettername = string.upper(letter)
return "<hr>\n<h2><a name=\"letter-%s\">%s</a></h2>\n\n" \
% (letter, lettername)
def format_html(nodes):
letter_groups = split_letters(nodes)
items = []
for letter, nodes in letter_groups:
s = "<b><a href=\"#letter-%s\">%s</a></b>" % (letter, letter)
items.append(s)
s = "<hr><center>\n%s</center>\n" % string.join(items, " |\n")
for letter, nodes in letter_groups:
s = s + format_letter(letter) + format_nodes(nodes)
return s
def collapse(nodes):
"""Collapse sequences of nodes with matching keys into a single node.
Destructive."""
if len(nodes) < 2:
return
prev = nodes[0]
i = 1
while i < len(nodes):
node = nodes[i]
if not node.cmp_entry(prev):
prev.links.append(node.links[0])
del nodes[i]
## sys.stderr.write("collapsing %s\n" % `node`)
else:
i = i + 1
prev = node
def dump(nodes, fp):
for node in nodes:
fp.write(node.dump())
def main():
fn = sys.argv[1]
nodes = load(open(fn))
nodes.sort()
dump(nodes, open(fn + ".dump-1", "w"))
collapse(nodes)
dump(nodes, open(fn + ".dump-2", "w"))
sys.stdout.write(format_html(nodes))
if __name__ == "__main__":
main()
|