-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_update_sidebars.sh
More file actions
executable file
·91 lines (75 loc) · 2.82 KB
/
_update_sidebars.sh
File metadata and controls
executable file
·91 lines (75 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# This script updates the sidebar navigation in all relevant HTML files.
# It can be run in two ways:
# 1. With a filename argument to update a single file: ./update_sidebars.sh path/to/file.html
# 2. Without arguments to update all HTML files in the project.
TEMPLATE_ROOT="_templates/sidebar_root.html"
TEMPLATE_SUB="_templates/sidebar_sub.html"
# Define a function to process a single file
process_file() {
local file="$1"
# --- SAFETY CHECK ---
# Check if both markers exist before proceeding.
if ! grep -q "<!-- SIDEBAR-START -->" "$file" || ! grep -q "<!-- SIDEBAR-END -->" "$file"; then
echo "Skipping $file: Markers not found."
return
fi
# --- END SAFETY CHECK ---
echo "Processing $file..."
# Determine which template to use based on directory path
local dir_name
dir_name=$(dirname "$file")
local template_file
local active_filename
if [[ "$dir_name" == "." ]]; then
template_file="$TEMPLATE_ROOT"
active_filename=$(basename "$file")
else
template_file="$TEMPLATE_SUB"
# For sub-page files, the href in the template is e.g., ../fbms/genus_zero.html
# We construct this path from the file variable.
active_filename="../${file#./}" # <-- THIS IS THE CORRECTED LINE
fi
# Read the content of the correct template
local sidebar_content
sidebar_content=$(cat "$template_file")
# Inject 'class="active"' into the correct link
local placeholder="<a href=\"$active_filename\""
local replacement="<a href=\"$active_filename\" class=\"active\""
local active_sidebar_content
active_sidebar_content=$(echo "$sidebar_content" | sed "s|$placeholder|$replacement|")
# Prepare the final content for injection
local final_content
final_content="<!-- SIDEBAR-START -->\n<div class=\"sidebar\">\n${active_sidebar_content}\n</div>\n<!-- SIDEBAR-END -->"
# Use awk for a safe, multiline replacement between the markers
awk -v replacement="$final_content" '
BEGIN {p=1}
/<!-- SIDEBAR-START -->/ {
print replacement;
p=0;
next;
}
/<!-- SIDEBAR-END -->/ {
p=1;
next;
}
p {print}
' "$file" > "$file.tmp" && mv "$file.tmp" "$file"
}
# --- Main script execution ---
if [ -n "$1" ]; then
# If an argument is provided, process only that file
if [ -f "$1" ]; then
process_file "$1"
echo -e "\nSingle file updated."
else
echo "Error: File '$1' not found."
exit 1
fi
else
# If no arguments, find and process all relevant HTML files
find . -type f -name "*.html" ! -path "./_templates/*" -print0 | while IFS= read -r -d $'\0' file; do
process_file "$file"
done
echo -e "\nAll sidebars updated successfully."
fi