Skip to content

Commit b9e1ff3

Browse files
updated and optimized insert_navbar.sh (#112)
* updated and optimized insert_navbar.sh * Update insert_navbar.sh
1 parent 529b812 commit b9e1ff3

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

assets/scripts/insert_navbar.sh

+31-24
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
@@ -39,12 +42,16 @@ while [[ $# -gt 0 ]]; do
3942
esac
4043
done
4144

42-
# Download the navigation bar HTML content
43-
NAVBAR_HTML=$(curl -s $NAVBAR_URL)
45+
# Determine if NAVBAR_SOURCE is a URL (starts with http or https) or a file path
46+
if [[ $NAVBAR_SOURCE == http* ]]; then
47+
NAVBAR_HTML=$(curl -s "$NAVBAR_SOURCE")
48+
else
49+
NAVBAR_HTML=$(cat "$NAVBAR_SOURCE")
50+
fi
4451

4552
# Check if the download was successful
4653
if [ -z "$NAVBAR_HTML" ]; then
47-
echo "Failed to download navbar HTML"
54+
echo "Failed to download navbar HTML from '$NAVBAR_URL'"
4855
exit 1
4956
fi
5057

@@ -60,32 +67,32 @@ should_exclude() {
6067
return 1 # Should not exclude
6168
}
6269

63-
# Process each HTML file in the directory and its subdirectories
64-
find "$HTML_DIR" -name "*.html" | while read file; do
70+
# Find and process each HTML file
71+
find "$HTML_DIR" -type f -name "*.html" | while read -r file; do
6572
# Check if the file should be excluded
66-
if [ ! -z "$EXCLUDE_LIST" ] && should_exclude "$file"; then
73+
if [ -n "$EXCLUDE_LIST" ] && should_exclude "$file"; then
6774
echo "Skipping excluded file: $file"
6875
continue
6976
fi
7077

71-
# Remove the existing navbar HTML section if present
78+
# Remove existing navbar (if any) between <!-- NAVBAR START --> and <!-- NAVBAR END -->
7279
if grep -q "<!-- NAVBAR START -->" "$file"; then
7380
awk '/<!-- NAVBAR START -->/{flag=1;next}/<!-- NAVBAR END -->/{flag=0;next}!flag' "$file" > temp && mv temp "$file"
7481
echo "Removed existing navbar from $file"
7582
fi
7683

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

80-
# Insert the navbar HTML after the <body> tag
81-
updated_contents="${file_contents/<body>/<body>
82-
$NAVBAR_HTML
83-
}"
84-
85-
# Write the updated contents back to the file
86-
echo "$updated_contents" > "$file"
87-
88-
# Remove trailing blank lines immediately after the navbar
94+
# Remove excessive trailing blank lines after insertion
8995
awk 'BEGIN {RS=""; ORS="\n\n"} {gsub(/\n+$/, ""); print}' "$file" > temp_cleaned && mv temp_cleaned "$file"
96+
9097
echo "Inserted new navbar into $file"
91-
done
98+
done

0 commit comments

Comments
 (0)