diff --git a/bdocs b/bdocs index 6ea1777f27e..d50df33cacd 100755 --- a/bdocs +++ b/bdocs @@ -17,6 +17,7 @@ export RELEASE="$PROJECT_ROOT/scripts/create_release_text.sh" export TLINKS="$PROJECT_ROOT/scripts/transform_reference_links.py" export RLINKS="$PROJECT_ROOT/scripts/remove_unused_reference_links.rb" export ULINKS="$PROJECT_ROOT/scripts/update_old_links.py" +export MREDIRECTS="$PROJECT_ROOT/scripts/make_redirects.py" export LREDIRECTS="$PROJECT_ROOT/scripts/list_new_redirect_urls.sh" # Utility scripts that are not directly used in bdocs: export MRD="$PROJECT_ROOT/scripts/utils/merge_redirect_descendants.py" @@ -36,6 +37,7 @@ OPTIONS: tlinks Transform reference links to inline links on 1 or more pages rlinks Remove unused reference links on 1 or more pages ulinks Update old links using newest redirect on 1 or more pages + mredirects Make redirects for all renamed files in this branch lredirects Test new redirects by listing old URLs in this branch help Display this help message and exit @@ -104,6 +106,9 @@ case $1 in "$ULINKS" "$2" # rm "$REDIRECT_MATCHES" ;; + mredirects) + "$MREDIRECTS" + ;; lredirects) if [[ $# -eq 2 ]]; then "$LREDIRECTS" "$2" diff --git a/scripts/make_redirects.py b/scripts/make_redirects.py new file mode 100755 index 00000000000..06e49948105 --- /dev/null +++ b/scripts/make_redirects.py @@ -0,0 +1,76 @@ +#!/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" + changed_files = [line.strip() for line in result.stdout.splitlines() if line.startswith("rename") or line.startswith(" rename")] + + return changed_files + +def create_redirect(line): + # Strip "rename " and the trailing percentage + line = line.split(" ", 1)[1] + line = re.sub(r"\s\(\d+%\)$", "", line) + + # Extract the `{old => new}` part. + brace_match = re.search(r"{([^}]+) => ([^}]+)}", line) + if not brace_match: + return None + + old_in_braces = brace_match.group(1) + new_in_braces = brace_match.group(2) + + # Grab everything before and after the braces + before_braces = line.split("{")[0] + after_braces = line.split("}")[1] # everything after the closing brace + + # Build the leading path (before the '{') + leading_path = before_braces.strip() + + # The trailing part after the '}' (e.g. "/catalogs.md") + # (strip leading slash, if any) + trailing_path = after_braces.strip() + + # Combine to form full old & new + # old_in_braces, new_in_braces might still have .md in them + # trailing_path might also have .md + + old_full_path = f"/{leading_path}{old_in_braces}{trailing_path}" + new_full_path = f"/{leading_path}{new_in_braces}{trailing_path}" + + # Clean underscores and strip .md + old_full_path = old_full_path.replace("/_", "/").replace(".md", "") + new_full_path = new_full_path.replace("/_", "/").replace(".md", "") + + # Return the final redirect line + return f"validurls['{old_full_path}'] = '{new_full_path}';" + + +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()