html_encode: support for data from stdin

merge-requests/2/head
Lauren Liberda 2021-06-23 00:40:35 +02:00
parent c73b8910d0
commit 911eca714c
2 changed files with 11 additions and 3 deletions

View File

@ -39,7 +39,11 @@ function post_dump() {
# html_encode(string)
function html_encode() {
sed 's/\&/\&amp;/g;s/</\&#60;/g;s/>/\&#62;/g;s/%/\&#37;/g;s/\//\&#47;/g;s/\\/\&#92;/g;s/'"'"'/\&#39;/g;s/"/\&#34;/g;s/`/\&#96;/g;s/?/\&#63;/g;' <<< "$1"
if [[ "$1" == "" ]]; then
sed 's/\&/\&amp;/g;s/</\&#60;/g;s/>/\&#62;/g;s/%/\&#37;/g;s/\//\&#47;/g;s/\\/\&#92;/g;s/'"'"'/\&#39;/g;s/"/\&#34;/g;s/`/\&#96;/g;s/?/\&#63;/g;'
else
sed 's/\&/\&amp;/g;s/</\&#60;/g;s/>/\&#62;/g;s/%/\&#37;/g;s/\//\&#47;/g;s/\\/\&#92;/g;s/'"'"'/\&#39;/g;s/"/\&#34;/g;s/`/\&#96;/g;s/?/\&#63;/g;' <<< "$1"
fi
}
# url_encode(string)

View File

@ -7,8 +7,12 @@ function render() {
local -n ref=$1
local tmp=$(mktemp)
for key in ${!ref[@]}; do
local value="$(html_encode "${ref[$key]}" | sed -E 's/\&/<2F>UwU<77>/g')"
echo 's/\{\{\.'"$key"'\}\}/'"$value"'/g' >> "$tmp"
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
done
template="$(sed -E -f "$tmp" <<< "$template")"
sed -E 's/<2F>UwU<77>/\&/g' <<< "$template"