specifications

Specification and standard documents
git clone git://git.finwo.net/misc/specifications
Log | Files | Refs | README | LICENSE

commit 7d227d6d938d8e58dfb60813e7f5ddc3ed4000e8
parent c4bcd8ef5e803fe9c3a0393e3bbfecc7060d6ace
Author: finwo <finwo@pm.me>
Date:   Mon, 10 May 2021 10:03:07 +0200

Made template tools track upstream

Diffstat:
Mbuild.sh | 8++++----
Dscript/ini.sh | 55-------------------------------------------------------
Dscript/template.sh | 93-------------------------------------------------------------------------------
Atool/ini.sh | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atool/template.sh | 101+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 161 insertions(+), 152 deletions(-)

diff --git a/build.sh b/build.sh @@ -14,14 +14,14 @@ find ${ORGDIR} -maxdepth 1 -type d -regextype posix-egrep -regex '.*[0-9]{4}' | declare -A DATA while IFS='=' read key value; do DATA["$key"]="$value" - done <<< "$(script/ini.sh ${spec}/data.ini)" + done <<< "$(tool/ini.sh ${spec}/data.ini)" # Show we're actually working echo " ${DATA[identifier]}: ${DATA[title]}" # Render readme & html entries - script/template.sh -c ${spec}/data.ini src/html/index.html.entry >> docs/index.html - script/template.sh -c ${spec}/data.ini src/markdown/README.md.entry >> README.md + tool/template.sh -c ${spec}/data.ini src/html/index.html.entry >> docs/index.html + tool/template.sh -c ${spec}/data.ini src/markdown/README.md.entry >> README.md # Skip rendering already-existing files if [ -f "${SPECDIR}/${DATA[identifier]}.pdf" ]; then @@ -42,7 +42,7 @@ find ${ORGDIR} -maxdepth 1 -type d -regextype posix-egrep -regex '.*[0-9]{4}' | ;; txt) # Include render data - script/template.sh -c ${spec}/data.ini ${filename} > ${filename}.rendered + tool/template.sh -c ${spec}/data.ini ${filename} > ${filename}.rendered # Font: Courier # 10pt font diff --git a/script/ini.sh b/script/ini.sh @@ -1,55 +0,0 @@ -#!/usr/bin/env bash -shopt -s extglob - -readonly PROGNAME=$(basename $0) -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" - -# Usage string -usage=" -Usage: - ${PROGNAME} ini-file [token] -" - -# No file = no data -inifile="${1}" -if [[ ! -f "$inifile" ]]; then - exit -fi - -# Process the file line-by-line -SECTION= -while read line; do - - # Remove surrounding whitespace - line=${line##*( )} # From the beginning - line=${line%%*( )} # From the end - - # Remove comments and empty lines - if [[ "${line:0:1}" == ';' ]] || [[ "${#line}" == 0 ]]; then - continue - fi - - # Handle section markers - if [[ "${line:0:1}" == "[" ]]; then - SECTION=$(echo $line | sed -e 's/\[\(.*\)\]/\1/') - SECTION=${SECTION##*( )} - SECTION=${SECTION%%*( )} - SECTION="${SECTION}." - continue - fi - - # Output found variable - NAME=${line%%=*} - NAME=${NAME%%*( )} - VALUE=${line##*=} - VALUE=${VALUE##*( )} - - # Output searched or all - if [[ -z "${2}" ]]; then - echo "${SECTION}${NAME}=${VALUE}" - fi - if [[ "${SECTION}${NAME}" = "${2}" ]]; then - echo "${VALUE}" - fi - -done < "${inifile}" diff --git a/script/template.sh b/script/template.sh @@ -1,93 +0,0 @@ -#!/usr/bin/env bash - -# Very simple templating system that replaces {{VAR}} by the value of $VAR. -# Supports default values by writting {{VAR=value}} in the template. - -# Replaces all {{VAR}} by the $VAR value in a template file and outputs it - -readonly PROGNAME=$(basename $0) -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" - -# Usage string -usage=" -Usage: - ${PROGNAME} -h|--help - ${PROGNAME} [-c|--config config_file] [-p|--partials partials_dir] template_file [...template_file] - -Options: - -h --help Show this help text - -c --config <path> Specify config file - -p --partials <path> Specify partials directory -" - -# Declare storage -declare -A TOKENS -declare -A TEMPLATES -declare -A PARTIALS -PARTIALARGS= -INDEX=0 - -# Parse arguments -while [ "$#" -gt 0 ]; do - case "$1" in - -h|--help) - echo "$usage" - exit 0 - ;; - -c|--config) - shift - PARTIALARGS="${PARTIALARGS} -c ${1}" - if [[ -f "${1}" ]]; then - while IFS='=' read key value; do - TOKENS["$key"]="$value" - done <<< "$(${DIR}/ini.sh ${1})" - fi - ;; - -p|--partials) - shift - PARTIALARGS="${PARTIALARGS} -p ${1}" - if [[ -d "${1}" ]]; then - while IFS=':' read name filename; do - PARTIALS["$name"]="${filename}" - done <<< "$(find "${1}" -type f -printf "%P:%p\n")" - fi - ;; - *) - if [[ -f "${1}" ]]; then - TEMPLATES[$INDEX]="${1}" - INDEX=$(( $INDEX + 1 )) - fi - ;; - esac - shift -done - -# Cancel if we have no config files -if [ "${INDEX}" -eq 0 ]; then - echo "ERROR: You need to specify a template file" >&2 - echo "$usage" - exit 1 -fi - -# Handle all given templates -for templatefile in "${TEMPLATES[@]}"; do - CONTENT=$(cat $templatefile); - - # Replace tokens - for token in "${!TOKENS[@]}"; do - CONTENT=${CONTENT//"{{$token}}"/"${TOKENS[$token]}"} - done - - # Handle partials - for partial in "${!PARTIALS[@]}"; do - if [[ "${CONTENT}" == *"{{>${partial}}}"* ]]; then - PARTIALCONTENT="$(${DIR}/${PROGNAME} ${PARTIALARGS} ${PARTIALS[$partial]})" - CONTENT=${CONTENT//"{{>$partial}}"/"${PARTIALCONTENT}"} - fi - done - - # Output the result - echo -e "$CONTENT" -done - - diff --git a/tool/ini.sh b/tool/ini.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# c90839f9-835f-4f11-b6cd-86d17220195d +shopt -s extglob + +readonly PROGNAME=$(basename $0) +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +# Usage string +usage=" +Usage: + ${PROGNAME} ini-file [token] +" + +# No file = no data +inifile="${1}" +if [[ ! -f "$inifile" ]]; then + exit +fi + +# Process the file line-by-line +SECTION= +while read line; do + + # Remove surrounding whitespace + line=${line##*( )} # From the beginning + line=${line%%*( )} # From the end + + # Remove comments and empty lines + if [[ "${line:0:1}" == ';' ]] || [[ "${#line}" == 0 ]]; then + continue + fi + + # Handle section markers + if [[ "${line:0:1}" == "[" ]]; then + SECTION=$(echo $line | sed -e 's/\[\(.*\)\]/\1/') + SECTION=${SECTION##*( )} + SECTION=${SECTION%%*( )} + SECTION="${SECTION}." + continue + fi + + # Output found variable + NAME=${line%%=*} + NAME=${NAME%%*( )} + VALUE=${line##*=} + VALUE=${VALUE##*( )} + + # Output searched or all + if [[ -z "${2}" ]]; then + echo "${SECTION}${NAME}=${VALUE}" + fi + if [[ "${SECTION}${NAME}" = "${2}" ]]; then + echo "${VALUE}" + fi + +done < "${inifile}" diff --git a/tool/template.sh b/tool/template.sh @@ -0,0 +1,101 @@ +#!/usr/bin/env bash +# e660cc6c-638a-4f9b-9527-ff96a19bbeed + +# Very simple templating system that replaces {{VAR}} by the value of $VAR. +# Supports default values by writting {{VAR=value}} in the template. + +# Replaces all {{VAR}} by the $VAR value in a template file and outputs it + +readonly PROGNAME=$(basename $0) +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +# Usage string +usage=" +Usage: + ${PROGNAME} -h|--help + ${PROGNAME} --update + ${PROGNAME} [-c|--config config_file] [-p|--partials partials_dir] template_file [...template_file] + +Options: + -h --help Show this help text + --update Perform a self-update + -c --config <path> Specify config file + -p --partials <path> Specify partials directory +" + +# Declare storage +declare -A TOKENS +declare -A TEMPLATES +declare -A PARTIALS +PARTIALARGS= +INDEX=0 + +# Parse arguments +while [ "$#" -gt 0 ]; do + case "$1" in + -h|--help) + echo "$usage" + exit 0 + ;; + --update) + curl -L -H 'Cache-Control: no-cache' https://raw.githubusercontent.com/finwo/ini-tools/master/template.sh > "${DIR}/${PROGNAME}" + exit $? + ;; + -c|--config) + shift + PARTIALARGS="${PARTIALARGS} -c ${1}" + if [[ -f "${1}" ]]; then + while IFS='=' read key value; do + if [ -z "$key" ]; then continue; fi + TOKENS["$key"]="$value" + done <<< "$(${DIR}/ini.sh ${1})" + fi + ;; + -p|--partials) + shift + PARTIALARGS="${PARTIALARGS} -p ${1}" + if [[ -d "${1}" ]]; then + while IFS=':' read name filename; do + PARTIALS["$name"]="${filename}" + done <<< "$(find "${1}" -type f -printf "%P:%p\n")" + fi + ;; + *) + if [[ -f "${1}" ]]; then + TEMPLATES[$INDEX]="${1}" + INDEX=$(( $INDEX + 1 )) + fi + ;; + esac + shift +done + +# Cancel if we have no config files +if [ "${INDEX}" -eq 0 ]; then + echo "ERROR: You need to specify a template file" >&2 + echo "$usage" + exit 1 +fi + +# Handle all given templates +for templatefile in "${TEMPLATES[@]}"; do + CONTENT=$(cat $templatefile); + + # Replace tokens + for token in "${!TOKENS[@]}"; do + CONTENT=${CONTENT//"{{$token}}"/"${TOKENS[$token]}"} + done + + # Handle partials + for partial in "${!PARTIALS[@]}"; do + if [[ "${CONTENT}" == *"{{>${partial}}}"* ]]; then + PARTIALCONTENT="$(${DIR}/${PROGNAME} ${PARTIALARGS} ${PARTIALS[$partial]})" + CONTENT=${CONTENT//"{{>$partial}}"/"${PARTIALCONTENT}"} + fi + done + + # Output the result + echo -e "$CONTENT" +done + +