-
Notifications
You must be signed in to change notification settings - Fork 8
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
bdocs: script for auto-generating redirects #8364
Draft
internetisaiah
wants to merge
9
commits into
develop
Choose a base branch
from
bdocs-improvements
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
66e3eee
Random bdocs improvements (during time off)
internetisaiah b18c690
adding credirects v1
internetisaiah 6baee95
Merge branch 'develop' into bdocs-improvements
internetisaiah 6b8c352
test commit
internetisaiah 061acf0
undoing test
internetisaiah 4279bfe
Merge branch 'develop' into bdocs-improvements
internetisaiah 39d2b16
fixing usage comments
internetisaiah 641ff87
Merge branch 'develop' into bdocs-improvements
internetisaiah 13d1ee6
removing work that was done in another branch
internetisaiah 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 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,70 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# DESCRIPTION | ||
# | ||
# Usage: ./bdocs credirects | ||
|
||
import re | ||
import subprocess | ||
|
||
# Paths (assuming these are sourced or set elsewhere in your environment) | ||
redirect_file = "./assets/js/broken_redirect_list.js" | ||
project_root = "./" # Adjust to your actual root if needed | ||
|
||
# Using Git, get the list of files that have been renamed. | ||
def get_changed_files(): | ||
cmd = f"git diff -M --summary develop HEAD -- {project_root}_docs" | ||
result = subprocess.run(cmd, shell=True, capture_output=True, text=True) | ||
# Filter lines that start with "rename" or " rename" | ||
return [line.strip() for line in result.stdout.splitlines() if line.startswith("rename") or line.startswith(" rename")] | ||
|
||
def create_redirect(line): | ||
# Remove everything up to and including the first space, but keep the initial underscore | ||
line = line.split(" ", 1)[1] | ||
|
||
# Remove any trailing `(NUM%)` from the line | ||
line = re.sub(r"\s\(\d+%\)$", "", line) | ||
|
||
# Get the relative paths for the old and new filenames | ||
line_separator = line.split("{")[0].strip() | ||
|
||
# Check if this is a directory rename (no `.md` in `{old => }/new.md` portion) | ||
if re.search(r"{([^{}]+) => }/[^\s]+\.md", line): | ||
# Directory-only rename handling | ||
old_path_part, new_filename = re.search(r"{([^{}]+) => }/(.+)", line).groups() | ||
|
||
# Construct the paths for old and new locations | ||
old_path = f"/{line_separator}{old_path_part}/{new_filename}" | ||
new_path = f"/{line_separator}{new_filename}" | ||
elif re.search(r"{(.+?) => (.+?)}", line): | ||
# Standard file rename handling with `{old => new}` pattern | ||
unformatted_old_path, unformatted_new_path = re.search(r"{(.+?) => (.+?)}", line).groups() | ||
old_path = f"/{line_separator}{unformatted_old_path}" | ||
new_path = f"/{line_separator}{unformatted_new_path}" | ||
else: | ||
return None | ||
|
||
# Remove leading underscores and .md extensions, and format paths | ||
old_path = old_path.replace("/_", "/").replace(".md", "") | ||
new_path = new_path.replace("/_", "/").replace(".md", "") | ||
|
||
# Convert paths to the redirect syntax: validurls['OLD'] = 'NEW'; | ||
redirect = f"validurls['{old_path}'] = '{new_path}';" | ||
|
||
return redirect | ||
|
||
def main(): | ||
# Fetch changed files | ||
changed_files = get_changed_files() | ||
|
||
# Process each line and write to redirect file | ||
with open(redirect_file, 'a') as f: | ||
for line in changed_files: | ||
formatted_redirect = create_redirect(line) | ||
if formatted_redirect: | ||
f.write(formatted_redirect + "\n") | ||
|
||
print("Redirects added successfully!") | ||
|
||
if __name__ == "__main__": | ||
main() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the global vars!