1 ;;; gnus-html.el --- Render HTML in a buffer.
3 ;; Copyright (C) 2010 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))
32 (eval-when-compile (require 'mm-decode))
42 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
43 "Time used to determine if we should use images from the cache."
48 (defcustom gnus-html-image-automatic-caching t
49 "Whether automatically cache retrieve images."
54 (defcustom gnus-html-frame-width 70
55 "What width to use when rendering HTML."
60 (defcustom gnus-blocked-images "."
61 "Images that have URLs matching this regexp will be blocked."
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)
93 (defalias 'gnus-html-encode-url-chars
94 (if (fboundp 'browse-url-url-encode-chars)
95 'browse-url-url-encode-chars
97 "URL-encode the chars in TEXT that match CHARS.
98 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
99 (let ((encoded-text (copy-sequence text))
101 (while (setq s (string-match chars encoded-text s))
103 (replace-match (format "%%%x"
105 (match-string 0 encoded-text)))
110 (defun gnus-html-encode-url (url)
112 (gnus-html-encode-url-chars url "[)$ ]"))
114 (defun gnus-html-cache-expired (url ttl)
115 "Check if URL is cached for more than TTL."
116 (cond (url-standalone-mode
117 (not (file-exists-p (url-cache-create-filename url))))
118 (t (let ((cache-time (url-is-cached url)))
128 (defun gnus-article-html (&optional handle)
129 (let ((article-buffer (current-buffer)))
131 (setq handle (mm-dissect-buffer t)))
133 (narrow-to-region (point) (point))
136 (let* ((coding-system-for-read 'utf-8)
137 (coding-system-for-write 'utf-8)
138 (default-process-coding-system
139 (cons coding-system-for-read coding-system-for-write))
140 (charset (mail-content-type-get (mm-handle-type handle)
143 (setq charset (mm-charset-to-coding-system charset))
144 (not (eq charset 'ascii)))
146 (mm-decode-coding-string (buffer-string) charset)
148 (mm-enable-multibyte))))
149 (call-process-region (point-min) (point-max)
151 nil article-buffer nil
156 "-o" "ext_halfdump=1"
157 "-o" "display_ins_del=2"
159 "-t" (format "%s" tab-width)
160 "-cols" (format "%s" gnus-html-frame-width)
161 "-o" "display_image=on"
163 (gnus-html-wash-tags))))
165 (defvar gnus-article-mouse-face)
167 (defun gnus-html-pre-wash ()
168 (goto-char (point-min))
169 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
170 (replace-match "" t t))
171 (goto-char (point-min))
172 (while (re-search-forward "<a name[^\n>]+>" nil t)
173 (replace-match "" t t)))
175 (defun gnus-html-wash-images ()
176 "Run through current buffer and replace img tags by images."
177 (let (tag parameters string start end images url)
178 (goto-char (point-min))
179 ;; Search for all the images first.
180 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
181 (setq parameters (match-string 1)
182 start (match-beginning 0))
183 (delete-region start (point))
184 (when (search-forward "</img_alt>" (line-end-position) t)
185 (delete-region (match-beginning 0) (match-end 0)))
187 (when (string-match "src=\"\\([^\"]+\\)" parameters)
188 (setq url (gnus-html-encode-url (match-string 1 parameters)))
189 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
190 (if (string-match "^cid:\\(.*\\)" url)
191 ;; URLs with cid: have their content stashed in other
192 ;; parts of the MIME structure, so just insert them
194 (let ((handle (mm-get-content-id
195 (setq url (match-string 1 url))))
199 (setq image (gnus-create-image (buffer-string)
202 (let ((string (buffer-substring start end)))
203 (delete-region start end)
204 (gnus-put-image image (gnus-string-or string "*") 'cid)
205 (gnus-add-image 'cid image))))
206 ;; Normal, external URL.
207 (let ((alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
209 (xml-substitute-special (match-string 2 parameters)))))
210 (gnus-put-text-property start end 'gnus-image-url url)
211 (if (gnus-html-image-url-blocked-p
213 (if (buffer-live-p gnus-summary-buffer)
214 (with-current-buffer gnus-summary-buffer
216 gnus-blocked-images))
218 (widget-convert-button
220 :action 'gnus-html-insert-image
222 :keymap gnus-html-image-map
223 :button-keymap gnus-html-image-map)
224 (let ((overlay (gnus-make-overlay start end))
225 (spec (list url start end alt-text)))
226 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
227 (gnus-overlay-put overlay 'gnus-image spec)
228 (gnus-put-text-property
233 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
234 (string-to-number (match-string 1 parameters))))
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)
244 (gnus-html-display-image url start end alt-text))))))))))
246 (defun gnus-html-display-image (url start end alt-text)
247 "Display image at URL on text from START to END.
248 Use ALT-TEXT for the image 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
287 (let ((overlay (gnus-make-overlay start end)))
288 (gnus-overlay-put overlay 'evaporate t)
289 (gnus-overlay-put overlay 'gnus-button-url url)
290 (gnus-put-text-property start end 'gnus-string url)
291 (when gnus-article-mouse-face
292 (gnus-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 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
303 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
306 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
309 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
310 ((or (equal tag "ins")
312 (gnus-overlay-put (gnus-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 ()
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) 'gnus-image-url)))
355 (defun gnus-html-browse-url ()
356 "Browse the image under point."
358 (let ((url (get-text-property (point) 'gnus-string)))
360 (message "No URL at point")
363 (defun gnus-html-schedule-image-fetching (buffer image)
364 "Retrieve IMAGE, and place it into BUFFER on arrival."
365 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
367 (let ((args (list (car image)
368 'gnus-html-image-fetched
369 (list buffer image))))
370 (when (> (length (help-function-arglist 'url-retrieve)) 4)
371 (setq args (nconc args (list t))))
372 (apply #'url-retrieve args)))
374 (defun gnus-html-image-fetched (status buffer image)
375 "Callback function called when image has been fetched."
376 (unless (plist-get status :error)
377 (when gnus-html-image-automatic-caching
378 (url-store-in-cache (current-buffer)))
379 (when (and (or (search-forward "\n\n" nil t)
380 (search-forward "\r\n\r\n" nil t))
381 (buffer-live-p buffer))
382 (let ((data (buffer-substring (point) (point-max))))
383 (with-current-buffer buffer
384 (let ((inhibit-read-only t))
385 (gnus-html-put-image data (car image) (cadr image)))))))
386 (kill-buffer (current-buffer)))
388 (defun gnus-html-get-image-data (url)
389 "Get image data for URL.
390 Return a string with image data."
392 (mm-disable-multibyte)
393 (url-cache-extract (url-cache-create-filename url))
394 (when (or (search-forward "\n\n" nil t)
395 (search-forward "\r\n\r\n" nil t))
396 (buffer-substring (point) (point-max)))))
398 (defun gnus-html-put-image (data url &optional alt-text)
399 (when (gnus-graphic-display-p)
400 (let* ((start (text-property-any (point-min) (point-max)
401 'gnus-image-url url))
403 (next-single-property-change start 'gnus-image-url))))
408 (gnus-create-image data nil t)))
410 (if (featurep 'xemacs)
411 (cons (glyph-width image) (glyph-height image))
412 (image-size image t)))))
415 (let ((alt-text (or alt-text
416 (buffer-substring-no-properties start end))))
418 ;; Kludge to avoid displaying 30x30 gif images, which
419 ;; seems to be a signal of a broken image.
420 (not (and (if (featurep 'xemacs)
423 (eq (if (featurep 'xemacs)
426 (glyph-image image)))))
429 (plist-get (cdr image) :type))
433 ;; Good image, add it!
434 (let ((image (gnus-rescale-image
436 (let ((edges (gnus-window-inside-pixel-edges
437 (get-buffer-window (current-buffer)))))
442 (* gnus-max-image-proportion
443 (- (nth 2 edges) (nth 0 edges))))
445 (truncate (* gnus-max-image-proportion
446 (- (nth 3 edges) (nth 1 edges)))))))))
447 (delete-region start end)
448 (gnus-put-image image alt-text 'external)
449 (gnus-put-text-property start (point) 'help-echo alt-text)
451 (gnus-make-overlay start (point)) 'local-map
452 gnus-html-displayed-image-map)
453 (gnus-put-text-property start (point)
454 'gnus-alt-text alt-text)
456 (gnus-put-text-property start (point)
457 'gnus-image-url url))
458 (gnus-add-image 'external image)
460 ;; Bad image, try to show something else
461 (when (fboundp 'find-image)
462 (delete-region start end)
463 (setq image (find-image
464 '((:type xpm :file "lock-broken.xpm"))))
465 (gnus-put-image image alt-text 'internal)
466 (gnus-add-image 'internal image))
469 (defun gnus-html-image-url-blocked-p (url blocked-images)
470 "Find out if URL is blocked by BLOCKED-IMAGES."
471 (let ((ret (and blocked-images
472 (string-match blocked-images url))))
474 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
476 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
480 (defun gnus-html-show-images ()
481 "Show any images that are in the HTML-rendered article buffer.
482 This only works if the article in question is HTML."
484 (gnus-with-article-buffer
485 (dolist (overlay (overlays-in (point-min) (point-max)))
486 (let ((o (overlay-get overlay 'gnus-image)))
488 (apply 'gnus-html-display-image o))))))
491 (defun gnus-html-prefetch-images (summary)
492 (when (buffer-live-p summary)
493 (let ((blocked-images (with-current-buffer summary
494 gnus-blocked-images)))
496 (while (re-search-forward "<img[^>]+src=[\"']\\([^\"']+\\)" nil t)
497 (let ((url (gnus-html-encode-url (match-string 1))))
498 (unless (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