1 #!/usr/bin/env bash
  2 
  3 # shellcheck disable=2059
  4 # Taken from pure-sh-bible
  5 basename() {
  6     dir=${1%${1##*[!/]}}
  7     dir=${dir##*/}
  8     dir=${dir%"$2"}
  9     printf '%s\n' "${dir:-/}"
 10 }
 11 
 12 dirname() {
 13     dir=${1:-.}
 14     dir=${dir%%"${dir##*[!/]}"}
 15     [ "${dir##*/*}" ] && dir=.
 16     dir=${dir%/*}
 17     dir=${dir%%"${dir##*[!/]}"}
 18     printf '%s\n' "${dir:-/}"
 19 }
 20 
 21 # Toad:
 22 # This is meant to be my super minimal SSG and meant to only fulfill my needs and do nothing more
 23 
 24 # Configuration
 25 port=5000
 26 
 27 # Extract relevant parts of the template
 28 pre=$(grep -Pzo '[^$]+(?=!CONTENT!)' template.html | tr -d '\0') # Posixify
 29 post=$(grep -Pzo '(?<=!CONTENT!)[^$]+' template.html | tr -d '\0') # Posixify
 30 
 31 # General build process
 32 process () {
 33     out=${2/.${out#*.}/.html}
 34     mkdir -p "$(dirname "$out")"
 35     printf "\nBuilding $1 -> $out\n"
 36 
 37     x=${out%%.html}
 38     x=${x##out/}
 39     header="${pre/!TITLE!/$x}" # Substitute Header
 40     printf '%s' "$header" > "$out"
 41     $3 "$1" 1>> "$out" # Generate HTML -- CHANGE
 42     printf '%s' "$post" >> "$out" # Output To File
 43 }
 44 
 45 # Primary Build Function - handles every file
 46 build () {
 47     # Output File Path
 48     out=${1/src/out}
 49 
 50     case "${out#*.}" in
 51         # HTML
 52         "html")
 53             cp -fv "$1" "$out"
 54             return
 55             ;;
 56         # Pond's Format
 57         "fmt.txt")
 58             process "$1" "$out" 'env MARKDOWN_COMPAT=1; ENABLE_HEADERS=1; ENABLE_CODE_LINES=1; bash tool/pond.sh'
 59             ;;
 60         # Markdown
 61         "md")
 62             process "$1" "$out" 'env MARKDOWN_COMPAT=1 bash tool/pond.sh'
 63         ;;
 64     esac
 65 
 66 }
 67 
 68 case $1 in
 69 
 70     # Serve files with hot reloading
 71     live)
 72         live-server --no-browser --port=$port --ignore=out --ignore=src &
 73         printf "$(find src -name "*.fmt.txt")\n$(realpath ./template.html)\n$(find tool -name "*.sh")" | entr ./make
 74         ;;
 75 
 76     # Generates a super simple index of all the articles found
 77     index)
 78         while read -r file; do
 79             filename=${file%%.html}
 80             printf "<a href=\"$file\">${filename/out\//}</a>\n"
 81         done < <(find out/ -type f -not -name "index.html")
 82         ;;
 83 
 84     # Serve without hot realoading
 85     serve)
 86         python3 -m http.server $port &
 87         ;;
 88     clean)
 89         rm -r out index.html
 90         ;;
 91 
 92     # Build Files
 93     *)
 94         mkdir -p src out
 95         for file in ${2:-src/**/*.fmt.txt src/*.fmt.txt}; do
 96             build "$file"
 97         done
 98         mv ./out/index.html .
 99         printf "\nFinished!\n"
100     ;;
101 esac