Support for recursively rendering a post’s reponses

This commit is contained in:
Jaidyn Ann 2024-03-28 18:23:54 -05:00
parent af146f65e4
commit 14126f301e

View File

@ -67,10 +67,6 @@ fetch_post() {
POST_TEMPLATE=' POST_TEMPLATE='
<!DOCTYPE html>
<html>
<body>
<link rel="stylesheet" href="comments.css" type="text/css">
<article class="comment"> <article class="comment">
<a class="user" href="$ACCOUNT_URL"> <a class="user" href="$ACCOUNT_URL">
<img class="avatar" src="$ACCOUNT_AVATAR"> <img class="avatar" src="$ACCOUNT_AVATAR">
@ -86,9 +82,11 @@ POST_TEMPLATE='
<div class="attachments"> <div class="attachments">
$POST_ATTACHMENTS $POST_ATTACHMENTS
</div> </div>
<b>$tree_level</b>¤
<div class="responses">
$POST_RESPONSES
</div>
</article> </article>
</body>
</html>
' '
@ -191,16 +189,43 @@ env_subst() {
# Given a notes JSON, render it as HTML. # Given a notes JSON, render it as HTML.
# The most important part of the script! # The most important part of the script!
# render_post $post_data # render_post $post_data $context_data $tree_level
render_post() { render_post() {
local post_data="$1" 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_URL="$(echo "$post_data" | jq -r .account.url)"
local ACCOUNT_ID="$(echo "$post_data" | jq -r .account.fqn)" 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 ACCOUNT_AVATAR="$(echo "$post_data" | jq -r .account.avatar)"
local POST_URL="$(echo "$post_data" | jq -r .url)" local POST_URL="$(echo "$post_data" | jq -r .url)"
local POST_DATE="$(echo "$post_data" | jq -r .created_at)" 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_CONTENT="$(echo "$post_data" | jq -r .content | replace_emojis "$post_data")"
local POST_ATTACHMENTS="$(media_attachments "$post_data")" local POST_ATTACHMENTS="$(media_attachments "$post_data")"
local POST_RESPONSES="$(render_context "$post_data" "$context_data" "$tree_level")"
env_subst "$POST_TEMPLATE" env_subst "$POST_TEMPLATE"
} }
# Render a posts 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
}