summaryrefslogtreecommitdiff
path: root/lib/template.source.sh
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2023-03-15 00:06:25 +0200
committerPaul Buetow <paul@buetow.org>2023-03-15 00:06:25 +0200
commit365a195cc0362bfeb52ebc0e9cb958b0b455aac4 (patch)
treeb8357b1a1122953e66ba37421b017180d1ac50a5 /lib/template.source.sh
parent06310ae8a5649bb41f32d1c752f369dc5e077ee1 (diff)
initial templating support
Diffstat (limited to 'lib/template.source.sh')
-rw-r--r--lib/template.source.sh49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/template.source.sh b/lib/template.source.sh
new file mode 100644
index 0000000..1b0df9d
--- /dev/null
+++ b/lib/template.source.sh
@@ -0,0 +1,49 @@
+template::generate () {
+ log INFO 'Generating files from templates'
+ local -i num_tpl_files=0
+
+ while read -r tpl_path; do
+ if test -n "$CONTENT_FILTER" && ! $GREP -q "$CONTENT_FILTER" <<< "$tpl_path"; then
+ continue
+ fi
+ num_tpl_files=$(( num_tpl_files + 1 ))
+ template::_generate "$tpl_path" &
+ done < <(find "$CONTENT_BASE_DIR/gemtext" -type f -name \*.tpl)
+
+ wait
+ log INFO "Converted $num_tpl_files template files"
+}
+
+template::_generate () {
+ local -r tpl_path="$1"; shift
+ local -r tpl_dir="$(dirname "$tpl_path")"
+ local -r tpl="$(basename "$tpl_path")"
+ local -r dest="${tpl/.tpl/}"
+
+ cd "$tpl_dir" || log PANIC "Unable to chdir to $tpl_dir"
+ log INFO "$tpl_path -> $dest"
+
+ while IFS='' read -r line; do
+ case "$line" in
+ '<< '*)
+ template::_line "$line"
+ ;;
+ *)
+ echo "$line"
+ ;;
+ esac
+ done < "$tpl" > "$dest.tmp"
+
+ mv "$dest.tmp" "$dest"
+}
+
+template::_line () {
+ eval "${1/<< /}"
+}
+
+template::test () {
+ assert::equals "$(template::_line '<< echo -n foo')" 'foo'
+ assert::equals "$(template::_line '<< echo foo')" 'foo'
+ assert::equals "$(template::_line '<< $DATE --date @0 +%Y%m%d')" '19700101'
+ assert::equals "$(template::_line '<< echo "$AUTHOR"')" "$AUTHOR"
+}