1 ;;; gnus-html.el --- Render HTML in a buffer.
3 ;; Copyright (C) 2010-2013 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 <http://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
))
42 (eval-and-compile (unless (featurep 'xemacs
) (require 'help-fns
)))
44 (defcustom gnus-html-image-cache-ttl
(days-to-time 7)
45 "Time used to determine if we should use images from the cache."
48 ;; FIXME hardly the friendliest type. The allowed value is actually
49 ;; any time value, but we are assuming no-one cares about USEC and
50 ;; PSEC here. It would be better to eg make it a number of minutes.
51 :type
'(list integer integer
))
53 (defcustom gnus-html-image-automatic-caching t
54 "Whether automatically cache retrieve images."
59 (defcustom gnus-html-frame-width
70
60 "What width to use when rendering HTML."
65 (defcustom gnus-max-image-proportion
0.9
66 "How big pictures displayed are in relation to the window they're in.
67 A value of 0.7 means that they are allowed to take up 70% of the
68 width and height of the window. If they are larger than this,
69 and Emacs supports it, then the images will be rescaled down to
75 (defvar gnus-html-image-map
76 (let ((map (make-sparse-keymap)))
77 (define-key map
"u" 'gnus-article-copy-string
)
78 (define-key map
"i" 'gnus-html-insert-image
)
79 (define-key map
"v" 'gnus-html-browse-url
)
82 (defvar gnus-html-displayed-image-map
83 (let ((map (make-sparse-keymap)))
84 (define-key map
"a" 'gnus-html-show-alt-text
)
85 (define-key map
"i" 'gnus-html-browse-image
)
86 (define-key map
"\r" 'gnus-html-browse-url
)
87 (define-key map
"u" 'gnus-article-copy-string
)
88 (define-key map
[tab] 'widget-forward)
92 (defalias 'gnus-html-encode-url-chars
93 (if (fboundp 'browse-url-url-encode-chars)
94 'browse-url-url-encode-chars
96 "URL-encode the chars in TEXT that match CHARS.
97 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
98 (let ((encoded-text (copy-sequence text))
100 (while (setq s (string-match chars encoded-text s))
102 (replace-match (format "%%%x"
104 (match-string 0 encoded-text)))
109 (defun gnus-html-encode-url (url)
111 (gnus-html-encode-url-chars url "[)$ ]"))
113 (defun gnus-html-cache-expired (url ttl)
114 "Check if URL is cached for more than TTL."
115 (cond (url-standalone-mode
116 (not (file-exists-p (url-cache-create-filename url))))
117 (t (let ((cache-time (url-is-cached url)))
127 (defun gnus-article-html (&optional handle)
128 (let ((article-buffer (current-buffer)))
130 (setq handle (mm-dissect-buffer t)))
132 (narrow-to-region (point) (point))
135 (let* ((coding-system-for-read 'utf-8)
136 (coding-system-for-write 'utf-8)
137 (default-process-coding-system
138 (cons coding-system-for-read coding-system-for-write))
139 (charset (mail-content-type-get (mm-handle-type handle)
142 (setq charset (mm-charset-to-coding-system charset))
143 (not (eq charset 'ascii)))
145 (mm-decode-coding-string (buffer-string) charset)
147 (mm-enable-multibyte))))
148 (call-process-region (point-min) (point-max)
150 nil article-buffer nil
155 "-o" "ext_halfdump=1"
156 "-o" "display_ins_del=2"
158 "-t" (format "%s" tab-width)
159 "-cols" (format "%s" gnus-html-frame-width)
160 "-o" "display_image=on"
162 (gnus-html-wash-tags))))
164 (defvar gnus-article-mouse-face)
166 (defun gnus-html-pre-wash ()
167 (goto-char (point-min))
168 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
169 (replace-match "" t t))
170 (goto-char (point-min))
171 (while (re-search-forward "<a name[^\n>]+>" nil t)
172 (replace-match "" t t)))
174 (defun gnus-html-wash-images ()
175 "Run through current buffer and replace img tags by images."
176 (let (tag parameters string start end images url alt-text
177 inhibit-images blocked-images)
178 (if (buffer-live-p gnus-summary-buffer)
179 (with-current-buffer gnus-summary-buffer
180 (setq inhibit-images gnus-inhibit-images
181 blocked-images (gnus-blocked-images)))
182 (setq inhibit-images gnus-inhibit-images
183 blocked-images (gnus-blocked-images)))
184 (goto-char (point-min))
185 ;; Search for all the images first.
186 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
187 (setq parameters (match-string 1)
188 start (match-beginning 0))
189 (delete-region start (point))
190 (when (search-forward "</img_alt>" (line-end-position) t)
191 (delete-region (match-beginning 0) (match-end 0)))
193 (when (string-match "src=\"\\([^\"]+\\)" parameters)
194 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
195 (setq url (gnus-html-encode-url (match-string 1 parameters))
196 alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
198 (xml-substitute-special (match-string 2 parameters))))
199 (gnus-add-text-properties
202 'image-displayer `(lambda (url start end)
203 (gnus-html-display-image url start end
205 'gnus-image (list url start end alt-text)))
206 (widget-convert-button
207 'url-link start (point)
209 :keymap gnus-html-image-map
211 (if (string-match "\\`cid:" url)
212 ;; URLs with cid: have their content stashed in other
213 ;; parts of the MIME structure, so just insert them
215 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
216 (image (when (and handle
217 (not inhibit-images))
219 (mm-with-part handle (buffer-string))
226 image (gnus-html-maximum-image-size))
227 (gnus-string-or (prog1
228 (buffer-substring start end)
229 (delete-region start end))
232 (widget-convert-button
234 :action 'gnus-html-insert-image
236 :keymap gnus-html-image-map
237 :button-keymap gnus-html-image-map)))
238 ;; Normal, external URL.
239 (if (or inhibit-images
240 (gnus-html-image-url-blocked-p url blocked-images))
241 (widget-convert-button
243 :action 'gnus-html-insert-image
245 :keymap gnus-html-image-map
246 :button-keymap gnus-html-image-map)
249 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
250 (string-to-number (match-string 1 parameters))))
252 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
253 (string-to-number (match-string 1 parameters)))))
254 ;; Don't fetch images that are really small. They're
255 ;; probably tracking pictures.
256 (when (and (or (null height)
260 (gnus-html-display-image url start end alt-text)))))))))
262 (defun gnus-html-display-image (url start end &optional alt-text)
263 "Display image at URL on text from START to END.
264 Use ALT-TEXT for the image string."
265 (or alt-text (setq alt-text "*"))
266 (if (string-match "\\`cid:" url)
267 (let ((handle (mm-get-content-id (substring url (match-end 0)))))
269 (gnus-html-put-image (mm-with-part handle (buffer-string))
271 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
272 ;; We don't have it, so schedule it for fetching
274 (gnus-html-schedule-image-fetching
277 ;; It's already cached, so just insert it.
278 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
280 (defun gnus-html-wash-tags ()
281 (let (tag parameters string start end images url)
283 (gnus-html-wash-images)
285 (goto-char (point-min))
286 ;; Then do the other tags.
287 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
288 (setq tag (match-string 1)
289 parameters (match-string 2)
290 start (match-beginning 0))
291 (when (> (length parameters) 0)
292 (set-text-properties 0 (1- (length parameters)) nil parameters))
293 (delete-region start (point))
294 (when (search-forward (concat "</" tag ">") nil t)
295 (delete-region (match-beginning 0) (match-end 0)))
298 ;; Fetch and insert a picture.
299 ((equal tag "img_alt"))
303 (when (string-match "href=\"\\([^\"]+\\)" parameters)
304 (setq url (match-string 1 parameters))
305 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
306 (gnus-article-add-button start end
307 'browse-url (mm-url-decode-entities-string url)
309 (let ((overlay (gnus-make-overlay start end)))
310 (gnus-overlay-put overlay 'evaporate t)
311 (gnus-overlay-put overlay 'gnus-button-url url)
312 (gnus-put-text-property start end 'gnus-string url)
313 (when gnus-article-mouse-face
314 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
315 ;; The upper-case IMG_ALT is apparently just an artifact that
316 ;; should be deleted.
317 ((equal tag "IMG_ALT")
318 (delete-region start end))
319 ;; w3m does not normalize the case
322 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
325 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
328 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
331 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
332 ((or (equal tag "ins")
334 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
335 ;; Handle different UL types
336 ((equal tag "_SYMBOL")
337 (when (string-match "TYPE=\\(.+\\)" parameters)
338 (let ((type (string-to-number (match-string 1 parameters))))
339 (delete-region start end)
340 (cond ((= type 33) (insert " "))
341 ((= type 34) (insert " "))
342 ((= type 35) (insert " "))
343 ((= type 36) (insert " "))
344 ((= type 37) (insert " "))
345 ((= type 38) (insert " "))
346 ((= type 39) (insert " "))
347 ((= type 40) (insert " "))
348 ((= type 42) (insert " "))
349 ((= type 43) (insert " "))
351 ;; Whatever. Just ignore the tag.
355 (goto-char (point-min))
356 ;; The output from -halfdump isn't totally regular, so strip
357 ;; off any </pre_int>s that were left over.
358 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
359 (replace-match "" t t))
360 (mm-url-decode-entities)))
362 (defun gnus-html-insert-image (&rest args)
363 "Fetch and insert the image under point."
365 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
367 (defun gnus-html-show-alt-text ()
368 "Show the ALT text of the image under point."
370 (message "%s" (get-text-property (point) 'gnus-alt-text)))
372 (defun gnus-html-browse-image ()
373 "Browse the image under point."
375 (browse-url (get-text-property (point) 'image-url)))
377 (defun gnus-html-browse-url ()
378 "Browse the image under point."
380 (let ((url (get-text-property (point) 'gnus-string)))
383 (message "No link under point"))
384 ((string-match "^mailto:" url)
385 (gnus-url-mailto url))
389 (defun gnus-html-schedule-image-fetching (buffer image)
390 "Retrieve IMAGE, and place it into BUFFER on arrival."
391 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
393 (if (fboundp 'url-queue-retrieve)
394 (url-queue-retrieve (car image)
395 'gnus-html-image-fetched
396 (list buffer image) t t)
398 (url-retrieve (car image)
399 'gnus-html-image-fetched
400 (list buffer image)))))
402 (defun gnus-html-image-fetched (status buffer image)
403 "Callback function called when image has been fetched."
404 (unless (plist-get status :error)
405 (when (and (or (search-forward "\n\n" nil t)
406 (search-forward "\r\n\r\n" nil t))
408 (when gnus-html-image-automatic-caching
409 (url-store-in-cache (current-buffer)))
410 (when (buffer-live-p buffer)
411 (let ((data (buffer-substring (point) (point-max))))
412 (with-current-buffer buffer
413 (let ((inhibit-read-only t))
414 (gnus-html-put-image data (car image) (cadr image))))))))
415 (kill-buffer (current-buffer)))
417 (defun gnus-html-get-image-data (url)
418 "Get image data for URL.
419 Return a string with image data."
421 (mm-disable-multibyte)
422 (url-cache-extract (url-cache-create-filename url))
423 (when (or (search-forward "\n\n" nil t)
424 (search-forward "\r\n\r\n" nil t))
425 (buffer-substring (point) (point-max)))))
427 (defun gnus-html-maximum-image-size ()
428 "Return the maximum size of an image according to `gnus-max-image-proportion'."
429 (let ((edges (gnus-window-inside-pixel-edges
430 (get-buffer-window (current-buffer)))))
435 (* gnus-max-image-proportion
436 (- (nth 2 edges) (nth 0 edges))))
438 (truncate (* gnus-max-image-proportion
439 (- (nth 3 edges) (nth 1 edges)))))))
441 (defun gnus-html-put-image (data url &optional alt-text)
442 "Put an image with DATA from URL and optional ALT-TEXT."
443 (when (gnus-graphic-display-p)
444 (let* ((start (text-property-any (point-min) (point-max)
447 (next-single-property-change start 'image-url))))
452 (gnus-create-image data nil t)))
454 (if (featurep 'xemacs)
455 (cons (glyph-width image) (glyph-height image))
456 (image-size image t)))))
459 (let ((alt-text (or alt-text
460 (buffer-substring-no-properties start end)))
461 (inhibit-read-only t))
463 ;; Kludge to avoid displaying 30x30 gif images, which
464 ;; seems to be a signal of a broken image.
465 (not (and (if (featurep 'xemacs)
468 (eq (if (featurep 'xemacs)
471 (glyph-image image)))))
474 (plist-get (cdr image) :type))
478 ;; Good image, add it!
479 (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
480 (delete-region start end)
481 (gnus-put-image image alt-text 'external)
482 (widget-convert-button
483 'url-link start (point)
485 :keymap gnus-html-displayed-image-map
487 (gnus-put-text-property start (point)
488 'gnus-alt-text alt-text)
490 (gnus-add-text-properties
495 (lambda (url start end)
496 (gnus-html-display-image url start end
498 (gnus-add-image 'external image)
500 ;; Bad image, try to show something else
501 (when (fboundp 'find-image)
502 (delete-region start end)
503 (setq image (find-image
504 '((:type xpm :file "lock-broken.xpm"))))
505 (gnus-put-image image alt-text 'internal)
506 (gnus-add-image 'internal image))
509 (defun gnus-html-image-url-blocked-p (url blocked-images)
510 "Find out if URL is blocked by BLOCKED-IMAGES."
511 (let ((ret (and blocked-images
512 (string-match blocked-images url))))
514 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
516 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
521 (defun gnus-html-prefetch-images (summary)
522 (when (buffer-live-p summary)
523 (let (inhibit-images blocked-images)
524 (with-current-buffer summary
525 (setq inhibit-images gnus-inhibit-images
526 blocked-images (gnus-blocked-images)))
528 (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
529 (let ((url (gnus-html-encode-url
530 (mm-url-decode-entities-string (match-string 1)))))
531 (unless (or inhibit-images
532 (gnus-html-image-url-blocked-p url blocked-images))
533 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
534 (gnus-html-schedule-image-fetching nil
539 ;;; gnus-html.el ends here