From 89120ccb7322efcf6adb96ecb03f73ed8e0cc4a9 Mon Sep 17 00:00:00 2001
From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com>
Date: Thu, 28 Mar 2024 01:00:23 -0500
Subject: [PATCH] Use template for emoji subs; write env_subst()
---
fedi2html.sh | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/fedi2html.sh b/fedi2html.sh
index 0525396..7122a96 100755
--- a/fedi2html.sh
+++ b/fedi2html.sh
@@ -80,6 +80,8 @@ POST_TEMPLATE='
'
+EMOJI_TEMPLATE=''
+
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}:%%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"
}
-
-