Skip to content

Instantly share code, notes, and snippets.

@tkapias
Last active March 22, 2024 20:33
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.

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

Use html2text.patch from gist on the html2text example

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

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

Build the patched example

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 --css option.
# Issue 1: But, currently the css feature overrides the
# defined colors in Rich Annotations.
# For now I don't use CSS which displays a useless and
# sometimes ugly preview in the message.
#iconv -f "$2" -t UTF-8 "$1" \
# | html2text --width $_columns --wrap-width $_columns --colour --css
iconv -f "$2" -t UTF-8 "$1" \
| html2text --width $_columns --wrap-width $_columns --colour
# separation between message and links references.
echo
# Issue 3: 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'
diff --git a/examples/html2text.rs b/examples/html2text.rs
index 2c14ddf..ced4959 100644
--- a/examples/html2text.rs
+++ b/examples/html2text.rs
@@ -22,41 +22,41 @@ fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
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) => {
@@ -83,6 +83,7 @@ fn default_colour_map(annotations: &[RichAnnotation], s: &str) -> String {
}
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; firefox-esr -P neomutt --offline --new-tab --private-window --class=firefox-neomutt '%s' &
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment