From 6b8c35248c608ac8d9338bea5346a3a8f2302f28 Mon Sep 17 00:00:00 2001 From: internetisaiah <95643215+internetisaiah@users.noreply.github.com> Date: Fri, 1 Nov 2024 11:24:43 -0700 Subject: [PATCH] test commit --- _docs/_contributing/{home.md => homeward.md} | 0 bdocs | 2 +- scripts/create_redirects.py | 70 ++++++++++++++++++++ scripts/create_redirects.sh | 47 ------------- 4 files changed, 71 insertions(+), 48 deletions(-) rename _docs/_contributing/{home.md => homeward.md} (100%) create mode 100755 scripts/create_redirects.py delete mode 100755 scripts/create_redirects.sh diff --git a/_docs/_contributing/home.md b/_docs/_contributing/homeward.md similarity index 100% rename from _docs/_contributing/home.md rename to _docs/_contributing/homeward.md diff --git a/bdocs b/bdocs index acf6125bf5e..f8709a6ba13 100755 --- a/bdocs +++ b/bdocs @@ -15,7 +15,7 @@ export DEPLOY="$PROJECT_ROOT/scripts/create_deploy_text.sh" 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 CREDIRECTS="$PROJECT_ROOT/scripts/create_redirects.sh" +export CREDIRECTS="$PROJECT_ROOT/scripts/create_redirects.py" export LREDIRECTS="$PROJECT_ROOT/scripts/list_redirect_urls.sh" # Displays usage for bdocs diff --git a/scripts/create_redirects.py b/scripts/create_redirects.py new file mode 100755 index 00000000000..18ccc37650a --- /dev/null +++ b/scripts/create_redirects.py @@ -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() diff --git a/scripts/create_redirects.sh b/scripts/create_redirects.sh deleted file mode 100755 index b59b2fafe03..00000000000 --- a/scripts/create_redirects.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash -# -# DESCRIPTION -# -# Usage: ./bdocs redirects -c - -# Get the list of renamed files in `_docs` directory from `develop` branch -CHANGED_FILES=$(git diff --name-status origin/develop -- "$PROJECT_ROOT/_docs" | awk '$1 == "R" {print $2 " " $3}') - -# Check if there are any renamed files -require_changed_files() { - if [ -z "$CHANGED_FILES" ]; then - echo "Error: No files or directories changed in the '_docs' directory." - exit 1 - fi -} - -# Function to format paths to remove underscores and `.md` extension -format_path() { - echo "$1" | sed -E 's|/_|/|g' | sed -E 's|\.md$||g' -} - -main() { - # Create redirects - redirects="" - while IFS= read -r line; do - old_path=$(echo "$line" | awk '{print $1}') - new_path=$(echo "$line" | awk '{print $2}') - - # Format the paths - formatted_old_path=$(format_path "$old_path") - formatted_new_path=$(format_path "$new_path") - - # Create the redirect entry - redirect="validurls['$formatted_old_path'] = '$formatted_new_path';" - redirects+="$redirect"$'\n' - done <<< "$CHANGED_FILES" - - # Write redirects to the redirect file - echo "Appending redirects to $REDIRECT_FILE..." - echo "$redirects" >> "$REDIRECT_FILE" - - echo "Redirects added successfully!" -} - -require_changed_files -main