feat: html rendering

This commit is contained in:
2026-03-12 14:52:29 +01:00
parent 60f763df19
commit eb8f4574bf
10 changed files with 191 additions and 93 deletions

View File

@@ -1,11 +1,4 @@
if [ ! "${AWK_LOADED:-}" ]; then
AWK_LASTMESSAGEINTHREAD=$(
cat <<'EOF'
@@include awk/lastmessageinthread.awk
EOF
)
export AWK_LASTMESSAGEINTHREAD
AWK_THREADOVERVIEW=$(
cat <<'EOF'
@@include awk/threadoverview.awk
@@ -13,5 +6,26 @@ EOF
)
export AWK_THREADOVERVIEW
AWK_MESSAGEHEADER=$(
cat <<'EOF'
@@include awk/messageheader.awk
EOF
)
export AWK_MESSAGEHEADER
AWK_MESSAGEPARTS=$(
cat <<'EOF'
@@include awk/messageparts.awk
EOF
)
export AWK_MESSAGEPARTS
AWK_PARTNR=$(
cat <<'EOF'
@@include awk/partnr.awk
EOF
)
export AWK_PARTNR
export AWK_LOADED=1
fi

32
src/sh/lists.sh Normal file
View File

@@ -0,0 +1,32 @@
# These functions generate the lists that are fed into fzf
# List the messages within a thread
# @argument $1: thread id
list_messages_in_thread() {
$NOTMUCH show thread:"$1" \
| awk "$AWK_THREADOVERVIEW" \
| sed "s/^\(\S*\)\t\([^\t]*\)\t\(.*\) (\([^()]*\)) (\([^()]*\))$/${COLFROM}\3${COLRESET}\t${COLSUBJ}\2${COLRESET}\t${COLDATE}\4${COLRESET}\t${COLTAGS}\5${COLRESET}\t\1/"
}
# List all threads that match a query
# @argument $1: query
list_threads() {
$NOTMUCH search "$1" \
| sed "s/^thread:\([[:xdigit:]]\+\)\s\+\([^[]\+\)\s\+\[\([^]]\+\)\]\([^;]*\); \(.*\) (\([^(]*\))$/${COLDATE}\2${COLRESET}\t${COLCNTS}\3${COLRESET}\t${COLFROM}\4${COLRESET}\t${COLSUBJ}\5${COLRESET}\t${COLTAGS}\6${COLRESET}\t\1/" \
| column -s "$(printf '\t')" -t -l 3 -R 1,2
}
# List all available tags
list_tags() {
$NOTMUCH search --output=tags tag:/./
}
# List all email addresses
list_addresses() {
$NOTMUCH address '*'
}
# List query history
list_query_history() {
cat "$NMFHIST"
}

33
src/sh/notmuch.sh Normal file
View File

@@ -0,0 +1,33 @@
# Wrapper around notmuch
# Print the message id of the last message within a thread.
# @argument $1: thread id
nm_last_message_in_thread() {
$NOTMUCH search --output=messages --offset=-1 thread:"$1" | sed 's/^...//'
}
# Print the header of a message (with trailing empty line)
# @argument $1: message id
nm_message_header() {
$NOTMUCH show --body=false id:"$1" | awk "$AWK_MESSAGEHEADER"
}
# Print the message parts (with indents)
# @argument $1: message id
nm_message_parts() {
$NOTMUCH show --body=true id:"$1" | awk "$AWK_MESSAGEPARTS"
}
# Print the message part number for text/plain or text/html
# @argument $1: message id
# @argument $2: if set, then the html part number will be printed
nm_message_part_nr() {
$NOTMUCH show --body=true id:"$1" | awk -v html="${2:-}" "$AWK_PARTNR"
}
# Print part of message
# @argument $1: message id
# @argument $2: part number
nm_message_get_part() {
$NOTMUCH show --part="$2" id:"$1"
}

21
src/sh/preview.sh Normal file
View File

@@ -0,0 +1,21 @@
# Preview functions
# Preview email
# If a thread identifier is given, then the last message in the thread is
# shown. If the HTML flag is set, then the HTML message is parsed using pandoc.
# This functions reverts to the HTML method if no HTML flag is set yet no
# text/plain part is found.
#
# @argument $1: "id" or "thread"
# @argument $2: message id or thread id
# @argument #3: if set, then the HTML messages will be taken
preview_message() {
[ "$1" = "id" ] && messageid="$2" || messageid="$(nm_last_message_in_thread "$2")"
parts="$(nm_message_parts "$messageid")"
html="${3:-}"
nr="$(nm_message_part_nr "$messageid" "$html")"
[ ! "$nr" ] && [ ! "$html" ] && html="html" && nr="$(nm_message_part_nr "$messageid" "html")"
[ "$html" ] && parser="$PANDOC" || parser="cat"
nm_message_header "$messageid" | $CATEMAIL
nm_message_get_part "$messageid" "$nr" | $parser | $CATMD
}

View File

@@ -20,9 +20,17 @@ if [ ! "${TOOLS_LOADED:-}" ]; then
elif command -v "batcat" >/dev/null; then
CAT="batcat"
fi
CAT=${CAT:+$CAT -l email --style=plain --color=always --wrap}
CAT=${CAT:-cat}
export CAT
CATEMAIL=${CAT:+$CAT -l email --style=plain --color=always}
CATMD=${CAT:+$CAT -l markdown --style=plain --color=always}
CATEMAIL=${CATEMAIL:-cat}
CATEMD=${CATMD:-cat}
export CATEMAIL CATMD
if command -v "pandoc" >/dev/null; then
PANDOC="pandoc --from html --to markdown_strict-raw_html"
fi
PANDOC=${PANDOC:-cat}
export PANDOC
export TOOLS_LOADED=1
fi