Created
July 30, 2017 19:55
-
-
Save Aaron1011/a87b0d5c95d285332174525238e710ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import requests | |
GH_RE = re.compile("https:\/\/github\.com\/(\w+)\/(\w+)\/blob\/(master)") | |
commit_map = {} | |
def get_commit(repo): | |
if not repo in commit_map: | |
resp = requests.get("https://api.github.com/repos/{}/git/refs/heads/master".format(repo)) | |
resp.raise_for_status() | |
commit_map[repo] = resp.json()['object']['sha'] | |
return commit_map[repo] | |
def replace_match(m): | |
repo = m.group(1) + '/' + m.group(2) | |
commit = get_commit(repo) | |
return m.expand(r"https://github.com/\1/\2/blob/{}".format(commit)) | |
def fixup_file(filename): | |
with open(filename, 'r+') as f: | |
data = f.read() | |
new_data = re.sub(GH_RE, replace_match, data) | |
f.seek(0) | |
f.write(new_data) | |
f.truncate() | |
f.flush() | |
for root, dirs, files in os.walk('.'): | |
for f in files: | |
if f.endswith(".md"): | |
fixup_file(os.path.join(root, f)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment