Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / gnus / gnus-html.el
blobf097028cb3ec4b2c7792ccd3edd7bbd8b2227dbc
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>
6 ;; Keywords: html, web
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/>.
23 ;;; Commentary:
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).
29 ;;; Code:
31 (require 'gnus-art)
32 (eval-when-compile (require 'mm-decode))
34 (require 'mm-url)
35 (require 'url)
36 (require 'url-cache)
37 (require 'xml)
38 (require 'browse-url)
39 (require 'mm-util)
40 (require 'help-fns)
41 (require 'url-queue)
43 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
44 "Time used to determine if we should use images from the cache."
45 :version "24.1"
46 :group 'gnus-art
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."
54 :version "24.1"
55 :group 'gnus-art
56 :type 'boolean)
58 (defcustom gnus-html-frame-width 70
59 "What width to use when rendering HTML."
60 :version "24.1"
61 :group 'gnus-art
62 :type 'integer)
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
69 fit these criteria."
70 :version "24.1"
71 :group 'gnus-art
72 :type 'float)
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)
79 map))
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)
88 map))
90 (defun gnus-html-encode-url (url)
91 "Encode 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)))
99 (if cache-time
100 (time-less-p (time-add cache-time ttl) nil)
101 t)))))
103 ;;;###autoload
104 (defun gnus-article-html (&optional handle)
105 (let ((article-buffer (current-buffer)))
106 (unless handle
107 (setq handle (mm-dissect-buffer t)))
108 (save-restriction
109 (narrow-to-region (point) (point))
110 (save-excursion
111 (mm-with-part handle
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)
117 'charset)))
118 (when (and charset
119 (setq charset (mm-charset-to-coding-system
120 charset nil t))
121 (not (eq charset 'ascii)))
122 (insert (prog1
123 (decode-coding-string (buffer-string) charset)
124 (erase-buffer)
125 (mm-enable-multibyte))))
126 (call-process-region (point-min) (point-max)
127 "w3m"
128 nil article-buffer nil
129 "-halfdump"
130 "-no-cookie"
131 "-I" "UTF-8"
132 "-O" "UTF-8"
133 "-o" "ext_halfdump=1"
134 "-o" "display_ins_del=2"
135 "-o" "pre_conv=1"
136 "-t" (format "%s" tab-width)
137 "-cols" (format "%s" gnus-html-frame-width)
138 "-o" "display_image=on"
139 "-T" "text/html"))))
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)))
170 (setq end (point))
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\\)=\"\\([^\"]+\\)"
175 parameters)
176 (xml-substitute-special (match-string 2 parameters))))
177 (add-text-properties
178 start end
179 (list 'image-url url
180 'image-displayer `(lambda (url start end)
181 (gnus-html-display-image url start end
182 ,alt-text))
183 'gnus-image (list url start end alt-text)))
184 (widget-convert-button
185 'url-link start (point)
186 :help-echo alt-text
187 :keymap gnus-html-image-map
188 url)
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
192 ;; immediately.
193 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
194 (image (when (and handle
195 (not inhibit-images))
196 (gnus-create-image
197 (mm-with-part handle (buffer-string))
198 nil t))))
199 (if image
200 (gnus-add-image
201 'cid
202 (gnus-put-image
203 (gnus-rescale-image
204 image (gnus-html-maximum-image-size))
205 (gnus-string-or (prog1
206 (buffer-substring start end)
207 (delete-region start end))
208 "*")
209 'cid))
210 (widget-convert-button
211 'link start end
212 :action 'gnus-html-insert-image
213 :help-echo url
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
220 'link start end
221 :action 'gnus-html-insert-image
222 :help-echo url
223 :keymap gnus-html-image-map
224 :button-keymap gnus-html-image-map)
225 ;; Non-blocked url
226 (let ((width
227 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
228 (string-to-number (match-string 1 parameters))))
229 (height
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)
235 (> height 4))
236 (or (null width)
237 (> width 4)))
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)))))
246 (when handle
247 (gnus-html-put-image (mm-with-part handle (buffer-string))
248 url alt-text)))
249 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
250 ;; We don't have it, so schedule it for fetching
251 ;; asynchronously.
252 (gnus-html-schedule-image-fetching
253 (current-buffer)
254 (list url alt-text))
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)
260 (gnus-html-pre-wash)
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)))
274 (setq end (point))
275 (cond
276 ;; Fetch and insert a picture.
277 ((equal tag "img_alt"))
278 ;; Add a link.
279 ((or (equal tag "a")
280 (equal tag "A"))
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)
286 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
298 ((or (equal tag "b")
299 (equal tag "B"))
300 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-bold))
301 ((or (equal tag "u")
302 (equal tag "U"))
303 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
304 ((or (equal tag "i")
305 (equal tag "I"))
306 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-italic))
307 ((or (equal tag "s")
308 (equal tag "S"))
309 (overlay-put (make-overlay start end) 'face 'gnus-emphasis-strikethru))
310 ((or (equal tag "ins")
311 (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 " "))
328 (t (insert " "))))))
329 ;; Whatever. Just ignore the tag.
332 (goto-char start))
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."
342 (interactive)
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."
347 (interactive)
348 (message "%s" (get-text-property (point) 'gnus-alt-text)))
350 (defun gnus-html-browse-image ()
351 "Browse the image under point."
352 (interactive)
353 (browse-url (get-text-property (point) 'image-url)))
355 (defun gnus-html-browse-url ()
356 "Browse the image under point."
357 (interactive)
358 (let ((url (get-text-property (point) 'gnus-string)))
359 (cond
360 ((not url)
361 (message "No link under point"))
362 ((string-match "^mailto:" url)
363 (gnus-url-mailto url))
365 (browse-url 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"
370 buffer image)
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))
380 (not (eobp)))
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."
393 (with-temp-buffer
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)))))
404 ;; (width . height)
405 (cons
406 ;; Aimed width
407 (truncate
408 (* gnus-max-image-proportion
409 (- (nth 2 edges) (nth 0 edges))))
410 ;; Aimed height
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)
421 'image-url url))
422 (end (when start
423 (next-single-property-change start 'image-url))))
424 ;; Image found?
425 (when start
426 (let* ((image
427 (ignore-errors
428 (gnus-create-image data nil t)))
429 (size (and image (image-size image t))))
430 (save-excursion
431 (goto-char start)
432 (let ((alt-text (or alt-text
433 (buffer-substring-no-properties start end)))
434 (inhibit-read-only t))
435 (if (and image
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)
440 'gif)
441 (= (car size) 30)
442 (= (cdr size) 30))))
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)
449 :help-echo alt-text
450 :keymap gnus-html-displayed-image-map
451 url)
452 (put-text-property start (point) 'gnus-alt-text alt-text)
453 (when url
454 (add-text-properties
455 start (point)
456 `(image-url
457 ,url
458 image-displayer
459 (lambda (url start end)
460 (gnus-html-display-image url start end
461 ,alt-text)))))
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))
471 nil))))))))
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))))
477 (if ret
478 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
479 url blocked-images)
480 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
481 url blocked-images))
482 ret))
484 ;;;###autoload
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)))
491 (save-match-data
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
499 (list url))))))))))
501 (provide 'gnus-html)
503 ;;; gnus-html.el ends here