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)
71 (define-key map "v" 'gnus-html-browse-url)
74 (defvar gnus-html-displayed-image-map
75 (let ((map (make-sparse-keymap)))
76 (define-key map "a" 'gnus-html-show-alt-text)
77 (define-key map "i" 'gnus-html-browse-image)
78 (define-key map "\r" 'gnus-html-browse-url)
79 (define-key map "u" 'gnus-article-copy-string)
80 (define-key map [tab] 'widget-forward)
83 (defun gnus-html-cache-expired (url ttl)
84 "Check if URL is cached for more than TTL."
85 (cond (url-standalone-mode
86 (not (file-exists-p (url-cache-create-filename url))))
87 (t (let ((cache-time (url-is-cached url)))
97 (defun gnus-article-html (&optional handle)
98 (let ((article-buffer (current-buffer)))
100 (setq handle (mm-dissect-buffer t)))
102 (narrow-to-region (point) (point))
105 (let* ((coding-system-for-read 'utf-8)
106 (coding-system-for-write 'utf-8)
107 (default-process-coding-system
108 (cons coding-system-for-read coding-system-for-write))
109 (charset (mail-content-type-get (mm-handle-type handle)
112 (setq charset (mm-charset-to-coding-system charset))
113 (not (eq charset 'ascii)))
115 (mm-decode-coding-string (buffer-string) charset)
117 (mm-enable-multibyte))))
118 (call-process-region (point-min) (point-max)
120 nil article-buffer nil
125 "-o" "ext_halfdump=1"
126 "-o" "display_ins_del=2"
128 "-t" (format "%s" tab-width)
129 "-cols" (format "%s" gnus-html-frame-width)
130 "-o" "display_image=on"
132 (gnus-html-wash-tags))))
134 (defvar gnus-article-mouse-face)
136 (defun gnus-html-pre-wash ()
137 (goto-char (point-min))
138 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
139 (replace-match "" t t))
140 (goto-char (point-min))
141 (while (re-search-forward "<a name[^\n>]+>" nil t)
142 (replace-match "" t t)))
144 (defun gnus-html-wash-images ()
145 "Run through current buffer and replace img tags by images."
146 (let (tag parameters string start end images url)
147 (goto-char (point-min))
148 ;; Search for all the images first.
149 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
150 (setq parameters (match-string 1)
151 start (match-beginning 0))
152 (delete-region start (point))
153 (when (search-forward "</img_alt>" (line-end-position) t)
154 (delete-region (match-beginning 0) (match-end 0)))
156 (when (string-match "src=\"\\([^\"]+\\)" parameters)
157 (setq url (match-string 1 parameters))
158 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
159 (if (string-match "^cid:\\(.*\\)" url)
160 ;; URLs with cid: have their content stashed in other
161 ;; parts of the MIME structure, so just insert them
163 (let ((handle (mm-get-content-id
164 (setq url (match-string 1 url))))
168 (setq image (gnus-create-image (buffer-string)
171 (let ((string (buffer-substring start end)))
172 (delete-region start end)
173 (gnus-put-image image (gnus-string-or string "*") 'cid)
174 (gnus-add-image 'cid image))))
175 ;; Normal, external URL.
176 (let ((alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
178 (match-string 2 parameters))))
179 (if (gnus-html-image-url-blocked-p
181 (if (buffer-live-p gnus-summary-buffer)
182 (with-current-buffer gnus-summary-buffer
184 gnus-blocked-images))
186 (widget-convert-button
188 :action 'gnus-html-insert-image
190 :keymap gnus-html-image-map
191 :button-keymap gnus-html-image-map)
192 (let ((overlay (gnus-make-overlay start end))
194 (set-marker (make-marker) start)
195 (set-marker (make-marker) end)
197 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
198 (gnus-overlay-put overlay 'gnus-image spec)
199 (gnus-put-text-property start end 'gnus-image-url url)
200 (gnus-put-text-property
205 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
206 (string-to-number (match-string 1 parameters))))
208 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
209 (string-to-number (match-string 1 parameters)))))
210 ;; Don't fetch images that are really small. They're
211 ;; probably tracking pictures.
212 (when (and (or (null height)
216 (gnus-html-display-image url start end alt-text))))))))))
218 (defun gnus-html-display-image (url start end alt-text)
219 "Display image at URL on text from START to END.
220 Use ALT-TEXT for the image string."
221 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
222 ;; We don't have it, so schedule it for fetching
224 (gnus-html-schedule-image-fetching
227 (set-marker (make-marker) start)
228 (set-marker (make-marker) end)
230 ;; It's already cached, so just insert it.
231 (gnus-html-put-image (gnus-html-get-image-data url)
232 start end url alt-text)))
234 (defun gnus-html-wash-tags ()
235 (let (tag parameters string start end images url)
237 (gnus-html-wash-images)
239 (goto-char (point-min))
240 ;; Then do the other tags.
241 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
242 (setq tag (match-string 1)
243 parameters (match-string 2)
244 start (match-beginning 0))
245 (when (plusp (length parameters))
246 (set-text-properties 0 (1- (length parameters)) nil parameters))
247 (delete-region start (point))
248 (when (search-forward (concat "</" tag ">") nil t)
249 (delete-region (match-beginning 0) (match-end 0)))
252 ;; Fetch and insert a picture.
253 ((equal tag "img_alt"))
257 (when (string-match "href=\"\\([^\"]+\\)" parameters)
258 (setq url (match-string 1 parameters))
259 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
260 (gnus-article-add-button start end
263 (let ((overlay (gnus-make-overlay start end)))
264 (gnus-overlay-put overlay 'evaporate t)
265 (gnus-overlay-put overlay 'gnus-button-url url)
266 (gnus-put-text-property start end 'gnus-string url)
267 (when gnus-article-mouse-face
268 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
269 ;; The upper-case IMG_ALT is apparently just an artifact that
270 ;; should be deleted.
271 ((equal tag "IMG_ALT")
272 (delete-region start end))
273 ;; w3m does not normalize the case
276 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
279 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
282 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
285 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
286 ((or (equal tag "ins")
288 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
289 ;; Handle different UL types
290 ((equal tag "_SYMBOL")
291 (when (string-match "TYPE=\\(.+\\)" parameters)
292 (let ((type (string-to-number (match-string 1 parameters))))
293 (delete-region start end)
294 (cond ((= type 33) (insert " "))
295 ((= type 34) (insert " "))
296 ((= type 35) (insert " "))
297 ((= type 36) (insert " "))
298 ((= type 37) (insert " "))
299 ((= type 38) (insert " "))
300 ((= type 39) (insert " "))
301 ((= type 40) (insert " "))
302 ((= type 42) (insert " "))
303 ((= type 43) (insert " "))
305 ;; Whatever. Just ignore the tag.
309 (goto-char (point-min))
310 ;; The output from -halfdump isn't totally regular, so strip
311 ;; off any </pre_int>s that were left over.
312 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
313 (replace-match "" t t))
314 (mm-url-decode-entities)))
316 (defun gnus-html-insert-image ()
317 "Fetch and insert the image under point."
319 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
321 (defun gnus-html-show-alt-text ()
322 "Show the ALT text of the image under point."
324 (message "%s" (get-text-property (point) 'gnus-alt-text)))
326 (defun gnus-html-browse-image ()
327 "Browse the image under point."
329 (browse-url (get-text-property (point) 'gnus-image-url)))
331 (defun gnus-html-browse-url ()
332 "Browse the image under point."
334 (let ((url (get-text-property (point) 'gnus-string)))
336 (message "No URL at point")
339 (defun gnus-html-schedule-image-fetching (buffer image)
340 "Retrieve IMAGE, and place it into BUFFER on arrival."
341 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
344 (url-retrieve (car image)
345 'gnus-html-image-fetched
346 (list buffer image))))
348 (defun gnus-html-image-fetched (status buffer image)
349 (url-store-in-cache (current-buffer))
350 (when (and (search-forward "\n\n" nil t)
351 (buffer-live-p buffer)
352 ;; If the `image' has no marker, do not replace anything
354 ;; If the position of the marker is 1, then that
355 ;; means that the text it was in has been deleted;
356 ;; i.e., that the user has selected a different
357 ;; article before the image arrived.
358 (not (= (marker-position (cadr image))
359 (with-current-buffer buffer
361 (let ((data (buffer-substring (point) (point-max))))
362 (with-current-buffer buffer
363 (let ((inhibit-read-only t))
364 (gnus-html-put-image data (cadr image) (caddr image) (car image) (cadddr image))))))
365 (kill-buffer (current-buffer)))
367 (defun gnus-html-get-image-data (url)
368 "Get image data for URL.
369 Return a string with image data."
371 (mm-disable-multibyte)
372 (url-cache-extract (url-cache-create-filename url))
373 (when (search-forward "\n\n" nil t)
374 (buffer-substring (point) (point-max)))))
376 (defun gnus-html-put-image (data start end &optional url alt-text)
377 (when (gnus-graphic-display-p)
378 (let* ((image (ignore-errors
379 (gnus-create-image data nil t)))
381 (if (featurep 'xemacs)
382 (cons (glyph-width image) (glyph-height image))
383 (image-size image t)))))
386 (let ((alt-text (or alt-text (buffer-substring-no-properties start end))))
388 ;; Kludge to avoid displaying 30x30 gif images, which
389 ;; seems to be a signal of a broken image.
390 (not (and (if (featurep 'xemacs)
393 (eq (if (featurep 'xemacs)
394 (let ((d (cdadar (specifier-spec-list
395 (glyph-image image)))))
398 (plist-get (cdr image) :type))
402 ;; Good image, add it!
403 (let ((image (gnus-html-rescale-image image data size)))
404 (delete-region start end)
405 (gnus-put-image image alt-text 'external)
406 (gnus-overlay-put (gnus-make-overlay start (point)) 'local-map
407 gnus-html-displayed-image-map)
408 (gnus-put-text-property start (point) 'gnus-alt-text alt-text)
410 (gnus-put-text-property start (point) 'gnus-image-url url))
411 (gnus-add-image 'external image)
413 ;; Bad image, try to show something else
414 (delete-region start end)
415 (when (fboundp 'find-image)
416 (setq image (find-image '((:type xpm :file "lock-broken.xpm"))))
417 (gnus-put-image image alt-text 'internal)
418 (gnus-add-image 'internal image))
421 (defun gnus-html-rescale-image (image data size)
422 (if (or (not (fboundp 'imagemagick-types))
423 (not (get-buffer-window (current-buffer))))
425 (let* ((width (car size))
427 (edges (window-pixel-edges (get-buffer-window (current-buffer))))
428 (window-width (truncate (* gnus-max-image-proportion
429 (- (nth 2 edges) (nth 0 edges)))))
430 (window-height (truncate (* gnus-max-image-proportion
431 (- (nth 3 edges) (nth 1 edges)))))
433 (when (> height window-height)
434 (setq image (or (create-image data 'imagemagick t
435 :height window-height)
437 (setq size (image-size image t)))
438 (when (> (car size) window-width)
440 (create-image data 'imagemagick t
445 (defun gnus-html-image-url-blocked-p (url blocked-images)
446 "Find out if URL is blocked by BLOCKED-IMAGES."
447 (let ((ret (and blocked-images
448 (string-match blocked-images url))))
450 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
452 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
456 (defun gnus-html-show-images ()
457 "Show any images that are in the HTML-rendered article buffer.
458 This only works if the article in question is HTML."
460 (gnus-with-article-buffer
461 (dolist (overlay (overlays-in (point-min) (point-max)))
462 (let ((o (overlay-get overlay 'gnus-image)))
464 (apply 'gnus-html-display-image o))))))
467 (defun gnus-html-prefetch-images (summary)
468 (when (buffer-live-p summary)
469 (let ((blocked-images (with-current-buffer summary
470 gnus-blocked-images)))
472 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
473 (let ((url (match-string 1)))
474 (unless (gnus-html-image-url-blocked-p url blocked-images)
475 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
476 (gnus-html-schedule-image-fetching nil
481 ;;; gnus-html.el ends here