2022-02-05 19:17:01 -08:00
|
|
|
## Library for generating a website
|
|
|
|
|
|
|
|
WXRX_WEB_DIR=${WXRX_WEB_DIR:=~/wxrx/web}
|
|
|
|
WXRX_WEB_TEMPLATES=${WXRX_WEB_TEMPLATES:="${WXRX_WEB_DIR}/templates"}
|
|
|
|
WXRX_WEB_PUBDIR=${WXRX_WEB_PUBDIR:="${WXRX_WEB_DIR}/public"}
|
|
|
|
|
2022-02-12 12:41:55 -08:00
|
|
|
# Determine the path of a template
|
|
|
|
# @param name
|
|
|
|
# @output path to template
|
|
|
|
# @return non-zero if path does not exist
|
2022-02-05 19:17:01 -08:00
|
|
|
function template_path() {
|
2022-07-08 07:10:24 -07:00
|
|
|
local file=${WXRX_WEB_TEMPLATES}/${1}.template
|
|
|
|
local fallback="${rootdir}/lib/web-templates/${1}.template"
|
2022-02-12 12:41:55 -08:00
|
|
|
if [ -f "$file" ]; then
|
|
|
|
echo $file
|
|
|
|
return
|
2022-07-08 07:10:24 -07:00
|
|
|
elif [ -f "$fallback" ]; then
|
|
|
|
echo $fallback
|
|
|
|
return
|
2022-02-12 12:41:55 -08:00
|
|
|
fi
|
|
|
|
return 1
|
2022-02-05 19:17:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Replaces every "{{$1}}" in stdin with $2
|
|
|
|
# @param VAR
|
|
|
|
# @param replacement
|
|
|
|
function template_subst() {
|
|
|
|
# ~ is a delimiter, but ~ in the replacement is escaped
|
|
|
|
replacement=$(echo ${2} | tr -d '\n')
|
|
|
|
sed -e "s~{{${1}}}~${replacement//'~'/'\~'}~g"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Replaces {{VAR}} from stdin with contents of file $2
|
|
|
|
# @param VAR
|
|
|
|
# @param file replacement
|
|
|
|
function template_fsubst() {
|
|
|
|
echo "TODO: implement website_generatorlib.sh:${0}"
|
|
|
|
}
|
|
|
|
|
2022-02-11 14:04:36 -08:00
|
|
|
# Moves a file to the WXRX_WEB_PUBDIR
|
|
|
|
# @param file path
|
|
|
|
# @param destination path (optional), if not supplied the path part of the first argument is used
|
|
|
|
# @side_effect copies file in first arg to the WXRX_WEB_PUBDIR tree
|
|
|
|
# @output relative path (from WXRX_WEB_PUBDUR)
|
2022-02-18 13:39:09 -08:00
|
|
|
# @deprecated - use publish_file
|
2022-02-05 19:17:01 -08:00
|
|
|
function move_to_public() {
|
|
|
|
src=${1}
|
2022-02-11 14:04:36 -08:00
|
|
|
relative_path=$([[ ${2} =~ \/$ ]] && echo "${2}${1}" || echo "${2:-${1}}")
|
2022-02-05 19:17:01 -08:00
|
|
|
dest=${WXRX_WEB_PUBDIR}/${relative_path}
|
|
|
|
mkdir -p $(dirname ${dest})
|
|
|
|
cp ${src} ${dest}
|
|
|
|
echo ${relative_path}
|
|
|
|
}
|
|
|
|
|
|
|
|
function publish_audio() {
|
|
|
|
move_to_public "${1}" "audio/${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function publish_image() {
|
|
|
|
move_to_public "${1}" "img/${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
function publish_file() {
|
|
|
|
move_to_public "${1}"
|
|
|
|
}
|