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