-
Notifications
You must be signed in to change notification settings - Fork 168
/
fix_links.py
65 lines (49 loc) · 1.51 KB
/
fix_links.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
import re
import sys
import textwrap
from pathlib import Path
USAGE = textwrap.dedent(
"""\
python fix_links.py FILENAME [FILENAME...]
Attempt to fix links in accordance with a predefined list of rules.
You must specify at least one FILENAME. Note that filenames are
treated as glob patterns relative to the working directory. After
any glob returns, we will filter for filenames that end in `.md`.
"""
)
def fix_md(path: Path) -> str:
"""
Given a file, read it and change all the links that look like::
http[s]://lost-stats.github.io/blah
into::
{{ "/blah" | relative_url }}
Args:
path: The path to transform
Returns:
The transformed md file
"""
with open(path, "rt") as infile:
md_file = infile.read()
return re.sub(
r"http[s]://lost-stats.github.io(/[a-zA-Z0-9/#-&=+_%\.]*)",
r'{{ "\1" | relative_url }}',
md_file,
)
def main():
if len(sys.argv) < 2:
print(USAGE, file=sys.stderr)
sys.exit(1)
cwd = Path(".")
for pattern in sys.argv[1:]:
for path in cwd.glob(pattern):
if path.is_dir():
# We skip directories
continue
if path.suffix == ".md":
fixed_md = fix_md(path)
with open(path, "wt") as outfile:
outfile.write(fixed_md)
else:
print(f"Skipping {path} as it is not an md")
if __name__ == "__main__":
main()