#!/bin/bash : ${REPO_FOLDER:=.} : ${GEN_FOLDER:=./website} : ${GEN_TINY_FOLDER:=./website.tiny} : ${AWK_FILE:=/tmp/syndamiadotcom-awk} # 0. Helper functions {{{ dur=0 startTimer() { dur="$(date +%s%N)" } endTimer() { dur="$(echo "scale=4 ; ($(date +%s%N) - $dur) / 1000000000" | bc -l)" printf "%.3fs $1\n" $dur } # }}} # 1. Markdown source to HTML {{{ [ $# -eq 0 -a "$(id -u -n)" != "www-data" ] && echo "[Warning] Running in \"production\" by a user that isn't www-root!" [ ! -d "$GEN_FOLDER" ] && mkdir "$GEN_FOLDER" [ $# -eq 0 -a "$(stat -c %U "$GEN_FOLDER")" != "www-data" ] && echo "[Warning] Running in \"production\" while $GEN_FOLDER isn't owned by www-data!" # Remove record of previously generated files rm -f "$GEN_FOLDER/.files" ssg "$REPO_FOLDER/src" "$GEN_FOLDER" "Syndamia" "https://syndamia.com" >/dev/null # }}} # 2. Editing HTML files with AWK {{{ createAWKscript() { # "librarires" find "$REPO_FOLDER/awk-edits" -type f -name "#*.awk" -exec cat {} + > "$AWK_FILE" # Scripts find "$REPO_FOLDER/awk-edits" -type f -name "[A-Z]*.awk" -exec cat {} + >> "$AWK_FILE" echo '{if ($0 != 0) print}' >> "$AWK_FILE" # Post-print part of scripts find "$REPO_FOLDER/awk-edits" -type f -name "[a-z]*.awk" -exec cat {} + >> "$AWK_FILE" } # $1 - $libs, for devs runAWKscript() { [ -z "$1" ] && params="-f $AWK_FILE" \ || params="$1 -f $AWK_FILE -f /tmp/awk-print $postfile" for file in $(find "$GEN_FOLDER" -type f -name "*.html"); do awk --posix $params "$file" > tmp && mv -f tmp "$file" done } if [ $# -eq 0 ]; then # Run in "production" createAWKscript runAWKscript elif [ "$1" == 'dev' ]; then startTimer createAWKscript endTimer "generation of $AWK_FILE" startTimer runAWKscript endTimer "$AWK_FILE" elif [ "$1" == 'devs' ]; then echo '{if ($0 != 0) print}' > /tmp/awk-print libs="" for l in $(find "$REPO_FOLDER/awk-edits" -type f -name "#*.awk"); do libs+=" -f $l " done for i in $(find "$REPO_FOLDER/awk-edits" -type f -name "[A-Z]*.awk") $(find "$REPO_FOLDER/awk-edits" -type f -name "[a-z]*.awk" -not -name "post-[A-Z]*.awk"); do AWK_FILE="$i" postfile="$(find "$REPO_FOLDER/awk-edits" -type f -name "post-${AWK_FILE##*/}")" if [ -n "$postfile" ]; then i="$i + $postfile" postfile=" -f ${postfile}" fi startTimer runAWKscript "$libs" endTimer "$i" done fi # }}} # 3. Tiny version, strip away styles and other stuff {{{ [ ! -d "$GEN_TINY_FOLDER" ] && mkdir "$GEN_TINY_FOLDER" [ $# -eq 0 -a "$(stat -c %U "$GEN_TINY_FOLDER")" != "www-data" ] && echo "[Warning] Running in \"production\" while $GEN_TINY_FOLDER isn't owned by www-data!" # Just take what is already made, no need to regenerate everything cp -r "$GEN_FOLDER"/* "$GEN_TINY_FOLDER" # Remove unnecessary files rm -f "$GEN_TINY_FOLDER"/*.css rm -rf "$GEN_TINY_FOLDER"/fonts/ rm -f "$GEN_TINY_FOLDER"/.files improveForTINY() { for file in $(find "$GEN_TINY_FOLDER" -type f -name "*.html"); do # Remove references to stylesheets in all files sed -i -E 's/^.*(rel="stylesheet|\Content/
Content/g' $file # Fix svg images being too big (scaling without limit) sed -i 's/<\/head>/\t