commit feaf54eb431d8b449c1401dff73561251338ce66
parent 0d957649339b37fc87666f3bb980e630d988e6d3
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Tue, 2 Feb 2021 23:34:50 -0500
Overhaul compile script
Switch to lowdown from pandoc
Remove extraneous options
Re-organize config/code sections
Diffstat:
M | http/compile | | | 480 | +++++++++++++++++++++++++++++++++++++------------------------------------------ |
1 file changed, 225 insertions(+), 255 deletions(-)
diff --git a/http/compile b/http/compile
@@ -13,11 +13,32 @@
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
-# along with this program. If not, see <https://www.gnu.org/licenses/>.
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+### CONFIGURATION ###
+# The name of the site in the <title> tags
+siteName="paritybit.ca"
+# Set to "false" to disable auto-upload
+autoUpload="true"
+# Where to upload files
+remote="paritybit.ca:/var/www/paritybit.ca/"
+# Where md files are
+pageDir="pages/"
+# Where compiled md -> html pages will go
+htmlDir="public/html"
+# Where minified css will go
+cssDir="public/css"
+# Where optimized images will go
+imgDir="public/img"
+# Your tinypng.com API key
+apiKey=""
+# Where intermediate build files will go
+buildDir="build/"
+### END CONFIGURATION ###
# POSIX Shell "Strict Mode"
set -o errexit
@@ -26,20 +47,20 @@ IFS=$(printf '\n\t')
# Check that all the required programs are installed
if [ ! -x "$(command -v rsync)" ]; then
- echo "The program 'rsync' is needed but was not found."
- exit 1
+ echo "The program 'rsync' is needed but was not found."
+ exit 1
fi
if [ ! -x "$(command -v curl)" ]; then
- echo "The program 'curl' is needed but was not found."
- exit 1
+ echo "The program 'curl' is needed but was not found."
+ exit 1
fi
-if [ ! -x "$(command -v pandoc)" ]; then
- echo "The program 'pandoc' is needed but was not found."
- exit 1
+if [ ! -x "$(command -v lowdown)" ]; then
+ echo "The program 'lowdown' is needed but was not found."
+ exit 1
fi
if [ ! -x "$(command -v convert)" ]; then
- echo "The program 'convert' from ImageMagick is needed but was not found."
- exit 1
+ echo "The program 'convert' from ImageMagick is needed but was not found."
+ exit 1
fi
# Define colours
@@ -48,309 +69,258 @@ 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
-autoUpload="true"
-# Set to "true" to enable pandoc's strict markdown compilation
-strictMode="false"
-# Where to upload files
-remote="paritybit.ca:uploads/"
-# Where md files are
-pageDir="pages/"
-# Where compiled md -> html pages will go
-htmlDir="public/html"
+# Ensure configured directories exist
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"
# Prints the message passed as the 1st argument in red.
-print_error_msg()
-{
- printf "$RED[ee] %s\n$CLEAR" "$1"
-}
-
+print_error_msg() { printf "$RED[ee] %s\n$CLEAR" "$1" ; }
# Prints the message passed as the 1st argument in green.
-print_success_msg()
-{
- printf "$GREEN[ok] %s\n$CLEAR" "$1"
-}
-
+print_success_msg() { printf "$GREEN[ok] %s\n$CLEAR" "$1" ; }
# Prints the message passed as the 1st argument in yellow.
-print_header_msg()
-{
- printf "$YELLOW[hh] %s\n$CLEAR" "$1"
-}
-
+print_header_msg() { printf "$YELLOW[hh] %s\n$CLEAR" "$1" ; }
# Prints the message passed as the 1st argument with default colours.
-print_info_msg()
-{
- printf "$CLEAR[ii] %s\n" "$1"
-}
+print_info_msg() { printf "$CLEAR[ii] %s\n" "$1" ; }
auto_upload()
{
- if [ "$autoUpload" != "true" ]; then
- return 0
- fi
- print_info_msg "Auto-uploading file"
- if [ "$autoUpload" = "true" ]; then
- if [ "$fileExt" = "css" ] || [ "$fileExt" = "md" ]; then
- outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
- cd public && rsync -rR "$outputFile" "$remote" && cd ..
- fi
- fi
+ if [ "$autoUpload" != "true" ]; then
+ return 0
+ fi
+ print_info_msg "Auto-uploading file"
+ if [ "$autoUpload" = "true" ]; then
+ if [ "$fileExt" = "css" ] || [ "$fileExt" = "md" ]; then
+ outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
+ cd public && rsync -rR "$outputFile" "$remote" && cd ..
+ fi
+ fi
}
optimize_img()
{
- print_header_msg "OPTIMIZING IMG: $file"
- outputFile="$imgDir/$fileName.$fileExt"
+ print_header_msg "OPTIMIZING IMG: $file"
+ outputFile="$imgDir/$fileName.$fileExt"
- response=$(curl https://api.tinify.com/shrink \
- --user api:"$apiKey" \
- --data-binary @"$file" \
- --dump-header /dev/stdout)
- downloadURL=$(echo "$response" | grep location: | cut -d' ' -f2 | tr -d '\r')
- if [ $? != 0 ]; then
- print_error_msg "There was an issue minifying this image."
- exit 1
- fi
+ response=$(curl https://api.tinify.com/shrink \
+ --user api:"$apiKey" \
+ --data-binary @"$file" \
+ --dump-header /dev/stdout)
+ downloadURL=$(echo "$response" | grep location: | cut -d' ' -f2 | tr -d '\r')
+ 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
+ curl --output "$outputFile" "$downloadURL"
+ if [ $? != 0 ]; then
+ print_error_msg "There was an issue downloading the minified image."
+ exit 1
+ fi
}
resize_img()
{
- print_header_msg "RESIZING IMG: $file"
- outputFile="$imgDir/$fileName.$fileExt"
- convert "$file" -resize "$resizeFactor"% "$outputFile"
- if [ $? != 0 ]; then
- print_error_msg "There was an error resizing the image."
- exit 1
- fi
+ print_header_msg "RESIZING IMG: $file"
+ outputFile="$imgDir/$fileName.$fileExt"
+ convert "$file" -resize "$resizeFactor"% "$outputFile"
+ if [ $? != 0 ]; then
+ print_error_msg "There was an error resizing the image."
+ exit 1
+ fi
}
minify_css()
{
- print_header_msg "MINIFYING CSS: $file"
- outputFile="$cssDir/$fileName.min.css"
- curl --data "input=$(cat "$file")" https://cssminifier.com/raw \
- > "$outputFile"
- if [ $? != 0 ]; then
- print_error_msg "There was an issue minifying this CSS."
- exit 1
- fi
+ print_header_msg "MINIFYING CSS: $file"
+ outputFile="$cssDir/$fileName.min.css"
+ curl --data "input=$(cat "$file")" https://cssminifier.com/raw \
+ > "$outputFile"
+ if [ $? != 0 ]; then
+ print_error_msg "There was an issue minifying this CSS."
+ exit 1
+ fi
}
compile_md()
{
- print_header_msg "COMPILING MARKDOWN: $file"
- mkdir -p "$buildDir"/"$subDir"
- mkdir -p "$htmlDir"/"$subDir"
+ print_header_msg "COMPILING MARKDOWN: $file"
+ mkdir -p "$buildDir"/"$subDir"
+ mkdir -p "$htmlDir"/"$subDir"
- if [ "$strictMode" = "true" ]; then
- pandoc -o "$buildDir"/"$subDir"/"$fileName"-content.html \
- -f markdown_strict -t html $file 2>/dev/null
- else
- pandoc -o "$buildDir"/"$subDir"/"$fileName"-content.html \
- -f markdown -t html $file 2>/dev/null
- fi
+ lowdown -o "$buildDir"/"$subDir"/"$fileName"-content.html -Thtml $file
- cat "$buildDir"/header.html \
- "$buildDir"/"$subDir"/"$fileName"-content.html \
- "$buildDir"/footer.html \
- > "$htmlDir"/"$subDir"/"$fileName".html
+ cat "$buildDir"/header.html \
+ "$buildDir"/"$subDir"/"$fileName"-content.html \
+ "$buildDir"/footer.html \
+ > "$htmlDir"/"$subDir"/"$fileName".html
- outputFile="$htmlDir/$subDir/$fileName.html"
- set_html_title
- set_html_meta
- set_html_csslink
- set_html_toc
+ outputFile="$htmlDir/$subDir/$fileName.html"
+ set_html_title
+ set_html_meta
+ set_html_csslink
+ set_html_toc
}
set_html_title()
{
- title="$(head -n1 "$file" | cut -d' ' -f2-)"
- print_info_msg "Setting <title>: \"$title\""
- sed -i -e "s/<title>.*<\/title>/<title>$title - $siteName<\/title>/" \
- "$outputFile"
+ title="$(head -n1 "$file" | cut -d' ' -f2-)"
+ print_info_msg "Setting <title>: \"$title\""
+ sed -i -e "s/<title>.*<\/title>/<title>$title - $siteName<\/title>/" \
+ "$outputFile"
}
set_html_meta()
{
- meta="$(head -n3 "$file" | tail -n1 \
- | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//')"
- print_info_msg "Setting <meta> description"
- sed -i -e "s/content=\"\"/content=$meta/" "$outputFile"
+ meta="$(head -n3 "$file" | tail -n1 \
+ | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//')"
+ print_info_msg "Setting <meta> description"
+ sed -i -e "s/content=\"\"/content=$meta/" "$outputFile"
}
set_html_csslink()
{
- cssList="$(head -n5 "$file" | tail -n1 \
- | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//')"
- print_info_msg "Adding CSS <link>s"
- echo "$cssList" | tr ' ' '\n' | while read cssFile; do
- cssFile="/css/$(echo "$cssFile" | tr -d '"')"
- sed -i -e "/<title>/a\\
- <link rel=\"stylesheet\" href=\"$cssFile\">" \
- "$outputFile"
- done
+ cssList="$(head -n5 "$file" | tail -n1 \
+ | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//')"
+ print_info_msg "Adding CSS <link>s"
+ echo "$cssList" | tr ' ' '\n' | while read cssFile; do
+ cssFile="/css/$(echo "$cssFile" | tr -d '"')"
+ sed -i -e "/<title>/a\\
+ <link rel=\"stylesheet\" href=\"$cssFile\">" \
+ "$outputFile"
+ done
}
# This still feels a bit messy
set_html_toc()
{
- tocList="$(head -n7 "$file" | tail -n1 \
- | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//' | tr -d '"')"
- print_info_msg "Adding Table of Contents"
- if [ -z "$tocList" ]; then
- return 0
- fi
- sed -i -e "/TOC/a\\
- <div id=\"table-of-contents\" aria-label=\"Table of Contents\">\n<h2 id=\"toc-title\">Table of Contents</h2>" \
- "$outputFile"
- sed -i -e "/toc-title/a\\
- </ul></div>" "$outputFile"
- echo "$tocList" | tr ';' '\n' \
- | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' \
- | while read tocEntry; do
- tocEntry="$(echo "$tocEntry" | sed -e 's/^[[:space:]]*//')"
- if echo "$tocEntry" | grep -qi "^subsub:"; then
- tocEntry="$(echo "$tocEntry" | cut -d':' -f2-)"
- tocEntryId="$(echo "$tocEntry" | tr ' ' '-' \
- | tr '[:upper:]' '[:lower:]' | tr -d ':' )"
- sed -i -e "/toc-title/a\\
- <ul><ul><li><a href=\"#$tocEntryId\">$tocEntry</a></li></ul></ul>" \
- "$outputFile"
- sed -i -e "/$tocEntry<\/h5>/i\\
- </section>\n<section>" \
- "$outputFile"
- elif echo "$tocEntry" | grep -qi "^sub:"; then
- tocEntry="$(echo "$tocEntry" | cut -d':' -f2-)"
- tocEntryId="$(echo "$tocEntry" | tr ' ' '-' \
- | tr '[:upper:]' '[:lower:]' | tr -d ':' )"
- sed -i -e "/toc-title/a\\
- <ul><li><a href=\"#$tocEntryId\">$tocEntry</a></li></ul>" \
- "$outputFile"
- sed -i -e "/$tocEntry<\/h4>/i\\
- </section>\n<section>" \
- "$outputFile"
- else
- tocEntryId="$(echo "$tocEntry" | tr ' ' '-' \
- | tr '[:upper:]' '[:lower:]' | tr -d ':' )"
- sed -i -e "/toc-title/a\\
- <li><a href=\"#$tocEntryId\">$tocEntry</a></li>" \
- "$outputFile"
- sed -i -e "/$tocEntry<\/h3>/i\\
- </section\>\n<section>" \
- "$outputFile"
- fi
- done
- sed -i -e "/toc-title/a\\
- <ul>" "$outputFile"
- sed -i -e "/END OF HEADER FILE/a\\
- <section>" "$outputFile"
- sed -i -e "/<\/main>/i\\
- </section>" "$outputFile"
+ tocList="$(head -n7 "$file" | tail -n1 \
+ | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//' | tr -d '"')"
+ print_info_msg "Adding Table of Contents"
+ if [ -z "$tocList" ]; then
+ return 0
+ fi
+ sed -i -e "/TOC/a\\
+ <div id=\"table-of-contents\" aria-label=\"Table of Contents\">\n<h2 id=\"toc-title\">Table of Contents</h2>" \
+ "$outputFile"
+ sed -i -e "/toc-title/a\\
+ </ul></div>" "$outputFile"
+ echo "$tocList" | tr ';' '\n' \
+ | awk '{a[i++]=$0} END {for (j=i-1; j>=0;) print a[j--] }' \
+ | while read tocEntry; do
+ tocEntry="$(echo "$tocEntry" | sed -e 's/^[[:space:]]*//')"
+ if echo "$tocEntry" | grep -qi "^subsub:"; then
+ tocEntry="$(echo "$tocEntry" | cut -d':' -f2-)"
+ tocEntryId="$(echo "$tocEntry" | tr ' ' '-' \
+ | tr '[:upper:]' '[:lower:]' | tr -d ':' )"
+ sed -i -e "/toc-title/a\\
+ <ul><ul><li><a href=\"#$tocEntryId\">$tocEntry</a></li></ul></ul>" \
+ "$outputFile"
+ sed -i -e "/$tocEntry<\/h5>/i\\
+ </section>\n<section>" \
+ "$outputFile"
+ elif echo "$tocEntry" | grep -qi "^sub:"; then
+ tocEntry="$(echo "$tocEntry" | cut -d':' -f2-)"
+ tocEntryId="$(echo "$tocEntry" | tr ' ' '-' \
+ | tr '[:upper:]' '[:lower:]' | tr -d ':' )"
+ sed -i -e "/toc-title/a\\
+ <ul><li><a href=\"#$tocEntryId\">$tocEntry</a></li></ul>" \
+ "$outputFile"
+ sed -i -e "/$tocEntry<\/h4>/i\\
+ </section>\n<section>" \
+ "$outputFile"
+ else
+ tocEntryId="$(echo "$tocEntry" | tr ' ' '-' \
+ | tr '[:upper:]' '[:lower:]' | tr -d ':' )"
+ sed -i -e "/toc-title/a\\
+ <li><a href=\"#$tocEntryId\">$tocEntry</a></li>" \
+ "$outputFile"
+ sed -i -e "/$tocEntry<\/h3>/i\\
+ </section\>\n<section>" \
+ "$outputFile"
+ fi
+ done
+ sed -i -e "/toc-title/a\\
+ <ul>" "$outputFile"
+ sed -i -e "/END OF HEADER FILE/a\\
+ <section>" "$outputFile"
+ sed -i -e "/<\/main>/i\\
+ </section>" "$outputFile"
}
# Read and parse command line flags
resizeFactor="0"
while test $# -gt 0; do
- case "$1" in
- -h|--help)
- echo "compile - generate static web pages"
- echo ""
- echo "usage: compile [options] files"
- echo ""
- echo "options:"
- echo "-h|--help show brief help"
- echo "-l|--loose use pandoc's default markdown compilation"
- echo "-s|--strict use strict markdown compilation"
- echo "-a|--auto-upload auto-upload generated files"
- echo "-n|--no-auto-upload do not auto-upload the generated files"
- echo "-r|--resize NUMBER resize input image by NUMBER percent"
- exit 0
- ;;
- -l|--loose)
- strictMode="false"
- shift
- ;;
- -s|--strict)
- strictMode="true"
- shift
- ;;
- -n|--no-auto-upload)
- autoUpload="false"
- shift
- ;;
- -a|--auto-upload)
- autoUpload="true"
- shift
- ;;
- -r|--resize)
- shift
- resizeFactor="$1"
- shift
- ;;
- --|*)
- break
- ;;
- esac
+ case "$1" in
+ -h|--help)
+ echo "compile - generate static web pages"
+ echo ""
+ echo "usage: compile [options] files"
+ echo ""
+ echo "options:"
+ echo "-h|--help show brief help"
+ echo "-a|--auto-upload auto-upload generated files"
+ echo "-n|--no-auto-upload do not auto-upload the generated files"
+ echo "-r|--resize NUMBER resize input image by NUMBER percent"
+ exit 0
+ ;;
+ -n|--no-auto-upload)
+ autoUpload="false"
+ shift
+ ;;
+ -a|--auto-upload)
+ autoUpload="true"
+ shift
+ ;;
+ -r|--resize)
+ shift
+ resizeFactor="$1"
+ shift
+ ;;
+ --|*)
+ break
+ ;;
+ esac
done
for file in "$@"; do
- if [ ! -f "$file" ]; then
- print_error_msg "ERROR: $file not found or is not a file."
- continue
- fi
+ if [ ! -f "$file" ]; then
+ print_error_msg "ERROR: $file not found or is not a file."
+ 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")
- 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//")
+ # 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")
+ 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//")
- if [ "$fileExt" = "css" ]; then
- minify_css
- auto_upload
- elif [ "$fileExt" = "md" ]; then
- compile_md
- auto_upload
- elif [ "$fileExt" = "png" ] || [ "$fileExt" = "jpg" ] || [ "$fileExt" = "jpeg" ]; then
- if [ "$resizeFactor" -gt 0 ]; then
- fileName="$fileName-thumb"
- resize_img
- else
- optimize_img
- auto_upload
- fi
- else
- print_error_msg "ERROR: nothing to do."
- fi
+ if [ "$fileExt" = "css" ]; then
+ minify_css
+ auto_upload
+ elif [ "$fileExt" = "md" ]; then
+ compile_md
+ auto_upload
+ elif [ "$fileExt" = "png" ] || [ "$fileExt" = "jpg" ] || [ "$fileExt" = "jpeg" ]; then
+ if [ "$resizeFactor" -gt 0 ]; then
+ fileName="$fileName-thumb"
+ resize_img
+ else
+ optimize_img
+ auto_upload
+ fi
+ else
+ print_error_msg "ERROR: nothing to do."
+ fi
- if [ "$fileExt" = "md" ]; then
- print_success_msg "FINISHED: https://$siteName/$subDir/$fileName.html"
- else
- print_success_msg "FINISHED: $fileName.$fileExt"
- fi
+ if [ "$fileExt" = "md" ]; then
+ print_success_msg "FINISHED: https://$siteName/$subDir/$fileName.html"
+ else
+ print_success_msg "FINISHED: $fileName.$fileExt"
+ fi
done