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 +} +