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
|
#!/usr/bin/env python
# create a table of contents for pages that need it
import sys
import re
import argparse
TOC_PAGES = ['man/index.html',
'man-4.3/index.html',
'man-3/index.html',
'man-2.0/index.html',
'man-1.2/index.html']
V2_PAGES = ['index.html']
INSERT_AFTER = '<!--TOC-->'
def read_toc_data(infile, debug):
topics_data = []
commands_data = []
f = open(infile)
for line in f:
if line.startswith('[['):
line = line[2:-3] # strip [[ and ]]\n
info, short_help = line.split(',', 1)
short_help = short_help.strip()
info_split = info.split('_')
if info_split[0] == 'topics':
if len(info_split) == 2:
topics_data.append((1, short_help, info))
elif len(info_split) >= 3:
topics_data.append((2, short_help, info))
elif info_split[0] == 'cmdhelp':
if len(info_split) == 2:
commands_data.append((2, info_split[1], info))
elif len(info_split) >= 3:
commands_data.append((3, '_'.join(info_split[2:]), info))
toc = ''
if len(topics_data) > 0 or len(commands_data) > 0:
toc = '<div id="toc">\n'
for depth, text, link in topics_data:
toc += '<div class="toclevel%s"><a href="#%s">%s</a></div>\n' % (
depth, link, text)
for depth, text, link in commands_data:
toc += '<div class="toclevel%s"><a href="#%s">%s</a></div>\n' % (
depth, link, text)
toc += '</div>\n'
return toc
def generate_toc(infile, outfile, debug):
if debug:
print "Infile:", infile
toc = read_toc_data(infile, debug)
'''
toc_data = []
section = re.compile(r"<h(?P<depth>[0-9])( id=\"(?P<id>[^\"]+)\")?>(?P<text>.*)</h[0-9]>")
for line in f:
m = section.match(line)
if m:
if debug:
print "toc_data: %s" % str(((m.group('depth'), m.group('text'), m.group('id'))))
toc_data.append((m.group('depth'), m.group('text'), m.group('id')))
toc = ''
if len(toc_data) > 0:
toc = '<div id="toc">\n'
for depth, text, link in toc_data:
if depth >= 2 and link is not None:
toc += '<div class="toclevel%s"><a href="#%s">%s</a></div>\n' % (
int(depth) - 1, link, text)
toc += '</div>\n'
'''
# Write TOC to outfile
if outfile:
if debug:
print "Writing TOC:"
print "----"
print toc
print "----"
print "Outfile:", outfile
fil = open(outfile)
f = fil.readlines()
fil.close()
f2 = open(outfile, 'w')
for line in f:
f2.write(line)
if toc and line.startswith(INSERT_AFTER):
f2.write(toc)
f2.close()
def generate_v2(page, debug):
f = open(page).readlines()
toc_data = []
section = re.compile(r"<h(?P<depth>[0-9])( id=\"(?P<id>[^\"]+)\")?>(?P<text>.*)</h[0-9]>")
for line in f:
m = section.match(line)
if m:
if debug:
print "toc_data: %s" % str(((m.group('depth'), m.group('text'), m.group('id'))))
toc_data.append((m.group('depth'), m.group('text'), m.group('id')))
toc = ''
if len(toc_data) > 0:
toc = '<div id="toc">\n'
for depth, text, link in toc_data:
if depth >= 2 and link is not None:
toc += '<div class="toclevel%s"><a href="#%s">%s</a></div>\n' % (
int(depth) - 1, link, text)
toc += '</div>\n'
f2 = open(page, 'w')
for line in f:
f2.write(line)
if toc and line.startswith(INSERT_AFTER):
f2.write(toc)
f2.close()
def main():
parser = argparse.ArgumentParser(description="Generate table of contents")
parser.add_argument('-d', '--debug', dest='debug', action='store_true',
help="Enable debug output")
parser.add_argument('-o', '--output', metavar='output', type=str,
help="File to inject TOC into")
parser.add_argument('input', metavar='input', type=str,
help="File to read TOC metadata from")
args = parser.parse_args()
debug = args.debug
outfile = args.output
infile = args.input
print "+ %s -> %s" % (infile, outfile)
gen = False
for tocpage in TOC_PAGES:
if not gen and outfile.endswith(tocpage):
generate_toc(infile, outfile, debug)
gen = True
for tocpage in V2_PAGES:
if not gen and outfile.endswith(tocpage):
generate_v2(outfile, debug)
gen = True
if __name__ == "__main__":
main()
|