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
))
35 (defcustom gnus-html-cache-directory
(nnheader-concat gnus-directory
"html-cache/")
36 "Where Gnus will cache images it downloads from the web."
41 (defcustom gnus-html-cache-size
500000000
42 "The size of the Gnus image cache."
47 (defcustom gnus-html-frame-width
70
48 "What width to use when rendering HTML."
53 (defcustom gnus-blocked-images
"."
54 "Images that have URLs matching this regexp will be blocked."
59 (defcustom gnus-max-image-proportion
0.7
60 "How big pictures displayed are in relation to the window they're in.
61 A value of 0.7 means that they are allowed to take up 70% of the
62 width and height of the window. If they are larger than this,
63 and Emacs supports it, then the images will be rescaled down to
69 (defvar gnus-html-image-map
70 (let ((map (make-sparse-keymap)))
71 (define-key map
"u" 'gnus-article-copy-string
)
72 (define-key map
"i" 'gnus-html-insert-image
)
76 (defun gnus-article-html (handle)
77 (let ((article-buffer (current-buffer)))
79 (narrow-to-region (point) (point))
82 (let* ((coding-system-for-read 'utf-8
)
83 (coding-system-for-write 'utf-8
)
84 (default-process-coding-system
85 (cons coding-system-for-read coding-system-for-write
))
86 (charset (mail-content-type-get (mm-handle-type handle
)
89 (setq charset
(mm-charset-to-coding-system charset
))
90 (not (eq charset
'ascii
)))
92 (mm-decode-coding-string (buffer-string) charset
)
94 (mm-enable-multibyte))))
95 (call-process-region (point-min) (point-max)
97 nil article-buffer nil
102 "-o" "ext_halfdump=1"
104 "-t" (format "%s" tab-width
)
105 "-cols" (format "%s" gnus-html-frame-width
)
106 "-o" "display_image=on"
108 (gnus-html-wash-tags))))
110 (defvar gnus-article-mouse-face
)
112 (defun gnus-html-wash-tags ()
113 (let (tag parameters string start end images url
)
114 (mm-url-decode-entities)
115 (goto-char (point-min))
116 (while (re-search-forward "<pre_int> *</pre_int>\n" nil t
)
117 (replace-match "" t t
))
118 (goto-char (point-min))
119 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t
)
120 (setq tag
(match-string 1)
121 parameters
(match-string 2)
122 start
(match-beginning 0))
123 (when (plusp (length parameters
))
124 (set-text-properties 0 (1- (length parameters
)) nil parameters
))
125 (delete-region start
(point))
126 (when (search-forward (concat "</" tag
">") nil t
)
127 (delete-region (match-beginning 0) (match-end 0)))
130 ;; Fetch and insert a picture.
131 ((equal tag
"img_alt")
132 (when (string-match "src=\"\\([^\"]+\\)" parameters
)
133 (setq url
(match-string 1 parameters
))
134 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url
)
135 (if (string-match "^cid:\\(.*\\)" url
)
136 ;; URLs with cid: have their content stashed in other
137 ;; parts of the MIME structure, so just insert them
139 (let ((handle (mm-get-content-id
140 (setq url
(match-string 1 url
))))
144 (setq image
(gnus-create-image (buffer-string)
147 (let ((string (buffer-substring start end
)))
148 (delete-region start end
)
149 (gnus-put-image image
(gnus-string-or string
"*")))))
150 ;; Normal, external URL.
151 (if (gnus-html-image-url-blocked-p
153 (if (buffer-live-p gnus-summary-buffer
)
154 (with-current-buffer gnus-summary-buffer
156 gnus-blocked-images
))
158 (widget-convert-button
160 :action
'gnus-html-insert-image
162 :keymap gnus-html-image-map
163 :button-keymap gnus-html-image-map
)
164 (let ((overlay (gnus-make-overlay start end
))
166 (set-marker (make-marker) start
)
167 (set-marker (make-marker) end
))))
168 (gnus-overlay-put overlay
'local-map gnus-html-image-map
)
169 (gnus-overlay-put overlay
'gnus-image spec
)
170 (gnus-put-text-property
173 (let ((file (gnus-html-image-id url
))
175 (when (string-match "height=\"?\\([0-9]+\\)" parameters
)
176 (setq height
(string-to-number (match-string 1 parameters
))))
177 (when (string-match "width=\"?\\([0-9]+\\)" parameters
)
178 (setq width
(string-to-number (match-string 1 parameters
))))
179 ;; Don't fetch images that are really small. They're
180 ;; probably tracking pictures.
181 (when (and (or (null height
)
185 (if (file-exists-p file
)
186 ;; It's already cached, so just insert it.
187 (let ((string (buffer-substring start end
)))
188 ;; Delete the ALT text.
189 (delete-region start end
)
190 (gnus-html-put-image file
(point) string
))
191 ;; We don't have it, so schedule it for fetching
194 (set-marker (make-marker) start
)
200 (when (string-match "href=\"\\([^\"]+\\)" parameters
)
201 (setq url
(match-string 1 parameters
))
202 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url
)
203 (gnus-article-add-button start end
206 (let ((overlay (gnus-make-overlay start end
)))
207 (gnus-overlay-put overlay
'evaporate t
)
208 (gnus-overlay-put overlay
'gnus-button-url url
)
209 (gnus-put-text-property start end
'gnus-string url
)
210 (when gnus-article-mouse-face
211 (gnus-overlay-put overlay
'mouse-face gnus-article-mouse-face
)))))
212 ;; The upper-case IMG_ALT is apparently just an artifact that
213 ;; should be deleted.
214 ((equal tag
"IMG_ALT")
215 (delete-region start end
))
216 ;; Whatever. Just ignore the tag.
220 (goto-char (point-min))
221 ;; The output from -halfdump isn't totally regular, so strip
222 ;; off any </pre_int>s that were left over.
223 (while (re-search-forward "</pre_int>" nil t
)
224 (replace-match "" t t
))
226 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images
)))))
228 (defun gnus-html-insert-image ()
229 "Fetch and insert the image under point."
231 (gnus-html-schedule-image-fetching
232 (current-buffer) (list (get-text-property (point) 'gnus-image
))))
234 (defun gnus-html-schedule-image-fetching (buffer images
)
235 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, images %s"
237 (let* ((url (caar images
))
238 (process (start-process
243 "-o" (gnus-html-image-id url
)
245 (process-kill-without-query process
)
246 (set-process-sentinel process
'gnus-html-curl-sentinel
)
247 (gnus-set-process-plist process
(list 'images images
250 (defun gnus-html-image-id (url)
251 (expand-file-name (sha1 url
) gnus-html-cache-directory
))
253 (defun gnus-html-curl-sentinel (process event
)
254 (when (string-match "finished" event
)
255 (let* ((images (gnus-process-get process
'images
))
256 (buffer (gnus-process-get process
'buffer
))
258 (file (gnus-html-image-id (car spec
))))
259 (when (and (buffer-live-p buffer
)
260 ;; If the position of the marker is 1, then that
261 ;; means that the text it was in has been deleted;
262 ;; i.e., that the user has selected a different
263 ;; article before the image arrived.
264 (not (= (marker-position (cadr spec
)) (point-min))))
265 (with-current-buffer buffer
266 (let ((inhibit-read-only t
)
267 (string (buffer-substring (cadr spec
) (caddr spec
))))
268 (delete-region (cadr spec
) (caddr spec
))
269 (gnus-html-put-image file
(cadr spec
) string
))))
271 (gnus-html-schedule-image-fetching buffer images
)))))
273 (defun gnus-html-put-image (file point string
)
274 (when (gnus-graphic-display-p)
275 (let* ((image (ignore-errors
276 (gnus-create-image file
)))
278 (if (featurep 'xemacs
)
279 (cons (glyph-width image
) (glyph-height image
))
280 (image-size image t
)))))
284 ;; Kludge to avoid displaying 30x30 gif images, which
285 ;; seems to be a signal of a broken image.
286 (not (and (if (featurep 'xemacs
)
289 (eq (if (featurep 'xemacs
)
290 (let ((data (cdadar (specifier-spec-list
291 (glyph-image image
)))))
294 (plist-get (cdr image
) :type
))
299 (setq image
(gnus-html-rescale-image image file size
))
300 (gnus-put-image image
301 (gnus-string-or string
"*")
303 (gnus-add-image 'external image
)
306 (when (fboundp 'find-image
)
307 (gnus-put-image (find-image
308 '((:type xpm
:file
"lock-broken.xpm")))
309 (gnus-string-or string
"*")))
312 (defun gnus-html-rescale-image (image file size
)
313 (if (or (not (fboundp 'imagemagick-types
))
314 (not (get-buffer-window (current-buffer))))
316 (let* ((width (car size
))
318 (edges (window-pixel-edges (get-buffer-window (current-buffer))))
319 (window-width (truncate (* gnus-max-image-proportion
320 (- (nth 2 edges
) (nth 0 edges
)))))
321 (window-height (truncate (* gnus-max-image-proportion
322 (- (nth 3 edges
) (nth 1 edges
)))))
324 (when (> height window-height
)
325 (setq image
(or (create-image file
'imagemagick nil
326 :height window-height
)
328 (setq size
(image-size image t
)))
329 (when (> (car size
) window-width
)
331 (create-image file
'imagemagick nil
336 (defun gnus-html-prune-cache ()
339 (dolist (file (directory-files gnus-html-cache-directory t nil t
))
340 (let ((attributes (file-attributes file
)))
341 (unless (nth 0 attributes
)
342 (incf total-size
(nth 7 attributes
))
343 (push (list (time-to-seconds (nth 5 attributes
))
344 (nth 7 attributes
) file
)
346 (when (> total-size gnus-html-cache-size
)
347 (setq files
(sort files
(lambda (f1 f2
)
348 (< (car f1
) (car f2
)))))
350 (when (> total-size gnus-html-cache-size
)
351 (decf total-size
(cadr file
))
352 (delete-file (nth 2 file
)))))))
354 (defun gnus-html-image-url-blocked-p (url blocked-images
)
355 "Find out if URL is blocked by BLOCKED-IMAGES."
356 (let ((ret (and blocked-images
357 (string-match blocked-images url
))))
359 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
361 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
365 (defun gnus-html-show-images ()
366 "Show any images that are in the HTML-rendered article buffer.
367 This only works if the article in question is HTML."
369 (gnus-with-article-buffer
370 (let ((overlays (overlays-in (point-min) (point-max)))
372 (while (setq overlay
(pop overlays
))
373 (when (overlay-get overlay
'gnus-image
)
374 (push (overlay-get overlay
'gnus-image
) images
)))
375 (gnus-html-schedule-image-fetching (current-buffer) images
))))
378 (defun gnus-html-prefetch-images (summary)
379 (let (blocked-images urls
)
380 (when (buffer-live-p summary
)
381 (with-current-buffer summary
382 (setq blocked-images gnus-blocked-images
))
384 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t
)
385 (let ((url (match-string 1)))
386 (unless (gnus-html-image-url-blocked-p url blocked-images
)
387 (unless (file-exists-p (gnus-html-image-id url
))
389 (push (gnus-html-image-id url
) urls
)
392 (apply 'start-process
398 (process-kill-without-query process
))))))
402 ;;; gnus-html.el ends here