Skip to content

Commit 3c5dafd

Browse files
updated and optimized insert_navbar.sh
1 parent 529b812 commit 3c5dafd

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

assets/scripts/insert_navbar.sh

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
2-
# This script inserts a top navigation bar into Documenter.jl generated sites.
2+
# This script inserts a top navigation bar into Documenter.jl-generated sites.
33
# The resulting output is similar to MultiDocumenter's navigation menu.
4-
# It checks all HTML files in the specified directory and its subdirectories.
4+
# It checks all HTML files in the specified directory and its subdirectories,
5+
# removes any existing navbar, then inserts the new navbar right after <body>.
56

67
# Function to print usage
78
print_usage() {
@@ -15,15 +16,17 @@ if [ "$#" -lt 2 ]; then
1516
exit 1
1617
fi
1718

18-
# Directory containing HTML files (passed as the first argument to the script)
19+
# Directory containing HTML files
1920
HTML_DIR=$1
20-
# URL of the navigation bar HTML file (passed as the second argument to the script)
21+
# URL of the navigation bar HTML file
2122
NAVBAR_URL=$2
23+
# Shift off the first two arguments so we can parse the rest
24+
shift 2
25+
2226
# Initialize exclude list
2327
EXCLUDE_LIST=""
2428

2529
# Parse optional arguments
26-
shift 2
2730
while [[ $# -gt 0 ]]; do
2831
key="$1"
2932
case $key in
@@ -40,11 +43,11 @@ while [[ $# -gt 0 ]]; do
4043
done
4144

4245
# Download the navigation bar HTML content
43-
NAVBAR_HTML=$(curl -s $NAVBAR_URL)
46+
NAVBAR_HTML=$(curl -s "$NAVBAR_URL")
4447

4548
# Check if the download was successful
4649
if [ -z "$NAVBAR_HTML" ]; then
47-
echo "Failed to download navbar HTML"
50+
echo "Failed to download navbar HTML from '$NAVBAR_URL'"
4851
exit 1
4952
fi
5053

@@ -60,32 +63,32 @@ should_exclude() {
6063
return 1 # Should not exclude
6164
}
6265

63-
# Process each HTML file in the directory and its subdirectories
64-
find "$HTML_DIR" -name "*.html" | while read file; do
66+
# Find and process each HTML file
67+
find "$HTML_DIR" -type f -name "*.html" | while read -r file; do
6568
# Check if the file should be excluded
66-
if [ ! -z "$EXCLUDE_LIST" ] && should_exclude "$file"; then
69+
if [ -n "$EXCLUDE_LIST" ] && should_exclude "$file"; then
6770
echo "Skipping excluded file: $file"
6871
continue
6972
fi
7073

71-
# Remove the existing navbar HTML section if present
74+
# Remove existing navbar (if any) between <!-- NAVBAR START --> and <!-- NAVBAR END -->
7275
if grep -q "<!-- NAVBAR START -->" "$file"; then
7376
awk '/<!-- NAVBAR START -->/{flag=1;next}/<!-- NAVBAR END -->/{flag=0;next}!flag' "$file" > temp && mv temp "$file"
7477
echo "Removed existing navbar from $file"
7578
fi
7679

77-
# Read the contents of the HTML file
78-
file_contents=$(cat "$file")
79-
80-
# Insert the navbar HTML after the <body> tag
81-
updated_contents="${file_contents/<body>/<body>
82-
$NAVBAR_HTML
83-
}"
80+
# Insert the new navbar right after the first <body> tag using awk
81+
awk -v nav="$NAVBAR_HTML" '
82+
/<body>/ {
83+
print $0
84+
print nav
85+
next
86+
}
87+
{ print }
88+
' "$file" > temp && mv temp "$file"
8489

85-
# Write the updated contents back to the file
86-
echo "$updated_contents" > "$file"
87-
88-
# Remove trailing blank lines immediately after the navbar
90+
# Remove excessive trailing blank lines after insertion
8991
awk 'BEGIN {RS=""; ORS="\n\n"} {gsub(/\n+$/, ""); print}' "$file" > temp_cleaned && mv temp_cleaned "$file"
92+
9093
echo "Inserted new navbar into $file"
91-
done
94+
done

0 commit comments

Comments
 (0)