1. Added lines at the top to exit the script if any command in
the script fails.
2. Quoted all variable accesses.
3. Redirected error messages to stderr.
4. Used '[[' and ']]' in 'if' statements.
5. Added a debug mode, that can be triggered by setting the TRACE
environment variable to 1.
6. Used 'dirname' instead of 'PWD'. Therefore, in order to use the
script, you must place it in the same directory as your website
files. However, you can run it from anywhere as long as it is in
the correct location.
cat $BASE_PATH/header.html | head -n -1 | cat - $1 > $BASE_PATH/temp/temp.html # For the index page, remove the last line of the header (date published)
cat "$BASE_PATH/header.html"| head -n -1 | cat - "$1" > "$BASE_PATH/temp/temp.html"# For the index page, remove the last line of the header (date published)
# Loop through the keys of the 'meta_array' array, search for all occurences of the key in the HTML doc, and replace them with the corresponding value..
# Loop through the keys of the 'meta_array' array, search for all occurences of the key in the HTML doc, and replace them with the corresponding value..
for arr_key in "${!meta_array[@]}";do
for arr_key in "${!meta_array[@]}";do
meta_array[$arr_key]=${meta_array[$arr_key]/\//\\/}# Escape all forward slashes in the value
meta_array["$arr_key"]="${meta_array["$arr_key"]/\//\\/}"# Escape all forward slashes in the value
sed -i "s/[\$][\$]$arr_key[\$][\$]/${meta_array[$arr_key]}/g"$1
sed -i "s/[\$][\$]$arr_key[\$][\$]/${meta_array[$arr_key]}/g""$1"
done
done
}
}
@ -110,30 +118,30 @@ replace_vars() {
md_to_html(){
md_to_html(){
# Convert .md files from 'source' and place them into the correct locations into 'output'
# Convert .md files from 'source' and place them into the correct locations into 'output'
files=$(find $BASE_PATH/source -name "*.md")
files=$(find "$BASE_PATH/source" -name "*.md")
for file in $files;do
for file in $files;do
read_metadata $file# Sets the 'metadata' variable
read_metadata "$file"# Sets the 'metadata' variable
convert_to_array "$metadata"#Sets the 'meta_array' array
convert_to_array "$metadata"#Sets the 'meta_array' array
add_date_to_array "$file"#Uses 'meta_array' array
add_date_to_array "$file"#Uses 'meta_array' array
# Copy file to temp dir and strip metadata
# Copy file to temp dir and strip metadata
cp $file$BASE_PATH/temp/
cp "$file""$BASE_PATH/temp/"
letnum_lines=$(echo"$metadata"| wc -l)+1
letnum_lines=$(echo"$metadata"| wc -l)+1
sed -i "1,${num_lines}d"$BASE_PATH/temp/`basename $file`
sed -i "1,${num_lines}d""$BASE_PATH/temp/$(basename "$file")"
gen_sorted_file_list(){# Generate a list of the HTMl files, sorted by when they were last modified (read from the contents of the HTML file)
gen_sorted_file_list(){# Generate a list of the HTMl files, sorted by when they were last modified (read from the contents of the HTML file)
files=$(find $BASE_PATH/output -name "*.html")
files=$(find "$BASE_PATH/output" -name "*.html")
local date_mod
for file in $files;do
for file in $files;do
if grep -q "date-published"$file;then
if grep -q "date-published""$file";then
echo"$file" >> $BASE_PATH/temp/file_listing.txt # Write files that have a date published to a temp file (we only want the files with date modified, because only these files can be listed with their date on the site map)
echo"$file" >> "$BASE_PATH/temp/file_listing.txt"# Write files that have a date published to a temp file (we only want the files with date modified, because only these files can be listed with their date on the site map)
@ -155,16 +164,16 @@ gen_sorted_file_list() { # Generate a list of the HTMl files, sorted by when the
# Line 2 re-arranges this information, and converts it into DD MM YY format
# Line 2 re-arranges this information, and converts it into DD MM YY format
# Line 3 converts this into a UNIX timestamp
# Line 3 converts this into a UNIX timestamp
date_mod+=$'\n'
date_mod+=$'\n'
fi
fi
done
done
date_mod=$(echo"$date_mod"| head -n -1)# Remove last (empty) line from variable
date_mod=$(echo"${date_mod-}"| head -n -1)# Remove last (empty) line from variable
echo"$date_mod" > $BASE_PATH/temp/date_mod.txt # Write the corresponding 'date modified' timestamps to a temp file
echo"${date_mod-}" > "$BASE_PATH/temp/date_mod.txt"# Write the corresponding 'date modified' timestamps to a temp file
paste $BASE_PATH/temp/file_listing.txt $BASE_PATH/temp/date_mod.txt > $BASE_PATH/temp/new_file_list.txt # Combine file list and date modified into a single file
paste "$BASE_PATH/temp/file_listing.txt""$BASE_PATH/temp/date_mod.txt" > "$BASE_PATH/temp/new_file_list.txt"# Combine file list and date modified into a single file
sorted_file_list=$(sort -r -k 2$BASE_PATH/temp/new_file_list.txt)# Sort the data in the file based on the timestamp (from newest to oldest), and store it into a variable
sorted_file_list=$(sort -r -k 2"$BASE_PATH/temp/new_file_list.txt")# Sort the data in the file based on the timestamp (from newest to oldest), and store it into a variable
sorted_file_list=$(echo"$sorted_file_list"| awk '{print $1}')# Store only the first column (the file path) in the variable
sorted_file_list=$(echo"$sorted_file_list"| awk '{print $1}')# Store only the first column (the file path) in the variable
}
}
@ -173,16 +182,16 @@ gen_index_page() { # Generate an index page (site map) that includes links to th
index_file_html="<nav class=\"toc\">"$'\n'# Variable to store the body HTML of the index page; enclose the list in a nav
index_file_html="<nav class=\"toc\">"$'\n'# Variable to store the body HTML of the index page; enclose the list in a nav
for file in $1;do
for file in $1;do
title=$(cat $file| grep "<title>"| head -n 1| awk -F'[<>]''{print $3}')# Find the title of the web page
title=$(cat "$file"| grep "<title>"| head -n 1| awk -F'[<>]''{print $3}')# Find the title of the web page
suffix=" - Two More Cents"
suffix=" - Two More Cents"
title=${title%"$suffix"}# Remove the website name from it
title="${title%"$suffix"}"# Remove the website name from it
pub_date=$(cat $file| grep "date-published"| head -n 1| awk -F'[<>]''{print $3}')# Find the date published
pub_date=$(cat "$file"| grep "date-published"| head -n 1| awk -F'[<>]''{print $3}')# Find the date published
prefix="Published on "# Find date published of webpage
prefix="Published on "# Find date published of webpage
pub_date=${pub_date#"$prefix"}# Remove the prefix from it
pub_date="${pub_date#"$prefix"}"# Remove the prefix from it
pub_date=$(echo"$pub_date"| tr -d ","| awk '{print $2" "$1" "$3}'| date -f - +"%m/%d/%Y")# Re-arrange the date and convert to mm/dd/yy
pub_date=$(echo"$pub_date"| tr -d ","| awk '{print $2" "$1" "$3}'| date -f - +"%m/%d/%Y")# Re-arrange the date and convert to mm/dd/yy
index_file_html+="<li><time>${pub_date}</time> - <a href=\"$file_path\">$title</a></li>"# Add a line of HTML containing the date and title of the article
index_file_html+="<li><time>${pub_date}</time> - <a href=\"$file_path\">$title</a></li>"# Add a line of HTML containing the date and title of the article
index_file_html+=$'\n'
index_file_html+=$'\n'
@ -200,14 +209,14 @@ gen_index_page() { # Generate an index page (site map) that includes links to th