commit 8d1d7aafae3e28e55edcf1abe66e9bf4b736e57a
parent ec2729f7aef5eec29d4dfe318cc752b1eaba907e
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Fri, 22 May 2020 21:55:15 -0400
Complete rewrite of compile script
Diffstat:
M | compile-new | | | 142 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------ |
1 file changed, 111 insertions(+), 31 deletions(-)
diff --git a/compile-new b/compile-new
@@ -1,7 +1,7 @@
#!/bin/sh
# compile
-# A POSIX shell script to build pages and minify css for paritybit.ca.
+# A POSIX shell script to build pages and minify css for a simple website.
# NOTE: See the README for the expected format of md files.
#
# Copyright (C) 2020 Jake Bauer
@@ -33,9 +33,17 @@ if [ ! -x "$(command -v curl)" ]; then
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
+fi
+# The name of the site in the <title> tags
+siteName="paritybit.ca"
# Set to "false" to disable auto-upload
-autoUpload="false"
+autoUpload="true"
+# Where to upload files
+remote="paritybit.ca:uploads/"
# Where md files are
pageDir="pages/"
# Where compiled md -> html pages will go
@@ -48,31 +56,37 @@ mkdir -p "$cssDir"
buildDir="build/"
mkdir -p "$buildDir"
-# Prints the message passed as the 1st argument in bold red.
+# Prints the message passed as the 1st argument in red.
print_error_msg()
{
- tput sgr0; tput setaf 1; tput bold
- printf "%s\n" "$1"
+ tput sgr0; tput setaf 1
+ printf "[ee] %s\n" "$1"
tput sgr0
}
-# Prints "DONE" in bold green.
+# Prints the message passed as the 1st argument in green.
print_success_msg()
{
- tput sgr0; tput setaf 2; tput bold
- echo "DONE\n"
+ tput sgr0; tput setaf 2
+ printf "[ok] %s\n" "$1"
tput sgr0
}
-# Prints the message passed as the 1st argument in bold yellow.
+# Prints the message passed as the 1st argument in yellow.
+print_header_msg()
+{
+ tput sgr0; tput setaf 3
+ printf "[hh] %s\n" "$1"
+ tput sgr0
+}
+
+# Prints the message passed as the 1st argument with default colours.
print_info_msg()
{
- tput sgr0; tput setaf 3; tput bold
- printf "%s\n" "$1"
tput sgr0
+ printf "[ii] %s\n" "$1"
}
-# Expects ~/.ssh/config to contain an entry for "paritybit.ca"
auto_upload()
{
if [ "$autoUpload" != "true" ]; then
@@ -81,20 +95,21 @@ auto_upload()
print_info_msg "Auto-uploading file..."
if [ "$autoUpload" = "true" ]; then
if [ "$fileExt" = "css" ]; then
- cd public && rsync -rR css/"$fileName".min.css \
- paritybit.ca:uploads/ && cd ..
+ outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
+ cd public && rsync -rR "$outputFile" "$remote" && cd ..
elif [ "$fileExt" = "md" ]; then
- cd public && rsync -rR html/"$savePath""$fileName".html \
- paritybit.ca:uploads/ && cd ..
+ outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
+ cd public && rsync -rR "$outputFile" "$remote" && cd ..
fi
fi
}
compile_css()
{
- echo "MINIFYING CSS: $fileName"
+ print_header_msg "MINIFYING CSS: $file"
+ outputFile="$cssDir/$fileName.min.css"
curl --data "input=$(cat "$file")" https://cssminifier.com/raw \
- > "$cssDir"/"$fileName".min.css
+ > "$outputFile"
if [ $? != 0 ]; then
print_error_msg "There was an issue minifying this CSS."
exit 1
@@ -103,27 +118,98 @@ compile_css()
compile_md()
{
- true
+ print_header_msg "COMPILING MARKDOWN: $file"
+ mkdir -p "$buildDir"/"$subDir"
+ mkdir -p "$htmlDir"/"$subDir"
+
+ pandoc -o "$buildDir"/"$subDir"/"$fileName"-content.html \
+ --highlight-style zenburn \
+ -f markdown -t html $file 2>/dev/null
+
+ 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
}
set_html_title()
{
- true
+ title=$(head -n1 $file | cut -d' ' -f2-)
+ print_info_msg "Setting <title>: \"$title\""
+ sed -i "s/<title>.*<\/title>/<title>$title - $siteName<\/title>/" \
+ "$outputFile"
}
set_html_meta()
{
- true
+ meta=$(head -n3 $file | tail -n1 \
+ | cut -d'#' -f2 | sed -e 's/^[[:space:]]*//')
+ print_info_msg "Setting <meta> description"
+ sed -i "s/content=\"\"/content=$meta/" "$outputFile"
}
set_html_csslink()
{
- true
+ 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 "/<title>/a\ \ \ \ <link rel=\"stylesheet\" href=\"$cssFile\">" \
+ "$outputFile"
+ done
}
+# This still feels a bit messy
set_html_toc()
{
- true
+ 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 "/TOC/a <div id=\"table-of-contents\" aria-label=\"Table of Contents\">\n<h2 id=\"toc-title\">Table of Contents</h2>" \
+ "$outputFile"
+ sed -i "/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:]' )"
+ sed -i "/toc-title/a <ul><ul><li><a href=\"#$tocEntryId\">$tocEntry</a></li></ul></ul>" \
+ "$outputFile"
+ sed -i "/<h5>$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:]' )"
+ sed -i "/toc-title/a <ul><li><a href=\"#$tocEntryId\">$tocEntry</a></li></ul>" \
+ "$outputFile"
+ sed -i "/<h4>$tocEntry<\/h4>/i </section>\n<section>" \
+ "$outputFile"
+ else
+ tocEntryId="$(echo "$tocEntry" | tr ' ' '-' \
+ | tr '[:upper:]' '[:lower:]' )"
+ sed -i "/toc-title/a <li><a href=\"#$tocEntryId\">$tocEntry</a></li>" \
+ "$outputFile"
+ sed -i "/<h3>$tocEntry<\/h3>/i </section\>\n<section>" \
+ "$outputFile"
+ fi
+ done
+ sed -i "/toc-title/a <ul>" "$outputFile"
+ sed -i "/END OF HEADER FILE/a <section>" "$outputFile"
+ sed -i "/<\/main>/i </section>" "$outputFile"
}
for file in "$@"; do
@@ -135,21 +221,15 @@ for file in "$@"; do
fileName=$(basename "$file" | cut -d'.' -f1)
fileExt=$(echo "$file" | awk -F . '{if (NF>1) {print $NF}}')
fileDir=$(dirname "$file")
- echo "$fileName"
- echo "$fileExt"
- echo "$fileDir"
+ subDir=$(echo "$fileDir" | sed "s/^pages//")
if [ "$fileExt" = "css" ]; then
compile_css
auto_upload
elif [ "$fileExt" = "md" ]; then
compile_md
- set_html_title
- set_html_meta
- set_html_csslink
- set_html_toc
auto_upload
fi
- print_success_msg
+ print_success_msg "FINISHED: $outputFile"
done