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))
39 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
40 "Time in seconds used to cache the image on disk."
45 (defcustom gnus-html-frame-width 70
46 "What width to use when rendering HTML."
51 (defcustom gnus-blocked-images "."
52 "Images that have URLs matching this regexp will be blocked."
57 (defcustom gnus-max-image-proportion 0.7
58 "How big pictures displayed are in relation to the window they're in.
59 A value of 0.7 means that they are allowed to take up 70% of the
60 width and height of the window. If they are larger than this,
61 and Emacs supports it, then the images will be rescaled down to
67 (defvar gnus-html-image-map
68 (let ((map (make-sparse-keymap)))
69 (define-key map "u" 'gnus-article-copy-string)
70 (define-key map "i" 'gnus-html-insert-image)
73 (defvar gnus-html-displayed-image-map
74 (let ((map (make-sparse-keymap)))
75 (define-key map "a" 'gnus-html-show-alt-text)
76 (define-key map "i" 'gnus-html-browse-image)
77 (define-key map "\r" 'gnus-html-browse-url)
78 (define-key map "u" 'gnus-article-copy-string)
79 (define-key map [tab] 'widget-forward)
82 (defun gnus-html-cache-expired (url ttl)
83 "Check if URL is cached for more than TTL."
84 (cond (url-standalone-mode
85 (not (file-exists-p (url-cache-create-filename url))))
86 (t (let ((cache-time (url-is-cached url)))
96 (defun gnus-article-html (&optional handle)
97 (let ((article-buffer (current-buffer)))
99 (setq handle (mm-dissect-buffer t)))
101 (narrow-to-region (point) (point))
104 (let* ((coding-system-for-read 'utf-8)
105 (coding-system-for-write 'utf-8)
106 (default-process-coding-system
107 (cons coding-system-for-read coding-system-for-write))
108 (charset (mail-content-type-get (mm-handle-type handle)
111 (setq charset (mm-charset-to-coding-system charset))
112 (not (eq charset 'ascii)))
114 (mm-decode-coding-string (buffer-string) charset)
116 (mm-enable-multibyte))))
117 (call-process-region (point-min) (point-max)
119 nil article-buffer nil
124 "-o" "ext_halfdump=1"
125 "-o" "display_ins_del=2"
127 "-t" (format "%s" tab-width)
128 "-cols" (format "%s" gnus-html-frame-width)
129 "-o" "display_image=on"
131 (gnus-html-wash-tags))))
133 (defvar gnus-article-mouse-face)
135 (defun gnus-html-pre-wash ()
136 (goto-char (point-min))
137 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
138 (replace-match "" t t))
139 (goto-char (point-min))
140 (while (re-search-forward "<a name[^\n>]+>" nil t)
141 (replace-match "" t t)))
143 (defun gnus-html-wash-images ()
144 "Run through current buffer and replace img tags by images."
145 (let (tag parameters string start end images url)
146 (goto-char (point-min))
147 ;; Search for all the images first.
148 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
149 (setq parameters (match-string 1)
150 start (match-beginning 0))
151 (delete-region start (point))
152 (when (search-forward "</img_alt>" (line-end-position) t)
153 (delete-region (match-beginning 0) (match-end 0)))
155 (when (string-match "src=\"\\([^\"]+\\)" parameters)
156 (setq url (match-string 1 parameters))
157 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
158 (if (string-match "^cid:\\(.*\\)" url)
159 ;; URLs with cid: have their content stashed in other
160 ;; parts of the MIME structure, so just insert them
162 (let ((handle (mm-get-content-id
163 (setq url (match-string 1 url))))
167 (setq image (gnus-create-image (buffer-string)
170 (let ((string (buffer-substring start end)))
171 (delete-region start end)
172 (gnus-put-image image (gnus-string-or string "*") 'cid)
173 (gnus-add-image 'cid image))))
174 ;; Normal, external URL.
175 (let ((alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
177 (match-string 2 parameters))))
178 (if (gnus-html-image-url-blocked-p
180 (if (buffer-live-p gnus-summary-buffer)
181 (with-current-buffer gnus-summary-buffer
183 gnus-blocked-images))
185 (widget-convert-button
187 :action 'gnus-html-insert-image
189 :keymap gnus-html-image-map
190 :button-keymap gnus-html-image-map)
191 (let ((overlay (gnus-make-overlay start end))
193 (set-marker (make-marker) start)
194 (set-marker (make-marker) end)
196 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
197 (gnus-overlay-put overlay 'gnus-image spec)
198 (gnus-put-text-property
203 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
204 (string-to-number (match-string 1 parameters))))
206 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
207 (string-to-number (match-string 1 parameters)))))
208 ;; Don't fetch images that are really small. They're
209 ;; probably tracking pictures.
210 (when (and (or (null height)
214 (gnus-html-display-image url start end alt-text))))))))))
216 (defun gnus-html-display-image (url start end alt-text)
217 "Display image at URL on text from START to END.
218 Use ALT-TEXT for the image string."
219 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
220 ;; We don't have it, so schedule it for fetching
222 (gnus-html-schedule-image-fetching
225 (set-marker (make-marker) start)
226 (set-marker (make-marker) end)
228 ;; It's already cached, so just insert it.
229 (gnus-html-put-image (gnus-html-get-image-data url)
230 start end url alt-text)))
232 (defun gnus-html-wash-tags ()
233 (let (tag parameters string start end images url)
235 (gnus-html-wash-images)
237 (goto-char (point-min))
238 ;; Then do the other tags.
239 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
240 (setq tag (match-string 1)
241 parameters (match-string 2)
242 start (match-beginning 0))
243 (when (plusp (length parameters))
244 (set-text-properties 0 (1- (length parameters)) nil parameters))
245 (delete-region start (point))
246 (when (search-forward (concat "</" tag ">") nil t)
247 (delete-region (match-beginning 0) (match-end 0)))
250 ;; Fetch and insert a picture.
251 ((equal tag "img_alt"))
255 (when (string-match "href=\"\\([^\"]+\\)" parameters)
256 (setq url (match-string 1 parameters))
257 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
258 (gnus-article-add-button start end
261 (let ((overlay (gnus-make-overlay start end)))
262 (gnus-overlay-put overlay 'evaporate t)
263 (gnus-overlay-put overlay 'gnus-button-url url)
264 (gnus-put-text-property start end 'gnus-string url)
265 (when gnus-article-mouse-face
266 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
267 ;; The upper-case IMG_ALT is apparently just an artifact that
268 ;; should be deleted.
269 ((equal tag "IMG_ALT")
270 (delete-region start end))
271 ;; w3m does not normalize the case
274 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
277 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
280 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
283 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
284 ((or (equal tag "ins")
286 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
287 ;; Handle different UL types
288 ((equal tag "_SYMBOL")
289 (when (string-match "TYPE=\\(.+\\)" parameters)
290 (let ((type (string-to-number (match-string 1 parameters))))
291 (delete-region start end)
292 (cond ((= type 33) (insert " "))
293 ((= type 34) (insert " "))
294 ((= type 35) (insert " "))
295 ((= type 36) (insert " "))
296 ((= type 37) (insert " "))
297 ((= type 38) (insert " "))
298 ((= type 39) (insert " "))
299 ((= type 40) (insert " "))
300 ((= type 42) (insert " "))
301 ((= type 43) (insert " "))
303 ;; Whatever. Just ignore the tag.
307 (goto-char (point-min))
308 ;; The output from -halfdump isn't totally regular, so strip
309 ;; off any </pre_int>s that were left over.
310 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
311 (replace-match "" t t))
312 (mm-url-decode-entities)))
314 (defun gnus-html-insert-image ()
315 "Fetch and insert the image under point."
317 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
319 (defun gnus-html-show-alt-text ()
320 "Show the ALT text of the image under point."
322 (message "%s" (get-text-property (point) 'gnus-alt-text)))
324 (defun gnus-html-browse-image ()
325 "Browse the image under point."
327 (browse-url (get-text-property (point) 'gnus-image-url)))
329 (defun gnus-html-browse-url ()
330 "Browse the image under point."
332 (let ((url (get-text-property (point) 'gnus-string)))
334 (message "No URL at point")
337 (defun gnus-html-schedule-image-fetching (buffer image)
338 "Retrieve IMAGE, and place it into BUFFER on arrival."
339 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
342 (url-retrieve (car image)
343 'gnus-html-image-fetched
344 (list buffer image))))
346 (defun gnus-html-image-fetched (status buffer image)
347 (url-store-in-cache (current-buffer))
348 (when (and (search-forward "\n\n" nil t)
349 (buffer-live-p buffer)
350 ;; If the `image' has no marker, do not replace anything
352 ;; If the position of the marker is 1, then that
353 ;; means that the text it was in has been deleted;
354 ;; i.e., that the user has selected a different
355 ;; article before the image arrived.
356 (not (= (marker-position (cadr image))
357 (with-current-buffer buffer
359 (let ((data (buffer-substring (point) (point-max))))
360 (with-current-buffer buffer
361 (let ((inhibit-read-only t))
362 (gnus-html-put-image data (cadr image) (caddr image) (car image) (cadddr image))))))
363 (kill-buffer (current-buffer)))
365 (defun gnus-html-get-image-data (url)
366 "Get image data for URL.
367 Return a string with image data."
369 (mm-disable-multibyte)
370 (url-cache-extract (url-cache-create-filename url))
371 (when (search-forward "\n\n" nil t)
372 (buffer-substring (point) (point-max)))))
374 (defun gnus-html-put-image (data start end &optional url alt-text)
375 (when (gnus-graphic-display-p)
376 (let* ((image (ignore-errors
377 (gnus-create-image data nil t)))
379 (if (featurep 'xemacs)
380 (cons (glyph-width image) (glyph-height image))
381 (image-size image t)))))
384 (let ((alt-text (or alt-text (buffer-substring-no-properties start end))))
386 ;; Kludge to avoid displaying 30x30 gif images, which
387 ;; seems to be a signal of a broken image.
388 (not (and (if (featurep 'xemacs)
391 (eq (if (featurep 'xemacs)
392 (let ((d (cdadar (specifier-spec-list
393 (glyph-image image)))))
396 (plist-get (cdr image) :type))
400 ;; Good image, add it!
401 (let ((image (gnus-html-rescale-image image data size)))
402 (delete-region start end)
403 (gnus-put-image image alt-text 'external)
404 (gnus-overlay-put (gnus-make-overlay start (point)) 'local-map
405 gnus-html-displayed-image-map)
406 (gnus-put-text-property start (point) 'gnus-alt-text alt-text)
408 (gnus-put-text-property start (point) 'gnus-image-url url))
409 (gnus-add-image 'external image)
411 ;; Bad image, try to show something else
412 (delete-region start end)
413 (when (fboundp 'find-image)
414 (setq image (find-image '((:type xpm :file "lock-broken.xpm"))))
415 (gnus-put-image image alt-text 'internal)
416 (gnus-add-image 'internal image))
419 (defun gnus-html-rescale-image (image data size)
420 (if (or (not (fboundp 'imagemagick-types))
421 (not (get-buffer-window (current-buffer))))
423 (let* ((width (car size))
425 (edges (window-pixel-edges (get-buffer-window (current-buffer))))
426 (window-width (truncate (* gnus-max-image-proportion
427 (- (nth 2 edges) (nth 0 edges)))))
428 (window-height (truncate (* gnus-max-image-proportion
429 (- (nth 3 edges) (nth 1 edges)))))
431 (when (> height window-height)
432 (setq image (or (create-image data 'imagemagick t
433 :height window-height)
435 (setq size (image-size image t)))
436 (when (> (car size) window-width)
438 (create-image data 'imagemagick t
443 (defun gnus-html-image-url-blocked-p (url blocked-images)
444 "Find out if URL is blocked by BLOCKED-IMAGES."
445 (let ((ret (and blocked-images
446 (string-match blocked-images url))))
448 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
450 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
454 (defun gnus-html-show-images ()
455 "Show any images that are in the HTML-rendered article buffer.
456 This only works if the article in question is HTML."
458 (gnus-with-article-buffer
459 (dolist (overlay (overlays-in (point-min) (point-max)))
460 (let ((o (overlay-get overlay 'gnus-image)))
462 (apply 'gnus-html-display-image o))))))
465 (defun gnus-html-prefetch-images (summary)
466 (when (buffer-live-p summary)
467 (let ((blocked-images (with-current-buffer summary
468 gnus-blocked-images)))
470 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
471 (let ((url (match-string 1)))
472 (unless (gnus-html-image-url-blocked-p url blocked-images)
473 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
474 (gnus-html-schedule-image-fetching nil
479 ;;; gnus-html.el ends here