Compare commits

...

2 commits

Author SHA1 Message Date
Dominique Liberda
e2dbc9e4bd * template nesting, fixes 2022-11-29 11:06:45 -05:00
Dominique Liberda
0c32e2fe00 * new template functions 2022-11-29 06:46:50 -05:00

View file

@ -1,50 +1,75 @@
#!/usr/bin/env bash
# template.sh - basic templating engine
# render(array, template_file)
# nightmare fuel
# render(array, template_file, recurse)
function render() {
local template="$(cat "$2" | tr -d $'\01'$'\02' | sed 's/\&/<2F>UwU<77>/g')"
if [[ "$3" != true ]]; then
local template="$(cat "$2" | tr -d $'\01'$'\02' | sed 's/\&/<2F>UwU<77>/g')"
else
local template="$(cat "$2")"
fi
local -n ref=$1
local tmp=$(mktemp)
local key
for key in ${!ref[@]}; do
if [[ "$key" == "_"* ]]; then # iter mode
local value=''
subtemplate=$(mktemp)
subtemplate_tmp=$(mktemp)
echo "$template" | sed 's/\&/<2F>UwU<77>/g' | grep "{{start $key}}" -A99999 | grep "{{end $key}}" -B99999 | tr -d $'\01'$'\02' | tr '\n' $'\01' > "$subtemplate"
local subtemplate=$(mktemp)
local subtemplate_tmp=$(mktemp)
echo "$template" | grep "{{start $key}}" -A99999 | grep "{{end $key}}" -B99999 | tr '\n' $'\01' > "$subtemplate"
echo 's'$'\02''\{\{start '"$key"'\}\}.*\{\{end '"$key"'\}\}'$'\02''\{\{'"$key"'\}\}'$'\02'';' >> "$tmp"
local -n asdf=${ref[$key]}
local j
local value=''
for j in ${!asdf[@]}; do
local -n fdsa=_${asdf[$j]}
for i in ${!fdsa[@]}; do
echo 's'$'\02''\{\{.'"$i"'\}\}'$'\02'''"${fdsa[$i]}"''$'\02''g;' | tr '\n' $'\01' | sed -E 's/'$'\02'';'$'\01''/'$'\02'';/g;s/'$'\02''g;'$'\01''/'$'\02''g;/g' >> "$subtemplate_tmp"
done
value+="$(render fdsa "$subtemplate" true | sed -E 's'$'\02''\{\{start '"$key"'\}\}'$'\02'$'\02'';s'$'\02''\{\{end '"$key"'\}\}'$'\02'$'\02')"
echo 's'$'\02''\{\{start '"$key"'\}\}'$'\02'$'\02' >> "$subtemplate_tmp"
echo 's'$'\02''\{\{end '"$key"'\}\}'$'\02'$'\02' >> "$subtemplate_tmp"
value+="$(cat "$subtemplate" | tr '\n' $'\01' | sed -E -f "$subtemplate_tmp" | tr $'\01' '\n')"
rm "$subtemplate_tmp"
done
echo 's'$'\02''\{\{'"$key"'\}\}'$'\02'''"$value"''$'\02'';' >> "$tmp"
rm "$subtemplate"
elif [[ "$key" == "@"* && "${ref[$key]}" != '' ]]; then
local value="$(sed -E 's/\&/<2F>UwU<77>/g' <<< "${ref[$key]}")"
echo 's'$'\02''\{\{\'"$key"'\}\}'$'\02'''"$value"''$'\02''g;' >> "$tmp"
elif [[ "$key" == '?'* ]]; then
local _key="\\?${key/?/}"
local subtemplate=$(mktemp)
echo 's'$'\02''\{\{start '"$_key"'\}\}(.*)\{\{end '"$_key"'\}\}'$'\02''\1'$'\02'';' >> "$subtemplate"
cat <<< $(cat "$subtemplate" "$tmp") > "$tmp" # call that cat abuse
elif [[ "${ref[$key]}" != "" ]]; then
local value="$(html_encode "${ref[$key]}" | sed -E 's/\&/<2F>UwU<77>/g')"
echo "VALUE: ${ref[$key]}" > /dev/stderr
if [[ "$3" != true ]]; then
local value="$(echo "${ref[$key]}" | html_encode | sed -E 's/\&/<2F>UwU<77>/g')"
else
local value="$(echo "${ref[$key]}" | sed -E 's/\\\\/<2F>OwO<77>/g;s/\\//g;s/<2F>OwO<77>/\\/g' | html_encode | sed -E 's/\&/<2F>UwU<77>/g')"
fi
echo 's'$'\02''\{\{\.'"$key"'\}\}'$'\02'''"$value"''$'\02''g;' >> "$tmp"
else
echo 's'$'\02''\{\{\.'"$key"'\}\}'$'\02'$'\02''g;' >> "$tmp"
fi
done
cat "$tmp" | tr '\n' $'\01' | sed -E 's/'$'\02'';'$'\01''/'$'\02'';/g;s/'$'\02''g;'$'\01''/'$'\02''g;/g' > "${tmp}_"
template="$(tr '\n' $'\01' <<< "$template" | sed -E -f "${tmp}_" | tr $'\01' '\n')"
sed -E 's/<2F>UwU<77>/\&/g' <<< "$template"
rm "$tmp"
if [[ "$3" != true ]]; then # are we recursing?
cat "$tmp" | tr '\n' $'\01' | sed -E 's/'$'\02'';'$'\01''/'$'\02'';/g;s/'$'\02''g;'$'\01''/'$'\02''g;/g' > "${tmp}_"
echo 's/\{\{start \?([a-zA-Z0-9_-]*[^}])\}\}.*\{\{end \?(\1)\}\}//g' >> "${tmp}_"
template="$(tr '\n' $'\01' <<< "$template" | sed -E -f "${tmp}_" | tr $'\01' '\n')"
sed -E 's/<2F>UwU<77>/\&/g' <<< "$template"
rm "$tmp" "${tmp}_"
else
tr '\n' $'\01' <<< "$template" | sed -E -f "$tmp" | tr $'\01' '\n'
rm "$tmp"
fi
}
# render_unsafe(array, template_file)
@ -57,12 +82,12 @@ function render_unsafe() {
# grep "start _test" -A99999 | grep "end _test" -B99999
local -n item_array=${ref[$key]}
local value
for ((i = 0; i < ${#item_array[@]}; i++)); do
value+="$(xxd -ps <<< "${item_array[$i]}" | tr -d '\n' | sed -E 's/../\\x&/g')"
for ((_i = 0; _i < ${#item_array[@]}; _i++)); do
value+="$(xxd -p <<< "${item_array[$_i]}" | tr -d '\n' | sed -E 's/../\\x&/g')"
done
echo 's/\{\{'"$key"'\}\}/'"$value"'/g' >> "$tmp"
else
local value="$(xxd -ps <<< "${ref[$key]}" | tr -d '\n' | sed -E 's/../\\x&/g')"
local value="$(xxd -p <<< "${ref[$key]}" | tr -d '\n' | sed -E 's/../\\x&/g')"
echo 's/\{\{\.'"$key"'\}\}/'"$value"'/g' >> "$tmp"
fi
done
@ -90,8 +115,8 @@ function nested_add() {
declare -g -A _$nested_id
# poor man's array copy
for i in ${!nested_ref[@]}; do
declare -g -A _$nested_id[$i]="${nested_ref[$i]}"
for k in ${!nested_ref[@]}; do
declare -g -A _$nested_id[$k]="${nested_ref[$k]}"
done
local -n ref=$1