HandBrake-docs/build-docs
2016-03-25 13:19:55 -04:00

135 lines
4.4 KiB
Bash
Executable File

#!/bin/bash
# vars
SELF="${BASH_SOURCE[0]}"
BASE_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)
BASE_DIR="${BASE_DIR:-$(pwd)}"
CONFIG="${BASE_DIR}/config.yaml"
DOCS_DIR="${BASE_DIR}/docs"
SOURCE_DIR="${BASE_DIR}/source"
TEMPLATE_DIR="${BASE_DIR}/templates"
TOOLS_DIR="${BASE_DIR}/tools"
# dependencies
MARKDOWN="${BASE_DIR}/tools/discount/markdown"
if [[ "${1}" == "--markdown" ]]; then # TODO: remove ugly hack
MARKDOWN="${2}"
fi
DEPS=("${MARKDOWN}")
# creates bash variables from yaml records
# https://gist.github.com/pkuczynski/8665367
function parse_yaml {
local prefix=$2
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
awk -F$fs '{
indent = length($1)/2;
vname[indent] = $2;
for (i in vname) {if (i > indent) {delete vname[i]}}
if (length($3) > 0) {
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
}
}'
}
# checks for required external tools
function check_dependencies { # takes 1 argument
DEPS=("${1}");
ERROR=false
for DEP in ${DEPS[@]}; do
if echo "${DEP}" | grep '/' >/dev/null 2>&1 && [[ ! -x "${DEP}" ]]; then
ERROR=true
elif ! hash "${DEP}" >/dev/null 2>&1; then
ERROR=true
fi
done
if [[ "${ERROR}" == true ]]; then
echo "Error: Unable to find command '${DEP#$BASE_DIR/}'." >&2
return 1
fi
}
# builds document(s) from a single source file
function build_source {
local SOURCE YAML META_Title META_Project META_Project_URL META_Project_Version META_Language META_Language_Code META_Text_Encoding META_Authors META_Copyright META_License META_License_URL
SOURCE="${1}"
BASE="${SOURCE%.markdown}"
# check for yaml header
META=false
if head -n 1 "${SOURCE}" | grep '^---$' >/dev/null 2>&1; then
META=true
fi
if [[ "${META}" == true ]]; then
# split yaml and markdown
awk '{ drop = 0; } /^---$/ { if (NR==1) { drop = 1 } else if (NR>1) { exit } else { drop = 0; next } } drop == 0 { print }' "${SOURCE}" > "${BASE}.yaml"
mv "${SOURCE}" "${BASE}-temp.markdown"
tail -n +$(wc -l "${BASE}.yaml" | awk '{ print $1+3 }') "${BASE}-temp.markdown" > "${SOURCE}"
rm -f "${BASE}-temp.markdown"
# parse yaml
eval $(parse_yaml "${BASE}.yaml" "META_")
fi
# generate document using template
DOCUMENT_Content=$("${MARKDOWN}" -fdlextra -ffencedcode -ffootnote -fgithubtags "${SOURCE}")
DOCUMENT_Content="${DOCUMENT_Content:-}"
VAR_Content="{{ document.content }}"
touch "${BASE}.html"
while IFS='' read TEMPLATE || [[ -n "${TEMPLATE}" ]]; do
echo "${TEMPLATE//$VAR_Content/$DOCUMENT_Content}" \
| sed -E 's:{{[ ]*meta\.language-code[ ]*}}:'"${META_Language_Code:-}"':g' \
| sed -E 's:{{[ ]*meta\.text-encoding[ ]*}}:'"${META_Text_Encoding:-}"':g' \
| sed -E 's:{{[ ]*meta\.authors[ ]*}}:'"${META_Authors:-}"':g' \
| sed -E 's:{{[ ]*meta\.project[ ]*}}:'"${META_Project:-}"':g' \
| sed -E 's:{{[ ]*meta\.title[ ]*}}:'"${META_Title:-}"':g' \
| sed -E 's:{{[ ]*base\.relpath[ ]*}}:'"${BASE_RELPATH:-}"':g' \
>> "${BASE}.html"
done < "${TEMPLATE_DIR}/document.html"
# clean up
rm -f "${BASE}.markdown"
rm -f "${BASE}.yaml"
}
# base directory (absolute)
cd "${BASE_DIR}"
# check deps
check_dependencies "${DEPS[@]}" || exit 1
# parse config
if [[ -e "${CONFIG}" ]]; then
eval $(parse_yaml "${CONFIG}" "CONFIG_")
else
echo "Configuration file not found." >&2
exit 1
fi
# check template
TEMPLATE_DIR+="/${CONFIG_Template}"
if [[ ! -d "${TEMPLATE_DIR}" ]] || [[ ! -e "${TEMPLATE_DIR}/document.html" ]]; then
echo "Template '${CONFIG_Template}' not found." >&2
exit 1
fi
# clean
rm -rf "${DOCS_DIR}"/*
# build
cp -R "${TEMPLATE_DIR}"/fonts "${TEMPLATE_DIR}"/style "${SOURCE_DIR}"/* "${DOCS_DIR}"/
SOURCES=($(find "${DOCS_DIR}" | sed 's/^\.\///' | grep -i '.markdown'))
for SOURCE in ${SOURCES[@]}; do
BASE_RELPATH="${SOURCE#$DOCS_DIR/}" # strip abs prefix
BASE_RELPATH="${BASE_RELPATH//[^\/]}" # leave only slashes
BASE_RELPATH="${BASE_RELPATH//[\/]/../}" # slashes to dirs
build_source "${SOURCE}"
done
# done
exit 0