1 ;;; gnus-html.el --- Render HTML in a buffer.
3 ;; Copyright (C) 2010-2017 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; The idea is to provide a simple, fast and pretty minimal way to
26 ;; render HTML (including links and images) in a buffer, based on an
27 ;; external HTML renderer (i.e., w3m).
31 (eval-when-compile (require 'cl
))
34 (eval-when-compile (require 'mm-decode
))
45 (defcustom gnus-html-image-cache-ttl
(days-to-time 7)
46 "Time used to determine if we should use images from the cache."
49 ;; FIXME hardly the friendliest type. The allowed value is actually
50 ;; any time value, but we are assuming no-one cares about USEC and
51 ;; PSEC here. It would be better to eg make it a number of minutes.
52 :type
'(list integer integer
))
54 (defcustom gnus-html-image-automatic-caching t
55 "Whether automatically cache retrieve images."
60 (defcustom gnus-html-frame-width
70
61 "What width to use when rendering HTML."
66 (defcustom gnus-max-image-proportion
0.9
67 "How big pictures displayed are in relation to the window they're in.
68 A value of 0.7 means that they are allowed to take up 70% of the
69 width and height of the window. If they are larger than this,
70 and Emacs supports it, then the images will be rescaled down to
76 (defvar gnus-html-image-map
77 (let ((map (make-sparse-keymap)))
78 (define-key map
"u" 'gnus-article-copy-string
)
79 (define-key map
"i" 'gnus-html-insert-image
)
80 (define-key map
"v" 'gnus-html-browse-url
)
83 (defvar gnus-html-displayed-image-map
84 (let ((map (make-sparse-keymap)))
85 (define-key map
"a" 'gnus-html-show-alt-text
)
86 (define-key map
"i" 'gnus-html-browse-image
)
87 (define-key map
"\r" 'gnus-html-browse-url
)
88 (define-key map
"u" 'gnus-article-copy-string
)
89 (define-key map
[tab] 'widget-forward)
92 (defun gnus-html-encode-url (url)
94 (browse-url-url-encode-chars url "[)$ ]"))
96 (defun gnus-html-cache-expired (url ttl)
97 "Check if URL is cached for more than TTL."
98 (cond (url-standalone-mode
99 (not (file-exists-p (url-cache-create-filename url))))
100 (t (let ((cache-time (url-is-cached url)))
110 (defun gnus-article-html (&optional handle)
111 (let ((article-buffer (current-buffer)))
113 (setq handle (mm-dissect-buffer t)))
115 (narrow-to-region (point) (point))
118 (let* ((coding-system-for-read 'utf-8)
119 (coding-system-for-write 'utf-8)
120 (default-process-coding-system
121 (cons coding-system-for-read coding-system-for-write))
122 (charset (mail-content-type-get (mm-handle-type handle)
125 (setq charset (mm-charset-to-coding-system
127 (not (eq charset 'ascii)))
129 (decode-coding-string (buffer-string) charset)
131 (mm-enable-multibyte))))
132 (call-process-region (point-min) (point-max)
134 nil article-buffer nil
139 "-o" "ext_halfdump=1"
140 "-o" "display_ins_del=2"
142 "-t" (format "%s" tab-width)
143 "-cols" (format "%s" gnus-html-frame-width)
144 "-o" "display_image=on"
146 (gnus-html-wash-tags))))
148 (defvar gnus-article-mouse-face)
150 (defun gnus-html-pre-wash ()
151 (goto-char (point-min))
152 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
153 (replace-match "" t t))
154 (goto-char (point-min))
155 (while (re-search-forward "<a name[^\n>]+>" nil t)
156 (replace-match "" t t)))
158 (defun gnus-html-wash-images ()
159 "Run through current buffer and replace img tags by images."
160 (let (tag parameters string start end images url alt-text
161 inhibit-images blocked-images)
162 (if (buffer-live-p gnus-summary-buffer)
163 (with-current-buffer gnus-summary-buffer
164 (setq inhibit-images gnus-inhibit-images
165 blocked-images (gnus-blocked-images)))
166 (setq inhibit-images gnus-inhibit-images
167 blocked-images (gnus-blocked-images)))
168 (goto-char (point-min))
169 ;; Search for all the images first.
170 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
171 (setq parameters (match-string 1)
172 start (match-beginning 0))
173 (delete-region start (point))
174 (when (search-forward "</img_alt>" (line-end-position) t)
175 (delete-region (match-beginning 0) (match-end 0)))
177 (when (string-match "src=\"\\([^\"]+\\)" parameters)
178 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
179 (setq url (gnus-html-encode-url (match-string 1 parameters))
180 alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
182 (xml-substitute-special (match-string 2 parameters))))
186 'image-displayer `(lambda (url start end)
187 (gnus-html-display-image url start end
189 'gnus-image (list url start end alt-text)))
190 (widget-convert-button
191 'url-link start (point)
193 :keymap gnus-html-image-map
195 (if (string-match "\\`cid:" url)
196 ;; URLs with cid: have their content stashed in other
197 ;; parts of the MIME structure, so just insert them
199 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
200 (image (when (and handle
201 (not inhibit-images))
203 (mm-with-part handle (buffer-string))
210 image (gnus-html-maximum-image-size))
211 (gnus-string-or (prog1
212 (buffer-substring start end)
213 (delete-region start end))
216 (widget-convert-button
218 :action 'gnus-html-insert-image
220 :keymap gnus-html-image-map
221 :button-keymap gnus-html-image-map)))
222 ;; Normal, external URL.
223 (if (or inhibit-images
224 (gnus-html-image-url-blocked-p url blocked-images))
225 (widget-convert-button
227 :action 'gnus-html-insert-image
229 :keymap gnus-html-image-map
230 :button-keymap gnus-html-image-map)
233 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
234 (string-to-number (match-string 1 parameters))))
236 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
237 (string-to-number (match-string 1 parameters)))))
238 ;; Don't fetch images that are really small. They're
239 ;; probably tracking pictures.
240 (when (and (or (null height)
244 (gnus-html-display-image url start end alt-text)))))))))
246 (defun gnus-html-display-image (url start end &optional alt-text)
247 "Display image at URL on text from START to END.
248 Use ALT-TEXT for the image string."
249 (or alt-text (setq alt-text "*"))
250 (if (string-match "\\`cid:" url)
251 (let ((handle (mm-get-content-id (substring url (match-end 0)))))
253 (gnus-html-put-image (mm-with-part handle (buffer-string))
255 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
256 ;; We don't have it, so schedule it for fetching
258 (gnus-html-schedule-image-fetching
261 ;; It's already cached, so just insert it.
262 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
264 (defun gnus-html-wash-tags ()
265 (let (tag parameters string start end images url)
267 (gnus-html-wash-images)
269 (goto-char (point-min))
270 ;; Then do the other tags.
271 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
272 (setq tag (match-string 1)
273 parameters (match-string 2)
274 start (match-beginning 0))
275 (when (> (length parameters) 0)
276 (set-text-properties 0 (1- (length parameters)) nil parameters))
277 (delete-region start (point))
278 (when (search-forward (concat "</" tag ">") nil t)
279 (delete-region (match-beginning 0) (match-end 0)))
282 ;; Fetch and insert a picture.
283 ((equal tag "img_alt"))
287 (when (string-match "href=\"\\([^\"]+\\)" parameters)
288 (setq url (match-string 1 parameters))
289 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
290 (gnus-article-add-button start end
291 'browse-url (mm-url-decode-entities-string url)
293 (let ((overlay (make-overlay start end)))
294 (overlay-put overlay 'evaporate t)
295 (overlay-put overlay 'gnus-button-url url)
296 (put-text-property start end 'gnus-string url)
297 (when gnus-article-mouse-face
298 (overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
299 ;; The upper-case IMG_ALT is apparently just an artifact that
300 ;; should be deleted.
301 ((equal tag "IMG_ALT")
302 (delete-region start end))
303 ;; w3m does not normalize the case
306 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-bold))
309 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
312 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-italic))
315 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-strikethru))
316 ((or (equal tag "ins")
318 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
319 ;; Handle different UL types
320 ((equal tag "_SYMBOL")
321 (when (string-match "TYPE=\\(.+\\)" parameters)
322 (let ((type (string-to-number (match-string 1 parameters))))
323 (delete-region start end)
324 (cond ((= type 33) (insert " "))
325 ((= type 34) (insert " "))
326 ((= type 35) (insert " "))
327 ((= type 36) (insert " "))
328 ((= type 37) (insert " "))
329 ((= type 38) (insert " "))
330 ((= type 39) (insert " "))
331 ((= type 40) (insert " "))
332 ((= type 42) (insert " "))
333 ((= type 43) (insert " "))
335 ;; Whatever. Just ignore the tag.
339 (goto-char (point-min))
340 ;; The output from -halfdump isn't totally regular, so strip
341 ;; off any </pre_int>s that were left over.
342 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
343 (replace-match "" t t))
344 (mm-url-decode-entities)))
346 (defun gnus-html-insert-image (&rest args)
347 "Fetch and insert the image under point."
349 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
351 (defun gnus-html-show-alt-text ()
352 "Show the ALT text of the image under point."
354 (message "%s" (get-text-property (point) 'gnus-alt-text)))
356 (defun gnus-html-browse-image ()
357 "Browse the image under point."
359 (browse-url (get-text-property (point) 'image-url)))
361 (defun gnus-html-browse-url ()
362 "Browse the image under point."
364 (let ((url (get-text-property (point) 'gnus-string)))
367 (message "No link under point"))
368 ((string-match "^mailto:" url)
369 (gnus-url-mailto url))
373 (defun gnus-html-schedule-image-fetching (buffer image)
374 "Retrieve IMAGE, and place it into BUFFER on arrival."
375 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
377 (url-queue-retrieve (car image)
378 'gnus-html-image-fetched
379 (list buffer image) t t))
381 (defun gnus-html-image-fetched (status buffer image)
382 "Callback function called when image has been fetched."
383 (unless (plist-get status :error)
384 (when (and (or (search-forward "\n\n" nil t)
385 (search-forward "\r\n\r\n" nil t))
387 (when gnus-html-image-automatic-caching
388 (url-store-in-cache (current-buffer)))
389 (when (buffer-live-p buffer)
390 (let ((data (buffer-substring (point) (point-max))))
391 (with-current-buffer buffer
392 (let ((inhibit-read-only t))
393 (gnus-html-put-image data (car image) (cadr image))))))))
394 (kill-buffer (current-buffer)))
396 (defun gnus-html-get-image-data (url)
397 "Get image data for URL.
398 Return a string with image data."
400 (mm-disable-multibyte)
401 (url-cache-extract (url-cache-create-filename url))
402 (when (or (search-forward "\n\n" nil t)
403 (search-forward "\r\n\r\n" nil t))
404 (buffer-substring (point) (point-max)))))
406 (defun gnus-html-maximum-image-size ()
407 "Return the maximum size of an image according to `gnus-max-image-proportion'."
408 (let ((edges (window-inside-pixel-edges
409 (get-buffer-window (current-buffer)))))
414 (* gnus-max-image-proportion
415 (- (nth 2 edges) (nth 0 edges))))
417 (truncate (* gnus-max-image-proportion
418 (- (nth 3 edges) (nth 1 edges)))))))
420 ;; Behind display-graphic-p test.
421 (declare-function image-size "image.c" (spec &optional pixels frame))
423 (defun gnus-html-put-image (data url &optional alt-text)
424 "Put an image with DATA from URL and optional ALT-TEXT."
425 (when (display-graphic-p)
426 (let* ((start (text-property-any (point-min) (point-max)
429 (next-single-property-change start 'image-url))))
434 (gnus-create-image data nil t)))
435 (size (and image (image-size image t))))
438 (let ((alt-text (or alt-text
439 (buffer-substring-no-properties start end)))
440 (inhibit-read-only t))
442 ;; Kludge to avoid displaying 30x30 gif images, which
443 ;; seems to be a signal of a broken image.
444 (not (and (listp image)
445 (eq (plist-get (cdr image) :type)
449 ;; Good image, add it!
450 (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
451 (delete-region start end)
452 (gnus-put-image image alt-text 'external)
453 (widget-convert-button
454 'url-link start (point)
456 :keymap gnus-html-displayed-image-map
458 (put-text-property start (point) 'gnus-alt-text alt-text)
465 (lambda (url start end)
466 (gnus-html-display-image url start end
468 (gnus-add-image 'external image)
470 ;; Bad image, try to show something else
471 (when (fboundp 'find-image)
472 (delete-region start end)
473 (setq image (find-image
474 '((:type xpm :file "lock-broken.xpm"))))
475 (gnus-put-image image alt-text 'internal)
476 (gnus-add-image 'internal image))
479 (defun gnus-html-image-url-blocked-p (url blocked-images)
480 "Find out if URL is blocked by BLOCKED-IMAGES."
481 (let ((ret (and blocked-images
482 (string-match blocked-images url))))
484 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
486 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
491 (defun gnus-html-prefetch-images (summary)
492 (when (buffer-live-p summary)
493 (let (inhibit-images blocked-images)
494 (with-current-buffer summary
495 (setq inhibit-images gnus-inhibit-images
496 blocked-images (gnus-blocked-images)))
498 (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
499 (let ((url (gnus-html-encode-url
500 (mm-url-decode-entities-string (match-string 1)))))
501 (unless (or inhibit-images
502 (gnus-html-image-url-blocked-p url blocked-images))
503 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
504 (gnus-html-schedule-image-fetching nil
509 ;;; gnus-html.el ends here