paritybit.ca

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit ffa30c82b25a4ca7bc37843335a27229d76154c1
parent f098a2fb2c35de64cbd44c35bbcf7ed61ab9c671
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Thu,  6 Aug 2020 18:41:09 -0400

Add image optimization to compile script

Diffstat:
Mcompile | 36+++++++++++++++++++++++++++++++++++-
1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/compile b/compile @@ -44,6 +44,7 @@ RED="\033[0;31m" GREEN="\033[0;32m" YELLOW="\033[0;33m" +### CONFIGURATION ### # The name of the site in the <title> tags siteName="paritybit.ca" # Set to "false" to disable auto-upload @@ -60,6 +61,11 @@ mkdir -p "$htmlDir" # Where minified css will go cssDir="public/css" mkdir -p "$cssDir" +# Where optimized images will go +imgDir="public/img" +mkdir -p "$imgDir" +# Your tinypng.com API key +apiKey="" # Where intermediate build files will go buildDir="build/" mkdir -p "$buildDir" @@ -105,6 +111,27 @@ auto_upload() fi } +compile_img() +{ + print_header_msg "OPTIMIZING IMG: $file" + outputFile="$imgDir/$fileName.$fileExt" + + response=$(curl https://api.tinify.com/shrink \ + --user api:"$apiKey" \ + --data-binary @"$file") + downloadURL=$(echo "$response" | cut -d',' -f8 | cut -d':' -f2- | tr -d "\"" | tr -d "}") + if [ $? != 0 ]; then + print_error_msg "There was an issue minifying this image." + exit 1 + fi + + curl --output "$outputFile" "$downloadURL" + if [ $? != 0 ]; then + print_error_msg "There was an issue downloading the minified image." + exit 1 + fi +} + compile_css() { print_header_msg "MINIFYING CSS: $file" @@ -273,8 +300,12 @@ for file in "$@"; do continue fi + # First run of fileName needed because if the for loop operates on $file, + # the extension will not be properly pruned fileName=$(basename -s .md "$file") - fileName=$(basename -s .css "$fileName") + for ext in md css png jpg jpeg; do + fileName=$(basename -s ."$ext" "$fileName") + done fileExt=$(echo "$file" | awk -F . '{if (NF>1) {print $NF}}') fileDir=$(dirname "$file") subDir=$(echo "$fileDir" | sed "s/^pages//") @@ -285,6 +316,9 @@ for file in "$@"; do elif [ "$fileExt" = "md" ]; then compile_md auto_upload + elif [ "$fileExt" = "png" ] || [ "$fileExt" = "jpg" ] || [ "$fileExt" = "jpeg" ]; then + compile_img + auto_upload fi print_success_msg "FINISHED: https://$siteName/$subDir/$fileName.$fileExt"