#!/usr/bin/env bash set -o errexit : ${language=en} : ${charset=utf-8} : ${viewport='width=device-width'} --app-language() { language=$1; } --app-attrs() { attrs+=" $1"; } --app-charset() { charset=$1; } --app-viewport() { viewport=$1; } preamble=(app-language app-attrs app-charset app-viewport) is-preamble-keyword() { printf '%s\n' ${preamble[*]} | grep "^$1\$" >/dev/null; } maybe-insert-preamble() if ! `is-preamble-keyword $1`; then head+=("$(printf "$charset_tag" "$charset")") head+=("$(printf "$viewport_tag" "$viewport")") maybe-insert-preamble() { :; } for x in ${preamble[*]}; do eval 'function --'$x' { echo >&2 $0: '$x' must come first; }' done; fi : ${html_tag:=''} : ${charset_tag:=''} : ${viewport_tag:=''} : ${title_tag:='%s'} : ${javascript_tag:=''} : ${stylesheet_tag:=''} --app-title() { head+=("$(printf "$title_tag" "$1")"); } --vendor-head() { head+=("$1"); } --app-head() { app_head+=("$1"); } --app-body() { body+=("$1"); } --vendor-foot() { foot+=("$1"); } --app-foot() { app_foot+=("$1"); } --stylesheet() { --${2}head "$(printf "$stylesheet_tag" "$1")"; } --javascript() { --${2}foot "$(printf "$javascript_tag" "$1")"; } --vendor-asset() { asset "$1" vendor-; } --app-asset() { asset "$1" app-; } asset() case "$1" in *.css) --stylesheet "$1" $2;; *.js) --javascript "$1" $2;; *) echo >&2 "$0: $1: ignoring unknown asset";; esac while read keyword words; do if [[ $keyword ]]; then keyword=$(echo $keyword | tr A-Z a-z | sed 's/:$//') [[ $words ]] || words=$keyword keyword=app-asset if type >&/dev/null -- "--$keyword"; then maybe-insert-preamble $keyword --$keyword "${words[@]}" elif [[ $keyword != '#' ]]; then echo >&2 "$0: unknown keyword \`$keyword'"; fi else while read line; do --app-body "$line"; done; fi; done maybe-insert-preamble head=("${head[@]}" "${app_head[@]}") body=("${body[@]}" "${foot[@]}" "${app_foot[@]}") sed 's/^ *$//' < `printf "$html_tag" "$language" "$attrs"` `[[ $head ]] && printf '\n %s' "${head[@]}"` `[[ $body ]] && printf '\n %s' "${body[@]}"` EOF