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() {
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!
if test -n "$post_data" -a -z "$emojis"; then
# Init case; start subsitutions in next level of recursion
replace_emojis \
"" \
"$(echo "$post_data" \
| jq -r '.emojis[]|(.url + "\t" + .shortcode)')"
elif test -n "$emojis"; then
# 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
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"
done
cat "$temp"
rm "$temp"
}