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
|
|
|
|
|
for key in ${!ref[@]}; do
|
|
|
|
|
local value="$(html_encode "${ref[$key]}" | sed -E 's/\&/<2F>UwU<77>/g')"
|
|
|
|
|
template="$(sed -E 's/\{\{\.'"$key"'\}\}/'"$value"'/g' <<< "$template")"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
sed -E 's/<2F>UwU<77>/\&/g' <<< "$template"
|
|
|
|
|
}
|
2021-02-28 12:39:46 +01:00
|
|
|
|
|
|
|
|
|
# render_unsafe(array, template_file)
|
|
|
|
|
function render_unsafe() {
|
|
|
|
|
local template="$(cat "$2")"
|
|
|
|
|
local -n ref=$1
|
|
|
|
|
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 12:39:46 +01:00
|
|
|
|
template="$(sed -E 's/\{\{\.'"$key"'\}\}/'"$value"'/g' <<< "$template")"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
echo "$template"
|
|
|
|
|
}
|