Made all variables inside functions local

master
Aadhavan Srinivasan 11 months ago
parent 45a136dfc3
commit c82b13f3ca

@ -53,6 +53,9 @@ read_metadata() {
} }
convert_to_array() { convert_to_array() {
local meta_key
local meta_value
# Converts the metadata into two arrays: one with the key, and the other with the value. # Converts the metadata into two arrays: one with the key, and the other with the value.
readarray -t meta_key < <(echo -e "$1" | awk -F: '{print $1}') readarray -t meta_key < <(echo -e "$1" | awk -F: '{print $1}')
readarray -t meta_value < <(echo -e "$1" | awk -F: '{st = index($0,":"); values = substr($0,st+1); print values}' | cut -c 2-) readarray -t meta_value < <(echo -e "$1" | awk -F: '{st = index($0,":"); values = substr($0,st+1); print values}' | cut -c 2-)
@ -118,7 +121,7 @@ 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") local 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
@ -132,7 +135,7 @@ md_to_html() {
sed -i "1,${num_lines}d" "$BASE_PATH/temp/$(basename "$file")" sed -i "1,${num_lines}d" "$BASE_PATH/temp/$(basename "$file")"
# Construct path for output file # Construct path for output file
path_for_output=$(realpath --relative-to="${BASE_PATH}/source" "$file") local path_for_output=$(realpath --relative-to="${BASE_PATH}/source" "$file")
path_for_output="${BASE_PATH}/output/${path_for_output}" path_for_output="${BASE_PATH}/output/${path_for_output}"
path_for_output="$(dirname $path_for_output)/$(basename $path_for_output .md).html" path_for_output="$(dirname $path_for_output)/$(basename $path_for_output .md).html"
@ -149,7 +152,7 @@ md_to_html() {
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") local files=$(find "$BASE_PATH/output" -name "*.html")
local date_mod local date_mod
for file in $files; do for file in $files; do
@ -179,19 +182,19 @@ gen_sorted_file_list() { # Generate a list of the HTMl files, sorted by when the
gen_index_page() { # Generate an index page (site map) that includes links to the other pages gen_index_page() { # Generate an index page (site map) that includes links to the other pages
index_file_html="<nav class=\"toc\">"$'\n' # Variable to store the body HTML of the index page; enclose the list in a nav local 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 local title=$(cat "$file" | grep "<title>" | head -n 1 | awk -F'[<>]' '{print $3}') # Find the title of the web page
suffix=" - Two More Cents" local 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 local 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
file_path=$(realpath --relative-to="${BASE_PATH}/output" "$file") local file_path=$(realpath --relative-to="${BASE_PATH}/output" "$file")
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'

Loading…
Cancel
Save