generated from roboflow/template-python
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Fix and augment relative links in MD files to GitHub blob URLs #2061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+95
−9
Merged
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3499878
Add script to augment relative links in MD files to GitHub blob URLs
Borda 541a117
fix(pre_commit): 🎨 auto format pre-commit hooks
pre-commit-ci[bot] 74d8ae4
update
Borda bd2edcc
update
Borda 37dc170
Merge branch 'develop' into mr/links
Borda 1afd59e
Revert "update"
Borda 64e7c59
ci
Borda e801d26
Merge branch 'develop' into mr/links
Borda e57a551
ci
Borda 4d51ae6
Apply suggestions from code review
Borda 98fcad8
Merge branch 'develop' into mr/links
Borda 597abae
Merge branch 'develop' into mr/links
Borda ad53ffa
Add type annotations to `augment_links.py` and update config
Borda 47ebaa6
Merge branch 'develop' into mr/links
Borda cd5c9c8
Merge branch 'develop' into mr/links
Borda 9229c1a
Add `lychee-action` for link checking in CI workflows
Borda 4c35bab
typo
Borda 93d987e
Apply suggestions from code review
Borda 7c41cce
Expand CI link check to all `README.md` files and refactor relative l…
Borda 42f7624
links in README.md
Borda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Script to augment relative links in markdown files to GitHub URLs. | ||
| """ | ||
|
|
||
| import argparse | ||
| import os | ||
| import re | ||
|
|
||
|
|
||
| def get_repo_root(): | ||
| """Get the repository root path.""" | ||
| script_dir = os.path.dirname(os.path.abspath(__file__)) | ||
| return os.path.dirname(script_dir) | ||
Borda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def augment_links_in_file(file_path, branch="main"): | ||
| """ | ||
| Augment relative links in a markdown file to GitHub URLs. | ||
|
|
||
| Args: | ||
| file_path (str): Path to the markdown file. | ||
| branch (str): Branch name, default "main". | ||
Borda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| repo_root = get_repo_root() | ||
|
|
||
| if not file_path.endswith(".md"): | ||
| return | ||
|
|
||
| with open(file_path) as f: | ||
| content = f.read() | ||
Borda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def replace_link(match): | ||
| full_match = match.group(0) | ||
| text = match.group(1) | ||
| url = match.group(2) | ||
Borda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not url.startswith("http"): | ||
| # Resolve relative to absolute path | ||
| abs_path = os.path.normpath(os.path.join(os.path.dirname(file_path), url)) | ||
| if os.path.exists(abs_path): | ||
| if full_match.startswith("!"): | ||
| ref = "blob" | ||
| else: | ||
| ref = "tree" | ||
Borda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| rel_to_root = os.path.relpath(abs_path, repo_root) | ||
| new_url = f"https://github.com/roboflow/supervision/{ref}/{branch}/{rel_to_root}" | ||
| if full_match.startswith("!"): | ||
| return f"" | ||
| else: | ||
| return f"[{text}]({new_url})" | ||
| return full_match | ||
|
|
||
| new_content = re.sub(r"(!?)\[([^\]]+)\]\(([^)]+)\)", replace_link, content) | ||
| with open(file_path, "w") as f: | ||
| f.write(new_content) | ||
Borda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def main(): | ||
| parser = argparse.ArgumentParser( | ||
| description="Augment relative links to GitHub URLs." | ||
| ) | ||
| parser.add_argument("--branch", default="main", help="Branch name") | ||
| parser.add_argument("files", nargs="+", help="Files to process") | ||
| args = parser.parse_args() | ||
|
|
||
| for file in args.files: | ||
| augment_links_in_file(file, args.branch) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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
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
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.