Add -R and -I args; use getopts

Adds -R (no responses) and -I (reverse-order)
parameters, thanks to the use of getopts
(rather than just getting $1 etc.)
This commit is contained in:
Jaidyn Ann 2024-03-29 00:19:16 -05:00
parent 6d4398f8a7
commit 9f69c3d54b

View File

@ -73,7 +73,9 @@ render_post() {
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_responses "$post_data" "$responses_data" "$POST_TREE_LEVEL")"
if test -z "$NO_RESPONSES"; then
local POST_RESPONSES="$(render_responses "$post_data" "$responses_data" "$POST_TREE_LEVEL")"
fi
env_subst "$POST_TEMPLATE"
}
@ -150,7 +152,7 @@ media_attachments() {
context_to_responses() {
jq '.descendants' \
| jq 'sort_by(.created_at)' \
| jq 'reverse' \
| maybe_jq_reverse \
| jq -cr '.[]'
}
@ -238,8 +240,23 @@ env_subst() {
}
# Based on the environment variable $REVERSE_ORDER (whether or not user provided
# the -I flag), reverse the JSON array over stdin.
# This is used to enable/disable reverse-chronological order of posts.
# fetch_context $url | jq '.descendants' | maybe_jq_reverse
maybe_jq_reverse() {
local input="$(cat)"
if test -n "$REVERSE_ORDER"; then
echo "$input" \
| jq 'reverse'
else
echo "$input"
fi
}
usage() {
echo "usage: $(basename "$0") POST_URL"
echo "usage: $(basename "$0") [-h] [-IR] POST_URL"
echo
echo "$(basename "$0") does exactly what it says on the tin: It formats"
echo "a fediverse post (and its replies) into simple-and-embeddable HTML."
@ -251,17 +268,35 @@ usage() {
echo "that images are all fetched from remote sources. It is recommended,"
echo "if privacy or total archival, is a concern, to use wget(1)s --mirror"
echo "(or something like it) to fetch even these foreign files."
echo
echo " -h print this message and exit"
echo " -I display posts in reverse-chronological order"
echo " -R do not recursively display posts responses"
}
while getopts 'hRI' arg; do
case $arg in
h)
usage
exit 0
;;
I)
REVERSE_ORDER="1"
;;
R)
NO_RESPONSES="1"
;;
esac
done
shift $((OPTIND-1))
URL="$1"
if test -z "$URL"; then
usage 1>&2
exit 2
elif test "$URL" = "-h" -o "$URL" = "--help"; then
usage
exit 0
fi