-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
refactor_readme.py
executable file
·46 lines (33 loc) · 1.01 KB
/
refactor_readme.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
#!/usr/bin/env python3
"""Script to resolve relative URLs in README prior to release."""
from __future__ import annotations
import re
import subprocess
import sys
from pathlib import Path
def main(args: list[str]) -> int:
if len(args) < 1:
print("No README path supplied.")
return 1
remote_url = (
subprocess.run(
["/usr/bin/env", "git", "remote", "get-url", "origin"],
check=True,
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)
# Convert SSH remote URLs to HTTPS
remote_url = re.sub(r"^ssh://git@", "https://", remote_url)
readme_path = Path(args[0])
readme_content = readme_path.read_text("utf-8")
new_content = re.sub(
r"(\[[^]]+]\()((?!https?:)[^)]+)(\))",
lambda m: m.group(1) + remote_url + "/blob/main/" + m.group(2) + m.group(3),
readme_content,
)
readme_path.write_text(new_content)
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))