Use template for emoji subs; write env_subst()

This commit is contained in:
Jaidyn Ann 2024-03-28 01:00:23 -05:00
parent fef4d8f4bc
commit 89120ccb73

View File

@ -80,6 +80,8 @@ POST_TEMPLATE='
'
EMOJI_TEMPLATE='<img class="emoji" src="$EMOJI_URL" alt="$EMOJI_SHORTCODE">'
replace_emojis() {
local post_data="$1"
local emojis="$(echo "$post_data" | jq -r '.emojis[]|(.url + "\t" + .shortcode)')"
@ -89,15 +91,25 @@ replace_emojis() {
cat > "$temp"
for line in $emojis; do
local url="$(echo "$line" | awk '{print $1}')"
local shortcode="$(echo "$line" | awk '{print $2}')"
sed -i "s%:${shortcode}:%<img class=\"emoji\" src=\"$url\" alt=\"${shortcode}\">%g" \
"$temp"
local EMOJI_URL="$(echo "$line" | awk '{print $1}')"
local EMOJI_SHORTCODE="$(echo "$line" | awk '{print $2}')"
local value="$(env_subst "$EMOJI_TEMPLATE")"
sed -i "s%:${EMOJI_SHORTCODE}:%${value}%g" "$temp"
done
cat "$temp"
rm "$temp"
}
prep_template() {
local template="$1"
echo "$template" \
| sed 's%\"%\\\"%g'
}
env_subst() {
local template="$1"
eval "echo \"$(prep_template "$template")\""
}
render_post() {
local post_data="$1"
@ -108,8 +120,6 @@ render_post() {
local POST_URL="$(echo "$post_data" | jq -r .url)"
local POST_DATE="$(echo "$post_data" | jq -r .created_at)"
local POST_CONTENT="$(echo "$post_data" | jq -r .content | replace_emojis "$post_data")"
eval "echo \"$(echo "$POST_TEMPLATE" | sed 's%\"%\\\"%g')\""
env_subst "$POST_TEMPLATE"
}