commit c7a086fb32d56b87d371987c48e2a30633c66b52
parent 8d1d7aafae3e28e55edcf1abe66e9bf4b736e57a
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Fri, 22 May 2020 21:55:34 -0400
Replace old script with new script
Diffstat:
M | compile | | | 436 | ++++++++++++++++++++++++++++++++++++++----------------------------------------- |
D | compile-new | | | 235 | ------------------------------------------------------------------------------- |
2 files changed, 209 insertions(+), 462 deletions(-)
diff --git a/compile b/compile
@@ -1,7 +1,10 @@
-#!/bin/bash
+#!/bin/sh
+
+# compile
+# 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.
#
-# Hand-written script to build webfiles and minify CSS for the paritybit.ca
-# website.
+# Copyright (C) 2020 Jake Bauer
#
# 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
@@ -16,238 +19,217 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
+# POSIX Shell "Strict Mode"
+set -o errexit
+set -o nounset
+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
+fi
+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="true"
+# Where to upload files
+remote="paritybit.ca:uploads/"
+# Where md files are
+pageDir="pages/"
+# Where compiled md -> html pages will go
htmlDir="public/html"
-cssdir="public/css"
-mkdir -p "$cssdir"
mkdir -p "$htmlDir"
-
-for file in $@; do
-
- if [[ ! -f "$file" ]]; then
- tput setaf 1; tput bold;
- printf " ERROR: File "
- tput smul
- printf "%s" "$file"; tput sgr0
- tput setaf 1; tput bold;
- printf " not found.\n\n"
- tput sgr0
- continue;
+# Where minified css will go
+cssDir="public/css"
+mkdir -p "$cssDir"
+# Where intermediate build files will go
+buildDir="build/"
+mkdir -p "$buildDir"
+
+# Prints the message passed as the 1st argument in red.
+print_error_msg()
+{
+ tput sgr0; tput setaf 1
+ printf "[ee] %s\n" "$1"
+ tput sgr0
+}
+
+# Prints the message passed as the 1st argument in green.
+print_success_msg()
+{
+ tput sgr0; tput setaf 2
+ printf "[ok] %s\n" "$1"
+ tput sgr0
+}
+
+# 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
+ printf "[ii] %s\n" "$1"
+}
+
+auto_upload()
+{
+ if [ "$autoUpload" != "true" ]; then
+ return 0
fi
-
- fileName=$(basename "$file" | cut -f 1 -d '.')
- fileExtension="${file##*.}"
- filedir=$(dirname "$file")
- removePrefix=$(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"
- tput sgr0
-
- curl --data "input=`cat $file`" https://cssminifier.com/raw > \
- "$cssdir"/"$fileName".min.css
- if [[ $? != 0 ]]; then
- printf "\nThere was an issue minifying this CSS."
- exit 1
+ print_info_msg "Auto-uploading file..."
+ if [ "$autoUpload" = "true" ]; then
+ if [ "$fileExt" = "css" ]; then
+ outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
+ cd public && rsync -rR "$outputFile" "$remote" && cd ..
+ elif [ "$fileExt" = "md" ]; then
+ outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
+ cd public && rsync -rR "$outputFile" "$remote" && cd ..
fi
-
- # Auto-upload the new css to the site
- printf " Uploading new css... "
-
- cd public && rsync -rR css/"$fileName".min.css \
- paritybit.ca:uploads/ && cd ..
-
- tput setaf 2; tput bold
- echo "[DONE]"
- tput sgr0
-
- echo ""
- continue
fi
-
- # Strips "pages" from the file path so pages are saved in the root of
- # $htmlDir
- if [[ "$filedir" == "pages" ]]; then
- savePath=""
- elif [[ "$removePrefix" == "pages" ]]; then
- savePath="$(echo ${filedir#$removePrefix/})/"
+}
+
+compile_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
-
- # Make the requisite directories in the output and intermediate build folder
- if [[ ! -d "$htmlDir/$savePath" ]]; then
- mkdir -p "$htmlDir"/"$savePath"
- mkdir -p build/"$savePath"
+}
+
+compile_md()
+{
+ 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()
+{
+ 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()
+{
+ 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()
+{
+ 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()
+{
+ 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
-
- # Compile the file content if valid file name given
- if [[ "$file" != "" && ! -d "$file" && "$fileName" != "" ]]; then
- tput setaf 3; tput bold; tput smul
- echo "### CREATING: $htmlDir/$savePath$fileName.html"
- tput sgr0
-
- # Turns the markdown into pure html
- printf " Compiling page content..."
- 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$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 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:
- # "## <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>${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 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 description..."
- sed -i "s/content=\"\"/content=${metaTextClean//&/\\&}/" \
- "$htmlDir"/"$savePath"/"$fileName".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' "$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")
- # 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$fileName.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 '[//]: #').
- #
- # Each ((sub)sub)section is added to the list in reverse order of
- # appearance as the entry is appended after the table of contents header
- # for simplicity (the header is an "anchor point" for sed to act on).
- # Also added is an internal link to the relevant header in the actual
- # content, <section> elements to divide each logical section of the
- # document for accessibility.
- #
- # 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' "$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")
- 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
- 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$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:]]*//')
- # 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$fileName.html
-
- sed -i "/<h5>$sectionName<\/h5>/i </section>\n<section>" \
- $htmlDir/$savePath$fileName.html
-
- sed -i "s/<h5>$sectionName<\/h5>/<h5 id=\"$sectionNameNS\">$sectionName<\/h5>/" \
- $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$fileName.html
-
- sed -i "/<h4>$sectionName<\/h4>/i </section>\n<section>" \
- $htmlDir/$savePath$fileName.html
-
- sed -i "s/<h4>$sectionName<\/h4>/<h4 id=\"$sectionNameNS\">$sectionName<\/h4>/" \
- $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$fileName.html
-
- sed -i "/<h3>$sectionName<\/h3>/i </section\>\n<section>" \
- $htmlDir/$savePath$fileName.html
-
- sed -i "s/<h3>$sectionName<\/h3>/<h3 id=\"$sectionNameNS\">$sectionName<\/h3>/" \
- $htmlDir/$savePath$fileName.html
- fi
- done
- # Closes out the table of contents list
- 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$fileName.html
-
- # Closes out the final section by inserting final </section> tag
- sed -i "/<\/main>/i </section>" $htmlDir/$savePath$fileName.html
+ 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
- tput setaf 2; tput bold
- echo " [DONE]"
- tput sgr0
-
- # Auto-upload the new page to the site
- printf " Uploading page... "
-
- cd public && rsync -rR html/"$savePath""$fileName".html \
- paritybit.ca:uploads/ && cd ..
-
- tput setaf 2; tput bold
- echo "[DONE]"
- tput sgr0
+ 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
+ if [ ! -f "$file" ]; then
+ print_error_msg "ERROR: $file not found or is not a file."
+ continue
+ fi
- tput bold
- echo -e "### CREATED: $htmlDir/$savePath$fileName.html\n"
- tput sgr0
+ fileName=$(basename "$file" | cut -d'.' -f1)
+ fileExt=$(echo "$file" | awk -F . '{if (NF>1) {print $NF}}')
+ fileDir=$(dirname "$file")
+ subDir=$(echo "$fileDir" | sed "s/^pages//")
+
+ if [ "$fileExt" = "css" ]; then
+ compile_css
+ auto_upload
+ elif [ "$fileExt" = "md" ]; then
+ compile_md
+ auto_upload
fi
+
+ print_success_msg "FINISHED: $outputFile"
done
-exit 0
diff --git a/compile-new b/compile-new
@@ -1,235 +0,0 @@
-#!/bin/sh
-
-# compile
-# 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
-#
-# 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/>.
-
-# POSIX Shell "Strict Mode"
-set -o errexit
-set -o nounset
-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
-fi
-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="true"
-# Where to upload files
-remote="paritybit.ca:uploads/"
-# Where md files are
-pageDir="pages/"
-# Where compiled md -> html pages will go
-htmlDir="public/html"
-mkdir -p "$htmlDir"
-# Where minified css will go
-cssDir="public/css"
-mkdir -p "$cssDir"
-# Where intermediate build files will go
-buildDir="build/"
-mkdir -p "$buildDir"
-
-# Prints the message passed as the 1st argument in red.
-print_error_msg()
-{
- tput sgr0; tput setaf 1
- printf "[ee] %s\n" "$1"
- tput sgr0
-}
-
-# Prints the message passed as the 1st argument in green.
-print_success_msg()
-{
- tput sgr0; tput setaf 2
- printf "[ok] %s\n" "$1"
- tput sgr0
-}
-
-# 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
- printf "[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" ]; then
- outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
- cd public && rsync -rR "$outputFile" "$remote" && cd ..
- elif [ "$fileExt" = "md" ]; then
- outputFile="$(echo "$outputFile" | cut -d'/' -f2-)"
- cd public && rsync -rR "$outputFile" "$remote" && cd ..
- fi
- fi
-}
-
-compile_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
-}
-
-compile_md()
-{
- 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()
-{
- 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()
-{
- 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()
-{
- 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()
-{
- 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
- if [ ! -f "$file" ]; then
- print_error_msg "ERROR: $file not found or is not a file."
- continue
- fi
-
- fileName=$(basename "$file" | cut -d'.' -f1)
- fileExt=$(echo "$file" | awk -F . '{if (NF>1) {print $NF}}')
- fileDir=$(dirname "$file")
- subDir=$(echo "$fileDir" | sed "s/^pages//")
-
- if [ "$fileExt" = "css" ]; then
- compile_css
- auto_upload
- elif [ "$fileExt" = "md" ]; then
- compile_md
- auto_upload
- fi
-
- print_success_msg "FINISHED: $outputFile"
-done