paritybit.ca

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

commit e804867b8d9522f419609a2ae7da1bed89b019b3
parent 848af5cab12f0a7b0a623c2857a4430f8a8b30b1
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Wed,  5 Feb 2020 17:25:03 -0500

Refactor CSS compression in compile script

Get rid of flags and instead take appropriate action based on the
extension of the file.

Rename page* to file* to bring variable names closer in line with this
refactor.

Diffstat:
Mcompile | 147++++++++++++++++++++++++++++++++++++-------------------------------------------
1 file changed, 67 insertions(+), 80 deletions(-)

diff --git a/compile b/compile @@ -1,6 +1,6 @@ #!/bin/bash # -# Hand-written script to build webpages and minify CSS for the paritybit.ca +# Hand-written script to build webfiles and minify CSS for the paritybit.ca # website. # # This program is free software: you can redistribute it and/or modify @@ -16,60 +16,47 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. -if [[ $1 == '-h' || $1 == '--help' ]]; then - printf "Usage: compile [-h|c] [<pages_to_compile>]\n" - printf "Options:" - printf " -h | --help – View this help screen" - printf " -c | --css – Treat file names as css files" - printf "\nExamples:" - printf " compile pages/**" - printf " compile pages/guides/new-guide.md pages/guides.md" - printf " compile -c base.css tables.css" - printf " compile -c build/*.css" - exit 0 -fi - -if [[ $1 == '-c' || $1 == '--css' ]]; then - cssdir="public/css" - mkdir -p "$cssdir" - - tput setaf 3; tput bold; tput smul - printf "### MINIFYING CSS...\n\n" - tput sgr0 - - for file in "${@:2}"; do - fileName=$(basename "$file" | cut -f 1 -d '.') +htmldir="public/html" +cssdir="public/css" +mkdir -p "$cssdir" +mkdir -p "$htmldir" +echo "Building $@..." + +for file in $@; do + filename=$(basename "$file" | cut -f 1 -d '.') + fileextension="${file##*.}" + echo "$fileextension" + filedir=$(dirname "$file") + checkfile=$(echo "$filedir" | cut -f 1 -d '/') + + if [[ "$fileextension" == "css" ]]; then + + tput setaf 3; tput bold; tput smul + printf "### MINIFYING CSS...\n\n" + tput sgr0 + printf " " tput bold; tput smul - echo "$file > $cssdir/$fileName.min.css" + echo "$file > $cssdir/$filename.min.css" tput sgr0 + curl --data "input=`cat $file`" https://cssminifier.com/raw > \ - "$cssdir"/"$fileName".min.css + "$cssdir"/"$filename".min.css if [[ $? != 0 ]]; then - printf "\nThere was an issue minifying the CSS." + printf "\nThere was an issue minifying this CSS." exit 1 fi - echo "" - done - - exit 0 -fi - -htmldir="public/html" -mkdir -p "$htmldir"/ -echo "Building $@..." -for page in $@; do - pagename=$(basename "$page" | cut -f 1 -d '.') - pagedir=$(dirname "$page") - checkpage=$(echo "$pagedir" | cut -f 1 -d '/') + echo "" + continue + fi - # Strips "pages" from the file path so files are saved in the root of + # Strips "pages" from the file path so pages are saved in the root of # $htmldir - if [[ "$pagedir" == "pages" ]]; then + if [[ "$filedir" == "pages" ]]; then savepath="." - elif [[ "$checkpage" == "pages" ]]; then - savepath=$(echo ${pagedir#$checkpage/}) + elif [[ "$checkfile" == "pages" ]]; then + savepath=$(echo ${filedir#$checkfile/}) fi # Make the requisite directories in the output and intermediate build folder @@ -78,62 +65,62 @@ for page in $@; do mkdir -p build/"$savepath" fi - # Compile the page content if valid page name given - if [[ "$page" != "" && ! -d "$page" && "$pagename" != "" && "$pagename" != "-c" ]]; then + # Compile the file content if valid file name given + if [[ "$file" != "" && ! -d "$file" && "$filename" != "" && "$filename" != "-c" ]]; then tput setaf 3; tput bold; tput smul - echo "### CREATING: $htmldir/$savepath/$pagename.html" + echo "### CREATING: $htmldir/$savepath/$filename.html" tput sgr0 # Turns the markdown into pure html printf " Compiling page content..." - markdown "$pagedir"/"$pagename".md > build/"$savepath"/"$pagename"-content.html + markdown "$filedir"/"$filename".md > build/"$savepath"/"$filename"-content.html tput setaf 2; tput bold echo " [DONE]" tput sgr0 # Combines output from above with pre-compiled header/footer html printf " Constructing full page..." - cat build/header.html > $htmldir/$savepath/$pagename.html - cat build/"$savepath"/"$pagename"-content.html >> "$htmldir"/"$savepath"/"$pagename".html - cat build/footer.html >> "$htmldir"/"$savepath"/"$pagename".html + cat build/header.html > $htmldir/$savepath/$filename.html + cat build/"$savepath"/"$filename"-content.html >> "$htmldir"/"$savepath"/"$filename".html + cat build/footer.html >> "$htmldir"/"$savepath"/"$filename".html tput setaf 2; tput bold echo " [DONE]" tput sgr0 # ====================================================================== - # Now that the page is compiled, changes are made to it to properly + # Now that the file is compiled, changes are made to it to properly # populate the <title>, <meta>, <link> (for css), and table of contents # fields of the html file. # ====================================================================== # Assumes first line of every markdown file is the header in the format: - # "## <page_title>" - # Sets the html <title> tag to be <page_title>. - pagetitle=$(head -n 1 "$pagedir"/"$pagename".md | cut -d" " -f 2-) + # "## <file_title>" + # Sets the html <title> tag to be <file_title>. + filetitle=$(head -n 1 "$filedir"/"$filename".md | cut -d" " -f 2-) printf " Setting page title..." - sed -i "s/<title>.*<\/title>/<title>${pagetitle//&/\\&} - paritybit.ca<\/title>/" \ - "$htmldir"/"$savepath"/"$pagename".html + sed -i "s/<title>.*<\/title>/<title>${filetitle//&/\\&} - paritybit.ca<\/title>/" \ + "$htmldir"/"$savepath"/"$filename".html tput setaf 2; tput bold echo " [DONE]" tput sgr0 # Assumes the third line of every markdown file is the <meta> tag - # information (preceded by '[//]: #') and sets html meta tag as such - metaText=$(sed '3!d' "$pagedir"/"$pagename".md | cut -d# -f 2) + # information (preceded by '[//]: #') and sets html meta description + metaText=$(sed '3!d' "$filedir"/"$filename".md | cut -d# -f 2) # Remove leading whitespace and forward slash characters metaTextClean=$(echo -e "${metaText}" | sed -e 's/^[[:space:]]*//') metaTextClean=$(sed -e 's/\//\\\//g' <<< "$metaTextClean") - printf " Setting meta tag..." + printf " Setting description..." sed -i "s/content=\"\"/content=${metaTextClean//&/\\&}/" \ - "$htmldir"/"$savepath"/"$pagename".html + "$htmldir"/"$savepath"/"$filename".html tput setaf 2; tput bold - echo " [DONE]" + echo " [DONE]" tput sgr0 # Assumes the fifth line of every markdown file defines the requisite # css files (preceded by '[//]: #') and adds link elements as necessary printf " Adding CSS links..." - CSSTags=$(sed '5!d' "$pagedir"/"$pagename".md | cut -d# -f 2) + CSSTags=$(sed '5!d' "$filedir"/"$filename".md | cut -d# -f 2) # Remove leading whitespace and quote marks CSSTagsClean=$(echo -e "${CSSTags}" | sed -e 's/^[[:space:]]*//') CSSTagsClean=$(sed -e 's/^"//' -e 's/"$//' <<< "$CSSTagsClean") @@ -142,7 +129,7 @@ for page in $@; do # Add each link element after the <title> tag for CSSFile in "${CSSFileList[@]}"; do sed -i "/<title>/a \ \ \ \ <link rel=\"stylesheet\" href=\"\/css\/$CSSFile\">" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html done tput setaf 2; tput bold echo " [DONE]" @@ -161,7 +148,7 @@ for page in $@; do # # Note: sectionNameNS is the name of the section without any spaces # because the HTML spec says no spaces in href property - toc=$(sed '7!d' "$pagedir"/"$pagename".md | cut -d# -f 2) + toc=$(sed '7!d' "$filedir"/"$filename".md | cut -d# -f 2) # Remove leading whitespace and quote marks tocClean=$(echo -e "${toc}" | sed -e 's/^[[:space:]]*//') tocClean=$(sed -e 's/^"//' -e 's/"$//' <<< "$tocClean") @@ -171,8 +158,8 @@ for page in $@; do # Check for an empty array, if it's not empty then populate the TOC if [ ! "${#sections[@]}" -eq "0" ]; then sed -i "/TOC/a <div id=\"table-of-contents\" aria-label=\"Table of Contents\">\n<h2 id=\"toc-title\">Table of Contents</h2>" \ - $htmldir/$savepath/$pagename.html - sed -i "/toc-title/a </ul></div>" $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html + sed -i "/toc-title/a </ul></div>" $htmldir/$savepath/$filename.html for ((i = ${#sections[@]}-1; i >= 0; i--)); do # Remove leading whitespaces sectionName=$(echo -e "${sections[$i]}" | sed -e 's/^[[:space:]]*//') @@ -181,56 +168,56 @@ for page in $@; do sectionName=${sectionName:7} sectionNameNS=$(sed -e 's/ //g' <<< "$sectionName") sed -i "/toc-title/a <ul><ul><li><a href=\"#$sectionNameNS\">$sectionName</a></li></ul></ul>" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html sed -i "/<h5>$sectionName<\/h5>/i </section>\n<section>" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html sed -i "s/<h5>$sectionName<\/h5>/<h5 id=\"$sectionNameNS\">$sectionName<\/h5>/" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html # Check if this is supposed to be a sub-section & add <ul> elif echo "$sectionName" | grep -qi "^sub:"; then sectionName=${sectionName:4} sectionNameNS=$(sed -e 's/ //g' <<< "$sectionName") sed -i "/toc-title/a <ul><li><a href=\"#$sectionNameNS\">$sectionName</a></li></ul>" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html sed -i "/<h4>$sectionName<\/h4>/i </section>\n<section>" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html sed -i "s/<h4>$sectionName<\/h4>/<h4 id=\"$sectionNameNS\">$sectionName<\/h4>/" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html # Otherwise, this is a normal section, no <ul> added else sectionNameNS=$(sed -e 's/ //g' <<< "$sectionName") sed -i "/toc-title/a <li><a href=\"#$sectionNameNS\">$sectionName</a></li>" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html sed -i "/<h3>$sectionName<\/h3>/i </section\>\n<section>" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html sed -i "s/<h3>$sectionName<\/h3>/<h3 id=\"$sectionNameNS\">$sectionName<\/h3>/" \ - $htmldir/$savepath/$pagename.html + $htmldir/$savepath/$filename.html fi done # Closes out the table of contents list - sed -i "/toc-title/a <ul>" $htmldir/$savepath/$pagename.html + sed -i "/toc-title/a <ul>" $htmldir/$savepath/$filename.html # Wraps the beginning of the document (stuff before first section) # in a section by adding the first <section> tag - sed -i "/END OF HEADER FILE/a <section>" $htmldir/$savepath/$pagename.html + sed -i "/END OF HEADER FILE/a <section>" $htmldir/$savepath/$filename.html # Closes out the final section by inserting final </section> tag - sed -i "/<\/main>/i </section>" $htmldir/$savepath/$pagename.html + sed -i "/<\/main>/i </section>" $htmldir/$savepath/$filename.html fi tput setaf 2; tput bold echo " [DONE]" tput sgr0 tput bold - echo -e "### CREATED: $htmldir/$savepath/$pagename.html\n" + echo -e "### CREATED: $htmldir/$savepath/$filename.html\n" tput sgr0 fi done