Skip to content

Commit 27167e3

Browse files
authored
Added update-user-guide-links.py from en
1 parent 48349c6 commit 27167e3

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import subprocess
2+
import re
3+
4+
# Finds the documents to rewrite for files that include user-guide-content-moved.md.
5+
# Then opens these files and processes the stuff after those lines to figure out where
6+
# the line should move to.
7+
# Returns a list of ('old/path', 'new/path') tuples.
8+
def find_documents_to_rewrite():
9+
cmd = "ag --markdown -Q -l \"{% include user-guide-content-moved.md %}\""
10+
moved_docs = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().splitlines()
11+
12+
rewrites = []
13+
for doc in moved_docs:
14+
location = doc_location(doc)
15+
destinations = get_destinations_for_doc(doc)
16+
17+
if len(destinations) == 0:
18+
print("Unable to get possible destinations for %s" % doc)
19+
elif len(destinations) > 1:
20+
print("%s has multiple potential destinations. Not rewriting links." % doc)
21+
else:
22+
# print("%s --> %s" % (location, destinations[0]))
23+
rewrites.append((location, destinations[0]))
24+
25+
return rewrites
26+
27+
# Returns the location of the documentation as we will refer to it in the markdown.
28+
# /docs/path/to/foo/index.md are available at /docs/path/to/foo/
29+
# /docs/path/to/foo/bar.md are available at /docs/path/to/foo/bar/
30+
def doc_location(filename):
31+
if filename.endswith('/index.md'):
32+
return "/docs/" + filename[:-9] + "/"
33+
else:
34+
return "/docs/" + filename[:-3] + "/"
35+
36+
REDIRECT_REGEX = re.compile("^.*\[(.*)\]\((.*)\)$")
37+
38+
def get_destinations_for_doc(filename):
39+
destination_paths = []
40+
with open(filename) as f:
41+
lines = [line.rstrip('\n').rstrip('\r') for line in f.readlines()]
42+
43+
# Remove empty lines
44+
lines = filter(bool, lines)
45+
46+
content_moved_index = lines.index("{% include user-guide-content-moved.md %}")
47+
48+
# Get everything after that line.
49+
destinations = lines[content_moved_index + 1:]
50+
for destination in destinations:
51+
result = REDIRECT_REGEX.match(destination)
52+
if not result:
53+
return []
54+
doc_title = result.group(1) # Unused, can print it out for more info.
55+
new_path = result.group(2)
56+
destination_paths.append(new_path)
57+
58+
return destination_paths
59+
60+
# Given a list of (old/path, new/path) tuples executes a sed command across all files in
61+
# to replace (/docs/path/to/old/doc/) with (/docs/path/to/new/doc/).
62+
def rewrite_documents(rewrites):
63+
cmd = "find . -name '*.md' -type f -exec sed -i.bak 's@(%s)@(%s)@g' '{}' \;"
64+
for original, new in rewrites:
65+
66+
print("%s --> %s" % (original, new))
67+
original = original.replace('-', '\-')
68+
new = new.replace('-', '\-')
69+
70+
#print(cmd % (original, new))
71+
subprocess.call(cmd % (original, new), shell=True)
72+
73+
# We can't have in-line replace across multiple files without sudo (I think), so it
74+
# creates a lot of backups that we have to delete.
75+
def remove_sed_backups():
76+
cmd = "find . -name '*.bak' -delete"
77+
subprocess.call(cmd, shell=True)
78+
79+
def main():
80+
rewrites = find_documents_to_rewrite()
81+
rewrite_documents(rewrites)
82+
remove_sed_backups()
83+
84+
if __name__ == "__main__":
85+
main()

0 commit comments

Comments
 (0)