mu4e: rework html2text conversion

Rework the conversion from html to text; the functions doing that now
expect to receive one parameter, the message, and return the converted
message.

In the old way, the function got invoked in a buffer with html text, and
were expected to modify it to text. This old way is still supported for
backward compatibility.
This commit is contained in:
djcb
2017-01-24 08:27:21 +02:00
parent edcae719e4
commit 02e651a8fc
2 changed files with 50 additions and 31 deletions

View File

@ -194,6 +194,7 @@ unless PREFER-HTML is non-nil."
(setq mu4e~message-body-html (mu4e~message-use-html-p msg prefer-html))
(let ((body
(if mu4e~message-body-html
(progn
;; use an HTML body
(cond
((stringp mu4e-html2text-command)
@ -201,10 +202,10 @@ unless PREFER-HTML is non-nil."
((functionp mu4e-html2text-command)
(if (help-function-arglist mu4e-html2text-command)
(funcall mu4e-html2text-command msg)
;; oldskool parameterless mu4e-html2text-command
(mu4e~html2text-wrapper mu4e-html2text-command msg)))
(t (mu4e-error "Invalid `mu4e-html2text-command'")))
(setq mu4e~message-body-html t)
(buffer-string)
(t (mu4e-error "Invalid `mu4e-html2text-command'"))
(setq mu4e~message-body-html t)))
;; use a text body
(or (mu4e-message-field msg :body-txt) ""))))
;; and finally, remove some crap from the remaining string; it seems
@ -275,11 +276,21 @@ point in eiter the headers buffer or the view buffer."
point in eiter the headers buffer or the view buffer."
(plist-get (mu4e-message-at-point) field))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4e-shr2text ()
"Html to text using the shr engine; this can be used in
`mu4e-html2text-command' in a new enough emacs. Based on code by
Titus von der Malsburg."
(defun mu4e~html2text-wrapper (func msg)
"Fill a temporary buffer with html from MSG, then call
FUNC. Return the buffer contents."
(with-temp-buffer
(insert (or (mu4e-message-field msg :body-html) ""))
(funcall func)
(message "buffer string...")
(or (buffer-string) "")))
(defun mu4e-shr2text (msg)
"Convert html in MSG to text using the shr engine; this can be
used in `mu4e-html2text-command' in a new enough emacs. Based on
code by Titus von der Malsburg."
(mu4e~html2text-wrapper
(lambda ()
(let (
;; When HTML emails contain references to remote images,
@ -289,8 +300,7 @@ Titus von der Malsburg."
;; to not retrieve images.
;; See this discussion on mu-discuss:
;; https://groups.google.com/forum/#!topic/mu-discuss/gr1cwNNZnXo
(shr-inhibit-images t))
(shr-render-region (point-min) (point-max))
(shr-inhibit-images t))
(shr-render-region (point-min) (point-max)))) msg))
(defun mu4e~html2text-shell (msg cmd)
@ -302,12 +312,4 @@ Titus von der Malsburg."
(erase-buffer)
(call-process-shell-command mu4e-html2text-command tmp-file t t)
(delete-file tmp-file))) msg))
(defun mu4e~html2text-wrapper (func msg)
"Fill a temporary buffer with html from MSG, then call
FUNC. Return the buffer contents."
(with-temp-buffer
(insert (mu4e-message-field msg :body-html))
(funcall func)
(buffer-string)))

View File

@ -1381,9 +1381,26 @@ alternative:
@subsection Html2text functions
@anchor{Html2text functions}
If @code{mu4e-html2text-command} refers to an elisp function, it is
expected to take the current buffer in html as input, and transform it
into text (just like the @code{html2text} function).
If @code{mu4e-html2text-command} refers to an elisp function, the
function is expected to take a message plist as its input, and returns
the transformed data.
You can easily create your own function, for instance:
@lisp
(defun my-mu4e-html2text (msg)
"My html2text function; shows short message inline, show
longer functions in browser."
(let ((html (or (mu4e-message-field msg :body-html) "")))
(if (> (length html) 1000)
(progn
(mu4e-action-view-in-browser msg)
"[Viewing message in external browser]")
(mu4e-shr2text msg))))
(setq mu4e-html2text-command 'my-mu4e-html2text)
@end lisp
@subsection Privacy aspects
@anchor{Privacy aspects}