2021-02-28 03:33:58 +01:00
|
|
|
|
#!/bin/bash
|
|
|
|
|
# template.sh - basic templating engine
|
|
|
|
|
|
|
|
|
|
# render(array, template_file)
|
|
|
|
|
function render() {
|
|
|
|
|
local template="$(cat "$2")"
|
|
|
|
|
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
|
2021-06-23 00:40:35 +02:00
|
|
|
|
if [[ "${ref[$key]}" != "" ]]; then
|
|
|
|
|
local value="$(html_encode "${ref[$key]}" | sed -E 's/\&/<2F>UwU<77>/g')"
|
|
|
|
|
echo 's/\{\{\.'"$key"'\}\}/'"$value"'/g' >> "$tmp"
|
|
|
|
|
else
|
|
|
|
|
echo 's/\{\{\.'"$key"'\}\}//g' >> "$tmp"
|
|
|
|
|
fi
|
2021-02-28 03:33:58 +01:00
|
|
|
|
done
|
2021-02-28 18:27:06 +01:00
|
|
|
|
template="$(sed -E -f "$tmp" <<< "$template")"
|
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
|
2021-02-28 13:45:29 +01:00
|
|
|
|
local value="$(xxd -ps <<< "${ref[$key]}" | tr -d '\n' | sed -E 's/.{2}/\\x&/g')"
|
2021-02-28 18:27:06 +01:00
|
|
|
|
echo 's/\{\{\.'"$key"'\}\}/'"$value"'/g' >> "$tmp"
|
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
|
|
|
|
}
|