paritybit.ca

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

commit b2d7541e45ed920cb097663ebfa843dddfb43b91
parent 727ebe590aeeedd576c6ff6f40fd2d49d3b7c45f
Author: Jake Bauer <jbauer@paritybit.ca>
Date:   Sun, 29 Dec 2019 23:49:03 -0500

Rename createpages.sh to compile

Diffstat:
Acompile | 213+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dcreatepages.sh | 213-------------------------------------------------------------------------------
2 files changed, 213 insertions(+), 213 deletions(-)

diff --git a/compile b/compile @@ -0,0 +1,213 @@ +#!/bin/bash +# +# Hand-written script to build webpages and minify CSS for the paritybit.ca +# website. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# 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 +# 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/>. + +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 '.') + printf " " + tput bold; tput smul + echo "$file > $cssdir/$fileName.min.css" + tput sgr0 + curl --data "input=`cat $file`" https://cssminifier.com/raw > \ + "$cssdir"/"$fileName".min.css + if [[ $? != 0 ]]; then + printf "\nThere was an issue minifying the 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 '/') + + # Stripts "pages" from the file path so files are saved in the root of + # $htmldir + if [[ "$pagedir" == "pages" ]]; then + savepath="." + elif [[ "$checkpage" == "pages" ]]; then + savepath=$(echo ${pagedir#$checkpage/}) + fi + + # Make the requisite directories in the output and intermediate build folder + if [[ ! -d "$htmldir/$savepath" ]]; then + mkdir -p "$htmldir"/"$savepath" + mkdir -p build/"$savepath" + fi + + # Compile the page content if valid page name given + if [[ "$page" != "" && ! -d "$page" && "$pagename" != "" && "$pagename" != "-c" ]]; then + tput setaf 3; tput bold; tput smul + echo "### CREATING: $htmldir/$savepath/$pagename.html" + tput sgr0 + + # Turns the markdown into pure html + printf " Compiling page content..." + markdown "$pagedir"/"$pagename".md > build/"$savepath"/"$pagename"-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 + tput setaf 2; tput bold + echo " [DONE]" + tput sgr0 + + # ====================================================================== + # Now that the page 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>" + # And sets the html <title> tag to be <page_title>. + pagetitle=$(head -n 1 "$pagedir"/"$pagename".md | cut -f 2- -d " ") + printf " Setting page title..." + sed -i "s/<title>.*<\/title>/<title>${pagetitle//&/\\&}<\/title>/" \ + "$htmldir"/"$savepath"/"$pagename".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) + # Remove leading whitespace and forward slash characters + metaTextClean=$(echo -e "${metaText}" | sed -e 's/^[[:space:]]*//') + metaTextClean=$(sed -e 's/\//\\\//' <<< "$metaTextClean") + printf " Setting meta tag..." + sed -i "s/content=\"\"/content=${metaTextClean//&/\\&}/" \ + "$htmldir"/"$savepath"/"$pagename".html + tput setaf 2; tput bold + 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) + # Remove leading whitespace and quote marks + CSSTagsClean=$(echo -e "${CSSTags}" | sed -e 's/^[[:space:]]*//') + CSSTagsClean=$(sed -e 's/^"//' -e 's/"$//' <<< "$CSSTagsClean") + # Split fifth line into separate css file names, separated by a space + IFS=' ' read -r -a CSSFileList <<< "$CSSTagsClean" + # 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 + done + tput setaf 2; tput bold + echo " [DONE]" + tput sgr0 + + # Assumes the seventh line of every markdown file defines the sections + # to add to a table of contents (preceded by '[//]: #'). + toc=$(sed '7!d' "$pagedir"/"$pagename".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") + printf " Parsing table of contents..." + # Split the line into separate entries, separated by a semicolon + IFS=';' read -r -a sections <<< "$tocClean" + # Check for an empty array, if it's not empty then populate the TOC + if [ ! "${#sections[@]}" -eq "0" ]; then + # Add a horizontal rule and heading + sed -i "/TOC/a <h3 id=\"toc-title\">Table of Contents</h3>" \ + $htmldir/$savepath/$pagename.html + sed -i "/TOC/a <hr id=\"toc-hr\">" \ + $htmldir/$savepath/$pagename.html + # Add each section to list after the h3 tag (done in reverse order + # because lines are appended immediately after the h3 tag) and add + # an internal link to the header of that section as well as a + # "back-to-top" arrow beside each regular section header. + # + # Note: sectionNameNS is the name of the section without any spaces + # because the HTML spec says no spaces in href property + sed -i "/toc-title/a </ul>" $htmldir/$savepath/$pagename.html + for ((i = ${#sections[@]}-1; i >= 0; i--)); do + # Remove leading whitespaces + sectionName=$(echo -e "${sections[$i]}" | sed -e 's/^[[:space:]]*//') + # Check if this is supposed to be a sub-sub-section & add 2x<ul> + if echo "$sectionName" | grep -qi "^subsub:"; then + 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 + # 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 + # Otherwise, this is a normal section + else + sectionNameNS=$(sed -e 's/ //g' <<< "$sectionName") + sed -i "/toc-title/a <li><a href=\"#$sectionNameNS\">$sectionName</a></li>" \ + $htmldir/$savepath/$pagename.html + sed -i "/<h3>$sectionName<\/h3>/i <a id=\"$sectionNameNS\"></a>" \ + $htmldir/$savepath/$pagename.html + sed -i "s/<h3>$sectionName<\/h3>/<h3>$sectionName <a class=\"back-to-top\" href=\"#\">\&\#10548\;<\/a><\/h3>/g" \ + $htmldir/$savepath/$pagename.html + fi + done + # Closes out the table of contents list + sed -i "/toc-title/a <ul>" $htmldir/$savepath/$pagename.html + fi + tput setaf 2; tput bold + echo " [DONE]" + tput sgr0 + + tput bold + echo -e "### CREATED: $htmldir/$savepath/$pagename.html\n" + tput sgr0 + fi +done +exit 0 diff --git a/createpages.sh b/createpages.sh @@ -1,213 +0,0 @@ -#!/bin/bash -# -# Hand-written script to build webpages and minify CSS for the paritybit.ca -# website. -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# 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 -# 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/>. - -if [[ $1 == '-h' || $1 == '--help' ]]; then - printf "Usage: createpages.sh [-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 " createpages.sh pages/**" - printf " createpages.sh pages/guides/new-guide.md pages/guides.md" - printf " createpages.sh -c base.css tables.css" - printf " createpages.sh -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 '.') - printf " " - tput bold; tput smul - echo "$file > $cssdir/$fileName.min.css" - tput sgr0 - curl --data "input=`cat $file`" https://cssminifier.com/raw > \ - "$cssdir"/"$fileName".min.css - if [[ $? != 0 ]]; then - printf "\nThere was an issue minifying the 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 '/') - - # Stripts "pages" from the file path so files are saved in the root of - # $htmldir - if [[ "$pagedir" == "pages" ]]; then - savepath="." - elif [[ "$checkpage" == "pages" ]]; then - savepath=$(echo ${pagedir#$checkpage/}) - fi - - # Make the requisite directories in the output and intermediate build folder - if [[ ! -d "$htmldir/$savepath" ]]; then - mkdir -p "$htmldir"/"$savepath" - mkdir -p build/"$savepath" - fi - - # Compile the page content if valid page name given - if [[ "$page" != "" && ! -d "$page" && "$pagename" != "" && "$pagename" != "-c" ]]; then - tput setaf 3; tput bold; tput smul - echo "### CREATING: $htmldir/$savepath/$pagename.html" - tput sgr0 - - # Turns the markdown into pure html - printf " Compiling page content..." - markdown "$pagedir"/"$pagename".md > build/"$savepath"/"$pagename"-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 - tput setaf 2; tput bold - echo " [DONE]" - tput sgr0 - - # ====================================================================== - # Now that the page 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>" - # And sets the html <title> tag to be <page_title>. - pagetitle=$(head -n 1 "$pagedir"/"$pagename".md | cut -f 2- -d " ") - printf " Setting page title..." - sed -i "s/<title>.*<\/title>/<title>${pagetitle//&/\\&}<\/title>/" \ - "$htmldir"/"$savepath"/"$pagename".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) - # Remove leading whitespace and forward slash characters - metaTextClean=$(echo -e "${metaText}" | sed -e 's/^[[:space:]]*//') - metaTextClean=$(sed -e 's/\//\\\//' <<< "$metaTextClean") - printf " Setting meta tag..." - sed -i "s/content=\"\"/content=${metaTextClean//&/\\&}/" \ - "$htmldir"/"$savepath"/"$pagename".html - tput setaf 2; tput bold - 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) - # Remove leading whitespace and quote marks - CSSTagsClean=$(echo -e "${CSSTags}" | sed -e 's/^[[:space:]]*//') - CSSTagsClean=$(sed -e 's/^"//' -e 's/"$//' <<< "$CSSTagsClean") - # Split fifth line into separate css file names, separated by a space - IFS=' ' read -r -a CSSFileList <<< "$CSSTagsClean" - # 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 - done - tput setaf 2; tput bold - echo " [DONE]" - tput sgr0 - - # Assumes the seventh line of every markdown file defines the sections - # to add to a table of contents (preceded by '[//]: #'). - toc=$(sed '7!d' "$pagedir"/"$pagename".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") - printf " Parsing table of contents..." - # Split the line into separate entries, separated by a semicolon - IFS=';' read -r -a sections <<< "$tocClean" - # Check for an empty array, if it's not empty then populate the TOC - if [ ! "${#sections[@]}" -eq "0" ]; then - # Add a horizontal rule and heading - sed -i "/TOC/a <h3 id=\"toc-title\">Table of Contents</h3>" \ - $htmldir/$savepath/$pagename.html - sed -i "/TOC/a <hr id=\"toc-hr\">" \ - $htmldir/$savepath/$pagename.html - # Add each section to list after the h3 tag (done in reverse order - # because lines are appended immediately after the h3 tag) and add - # an internal link to the header of that section as well as a - # "back-to-top" arrow beside each regular section header. - # - # Note: sectionNameNS is the name of the section without any spaces - # because the HTML spec says no spaces in href property - sed -i "/toc-title/a </ul>" $htmldir/$savepath/$pagename.html - for ((i = ${#sections[@]}-1; i >= 0; i--)); do - # Remove leading whitespaces - sectionName=$(echo -e "${sections[$i]}" | sed -e 's/^[[:space:]]*//') - # Check if this is supposed to be a sub-sub-section & add 2x<ul> - if echo "$sectionName" | grep -qi "^subsub:"; then - 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 - # 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 - # Otherwise, this is a normal section - else - sectionNameNS=$(sed -e 's/ //g' <<< "$sectionName") - sed -i "/toc-title/a <li><a href=\"#$sectionNameNS\">$sectionName</a></li>" \ - $htmldir/$savepath/$pagename.html - sed -i "/<h3>$sectionName<\/h3>/i <a id=\"$sectionNameNS\"></a>" \ - $htmldir/$savepath/$pagename.html - sed -i "s/<h3>$sectionName<\/h3>/<h3>$sectionName <a class=\"back-to-top\" href=\"#\">\&\#10548\;<\/a><\/h3>/g" \ - $htmldir/$savepath/$pagename.html - fi - done - # Closes out the table of contents list - sed -i "/toc-title/a <ul>" $htmldir/$savepath/$pagename.html - fi - tput setaf 2; tput bold - echo " [DONE]" - tput sgr0 - - tput bold - echo -e "### CREATED: $htmldir/$savepath/$pagename.html\n" - tput sgr0 - fi -done -exit 0