Make replace_emojis() a for-loop, not recursive

… this is, in shell, a bit more readable. :P
This commit is contained in:
Jaidyn Ann 2024-03-28 00:49:26 -05:00
parent bacd6ee2f9
commit fef4d8f4bc

View File

@ -82,29 +82,20 @@ POST_TEMPLATE='
replace_emojis() { replace_emojis() {
local post_data="$1" local post_data="$1"
local emojis="$2" local emojis="$(echo "$post_data" | jq -r '.emojis[]|(.url + "\t" + .shortcode)')"
local temp="$(mktemp)"
local IFS="
"
cat > "$temp"
# So, this function is recusive; in hindsight, it didnt have to be. oh well! for line in $emojis; do
if test -n "$post_data" -a -z "$emojis"; then local url="$(echo "$line" | awk '{print $1}')"
# Init case; start subsitutions in next level of recursion local shortcode="$(echo "$line" | awk '{print $2}')"
replace_emojis \ sed -i "s%:${shortcode}:%<img class=\"emoji\" src=\"$url\" alt=\"${shortcode}\">%g" \
"" \ "$temp"
"$(echo "$post_data" \ done
| jq -r '.emojis[]|(.url + "\t" + .shortcode)')" cat "$temp"
elif test -n "$emojis"; then rm "$temp"
# Substitute the current emoji (first line of $emojis)
local url="$(echo "$emojis" | head -1 | awk '{print $1}')"
local shortcode="$(echo "$emojis" | head -1 | awk '{print $2}')"
sed "s%:${shortcode}:%<img src='$url'>%g" \
| replace_emojis \
"" \
"$(echo "$emojis" | tail +2)"
else
# End-case; $emojis is empty, so our job is done.
# Just print the results.
cat
fi
} }