forked from sympy/sympy_doc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_indexes.py
executable file
·85 lines (71 loc) · 2.88 KB
/
generate_indexes.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
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
#!/usr/bin/env python
"""
Automatically generate "Docs for other versions".
"""
import codecs
import os
import os.path
import sys
import shutil
def main():
# Get a list of all the releases, in order.
# These are held in a file, releases.txt where
# each line includes RELEASEDIR:Release Name.
releases = []
with codecs.open("releases.txt", "r", "utf8") as f:
for linei, line in enumerate(f):
parts = line.strip().split(":")
if len(parts) != 2:
print "Error parsing line {0}".format(linei)
sys.exit(1)
releases.append((parts[0], parts[1]))
# Simply update the releases list in each $RELEASEDIR/index.html.
# This should be idempotent.
for releasedir, releasename in releases:
with codecs.open(os.path.join(releasedir, "index.html"), "r", "utf8") as f:
lines = f.readlines()
# Find where in the file we need to edit
context = [
'<div class="sphinxsidebarwrapper">',
'<p class="logo">',
'<img class="logo" src="_static/sympylogo.png" alt="Logo"/>',
'</a></p>'
]
contexti = 0
for linei, line in enumerate(lines):
# Did we match all we need to match?
if contexti >= len(context):
break
# When we find a partial match, move to the next line to see if the
# match continues. Otherwise, we start over.
if context[contexti] in line:
contexti += 1
else:
contexti = 0
inserti = linei
# If we didn't find a match in the whole file, we need to stop and fix something.
if contexti < len(context):
print "Couldn't find where to insert Docs for other versions in {0}".format(releasedir)
sys.exit(2)
# Do we want to delete anything? Is there a table there already?
if "Docs for other versions" in lines[linei]:
# Where does the current table end?
# We want endi to be the first line not part of the current table.
while linei < len(lines):
linei += 1
if 'p class="topless"' not in lines[linei]:
break
endi = linei
# Remove the current table and insert a new one.
del lines[inserti:endi]
for insertreleasedir, insertrelease in reversed(releases):
lines.insert(inserti, ' <p class="topless"><a href="../{0}/index.html">{1}</a></p>\n'.format(
insertreleasedir,
insertrelease
))
lines.insert(inserti, " <h4>Docs for other versions</h4>\n")
# Write the changed file back out.
with codecs.open(os.path.join(releasedir, "index.html"), "w", "utf8") as f:
f.writelines(lines)
if __name__ == '__main__':
sys.exit(main())