#!/usr/bin/env bash # ----------------------------------------------------------------------------- # Copyright 2019 Dave Jarvis # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be included # in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # ----------------------------------------------------------------------------- source $HOME/bin/build-template DEPENDENCIES=( "inotifywait,https://github.com/rvoicilas/inotify-tools/wiki" "minify,https://github.com/tdewolff/minify" ) PATH_SOURCE=src # ----------------------------------------------------------------------------- # Perform all commands that the script requires. # # @return 0 - Indicate to terminate the script with non-zero exit level # @return 1 - All tasks completed successfully (default) # ----------------------------------------------------------------------------- execute() { local -r await=close_write,delete $log "Await modifications in ${SCRIPT_DIR}" inotifywait -q -e "${await}" -m -r ${PATH_SOURCE}/* | \ while read -r directory event filename; do if [[ ! "${event}" == *ISDIR* ]] && filter "${filename}"; then $log "${directory}${filename} (${event})" build_document fi done return 1 } # ----------------------------------------------------------------------------- # Determine whether the given filename is Markdown or TeX. # # @param $1 - The filename to check. # # @return 0 - The filename passes the filter. # @return 1 - The filename does not pass through the filter. # ----------------------------------------------------------------------------- filter() { [[ "${1,,}" =~ \.(css|html|js)$ ]] return $? } # ----------------------------------------------------------------------------- # Builds the document; called after a Markdown file update or delete event. # # Requires that Markdown filenames be named consecutively with zero padding. # Build artefacts are stored in the aptly named artefacts directory. # ----------------------------------------------------------------------------- build_document() { $minify_css $process_js $combine_html $minify_html } # ----------------------------------------------------------------------------- # Combines static HTML pages. # ----------------------------------------------------------------------------- utile_combine_html() { local -r DIR_SOURCES="${PATH_SOURCE}/html" local -r DIR_INC="includes" pushd ${DIR_SOURCES} > /dev/null for f in $(find * -type f -name "*.html" -not -path "${DIR_INC}/*"); do filename=$(basename $f) filepath=$(dirname $f) dir_out="../.." if [ ! -d "${dir_out}/${filepath}" ]; then $log "Create ${dir_out}/${filepath}" mkdir -p ${dir_out}/${filepath} fi path_out="${dir_out}/${filepath}/${filename}" $log "Build ${f} as ${path_out}" cat ${DIR_INC}/header.html ${f} ${DIR_INC}/footer.html > ${path_out} done popd > /dev/null } # ----------------------------------------------------------------------------- # Minifies static HTML pages. # ----------------------------------------------------------------------------- utile_minify_html() { $log "Minify HTML" for f in $(find * -maxdepth 0 -type f -name "*.html"); do minify "${f}" > "${f}.min" mv -f "${f}.min" "${f}" done } # ----------------------------------------------------------------------------- # Parses a single command-line argument. # ----------------------------------------------------------------------------- utile_minify_css() { minify ${PATH_SOURCE}/css/*.css > css/impacts.min.css } utile_process_js() { minify ${PATH_SOURCE}/js/??-*.js > js/impacts.min.js cp ${PATH_SOURCE}/js/lightbox.min.js js } combine_html=utile_combine_html minify_html=utile_minify_html minify_css=utile_minify_css process_js=utile_process_js main "$@"