diff --git a/fedi2html.sh b/fedi2html.sh index 7122a96..85780b1 100755 --- a/fedi2html.sh +++ b/fedi2html.sh @@ -62,25 +62,53 @@ POST_TEMPLATE=' -
- - - $ACCOUNT_NAME - $ACCOUNT_ID +
+ + + $ACCOUNT_NAME + $ACCOUNT_ID - - + + -
+
$POST_CONTENT +
+
+ $POST_ATTACHMENTS
-
+
' -EMOJI_TEMPLATE='$EMOJI_SHORTCODE' +ATTACH_TEMPLATE=' + +
+ $ATTACH_NAME + $ATTACH_DESC +
+
' + +ATTACH_IMAGE_TEMPLATE=' +
+ + $ATTACH_DESC + +
+ $ATTACH_NAME
+ $(if test $(echo "$ATTACH_DESC" | wc -c) -gt 52; then + echo $ATTACH_DESC | head -c 53 | sed 's%[[:space:]]*$%%' + echo … + else + echo $ATTACH_DESC + fi) +
+
+' + +EMOJI_TEMPLATE='$EMOJI_SHORTCODE' replace_emojis() { local post_data="$1" @@ -91,8 +119,8 @@ replace_emojis() { cat > "$temp" for line in $emojis; do - local EMOJI_URL="$(echo "$line" | awk '{print $1}')" - local EMOJI_SHORTCODE="$(echo "$line" | awk '{print $2}')" + local EMOJI_URL="$(echo "$line" | awk -F'\t' '{print $1}')" + local EMOJI_SHORTCODE="$(echo "$line" | awk -F'\t' '{print $2}')" local value="$(env_subst "$EMOJI_TEMPLATE")" sed -i "s%:${EMOJI_SHORTCODE}:%${value}%g" "$temp" done @@ -100,6 +128,28 @@ replace_emojis() { rm "$temp" } + +media_attachments() { + local post_data="$1" + local attachments="$(echo "$post_data" | jq -r '.media_attachments[]|(.type + "\t" + .url + "\t" + .description + "\t" + .preview_url)')" + local IFS=" +" + + echo "$attachments" > /tmp/d + for line in $attachments; do + local ATTACH_TYPE="$(echo "$line" | awk -F'\t' '{print $1}')" + local ATTACH_URL="$(echo "$line" | awk -F'\t' '{print $2}')" + local ATTACH_DESC="$(echo "$line" | awk -F'\t' '{print $3}')" + local ATTACH_PREVIEW="$(echo "$line" | awk -F'\t' '{print $4}')" + local ATTACH_NAME="$(basename "$ATTACH_URL")" + if test "$ATTACH_TYPE" = "image"; then + env_subst "$ATTACH_IMAGE_TEMPLATE" + else + env_subst "$ATTACH_TEMPLATE" + fi + done +} + prep_template() { local template="$1" echo "$template" \ @@ -120,6 +170,7 @@ render_post() { 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")" env_subst "$POST_TEMPLATE" }