This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
generate-index.py
57 lines (49 loc) · 1.61 KB
/
generate-index.py
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
#!/usr/bin/env python
# To generate index, run "generate-index.py < workspacepilot.py"
# If a section is added make sure to add a line for that in the INDEX
# definition in workspacepilot.py or the line numbers will be off.
# Not spending the time to auto replace the INDEX definition, just
# paste in the output.
import sys
begin=0
end=0
insection=False
namedict = {}
romandict = {}
linenumdict = {}
entrynum = 0
longestname = 0
longestroman = 0
for n,line in enumerate(sys.stdin):
if line.startswith("# #############################") and not insection:
begin = n
insection = True
if n == begin + 1:
name = line[2:] # strip "# "
name = name[:-1] # strip newline
idx = name.rfind(". ")
roman = name[:idx+1]
romandict[entrynum] = roman
if len(roman) > longestroman:
longestroman = len(roman)
name = name[idx+2:]
namedict[entrynum] = name
if len(name) > longestname:
longestname = len(name)
if insection and line.startswith("# }}} END"):
end = n
insection = False
linenumdict[entrynum] = "(lines %d-%d)" % (begin+1, end+1)
entrynum += 1
# justify right
for n,roman in enumerate(romandict.values()):
if len(roman) < longestroman:
diff = longestroman - len(roman)
romandict[n] = diff * " " + roman
# justify left
for n,name in enumerate(namedict.values()):
if len(name) < longestname:
diff = longestname - len(name)
namedict[n] = name + diff * " "
for n in romandict.keys():
print " %s %s %s" % (romandict[n], namedict[n], linenumdict[n])