From 14126f301ecf79945302559d50cf387f3fde1d80 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Thu, 28 Mar 2024 18:23:54 -0500 Subject: [PATCH] =?UTF-8?q?Support=20for=20recursively=20rendering=20a=20p?= =?UTF-8?q?ost=E2=80=99s=20reponses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fedi2html.sh | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/fedi2html.sh b/fedi2html.sh index 14eb917..42d3255 100755 --- a/fedi2html.sh +++ b/fedi2html.sh @@ -67,10 +67,6 @@ fetch_post() { POST_TEMPLATE=' - - - -
@@ -86,9 +82,11 @@ POST_TEMPLATE='
$POST_ATTACHMENTS
+$tree_level¤ +
+ $POST_RESPONSES +
- - ' @@ -191,16 +189,43 @@ env_subst() { # Given a note’s JSON, render it as HTML. # The most important part of the script! -# render_post $post_data +# render_post $post_data $context_data $tree_level render_post() { local post_data="$1" + local context_data="$2" + local POST_TREE_LEVEL="$3" + local ACCOUNT_URL="$(echo "$post_data" | jq -r .account.url)" local ACCOUNT_ID="$(echo "$post_data" | jq -r .account.fqn)" - local ACCOUNT_NAME="$(echo "$post_data" | jq -r .account.display_name | replace_emojis "$post_data")" + local ACCOUNT_NAME="$(echo "$post_data" | jq -r .account.display_name | replace_emojis "$(echo "$post_data" | jq -r '.account')")" local ACCOUNT_AVATAR="$(echo "$post_data" | jq -r .account.avatar)" 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")" local POST_ATTACHMENTS="$(media_attachments "$post_data")" + local POST_RESPONSES="$(render_context "$post_data" "$context_data" "$tree_level")" env_subst "$POST_TEMPLATE" } + + +# Render a post’s responses one-by-one and recursively. +# Each branch of the response tree will be rendered completely before proceeding +# to the next. +# render_context $post_data $context_data $tree_level +render_context() { + + local post_data="$1" + local context_data="$2" + local level="$3" + if test -z "$level"; then level=0; fi + + local id="$(echo "$post_data" | jq -r '.id')" + local responses="$(echo "$context_data" | jq -cr '.descendants[]' | grep "in_reply_to_id.*$id")" + local IFS=" +" + + for response in $responses; do + render_post "$response" "$context_data" "$(expr "$level" + 1)" + done +} +