Created
November 2, 2019 03:15
-
-
Save zh4n7wm/f13d18a9c6c341ee08646a1ed873ba28 to your computer and use it in GitHub Desktop.
修复从 bitbucket 导入到 github wiki 中的 markdown link
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 | |
| def fix_github_link(filename): | |
| pattern = re.compile( | |
| r'\[([^\]]+)\]\(((?!http://|https://|prs://|zpl.io)[^(]+)\)', re.I | |
| ) | |
| new_lines = [] | |
| with open(filename) as fp: | |
| for line in fp: | |
| for match in pattern.finditer(line): | |
| desc, link = match.groups() | |
| old_text = f'[{desc}]({link})' | |
| new_link = link.split('/')[-1] | |
| if new_link[-3:].lower() == '.md': | |
| new_link = new_link.replace('.md', '') | |
| new_text = f'[{desc}]({new_link})' | |
| line = line.replace(old_text, new_text) | |
| new_lines.append(line) | |
| with open(filename, 'w') as fp: | |
| fp.writelines(new_lines) | |
| def main(path): | |
| for root, dirs, files in os.walk(path): | |
| if '.git' in dirs: | |
| dirs.remove('.git') | |
| for f in files: | |
| fpath = os.path.join(root, f) | |
| if fpath[-6:].lower() == '.md.md': # rename 'x.md.md' to 'x.md' | |
| os.rename(fpath, fpath[:-3]) | |
| fpath = fpath[:-3] | |
| ext = os.path.splitext(fpath)[-1].lower() | |
| if ext not in {'', '.md'}: | |
| continue | |
| fix_github_link(fpath) | |
| if __name__ == '__main__': | |
| main('.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment