1 ;;; gnus-html.el --- Render HTML in a buffer.
3 ;; Copyright (C) 2010-2018 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).
32 (eval-when-compile (require 'mm-decode
))
43 (defcustom gnus-html-image-cache-ttl
(days-to-time 7)
44 "Time used to determine if we should use images from the cache."
47 ;; FIXME hardly the friendliest type. The allowed value is actually
48 ;; any time value, but we are assuming no-one cares about USEC and
49 ;; PSEC here. It would be better to eg make it a number of minutes.
50 :type
'(list integer integer
))
52 (defcustom gnus-html-image-automatic-caching t
53 "Whether automatically cache retrieve images."
58 (defcustom gnus-html-frame-width
70
59 "What width to use when rendering HTML."
64 (defcustom gnus-max-image-proportion
0.9
65 "How big pictures displayed are in relation to the window they're in.
66 A value of 0.7 means that they are allowed to take up 70% of the
67 width and height of the window. If they are larger than this,
68 and Emacs supports it, then the images will be rescaled down to
74 (defvar gnus-html-image-map
75 (let ((map (make-sparse-keymap)))
76 (define-key map
"u" 'gnus-article-copy-string
)
77 (define-key map
"i" 'gnus-html-insert-image
)
78 (define-key map
"v" 'gnus-html-browse-url
)
81 (defvar gnus-html-displayed-image-map
82 (let ((map (make-sparse-keymap)))
83 (define-key map
"a" 'gnus-html-show-alt-text
)
84 (define-key map
"i" 'gnus-html-browse-image
)
85 (define-key map
"\r" 'gnus-html-browse-url
)
86 (define-key map
"u" 'gnus-article-copy-string
)
87 (define-key map
[tab] 'widget-forward)
90 (defun gnus-html-encode-url (url)
92 (browse-url-url-encode-chars url "[)$ ]"))
94 (defun gnus-html-cache-expired (url ttl)
95 "Check if URL is cached for more than TTL."
96 (cond (url-standalone-mode
97 (not (file-exists-p (url-cache-create-filename url))))
98 (t (let ((cache-time (url-is-cached url)))
100 (time-less-p (time-add cache-time ttl) nil)
104 (defun gnus-article-html (&optional handle)
105 (let ((article-buffer (current-buffer)))
107 (setq handle (mm-dissect-buffer t)))
109 (narrow-to-region (point) (point))
112 (let* ((coding-system-for-read 'utf-8)
113 (coding-system-for-write 'utf-8)
114 (default-process-coding-system
115 (cons coding-system-for-read coding-system-for-write))
116 (charset (mail-content-type-get (mm-handle-type handle)
119 (setq charset (mm-charset-to-coding-system
121 (not (eq charset 'ascii)))
123 (decode-coding-string (buffer-string) charset)
125 (mm-enable-multibyte))))
126 (call-process-region (point-min) (point-max)
128 nil article-buffer nil
133 "-o" "ext_halfdump=1"
134 "-o" "display_ins_del=2"
136 "-t" (format "%s" tab-width)
137 "-cols" (format "%s" gnus-html-frame-width)
138 "-o" "display_image=on"
140 (gnus-html-wash-tags))))
142 (defvar gnus-article-mouse-face)
144 (defun gnus-html-pre-wash ()
145 (goto-char (point-min))
146 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
147 (replace-match "" t t))
148 (goto-char (point-min))
149 (while (re-search-forward "<a name[^\n>]+>" nil t)
150 (replace-match "" t t)))
152 (defun gnus-html-wash-images ()
153 "Run through current buffer and replace img tags by images."
154 (let (tag parameters string start end images url alt-text
155 inhibit-images blocked-images)
156 (if (buffer-live-p gnus-summary-buffer)
157 (with-current-buffer gnus-summary-buffer
158 (setq inhibit-images gnus-inhibit-images
159 blocked-images (gnus-blocked-images)))
160 (setq inhibit-images gnus-inhibit-images
161 blocked-images (gnus-blocked-images)))
162 (goto-char (point-min))
163 ;; Search for all the images first.
164 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
165 (setq parameters (match-string 1)
166 start (match-beginning 0))
167 (delete-region start (point))
168 (when (search-forward "</img_alt>" (line-end-position) t)
169 (delete-region (match-beginning 0) (match-end 0)))
171 (when (string-match "src=\"\\([^\"]+\\)" parameters)
172 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
173 (setq url (gnus-html-encode-url (match-string 1 parameters))
174 alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
176 (xml-substitute-special (match-string 2 parameters))))
180 'image-displayer `(lambda (url start end)
181 (gnus-html-display-image url start end
183 'gnus-image (list url start end alt-text)))
184 (widget-convert-button
185 'url-link start (point)
187 :keymap gnus-html-image-map
189 (if (string-match "\\`cid:" url)
190 ;; URLs with cid: have their content stashed in other
191 ;; parts of the MIME structure, so just insert them
193 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
194 (image (when (and handle
195 (not inhibit-images))
197 (mm-with-part handle (buffer-string))
204 image (gnus-html-maximum-image-size))
205 (gnus-string-or (prog1
206 (buffer-substring start end)
207 (delete-region start end))
210 (widget-convert-button
212 :action 'gnus-html-insert-image
214 :keymap gnus-html-image-map
215 :button-keymap gnus-html-image-map)))
216 ;; Normal, external URL.
217 (if (or inhibit-images
218 (gnus-html-image-url-blocked-p url blocked-images))
219 (widget-convert-button
221 :action 'gnus-html-insert-image
223 :keymap gnus-html-image-map
224 :button-keymap gnus-html-image-map)
227 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
228 (string-to-number (match-string 1 parameters))))
230 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
231 (string-to-number (match-string 1 parameters)))))
232 ;; Don't fetch images that are really small. They're
233 ;; probably tracking pictures.
234 (when (and (or (null height)
238 (gnus-html-display-image url start end alt-text)))))))))
240 (defun gnus-html-display-image (url start end &optional alt-text)
241 "Display image at URL on text from START to END.
242 Use ALT-TEXT for the image string."
243 (or alt-text (setq alt-text "*"))
244 (if (string-match "\\`cid:" url)
245 (let ((handle (mm-get-content-id (substring url (match-end 0)))))
247 (gnus-html-put-image (mm-with-part handle (buffer-string))
249 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
250 ;; We don't have it, so schedule it for fetching
252 (gnus-html-schedule-image-fetching
255 ;; It's already cached, so just insert it.
256 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
258 (defun gnus-html-wash-tags ()
259 (let (tag parameters string start end images url)
261 (gnus-html-wash-images)
263 (goto-char (point-min))
264 ;; Then do the other tags.
265 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
266 (setq tag (match-string 1)
267 parameters (match-string 2)
268 start (match-beginning 0))
269 (when (> (length parameters) 0)
270 (set-text-properties 0 (1- (length parameters)) nil parameters))
271 (delete-region start (point))
272 (when (search-forward (concat "</" tag ">") nil t)
273 (delete-region (match-beginning 0) (match-end 0)))
276 ;; Fetch and insert a picture.
277 ((equal tag "img_alt"))
281 (when (string-match "href=\"\\([^\"]+\\)" parameters)
282 (setq url (match-string 1 parameters))
283 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
284 (gnus-article-add-button start end
285 'browse-url (mm-url-decode-entities-string url)
287 (let ((overlay (make-overlay start end)))
288 (overlay-put overlay 'evaporate t)
289 (overlay-put overlay 'gnus-button-url url)
290 (put-text-property start end 'gnus-string url)
291 (when gnus-article-mouse-face
292 (overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
293 ;; The upper-case IMG_ALT is apparently just an artifact that
294 ;; should be deleted.
295 ((equal tag "IMG_ALT")
296 (delete-region start end))
297 ;; w3m does not normalize the case
300 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-bold))
303 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
306 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-italic))
309 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-strikethru))
310 ((or (equal tag "ins")
312 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
313 ;; Handle different UL types
314 ((equal tag "_SYMBOL")
315 (when (string-match "TYPE=\\(.+\\)" parameters)
316 (let ((type (string-to-number (match-string 1 parameters))))
317 (delete-region start end)
318 (cond ((= type 33) (insert " "))
319 ((= type 34) (insert " "))
320 ((= type 35) (insert " "))
321 ((= type 36) (insert " "))
322 ((= type 37) (insert " "))
323 ((= type 38) (insert " "))
324 ((= type 39) (insert " "))
325 ((= type 40) (insert " "))
326 ((= type 42) (insert " "))
327 ((= type 43) (insert " "))
329 ;; Whatever. Just ignore the tag.
333 (goto-char (point-min))
334 ;; The output from -halfdump isn't totally regular, so strip
335 ;; off any </pre_int>s that were left over.
336 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
337 (replace-match "" t t))
338 (mm-url-decode-entities)))
340 (defun gnus-html-insert-image (&rest args)
341 "Fetch and insert the image under point."
343 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
345 (defun gnus-html-show-alt-text ()
346 "Show the ALT text of the image under point."
348 (message "%s" (get-text-property (point) 'gnus-alt-text)))
350 (defun gnus-html-browse-image ()
351 "Browse the image under point."
353 (browse-url (get-text-property (point) 'image-url)))
355 (defun gnus-html-browse-url ()
356 "Browse the image under point."
358 (let ((url (get-text-property (point) 'gnus-string)))
361 (message "No link under point"))
362 ((string-match "^mailto:" url)
363 (gnus-url-mailto url))
367 (defun gnus-html-schedule-image-fetching (buffer image)
368 "Retrieve IMAGE, and place it into BUFFER on arrival."
369 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
371 (url-queue-retrieve (car image)
372 'gnus-html-image-fetched
373 (list buffer image) t t))
375 (defun gnus-html-image-fetched (status buffer image)
376 "Callback function called when image has been fetched."
377 (unless (plist-get status :error)
378 (when (and (or (search-forward "\n\n" nil t)
379 (search-forward "\r\n\r\n" nil t))
381 (when gnus-html-image-automatic-caching
382 (url-store-in-cache (current-buffer)))
383 (when (buffer-live-p buffer)
384 (let ((data (buffer-substring (point) (point-max))))
385 (with-current-buffer buffer
386 (let ((inhibit-read-only t))
387 (gnus-html-put-image data (car image) (cadr image))))))))
388 (kill-buffer (current-buffer)))
390 (defun gnus-html-get-image-data (url)
391 "Get image data for URL.
392 Return a string with image data."
394 (mm-disable-multibyte)
395 (url-cache-extract (url-cache-create-filename url))
396 (when (or (search-forward "\n\n" nil t)
397 (search-forward "\r\n\r\n" nil t))
398 (buffer-substring (point) (point-max)))))
400 (defun gnus-html-maximum-image-size ()
401 "Return the maximum size of an image according to `gnus-max-image-proportion'."
402 (let ((edges (window-inside-pixel-edges
403 (get-buffer-window (current-buffer)))))
408 (* gnus-max-image-proportion
409 (- (nth 2 edges) (nth 0 edges))))
411 (truncate (* gnus-max-image-proportion
412 (- (nth 3 edges) (nth 1 edges)))))))
414 ;; Behind display-graphic-p test.
415 (declare-function image-size "image.c" (spec &optional pixels frame))
417 (defun gnus-html-put-image (data url &optional alt-text)
418 "Put an image with DATA from URL and optional ALT-TEXT."
419 (when (display-graphic-p)
420 (let* ((start (text-property-any (point-min) (point-max)
423 (next-single-property-change start 'image-url))))
428 (gnus-create-image data nil t)))
429 (size (and image (image-size image t))))
432 (let ((alt-text (or alt-text
433 (buffer-substring-no-properties start end)))
434 (inhibit-read-only t))
436 ;; Kludge to avoid displaying 30x30 gif images, which
437 ;; seems to be a signal of a broken image.
438 (not (and (listp image)
439 (eq (plist-get (cdr image) :type)
443 ;; Good image, add it!
444 (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
445 (delete-region start end)
446 (gnus-put-image image alt-text 'external)
447 (widget-convert-button
448 'url-link start (point)
450 :keymap gnus-html-displayed-image-map
452 (put-text-property start (point) 'gnus-alt-text alt-text)
459 (lambda (url start end)
460 (gnus-html-display-image url start end
462 (gnus-add-image 'external image)
464 ;; Bad image, try to show something else
465 (when (fboundp 'find-image)
466 (delete-region start end)
467 (setq image (find-image
468 '((:type xpm :file "lock-broken.xpm"))))
469 (gnus-put-image image alt-text 'internal)
470 (gnus-add-image 'internal image))
473 (defun gnus-html-image-url-blocked-p (url blocked-images)
474 "Find out if URL is blocked by BLOCKED-IMAGES."
475 (let ((ret (and blocked-images
476 (string-match blocked-images url))))
478 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
480 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
485 (defun gnus-html-prefetch-images (summary)
486 (when (buffer-live-p summary)
487 (let (inhibit-images blocked-images)
488 (with-current-buffer summary
489 (setq inhibit-images gnus-inhibit-images
490 blocked-images (gnus-blocked-images)))
492 (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
493 (let ((url (gnus-html-encode-url
494 (mm-url-decode-entities-string (match-string 1)))))
495 (unless (or inhibit-images
496 (gnus-html-image-url-blocked-p url blocked-images))
497 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
498 (gnus-html-schedule-image-fetching nil
503 ;;; gnus-html.el ends here