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).
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)))
102 (time-less-p (time-add cache-time ttl) nil)
106 (defun gnus-article-html (&optional handle)
107 (let ((article-buffer (current-buffer)))
109 (setq handle (mm-dissect-buffer t)))
111 (narrow-to-region (point) (point))
114 (let* ((coding-system-for-read 'utf-8)
115 (coding-system-for-write 'utf-8)
116 (default-process-coding-system
117 (cons coding-system-for-read coding-system-for-write))
118 (charset (mail-content-type-get (mm-handle-type handle)
121 (setq charset (mm-charset-to-coding-system
123 (not (eq charset 'ascii)))
125 (decode-coding-string (buffer-string) charset)
127 (mm-enable-multibyte))))
128 (call-process-region (point-min) (point-max)
130 nil article-buffer nil
135 "-o" "ext_halfdump=1"
136 "-o" "display_ins_del=2"
138 "-t" (format "%s" tab-width)
139 "-cols" (format "%s" gnus-html-frame-width)
140 "-o" "display_image=on"
142 (gnus-html-wash-tags))))
144 (defvar gnus-article-mouse-face)
146 (defun gnus-html-pre-wash ()
147 (goto-char (point-min))
148 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
149 (replace-match "" t t))
150 (goto-char (point-min))
151 (while (re-search-forward "<a name[^\n>]+>" nil t)
152 (replace-match "" t t)))
154 (defun gnus-html-wash-images ()
155 "Run through current buffer and replace img tags by images."
156 (let (tag parameters string start end images url alt-text
157 inhibit-images blocked-images)
158 (if (buffer-live-p gnus-summary-buffer)
159 (with-current-buffer gnus-summary-buffer
160 (setq inhibit-images gnus-inhibit-images
161 blocked-images (gnus-blocked-images)))
162 (setq inhibit-images gnus-inhibit-images
163 blocked-images (gnus-blocked-images)))
164 (goto-char (point-min))
165 ;; Search for all the images first.
166 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
167 (setq parameters (match-string 1)
168 start (match-beginning 0))
169 (delete-region start (point))
170 (when (search-forward "</img_alt>" (line-end-position) t)
171 (delete-region (match-beginning 0) (match-end 0)))
173 (when (string-match "src=\"\\([^\"]+\\)" parameters)
174 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
175 (setq url (gnus-html-encode-url (match-string 1 parameters))
176 alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
178 (xml-substitute-special (match-string 2 parameters))))
182 'image-displayer `(lambda (url start end)
183 (gnus-html-display-image url start end
185 'gnus-image (list url start end alt-text)))
186 (widget-convert-button
187 'url-link start (point)
189 :keymap gnus-html-image-map
191 (if (string-match "\\`cid:" url)
192 ;; URLs with cid: have their content stashed in other
193 ;; parts of the MIME structure, so just insert them
195 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
196 (image (when (and handle
197 (not inhibit-images))
199 (mm-with-part handle (buffer-string))
206 image (gnus-html-maximum-image-size))
207 (gnus-string-or (prog1
208 (buffer-substring start end)
209 (delete-region start end))
212 (widget-convert-button
214 :action 'gnus-html-insert-image
216 :keymap gnus-html-image-map
217 :button-keymap gnus-html-image-map)))
218 ;; Normal, external URL.
219 (if (or inhibit-images
220 (gnus-html-image-url-blocked-p url blocked-images))
221 (widget-convert-button
223 :action 'gnus-html-insert-image
225 :keymap gnus-html-image-map
226 :button-keymap gnus-html-image-map)
229 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
230 (string-to-number (match-string 1 parameters))))
232 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
233 (string-to-number (match-string 1 parameters)))))
234 ;; Don't fetch images that are really small. They're
235 ;; probably tracking pictures.
236 (when (and (or (null height)
240 (gnus-html-display-image url start end alt-text)))))))))
242 (defun gnus-html-display-image (url start end &optional alt-text)
243 "Display image at URL on text from START to END.
244 Use ALT-TEXT for the image string."
245 (or alt-text (setq alt-text "*"))
246 (if (string-match "\\`cid:" url)
247 (let ((handle (mm-get-content-id (substring url (match-end 0)))))
249 (gnus-html-put-image (mm-with-part handle (buffer-string))
251 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
252 ;; We don't have it, so schedule it for fetching
254 (gnus-html-schedule-image-fetching
257 ;; It's already cached, so just insert it.
258 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
260 (defun gnus-html-wash-tags ()
261 (let (tag parameters string start end images url)
263 (gnus-html-wash-images)
265 (goto-char (point-min))
266 ;; Then do the other tags.
267 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
268 (setq tag (match-string 1)
269 parameters (match-string 2)
270 start (match-beginning 0))
271 (when (> (length parameters) 0)
272 (set-text-properties 0 (1- (length parameters)) nil parameters))
273 (delete-region start (point))
274 (when (search-forward (concat "</" tag ">") nil t)
275 (delete-region (match-beginning 0) (match-end 0)))
278 ;; Fetch and insert a picture.
279 ((equal tag "img_alt"))
283 (when (string-match "href=\"\\([^\"]+\\)" parameters)
284 (setq url (match-string 1 parameters))
285 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
286 (gnus-article-add-button start end
287 'browse-url (mm-url-decode-entities-string url)
289 (let ((overlay (make-overlay start end)))
290 (overlay-put overlay 'evaporate t)
291 (overlay-put overlay 'gnus-button-url url)
292 (put-text-property start end 'gnus-string url)
293 (when gnus-article-mouse-face
294 (overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
295 ;; The upper-case IMG_ALT is apparently just an artifact that
296 ;; should be deleted.
297 ((equal tag "IMG_ALT")
298 (delete-region start end))
299 ;; w3m does not normalize the case
302 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-bold))
305 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
308 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-italic))
311 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-strikethru))
312 ((or (equal tag "ins")
314 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
315 ;; Handle different UL types
316 ((equal tag "_SYMBOL")
317 (when (string-match "TYPE=\\(.+\\)" parameters)
318 (let ((type (string-to-number (match-string 1 parameters))))
319 (delete-region start end)
320 (cond ((= type 33) (insert " "))
321 ((= type 34) (insert " "))
322 ((= type 35) (insert " "))
323 ((= type 36) (insert " "))
324 ((= type 37) (insert " "))
325 ((= type 38) (insert " "))
326 ((= type 39) (insert " "))
327 ((= type 40) (insert " "))
328 ((= type 42) (insert " "))
329 ((= type 43) (insert " "))
331 ;; Whatever. Just ignore the tag.
335 (goto-char (point-min))
336 ;; The output from -halfdump isn't totally regular, so strip
337 ;; off any </pre_int>s that were left over.
338 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
339 (replace-match "" t t))
340 (mm-url-decode-entities)))
342 (defun gnus-html-insert-image (&rest args)
343 "Fetch and insert the image under point."
345 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
347 (defun gnus-html-show-alt-text ()
348 "Show the ALT text of the image under point."
350 (message "%s" (get-text-property (point) 'gnus-alt-text)))
352 (defun gnus-html-browse-image ()
353 "Browse the image under point."
355 (browse-url (get-text-property (point) 'image-url)))
357 (defun gnus-html-browse-url ()
358 "Browse the image under point."
360 (let ((url (get-text-property (point) 'gnus-string)))
363 (message "No link under point"))
364 ((string-match "^mailto:" url)
365 (gnus-url-mailto url))
369 (defun gnus-html-schedule-image-fetching (buffer image)
370 "Retrieve IMAGE, and place it into BUFFER on arrival."
371 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
373 (url-queue-retrieve (car image)
374 'gnus-html-image-fetched
375 (list buffer image) t t))
377 (defun gnus-html-image-fetched (status buffer image)
378 "Callback function called when image has been fetched."
379 (unless (plist-get status :error)
380 (when (and (or (search-forward "\n\n" nil t)
381 (search-forward "\r\n\r\n" nil t))
383 (when gnus-html-image-automatic-caching
384 (url-store-in-cache (current-buffer)))
385 (when (buffer-live-p buffer)
386 (let ((data (buffer-substring (point) (point-max))))
387 (with-current-buffer buffer
388 (let ((inhibit-read-only t))
389 (gnus-html-put-image data (car image) (cadr image))))))))
390 (kill-buffer (current-buffer)))
392 (defun gnus-html-get-image-data (url)
393 "Get image data for URL.
394 Return a string with image data."
396 (mm-disable-multibyte)
397 (url-cache-extract (url-cache-create-filename url))
398 (when (or (search-forward "\n\n" nil t)
399 (search-forward "\r\n\r\n" nil t))
400 (buffer-substring (point) (point-max)))))
402 (defun gnus-html-maximum-image-size ()
403 "Return the maximum size of an image according to `gnus-max-image-proportion'."
404 (let ((edges (window-inside-pixel-edges
405 (get-buffer-window (current-buffer)))))
410 (* gnus-max-image-proportion
411 (- (nth 2 edges) (nth 0 edges))))
413 (truncate (* gnus-max-image-proportion
414 (- (nth 3 edges) (nth 1 edges)))))))
416 ;; Behind display-graphic-p test.
417 (declare-function image-size "image.c" (spec &optional pixels frame))
419 (defun gnus-html-put-image (data url &optional alt-text)
420 "Put an image with DATA from URL and optional ALT-TEXT."
421 (when (display-graphic-p)
422 (let* ((start (text-property-any (point-min) (point-max)
425 (next-single-property-change start 'image-url))))
430 (gnus-create-image data nil t)))
431 (size (and image (image-size image t))))
434 (let ((alt-text (or alt-text
435 (buffer-substring-no-properties start end)))
436 (inhibit-read-only t))
438 ;; Kludge to avoid displaying 30x30 gif images, which
439 ;; seems to be a signal of a broken image.
440 (not (and (listp image)
441 (eq (plist-get (cdr image) :type)
445 ;; Good image, add it!
446 (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
447 (delete-region start end)
448 (gnus-put-image image alt-text 'external)
449 (widget-convert-button
450 'url-link start (point)
452 :keymap gnus-html-displayed-image-map
454 (put-text-property start (point) 'gnus-alt-text alt-text)
461 (lambda (url start end)
462 (gnus-html-display-image url start end
464 (gnus-add-image 'external image)
466 ;; Bad image, try to show something else
467 (when (fboundp 'find-image)
468 (delete-region start end)
469 (setq image (find-image
470 '((:type xpm :file "lock-broken.xpm"))))
471 (gnus-put-image image alt-text 'internal)
472 (gnus-add-image 'internal image))
475 (defun gnus-html-image-url-blocked-p (url blocked-images)
476 "Find out if URL is blocked by BLOCKED-IMAGES."
477 (let ((ret (and blocked-images
478 (string-match blocked-images url))))
480 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
482 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
487 (defun gnus-html-prefetch-images (summary)
488 (when (buffer-live-p summary)
489 (let (inhibit-images blocked-images)
490 (with-current-buffer summary
491 (setq inhibit-images gnus-inhibit-images
492 blocked-images (gnus-blocked-images)))
494 (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
495 (let ((url (gnus-html-encode-url
496 (mm-url-decode-entities-string (match-string 1)))))
497 (unless (or inhibit-images
498 (gnus-html-image-url-blocked-p url blocked-images))
499 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
500 (gnus-html-schedule-image-fetching nil
505 ;;; gnus-html.el ends here