125 lines
3.4 KiB
Bash
Executable File
125 lines
3.4 KiB
Bash
Executable File
#!/bin/sh
|
||
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||
# Name: fedi2html.sh
|
||
# Desc:
|
||
# Reqs:
|
||
# Date:
|
||
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
|
||
|
||
|
||
url_id() {
|
||
local url="$1"
|
||
# Pleroma-style URLs: https://jam.xwx.moe/notice/Ac6PIZAP0ZzkMTYBBg
|
||
# Mastodon-style URLs: https://esperanto.masto.host/@minjo/111461250815264185
|
||
if echo "$url" | grep "/notice/" > /dev/null; then
|
||
echo "$url" \
|
||
| sed 's%.*/notice/%%'
|
||
else
|
||
echo "$url" \
|
||
| sed 's%.*/@[[:alnum:]]*/%%'
|
||
fi
|
||
}
|
||
|
||
|
||
url_server() {
|
||
local url="$1"
|
||
local protocol="$(echo "$url" | grep --only-matching '[[:alnum:]]*://')"
|
||
printf "$protocol"
|
||
echo "$url" \
|
||
| sed 's%^'"$protocol"'%%' \
|
||
| sed 's%/.*%%'
|
||
}
|
||
|
||
|
||
statuses_api_request() {
|
||
local post_url="$1"
|
||
local api_request="$2"
|
||
if test -n "$api_request"; then
|
||
api_request="/$api_request"
|
||
fi
|
||
|
||
local id="$(url_id "$url")"
|
||
local server="$(url_server "$url")"
|
||
curl --location --header 'Accept: application/json,application/activity+json' \
|
||
"$server/api/v1/statuses/${id}${api_request}"
|
||
}
|
||
|
||
|
||
fetch_post_context() {
|
||
local url="$1"
|
||
statuses_api_request "$url" "context"
|
||
}
|
||
|
||
|
||
fetch_post() {
|
||
local url="$1"
|
||
statuses_api_request "$url"
|
||
}
|
||
|
||
|
||
POST_TEMPLATE='
|
||
<!DOCTYPE html>
|
||
<html>
|
||
<body>
|
||
<link rel="stylesheet" href="comments.css" type="text/css">
|
||
<div id="comments">
|
||
<a class="comment-user" href="$ACCOUNT_URL">
|
||
<img class="comment-avatar" src="$ACCOUNT_AVATAR">
|
||
<strong class="comment-username">$ACCOUNT_NAME</strong>
|
||
<em class="comment-useraddress">$ACCOUNT_ID</em>
|
||
</a>
|
||
<a class="comment-address" href="$POST_URL">
|
||
<time class="comment-time" title="$POST_DATE">$(date --date=$POST_DATE)</time>
|
||
</a>
|
||
<div class="comment-body">
|
||
$POST_CONTENT
|
||
</div>
|
||
</div>
|
||
</body>
|
||
</html>
|
||
'
|
||
|
||
|
||
replace_emojis() {
|
||
local post_data="$1"
|
||
local emojis="$2"
|
||
|
||
# So, this function is recusive; in hindsight, it didn’t have to be. oh well!
|
||
if test -n "$post_data" -a -z "$emojis"; then
|
||
# Init case; start subsitutions in next level of recursion
|
||
replace_emojis \
|
||
"" \
|
||
"$(echo "$post_data" \
|
||
| jq -r '.emojis[]|(.url + "\t" + .shortcode)')"
|
||
elif test -n "$emojis"; then
|
||
# Substitute the current emoji (first line of $emojis)
|
||
local url="$(echo "$emojis" | head -1 | awk '{print $1}')"
|
||
local shortcode="$(echo "$emojis" | head -1 | awk '{print $2}')"
|
||
|
||
sed "s%:${shortcode}:%<img src='$url'>%g" \
|
||
| replace_emojis \
|
||
"" \
|
||
"$(echo "$emojis" | tail +2)"
|
||
else
|
||
# End-case; $emojis is empty, so our job is done.
|
||
# Just print the results.
|
||
cat
|
||
fi
|
||
}
|
||
|
||
|
||
render_post() {
|
||
local post_data="$1"
|
||
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_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")"
|
||
eval "echo \"$(echo "$POST_TEMPLATE" | sed 's%\"%\\\"%g')\""
|
||
}
|
||
|
||
|
||
|