#!/bin/bash
##
# Generate html page with blog article excerpts from ./posts.txt. Post file names should
# be added to ./posts.txt in the exact order that they are supposed to appear on the blog
# page.
# Check if required executables can be found
if ! output="$(type readlink dirname html2text mv cat cksum base64 pup 2>&1)"; then
echo 'One or more required executables are not present. Generation cancelled' >&2
echo "'type' output: $output" >&2
echo 'Note: You can install pup with "go get github.com/ericchiang/pup"' >&2
exit 1
fi
# Determine script directory (requires GNU readlink)
here="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
printf 'Changing directory: '
pushd "$here" || exit $?
posts_file="$here/publish.txt"
if ! [[ -f "$posts_file" ]]; then
printf 'Posts file "%s" not found. Generation cancelled.\n' "$posts_file" >&2
exit 1
fi
link-back-to-root() {
declare root="$1" directory="$2"
directory="${directory#$root}"
directory="${directory%/}"
declare link=''
while read -rd '/' slug; do
link="${link}../"
done <<<"$directory"
echo "$link"
}
escape-html() {
sed 's/&/\&/g; s/\</g; s/>/\>/g; s/"/\"/g; s/'"'"'/\'/g'
}
html-to-text() {
html2text -nobs -style compact "$@"
}
timestamp() {
date +'%s'
}
rfc-822-date-time() {
LC_ALL=C date "$@" --rfc-email
}
print-html-top() {
declare title="$1" root="$2" directory="$3"
declare backlink=''
backlink="$(link-back-to-root "$root" "$directory")" || return $?
cat <${title}
EOF
}
print-html-bottom() {
echo '
'
}
print-post-html-bottom() {
declare publish_date="$1" last_edit_date="$2"
cat <First published: ${publish_date} Last edited: ${last_edit_date}