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
@@ -39,12 +42,16 @@ while [[ $# -gt 0 ]]; do
39
42
esac
40
43
done
41
44
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
44
51
45
52
# Check if the download was successful
46
53
if [ -z " $NAVBAR_HTML " ]; then
47
- echo " Failed to download navbar HTML"
54
+ echo " Failed to download navbar HTML from ' $NAVBAR_URL ' "
48
55
exit 1
49
56
fi
50
57
@@ -60,32 +67,32 @@ should_exclude() {
60
67
return 1 # Should not exclude
61
68
}
62
69
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
65
72
# 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
67
74
echo " Skipping excluded file: $file "
68
75
continue
69
76
fi
70
77
71
- # Remove the existing navbar HTML section if present
78
+ # Remove existing navbar (if any) between <!-- NAVBAR START --> and <!-- NAVBAR END -->
72
79
if grep -q " <!-- NAVBAR START -->" " $file " ; then
73
80
awk ' /<!-- NAVBAR START -->/{flag=1;next}/<!-- NAVBAR END -->/{flag=0;next}!flag' " $file " > temp && mv temp " $file "
74
81
echo " Removed existing navbar from $file "
75
82
fi
76
83
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 "
79
93
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
89
95
awk ' BEGIN {RS=""; ORS="\n\n"} {gsub(/\n+$/, ""); print}' " $file " > temp_cleaned && mv temp_cleaned " $file "
96
+
90
97
echo " Inserted new navbar into $file "
91
- done
98
+ done
0 commit comments