From fef4d8f4bc93734f6474209fd662a438217c7316 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:49:26 -0500 Subject: [PATCH] Make replace_emojis() a for-loop, not recursive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … this is, in shell, a bit more readable. :P --- fedi2html.sh | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/fedi2html.sh b/fedi2html.sh index 1dcb364..0525396 100755 --- a/fedi2html.sh +++ b/fedi2html.sh @@ -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 didn’t 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}:%%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}:%\"${shortcode}\"%g" \ + "$temp" + done + cat "$temp" + rm "$temp" }