Skip to content

Instantly share code, notes, and snippets.

@tkapias
Last active May 30, 2024 14:13
Show Gist options
  • Save tkapias/b0df814c40daf642209b8023f80fbc50 to your computer and use it in GitHub Desktop.
Save tkapias/b0df814c40daf642209b8023f80fbc50 to your computer and use it in GitHub Desktop.
Displaying HTML emails in Neomutt's pager

Displaying HTML emails in Neomutt's pager

Neomutt configuration

My neomutt config files are huge and even their path is cusmotized with $VIMINIT.

I'm just sharing parts about auto_view and HTML mailcap, please update your config accordingly.

Customize and Build html2text

Clone rust-html2text

git clone --depth=1 https://github.com/jugglerchris/rust-html2text

cd rust-html2text/examples/

Use html2text.patch from gist on the html2text example

My patch customize Rich Annotations inline formatting and disable drawing borders.

patch html2text.rs -o html2text-patched.rs < html2text.patch

Build the patched example

cd ../
cargo build --features css --example html2text-patched

Copy the binary for user

cp target/debug/examples/html2text-patched $HOME/.local/bin/html2text

#!/usr/bin/env bash
# ~/.config/neomutt/scripts/auto-view_html.sh
# linked to ~/.local/bin/auto-view_html
# takes a temporary HTML attachment from Neomutt's autoview and
# return a cleaned, formated, colored output, ready for the builtin pager.
# requires 3 attributes: filename, charset, columns
# example: auto-view_html.sh %s %{charset} ${COLUMNS:-80}
shopt -s extglob
export LC_ALL="C.UTF-8"
export TZ=:/etc/localtime
if [[ $3 -lt 80 ]]; then
_columns=$3
else
_columns=80
fi
# To hide preview divs html2text needs to be compiled with
# the css feature and we need to use --ignore-css-colour option
# to hide hidden elements but not ovveride custom colors.
iconv -f "$2" -t UTF-8 "$1" \
| html2text --width $_columns --wrap-width $_columns --colour --ignore-css-colour
# separation between message and links references.
echo
# Issue: html2text lack links references in --colour for now:
# Convert the same file in plain text but with links references,
# delete the message to keep only the references.
html2text "$1" \
| sed -E '/^\[1\]: /,$!d' \
| tr -d '\n' \
| sed -E 's/(.)(\[[0-9]*\]: )/\1\n\2/g'
--- html2text.rs 2024-05-30 20:42:40.295760837 +0700
+++ html2text-patched.rs 2024-05-30 20:44:45.296780972 +0700
@@ -27,41 +27,41 @@
match annotation {
Default => {}
Link(_) => {
- start.push(format!("{}", termion::style::Underline));
- finish.push(format!("{}", termion::style::Reset));
+ start.push(format!("{}{}", Fg(AnsiValue(153)), termion::style::Underline));
+ finish.push(format!("{}{}", Fg(White), termion::style::NoUnderline));
}
Image(_) => {
if !have_explicit_colour {
- start.push(format!("{}", Fg(Blue)));
- finish.push(format!("{}", Fg(Reset)));
+ start.push(format!("{}{}", Fg(AnsiValue(225)), termion::style::Italic));
+ finish.push(format!("{}{}", Fg(White), termion::style::NoItalic));
}
}
Emphasis => {
- start.push(format!("{}", termion::style::Bold));
- finish.push(format!("{}", termion::style::Reset));
+ start.push(format!("{}", termion::style::Italic));
+ finish.push(format!("{}", termion::style::NoItalic));
}
Strong => {
if !have_explicit_colour {
- start.push(format!("{}", Fg(LightYellow)));
- finish.push(format!("{}", Fg(Reset)));
+ start.push(format!("{}", termion::style::Bold));
+ finish.push(format!("{}", termion::style::NoBold));
}
}
Strikeout => {
if !have_explicit_colour {
- start.push(format!("{}", Fg(LightBlack)));
- finish.push(format!("{}", Fg(Reset)));
+ start.push(format!("{}{}", Fg(AnsiValue(7)), termion::style::CrossedOut));
+ finish.push(format!("{}{}", Fg(White), termion::style::NoCrossedOut));
}
}
Code => {
if !have_explicit_colour {
- start.push(format!("{}", Fg(Blue)));
- finish.push(format!("{}", Fg(Reset)));
+ start.push(format!("{}{}", Bg(AnsiValue(25)), Fg(AnsiValue(222))));
+ finish.push(format!("{}{}", Bg(Reset) ,Fg(White)));
}
}
Preformat(_) => {
if !have_explicit_colour {
- start.push(format!("{}", Fg(Blue)));
- finish.push(format!("{}", Fg(Reset)));
+ start.push(format!("{}{}", Bg(AnsiValue(25)), Fg(AnsiValue(229))));
+ finish.push(format!("{}{}", Bg(Reset), Fg(White)));
}
}
Colour(c) => {
@@ -92,6 +92,7 @@
}
fn update_config<T: TextDecorator>(mut config: Config<T>, flags: &Flags) -> Config<T> {
+ config = config.raw_mode(true);
if let Some(wrap_width) = flags.wrap_width {
config = config.max_wrap_width(wrap_width);
}
# ~/.config/neomutt/mailcap
# HTML
text/html; dillo --local --fullwindow %s 1>/dev/null &; nametemplate=%s.html; x-neomutt-nowrap
#text/html; firefox-esr -P neomutt --offline --new-tab --private-window --class=firefox-neomutt '%s' &; nametemplate=%s.html
#text/html; netsurf-gtk %s 1>/dev/null &; nametemplate=%s.html; x-neomutt-nowrap
text/html; auto-view_html %s %{charset} ${COLUMNS:-80}; nametemplate=%s.html; copiousoutput; x-neomutt-nowrap;
# vim: filetype=neomuttrc
## COMMANDS
alternative_order text/markdown text/html text/plain text/enriched text/calendar application/ics
attachments +A */.*
attachments -A text/vcard text/x-vcard
attachments -A application/pgp.*
attachments -A application/pkcs7-.* application/x-pkcs7-.*
attachments +I text/plain
attachments -A message/external-body
attachments -I message/external-body
auto_view text/markdown text/html text/plain text/enriched text/calendar application/ics
ignore *
hdr_order date from to cc subject
mime_lookup application/octet-stream
unignore from: subject to cc date x-mailer x-url user-agent
@tkapias
Copy link
Author

tkapias commented May 30, 2024

Updated to work with rust-html2text new release 0.13.0-alpha.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment