]> code.delx.au - gnu-emacs/blob - lisp/gnus/gnus-html.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / gnus / gnus-html.el
1 ;;; gnus-html.el --- Render HTML in a buffer.
2
3 ;; Copyright (C) 2010-2016 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html, web
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
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).
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus-art)
34 (eval-when-compile (require 'mm-decode))
35
36 (require 'mm-url)
37 (require 'url)
38 (require 'url-cache)
39 (require 'xml)
40 (require 'browse-url)
41 (require 'mm-util)
42 (require 'help-fns)
43 (require 'url-queue)
44
45 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
46 "Time used to determine if we should use images from the cache."
47 :version "24.1"
48 :group 'gnus-art
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))
53
54 (defcustom gnus-html-image-automatic-caching t
55 "Whether automatically cache retrieve images."
56 :version "24.1"
57 :group 'gnus-art
58 :type 'boolean)
59
60 (defcustom gnus-html-frame-width 70
61 "What width to use when rendering HTML."
62 :version "24.1"
63 :group 'gnus-art
64 :type 'integer)
65
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
71 fit these criteria."
72 :version "24.1"
73 :group 'gnus-art
74 :type 'float)
75
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)
81 map))
82
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)
90 map))
91
92 (defun gnus-html-encode-url (url)
93 "Encode URL."
94 (browse-url-url-encode-chars url "[)$ ]"))
95
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)))
101 (if cache-time
102 (time-less-p
103 (time-add
104 cache-time
105 ttl)
106 (current-time))
107 t)))))
108
109 ;;;###autoload
110 (defun gnus-article-html (&optional handle)
111 (let ((article-buffer (current-buffer)))
112 (unless handle
113 (setq handle (mm-dissect-buffer t)))
114 (save-restriction
115 (narrow-to-region (point) (point))
116 (save-excursion
117 (mm-with-part handle
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)
123 'charset)))
124 (when (and charset
125 (setq charset (mm-charset-to-coding-system
126 charset nil t))
127 (not (eq charset 'ascii)))
128 (insert (prog1
129 (decode-coding-string (buffer-string) charset)
130 (erase-buffer)
131 (mm-enable-multibyte))))
132 (call-process-region (point-min) (point-max)
133 "w3m"
134 nil article-buffer nil
135 "-halfdump"
136 "-no-cookie"
137 "-I" "UTF-8"
138 "-O" "UTF-8"
139 "-o" "ext_halfdump=1"
140 "-o" "display_ins_del=2"
141 "-o" "pre_conv=1"
142 "-t" (format "%s" tab-width)
143 "-cols" (format "%s" gnus-html-frame-width)
144 "-o" "display_image=on"
145 "-T" "text/html"))))
146 (gnus-html-wash-tags))))
147
148 (defvar gnus-article-mouse-face)
149
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)))
157
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)))
176 (setq end (point))
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\\)=\"\\([^\"]+\\)"
181 parameters)
182 (xml-substitute-special (match-string 2 parameters))))
183 (add-text-properties
184 start end
185 (list 'image-url url
186 'image-displayer `(lambda (url start end)
187 (gnus-html-display-image url start end
188 ,alt-text))
189 'gnus-image (list url start end alt-text)))
190 (widget-convert-button
191 'url-link start (point)
192 :help-echo alt-text
193 :keymap gnus-html-image-map
194 url)
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
198 ;; immediately.
199 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
200 (image (when (and handle
201 (not inhibit-images))
202 (gnus-create-image
203 (mm-with-part handle (buffer-string))
204 nil t))))
205 (if image
206 (gnus-add-image
207 'cid
208 (gnus-put-image
209 (gnus-rescale-image
210 image (gnus-html-maximum-image-size))
211 (gnus-string-or (prog1
212 (buffer-substring start end)
213 (delete-region start end))
214 "*")
215 'cid))
216 (widget-convert-button
217 'link start end
218 :action 'gnus-html-insert-image
219 :help-echo url
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
226 'link start end
227 :action 'gnus-html-insert-image
228 :help-echo url
229 :keymap gnus-html-image-map
230 :button-keymap gnus-html-image-map)
231 ;; Non-blocked url
232 (let ((width
233 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
234 (string-to-number (match-string 1 parameters))))
235 (height
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)
241 (> height 4))
242 (or (null width)
243 (> width 4)))
244 (gnus-html-display-image url start end alt-text)))))))))
245
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)))))
252 (when handle
253 (gnus-html-put-image (mm-with-part handle (buffer-string))
254 url alt-text)))
255 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
256 ;; We don't have it, so schedule it for fetching
257 ;; asynchronously.
258 (gnus-html-schedule-image-fetching
259 (current-buffer)
260 (list url alt-text))
261 ;; It's already cached, so just insert it.
262 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
263
264 (defun gnus-html-wash-tags ()
265 (let (tag parameters string start end images url)
266 (gnus-html-pre-wash)
267 (gnus-html-wash-images)
268
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)))
280 (setq end (point))
281 (cond
282 ;; Fetch and insert a picture.
283 ((equal tag "img_alt"))
284 ;; Add a link.
285 ((or (equal tag "a")
286 (equal tag "A"))
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)
292 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
304 ((or (equal tag "b")
305 (equal tag "B"))
306 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-bold))
307 ((or (equal tag "u")
308 (equal tag "U"))
309 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
310 ((or (equal tag "i")
311 (equal tag "I"))
312 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-italic))
313 ((or (equal tag "s")
314 (equal tag "S"))
315 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-strikethru))
316 ((or (equal tag "ins")
317 (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 " "))
334 (t (insert " "))))))
335 ;; Whatever. Just ignore the tag.
336 (t
337 ))
338 (goto-char start))
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)))
345
346 (defun gnus-html-insert-image (&rest args)
347 "Fetch and insert the image under point."
348 (interactive)
349 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
350
351 (defun gnus-html-show-alt-text ()
352 "Show the ALT text of the image under point."
353 (interactive)
354 (message "%s" (get-text-property (point) 'gnus-alt-text)))
355
356 (defun gnus-html-browse-image ()
357 "Browse the image under point."
358 (interactive)
359 (browse-url (get-text-property (point) 'image-url)))
360
361 (defun gnus-html-browse-url ()
362 "Browse the image under point."
363 (interactive)
364 (let ((url (get-text-property (point) 'gnus-string)))
365 (cond
366 ((not url)
367 (message "No link under point"))
368 ((string-match "^mailto:" url)
369 (gnus-url-mailto url))
370 (t
371 (browse-url url)))))
372
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"
376 buffer image)
377 (url-queue-retrieve (car image)
378 'gnus-html-image-fetched
379 (list buffer image) t t))
380
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))
386 (not (eobp)))
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)))
395
396 (defun gnus-html-get-image-data (url)
397 "Get image data for URL.
398 Return a string with image data."
399 (with-temp-buffer
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)))))
405
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)))))
410 ;; (width . height)
411 (cons
412 ;; Aimed width
413 (truncate
414 (* gnus-max-image-proportion
415 (- (nth 2 edges) (nth 0 edges))))
416 ;; Aimed height
417 (truncate (* gnus-max-image-proportion
418 (- (nth 3 edges) (nth 1 edges)))))))
419
420 ;; Behind display-graphic-p test.
421 (declare-function image-size "image.c" (spec &optional pixels frame))
422
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)
427 'image-url url))
428 (end (when start
429 (next-single-property-change start 'image-url))))
430 ;; Image found?
431 (when start
432 (let* ((image
433 (ignore-errors
434 (gnus-create-image data nil t)))
435 (size (and image (image-size image t))))
436 (save-excursion
437 (goto-char start)
438 (let ((alt-text (or alt-text
439 (buffer-substring-no-properties start end)))
440 (inhibit-read-only t))
441 (if (and image
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)
446 'gif)
447 (= (car size) 30)
448 (= (cdr size) 30))))
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)
455 :help-echo alt-text
456 :keymap gnus-html-displayed-image-map
457 url)
458 (put-text-property start (point) 'gnus-alt-text alt-text)
459 (when url
460 (add-text-properties
461 start (point)
462 `(image-url
463 ,url
464 image-displayer
465 (lambda (url start end)
466 (gnus-html-display-image url start end
467 ,alt-text)))))
468 (gnus-add-image 'external image)
469 t)
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))
477 nil))))))))
478
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))))
483 (if ret
484 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
485 url blocked-images)
486 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
487 url blocked-images))
488 ret))
489
490 ;;;###autoload
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)))
497 (save-match-data
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
505 (list url))))))))))
506
507 (provide 'gnus-html)
508
509 ;;; gnus-html.el ends here