#!/usr/bin/env bash
#shellcheck disable=SC2155
error() {
echo "error: $*" >&2
}
escape() {
jq -Rr @html
}
_json_str() {
printf '%s' "$1" | jq -Rr @json
}
_json_str_opt() {
if [[ "${1:+x}" == 'x' ]]; then
_json_str "$1"
else
printf 'null'
fi
}
#shellcheck disable=SC2153
metadata() {
local title=$(_json_str "${TITLE?}")
local description=$(_json_str "${DESCRIPTION?}")
local author=$(_json_str "${AUTHOR:-$(id -un)}")
local icon=$(_json_str_opt "$ICON")
local icon_alt=$(_json_str_opt "$ICON_ALT")
local color=$(_json_str_opt "$COLOR")
local created_at=$(_json_str_opt "$CREATED_AT")
local modified_at=$(_json_str_opt "$MODIFIED_AT")
local tags=$(for tag in "${TAGS[@]}"; do printf '%s' "$tag" | jq -Rs .; done | jq -sr @json)
local dont_cache=false
[[ "${DONT_CACHE:+x}" == 'x' ]] && dont_cache=true
printf '{"title":%s,"description":%s,"author":%s,"icon":%s,"icon_alt":%s,"color":%s,"created_at":%s,"modified_at":%s,"tags":%s,"dont_cache":%s}\n' \
"$title" "$description" "$author" \
"$icon" "$icon_alt" "$color" \
"$created_at" "$modified_at" \
"$tags" "$dont_cache"
}
header() {
level=${1?need header level}
if ! (( level >= 1 && level <= 5 )); then
error "header level must be a number from 1 to 5"
return 1
fi
printf '
'
escape
printf '
'
}
render() {
local birth=$(stat -c'%W' "$1")
if (( birth > 0 )); then
CREATED_AT=$(date --rfc-3339=seconds -d "@$birth")
fi
MODIFIED_AT=$(date --rfc-3339=seconds -d "@$(stat -c '%Y' "$1")")
#shellcheck disable=SC1090
( . "$1"; ) # the moment you've all been waiting for
}
print_help() {
echo "Usage: $0