Wie bei vielen Sündenfällen folgt auch beim Versand von HTML-Mails die Strafe relativ rasch. markdown2html erweist sich als unfähig, mit mehreren unsauber formatierten Zitatebenen umzugehen und gibt unter anderem die CSS-Klasse .quotesubsequent
sichtbar im Mailtext aus. Um wenigstens wohlgeformtes HTML zu versenden, greife ich einen Hinweis von Akkana Peck auf:
Write the [HTML] file and exit the editor. Then, in the Compose menu, type Ctrl-T to edit the attachment type. Change the type from text/plain to text/html.
Mit folgenden Ergänzungen in .vimrc
und .muttrc
lässt sich der Redaktionsprozess etwas vereinfachen:
# .vimrc " insert HTML template nnoremap ,d :0r ~/signature_html<CR>4G<CR>i function EncloseParagraphs() execute "normal `>a</p>\<Esc>`<i<p>\<Esc>" %s/\%V\n\{2,}\%V/<\/p>\r\r<p>/ge nohl endfunction " <C-U> is required to keep the called function from being executed for each " line of the selected range vnoremap <buffer> <silent> ,p :<C-U>call EncloseParagraphs()<CR> function AddLinebreaks() '<,'>s:\(.\)\zs\n\ze\(.\):\<br \/\>\r:ge nohl endfunction " select email content (visual mode) and add linebreaks/paragraphs nnoremap ,f kV5G:<C-U>call AddLinebreaks()<CR>gv:<C-U>call EncloseParagraphs()<CR> # .muttrc macro compose ,t "<edit-type>^Utext/html; charset=utf-8<enter>y"
Ein Nachteil dieser Vorgehensweise ist die voreingestellte Zeichenkodierung, während mutt flexibel die am wenigsten anspruchsvolle Kodierung (in meinem Fall us-ascii
, iso-8859-1
oder utf-8
) verwendet, und die Beschränkung auf eine HTML-Version. Die Lösung ist offensichtlich ein Skript, das (wie markdown2html) als Filter funktioniert und meinen E-Mails eine HTML-Version hinzufügt:
#!/opt/homebrew/bin/python3 import re import sys import html def convert_to_html(text_input): html_signature = '[redacted]' parts = re.split(r'^-- $', text_input, 1, flags=re.MULTILINE) body = parts[0] body = html.escape(body) # add linebreaks (with lookaround for overlapping searches) body = re.sub(r'(?<=.)\n(?=.)', r'<br />', body) # add paragraphs body = re.sub(r'\n{2,}', r'</p>\n\n<p>', body) body = f'<p>{body}</p>' # color quoted paragraphs body = re.sub(r'<p>((> *){3,})', r'<p style="color:#009900;">\1', body) body = re.sub(r'<p>((> *){2})', r'<p style="color:#ff002d;">\1', body) body = re.sub(r'<p>(> *)', r'<p style="color:#3366ff;">\1', body) html_body = f'<html><body style="font:14px Helvetica;">{body}{html_signature}</body></html>' return html_body def main(): html_output = convert_to_html(sys.stdin.read()) if html_output: print(f'text/html\n\n{html_output}') if __name__ == '__main__': main()
In .vimrc
sind nun nur noch die üblichen Staubwedel eingetragen, und .muttrc
benötigt eine kleine Anpassung der attribution
-Variablen, um die Einfärbung von zitierten Absätzen optimal zu unterstützen:
# .vimrc function CleanMail() " remove excess blank/quoted lines in mail messages silent %s/\(^[> ]*$\n\)\{2,}/\1/ge " remove quotes in lines below an attribution (to support HTML paragraph " coloring via html_mail.py) silent %s/^\(\(> *\)*On.*wrote:\n\)\(> *\)\+$/\1/ge nohl normal 1G endfunction autocmd FileType mail call CleanMail() " properly wrap lines vnoremap ,r gq nnoremap ,r gqap " remove e-mail thread below cursor (but leave signature untouched) map ,e VG16kdzz # .muttrc set attribution="On %d, %n wrote:\n"
Es ist eben nicht so, dass reguläre Ausdrücke stets die Anzahl der Probleme verdoppeln.