mu4e: pass msg to html2text functions

Add a `msg' parameter to html2text functions, but for backward
compatibility, introspect the functions, and if they don't support it,
use the old ways of running the function in the context of a buffer with
the html text.
This commit is contained in:
djcb
2017-01-22 22:13:19 +02:00
parent 014d33d050
commit edcae719e4

View File

@ -45,11 +45,14 @@ htmltext program, it's recommended you use \"html2text -utf8
-width 72\". Alternatives are the python-based html2markdown, w3m -width 72\". Alternatives are the python-based html2markdown, w3m
and on MacOS you may want to use textutil. and on MacOS you may want to use textutil.
It can also be a function, which takes the current buffer in html It can also be a function, which takes a messsage-plist as
as input, and transforms it into html (like the `html2text' argument and is expected to return the textified html as output.
function).
In both cases, the output is expected to be in UTF-8 encoding. For backward compatibility, it can also be a parameterless
function which is run in the context of a buffer with the html
and expected to transform this (like the `html2text' function).
In all cases, the output is expected to be in UTF-8 encoding.
Newer emacs has the shr renderer, and when it's available Newer emacs has the shr renderer, and when it's available
conversion defaults to `mu4e-shr2text'; otherwise, the default is conversion defaults to `mu4e-shr2text'; otherwise, the default is
@ -192,20 +195,16 @@ unless PREFER-HTML is non-nil."
(let ((body (let ((body
(if mu4e~message-body-html (if mu4e~message-body-html
;; use an HTML body ;; use an HTML body
(with-temp-buffer (cond
(insert (mu4e-message-field msg :body-html)) ((stringp mu4e-html2text-command)
(cond (mu4e-html2text-shell msg mu4e-html2text-command))
((stringp mu4e-html2text-command) ((functionp mu4e-html2text-command)
(let* ((tmp-file (mu4e-make-temp-file "html"))) (if (help-function-arglist mu4e-html2text-command)
(write-region (point-min) (point-max) tmp-file) (funcall mu4e-html2text-command msg)
(erase-buffer) (mu4e~html2text-wrapper mu4e-html2text-command msg)))
(call-process-shell-command mu4e-html2text-command tmp-file t t) (t (mu4e-error "Invalid `mu4e-html2text-command'")))
(delete-file tmp-file))) (setq mu4e~message-body-html t)
((functionp mu4e-html2text-command) (buffer-string)
(funcall mu4e-html2text-command))
(t (mu4e-error "Invalid `mu4e-html2text-command'")))
(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
@ -276,7 +275,6 @@ 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-shr2text ()
"Html to text using the shr engine; this can be used in "Html to text using the shr engine; this can be used in
@ -294,4 +292,22 @@ Titus von der Malsburg."
(shr-inhibit-images t)) (shr-inhibit-images t))
(shr-render-region (point-min) (point-max)) (shr-render-region (point-min) (point-max))
(goto-char (point-min)))) (goto-char (point-min))))
(defun mu4e~html2text-shell (msg cmd)
"Convert html2 text using a shell function."
(mu4e~html2-text-wrapper
(lambda ()
(let* ((tmp-file (mu4e-make-temp-file "html")))
(write-region (point-min) (point-max) tmp-file)
(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)))