From 7d82d5ece36bcce89455268e95532e70f003c932 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 8 Jan 2024 22:14:14 -0500 Subject: [PATCH] Working on generating index page; moved the generation of a sorted file list to a separate function --- build.sh | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/build.sh b/build.sh index c02b324..e596747 100755 --- a/build.sh +++ b/build.sh @@ -108,10 +108,35 @@ md_to_html() { done } -gen_index_page() { # Generate an index page (site map) that includes links to the other pages + +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") + echo "$files" > $BASE_PATH/temp/file_listing.txt # Write file list to a temp file for file in $files; do + date_mod+=$(cat "$file" | grep "date-published" | awk -F'[<>]' '{print $3}' \ + | cut -d' ' -f '1,2' --complement | tr -d "," | awk '{print $2" "$1" "$3}' \ + | date -f - +"%s") + + # Explanation: +# Line 1 extracts the published date from the HTML file +# Line 2 re-arranges this information, and converts it into DD MM YY format +# Line 3 converts this into a UNIX timestamp + + date_mod+=$'\n' + done + + 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 + + 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 -k 2 $BASE_PATH/temp/new_file_list.txt) # Sort the data in the file, 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 +} + +gen_index_page() { # Generate an index page (site map) that includes links to the other pages + for file in $1; do title=$(cat $file | grep "" | head -n 1 | awk -F'[<>]' '{print $3}') # Find the title of the web page suffix=" - Two More Cents" title=${title%"$suffix"} # Remove the website name from it @@ -119,13 +144,18 @@ gen_index_page() { # Generate an index page (site map) that includes links to th 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 pub_date=${pub_date#"$prefix"} # Remove the prefix from it - pub_date=${awk "{print $1 $3}" "$pub_date"} # Extract the month and year - - echo "$title $pub_date" + 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) + echo "$pub_date - $file_path" + index_file_list+="<li><time>${pub_date}</time> - <a href=\"$file_path\">$title</a></li>" + index_file_list+=$'\n' done + + index_file_list=$(echo "$index_file_list" | head -n -1) # Remove last (empty) line from variable + + } clean_up() { @@ -138,5 +168,6 @@ setup_temp_dir setup_output_dir del_files_in_output md_to_html -gen_index_page +gen_sorted_file_list # Sets the 'sorted_file_list' variable +gen_index_page "$sorted_file_list" # Uses the 'sorted_file_list' variable clean_up