Skip to content

Commit

Permalink
test commit
Browse files Browse the repository at this point in the history
  • Loading branch information
internetisaiah committed Nov 1, 2024
1 parent 6baee95 commit 6b8c352
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion bdocs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions scripts/create_redirects.py
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()
47 changes: 0 additions & 47 deletions scripts/create_redirects.sh

This file was deleted.

0 comments on commit 6b8c352

Please sign in to comment.