2021-09-22 14:52:03 +02:00
|
|
|
|
#!/usr/bin/env bash
|
2021-02-28 03:33:58 +01:00
|
|
|
|
# template.sh - basic templating engine
|
|
|
|
|
|
|
|
|
|
# render(array, template_file)
|
|
|
|
|
function render() {
|
2022-06-25 11:03:39 +02:00
|
|
|
|
local template="$(cat "$2" | sed 's/\&/<2F>UwU<77>/g')"
|
2021-02-28 03:33:58 +01:00
|
|
|
|
local -n ref=$1
|
2021-02-28 18:27:06 +01:00
|
|
|
|
local tmp=$(mktemp)
|
2021-02-28 03:33:58 +01:00
|
|
|
|
for key in ${!ref[@]}; do
|
2022-06-20 04:56:54 +02:00
|
|
|
|
if [[ "$key" == "_"* ]]; then # iter mode
|
2022-06-21 04:00:17 +02:00
|
|
|
|
local value=''
|
|
|
|
|
subtemplate=$(mktemp)
|
|
|
|
|
subtemplate_tmp=$(mktemp)
|
2022-06-25 11:03:39 +02:00
|
|
|
|
echo "$template" | sed 's/\&/<2F>UwU<77>/g' | grep "{{start $key}}" -A99999 | grep "{{end $key}}" -B99999 | tr '\n' $'\01' > "$subtemplate"
|
2022-06-21 04:00:17 +02:00
|
|
|
|
|
|
|
|
|
echo 's'$'\02''\{\{start '"$key"'\}\}.*\{\{end '"$key"'\}\}'$'\02''\{\{'"$key"'\}\}'$'\02'';' >> "$tmp"
|
|
|
|
|
|
|
|
|
|
local -n asdf=${ref[$key]}
|
|
|
|
|
|
|
|
|
|
for j in ${!asdf[@]}; do
|
|
|
|
|
local -n fdsa=_${asdf[$j]}
|
|
|
|
|
|
|
|
|
|
for i in ${!fdsa[@]}; do
|
2022-06-25 09:23:16 +02:00
|
|
|
|
echo 's'$'\02''\{\{.'"$i"'\}\}'$'\02'''"${fdsa[$i]}"''$'\02'';' | tr '\n' $'\01' | sed 's/'$'\02'';'$'\01''/'$'\02'';/g' >> "$subtemplate_tmp"
|
2022-06-20 04:56:54 +02:00
|
|
|
|
done
|
2022-06-21 04:00:17 +02:00
|
|
|
|
|
|
|
|
|
echo 's'$'\02''\{\{start '"$key"'\}\}'$'\02'$'\02' >> "$subtemplate_tmp"
|
|
|
|
|
echo 's'$'\02''\{\{end '"$key"'\}\}'$'\02'$'\02' >> "$subtemplate_tmp"
|
|
|
|
|
|
2022-06-25 09:23:16 +02:00
|
|
|
|
value+="$(cat "$subtemplate" | tr '\n' $'\01' | sed -E -f "$subtemplate_tmp" | tr $'\01' '\n')"
|
2022-06-21 04:00:17 +02:00
|
|
|
|
rm "$subtemplate_tmp"
|
2022-06-20 04:56:54 +02:00
|
|
|
|
done
|
2022-06-21 04:00:17 +02:00
|
|
|
|
|
|
|
|
|
echo 's'$'\02''\{\{'"$key"'\}\}'$'\02'''"$value"''$'\02'';' >> "$tmp"
|
2022-06-20 04:56:54 +02:00
|
|
|
|
rm "$subtemplate"
|
|
|
|
|
elif [[ "${ref[$key]}" != "" ]]; then
|
2021-06-23 00:40:35 +02:00
|
|
|
|
local value="$(html_encode "${ref[$key]}" | sed -E 's/\&/<2F>UwU<77>/g')"
|
2022-06-21 04:00:17 +02:00
|
|
|
|
echo 's'$'\02''\{\{\.'"$key"'\}\}'$'\02'''"$value"''$'\02'';' >> "$tmp"
|
2021-06-23 00:40:35 +02:00
|
|
|
|
else
|
2022-06-21 04:00:17 +02:00
|
|
|
|
echo 's'$'\02''\{\{\.'"$key"'\}\}'$'\02'$'\02'';' >> "$tmp"
|
2021-06-23 00:40:35 +02:00
|
|
|
|
fi
|
2021-02-28 03:33:58 +01:00
|
|
|
|
done
|
2022-06-21 04:00:17 +02:00
|
|
|
|
|
2022-06-25 09:23:16 +02:00
|
|
|
|
cat "$tmp" | tr '\n' $'\01' | sed 's/'$'\02'';'$'\01''/'$'\02'';/g' > "${tmp}_"
|
2022-06-21 04:00:17 +02:00
|
|
|
|
template="$(tr '\n' $'\01' <<< "$template" | sed -E -f "${tmp}_" | tr $'\01' '\n')"
|
2021-02-28 03:33:58 +01:00
|
|
|
|
sed -E 's/<2F>UwU<77>/\&/g' <<< "$template"
|
2021-02-28 18:27:06 +01:00
|
|
|
|
rm "$tmp"
|
2021-02-28 03:33:58 +01:00
|
|
|
|
}
|
2021-02-28 12:39:46 +01:00
|
|
|
|
|
|
|
|
|
# render_unsafe(array, template_file)
|
|
|
|
|
function render_unsafe() {
|
|
|
|
|
local template="$(cat "$2")"
|
|
|
|
|
local -n ref=$1
|
2021-02-28 18:27:06 +01:00
|
|
|
|
local tmp=$(mktemp)
|
2021-02-28 12:39:46 +01:00
|
|
|
|
for key in ${!ref[@]}; do
|
2022-06-20 04:56:54 +02:00
|
|
|
|
if [[ "$key" == "_"* ]]; then # iter mode
|
|
|
|
|
# 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')"
|
|
|
|
|
done
|
|
|
|
|
echo 's/\{\{'"$key"'\}\}/'"$value"'/g' >> "$tmp"
|
|
|
|
|
else
|
|
|
|
|
local value="$(xxd -ps <<< "${ref[$key]}" | tr -d '\n' | sed -E 's/../\\x&/g')"
|
|
|
|
|
echo 's/\{\{\.'"$key"'\}\}/'"$value"'/g' >> "$tmp"
|
2022-06-21 04:00:17 +02:00
|
|
|
|
fi
|
2021-02-28 12:39:46 +01:00
|
|
|
|
done
|
|
|
|
|
|
2021-02-28 18:27:06 +01:00
|
|
|
|
sed -E -f "$tmp" <<< "$template"
|
|
|
|
|
rm "$tmp"
|
2021-02-28 12:39:46 +01:00
|
|
|
|
}
|
2022-06-20 04:56:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# mmmm this should be a library because i am so much copying those later
|
|
|
|
|
# _nested_random
|
|
|
|
|
function _nested_random() {
|
|
|
|
|
dd if=/dev/urandom bs=1 count=16 status=none | xxd -p
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# nested_declare(ref)
|
|
|
|
|
function nested_declare() {
|
|
|
|
|
declare -g -a $1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# nested_add(ref, array)
|
|
|
|
|
function nested_add() {
|
|
|
|
|
local nested_id=$(_nested_random)
|
2022-06-25 09:00:25 +02:00
|
|
|
|
declare -n nested_ref=$2
|
|
|
|
|
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]}"
|
|
|
|
|
done
|
|
|
|
|
|
2022-06-20 04:56:54 +02:00
|
|
|
|
local -n ref=$1
|
|
|
|
|
ref+=("$nested_id")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# nested_get(ref, i)
|
|
|
|
|
function nested_get() {
|
|
|
|
|
local -n ref=$1
|
|
|
|
|
declare -g -n res=_${ref[$2]}
|
|
|
|
|
}
|