1
1
#! /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.
3
3
# 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>.
5
6
6
7
# Function to print usage
7
8
print_usage () {
@@ -15,15 +16,17 @@ if [ "$#" -lt 2 ]; then
15
16
exit 1
16
17
fi
17
18
18
- # Directory containing HTML files (passed as the first argument to the script)
19
+ # Directory containing HTML files
19
20
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
21
22
NAVBAR_URL=$2
23
+ # Shift off the first two arguments so we can parse the rest
24
+ shift 2
25
+
22
26
# Initialize exclude list
23
27
EXCLUDE_LIST=" "
24
28
25
29
# Parse optional arguments
26
- shift 2
27
30
while [[ $# -gt 0 ]]; do
28
31
key=" $1 "
29
32
case $key in
@@ -40,11 +43,11 @@ while [[ $# -gt 0 ]]; do
40
43
done
41
44
42
45
# Download the navigation bar HTML content
43
- NAVBAR_HTML=$( curl -s $NAVBAR_URL )
46
+ NAVBAR_HTML=$( curl -s " $NAVBAR_URL " )
44
47
45
48
# Check if the download was successful
46
49
if [ -z " $NAVBAR_HTML " ]; then
47
- echo " Failed to download navbar HTML"
50
+ echo " Failed to download navbar HTML from ' $NAVBAR_URL ' "
48
51
exit 1
49
52
fi
50
53
@@ -60,32 +63,32 @@ should_exclude() {
60
63
return 1 # Should not exclude
61
64
}
62
65
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
65
68
# 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
67
70
echo " Skipping excluded file: $file "
68
71
continue
69
72
fi
70
73
71
- # Remove the existing navbar HTML section if present
74
+ # Remove existing navbar (if any) between <!-- NAVBAR START --> and <!-- NAVBAR END -->
72
75
if grep -q " <!-- NAVBAR START -->" " $file " ; then
73
76
awk ' /<!-- NAVBAR START -->/{flag=1;next}/<!-- NAVBAR END -->/{flag=0;next}!flag' " $file " > temp && mv temp " $file "
74
77
echo " Removed existing navbar from $file "
75
78
fi
76
79
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 "
84
89
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
89
91
awk ' BEGIN {RS=""; ORS="\n\n"} {gsub(/\n+$/, ""); print}' " $file " > temp_cleaned && mv temp_cleaned " $file "
92
+
90
93
echo " Inserted new navbar into $file "
91
- done
94
+ done
0 commit comments