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)
75 (defvar gnus-html-displayed-image-map
76 (let ((map (make-sparse-keymap)))
77 (define-key map "a" 'gnus-html-show-alt-text)
78 (define-key map "i" 'gnus-html-browse-image)
79 (define-key map "\r" 'gnus-html-browse-url)
80 (define-key map "u" 'gnus-article-copy-string)
81 (define-key map [tab] 'widget-forward)
85 (defun gnus-article-html (&optional handle)
86 (let ((article-buffer (current-buffer)))
88 (setq handle (mm-dissect-buffer t)))
90 (narrow-to-region (point) (point))
93 (let* ((coding-system-for-read 'utf-8)
94 (coding-system-for-write 'utf-8)
95 (default-process-coding-system
96 (cons coding-system-for-read coding-system-for-write))
97 (charset (mail-content-type-get (mm-handle-type handle)
100 (setq charset (mm-charset-to-coding-system charset))
101 (not (eq charset 'ascii)))
103 (mm-decode-coding-string (buffer-string) charset)
105 (mm-enable-multibyte))))
106 (call-process-region (point-min) (point-max)
108 nil article-buffer nil
113 "-o" "ext_halfdump=1"
115 "-t" (format "%s" tab-width)
116 "-cols" (format "%s" gnus-html-frame-width)
117 "-o" "display_image=on"
119 (gnus-html-wash-tags))))
121 (defvar gnus-article-mouse-face)
123 (defun gnus-html-pre-wash ()
124 (goto-char (point-min))
125 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
126 (replace-match "" t t))
127 (goto-char (point-min))
128 (while (re-search-forward "<a name[^\n>]+>" nil t)
129 (replace-match "" t t)))
131 (defun gnus-html-wash-tags ()
132 (let (tag parameters string start end images url)
134 (goto-char (point-min))
136 ;; Search for all the images first.
137 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
138 (setq parameters (match-string 1)
139 start (match-beginning 0))
140 (delete-region start (point))
141 (when (search-forward "</img_alt>" (line-end-position) t)
142 (delete-region (match-beginning 0) (match-end 0)))
144 (when (string-match "src=\"\\([^\"]+\\)" parameters)
145 (setq url (match-string 1 parameters))
146 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
147 (if (string-match "^cid:\\(.*\\)" url)
148 ;; URLs with cid: have their content stashed in other
149 ;; parts of the MIME structure, so just insert them
151 (let ((handle (mm-get-content-id
152 (setq url (match-string 1 url))))
156 (setq image (gnus-create-image (buffer-string)
159 (let ((string (buffer-substring start end)))
160 (delete-region start end)
161 (gnus-put-image image (gnus-string-or string "*") 'cid)
162 (gnus-add-image 'cid image))))
163 ;; Normal, external URL.
164 (if (gnus-html-image-url-blocked-p
166 (if (buffer-live-p gnus-summary-buffer)
167 (with-current-buffer gnus-summary-buffer
169 gnus-blocked-images))
171 (widget-convert-button
173 :action 'gnus-html-insert-image
175 :keymap gnus-html-image-map
176 :button-keymap gnus-html-image-map)
177 (let ((overlay (gnus-make-overlay start end))
179 (set-marker (make-marker) start)
180 (set-marker (make-marker) end))))
181 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
182 (gnus-overlay-put overlay 'gnus-image spec)
183 (gnus-put-text-property
186 (let ((file (gnus-html-image-id url))
187 width height alt-text)
188 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
189 (setq height (string-to-number (match-string 1 parameters))))
190 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
191 (setq width (string-to-number (match-string 1 parameters))))
192 (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
194 (setq alt-text (match-string 2 parameters)))
195 ;; Don't fetch images that are really small. They're
196 ;; probably tracking pictures.
197 (when (and (or (null height)
201 (if (file-exists-p file)
202 ;; It's already cached, so just insert it.
203 (let ((string (buffer-substring start end)))
204 ;; Delete the IMG text.
205 (delete-region start end)
206 (gnus-html-put-image file (point) string url alt-text))
207 ;; We don't have it, so schedule it for fetching
210 (set-marker (make-marker) start)
214 (goto-char (point-min))
215 ;; Then do the other tags.
216 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
217 (setq tag (match-string 1)
218 parameters (match-string 2)
219 start (match-beginning 0))
220 (when (plusp (length parameters))
221 (set-text-properties 0 (1- (length parameters)) nil parameters))
222 (delete-region start (point))
223 (when (search-forward (concat "</" tag ">") (line-end-position) t)
224 (delete-region (match-beginning 0) (match-end 0)))
227 ;; Fetch and insert a picture.
228 ((equal tag "img_alt"))
232 (when (string-match "href=\"\\([^\"]+\\)" parameters)
233 (setq url (match-string 1 parameters))
234 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
235 (gnus-article-add-button start end
238 (let ((overlay (gnus-make-overlay start end)))
239 (gnus-overlay-put overlay 'evaporate t)
240 (gnus-overlay-put overlay 'gnus-button-url url)
241 (gnus-put-text-property start end 'gnus-string url)
242 (when gnus-article-mouse-face
243 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
244 ;; The upper-case IMG_ALT is apparently just an artifact that
245 ;; should be deleted.
246 ((equal tag "IMG_ALT")
247 (delete-region start end))
248 ;; Whatever. Just ignore the tag.
252 (goto-char (point-min))
253 ;; The output from -halfdump isn't totally regular, so strip
254 ;; off any </pre_int>s that were left over.
255 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
256 (replace-match "" t t))
258 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))
259 (mm-url-decode-entities)))
261 (defun gnus-html-insert-image ()
262 "Fetch and insert the image under point."
264 (gnus-html-schedule-image-fetching
265 (current-buffer) (list (get-text-property (point) 'gnus-image))))
267 (defun gnus-html-show-alt-text ()
268 "Show the ALT text of the image under point."
270 (message "%s" (get-text-property (point) 'gnus-alt-text)))
272 (defun gnus-html-browse-image ()
273 "Browse the image under point."
275 (browse-url (get-text-property (point) 'gnus-image)))
277 (defun gnus-html-browse-url ()
278 "Browse the image under point."
280 (let ((url (get-text-property (point) 'gnus-string)))
282 (message "No URL at point")
285 (defun gnus-html-schedule-image-fetching (buffer images)
286 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, images %s"
288 (let* ((url (caar images))
289 (process (start-process
294 "-o" (gnus-html-image-id url)
295 (mm-url-decode-entities-string url))))
296 (process-kill-without-query process)
297 (set-process-sentinel process 'gnus-html-curl-sentinel)
298 (gnus-set-process-plist process (list 'images images
301 (defun gnus-html-image-id (url)
302 (expand-file-name (sha1 url) gnus-html-cache-directory))
304 (defun gnus-html-curl-sentinel (process event)
305 (when (string-match "finished" event)
306 (let* ((images (gnus-process-get process 'images))
307 (buffer (gnus-process-get process 'buffer))
309 (file (gnus-html-image-id (car spec))))
310 (when (and (buffer-live-p buffer)
311 ;; If the position of the marker is 1, then that
312 ;; means that the text it was in has been deleted;
313 ;; i.e., that the user has selected a different
314 ;; article before the image arrived.
315 (not (= (marker-position (cadr spec)) (point-min))))
316 (with-current-buffer buffer
317 (let ((inhibit-read-only t)
318 (string (buffer-substring (cadr spec) (caddr spec))))
319 (delete-region (cadr spec) (caddr spec))
320 (gnus-html-put-image file (cadr spec) string))))
322 (gnus-html-schedule-image-fetching buffer images)))))
324 (defun gnus-html-put-image (file point string &optional url alt-text)
325 (when (gnus-graphic-display-p)
326 (let* ((image (ignore-errors
327 (gnus-create-image file)))
329 (if (featurep 'xemacs)
330 (cons (glyph-width image) (glyph-height image))
331 (image-size image t)))))
335 ;; Kludge to avoid displaying 30x30 gif images, which
336 ;; seems to be a signal of a broken image.
337 (not (and (if (featurep 'xemacs)
340 (eq (if (featurep 'xemacs)
341 (let ((data (cdadar (specifier-spec-list
342 (glyph-image image)))))
345 (plist-get (cdr image) :type))
349 (let ((start (point)))
350 (setq image (gnus-html-rescale-image image file size))
351 (gnus-put-image image
352 (gnus-string-or string "*")
354 (let ((overlay (gnus-make-overlay start (point))))
355 (gnus-overlay-put overlay 'local-map
356 gnus-html-displayed-image-map)
357 (gnus-put-text-property start (point) 'gnus-alt-text alt-text)
359 (gnus-put-text-property start (point) 'gnus-image url)))
360 (gnus-add-image 'external image)
363 (when (fboundp 'find-image)
364 (setq image (find-image '((:type xpm :file "lock-broken.xpm"))))
365 (gnus-put-image image
366 (gnus-string-or string "*")
368 (gnus-add-image 'internal image))
371 (defun gnus-html-rescale-image (image file size)
372 (if (or (not (fboundp 'imagemagick-types))
373 (not (get-buffer-window (current-buffer))))
375 (let* ((width (car size))
377 (edges (window-pixel-edges (get-buffer-window (current-buffer))))
378 (window-width (truncate (* gnus-max-image-proportion
379 (- (nth 2 edges) (nth 0 edges)))))
380 (window-height (truncate (* gnus-max-image-proportion
381 (- (nth 3 edges) (nth 1 edges)))))
383 (when (> height window-height)
384 (setq image (or (create-image file 'imagemagick nil
385 :height window-height)
387 (setq size (image-size image t)))
388 (when (> (car size) window-width)
390 (create-image file 'imagemagick nil
395 (defun gnus-html-prune-cache ()
398 (dolist (file (directory-files gnus-html-cache-directory t nil t))
399 (let ((attributes (file-attributes file)))
400 (unless (nth 0 attributes)
401 (incf total-size (nth 7 attributes))
402 (push (list (time-to-seconds (nth 5 attributes))
403 (nth 7 attributes) file)
405 (when (> total-size gnus-html-cache-size)
406 (setq files (sort files (lambda (f1 f2)
407 (< (car f1) (car f2)))))
409 (when (> total-size gnus-html-cache-size)
410 (decf total-size (cadr file))
411 (delete-file (nth 2 file)))))))
413 (defun gnus-html-image-url-blocked-p (url blocked-images)
414 "Find out if URL is blocked by BLOCKED-IMAGES."
415 (let ((ret (and blocked-images
416 (string-match blocked-images url))))
418 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
420 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
424 (defun gnus-html-show-images ()
425 "Show any images that are in the HTML-rendered article buffer.
426 This only works if the article in question is HTML."
428 (gnus-with-article-buffer
429 (let ((overlays (overlays-in (point-min) (point-max)))
431 (while (setq overlay (pop overlays))
432 (when (overlay-get overlay 'gnus-image)
433 (push (overlay-get overlay 'gnus-image) images)))
435 (message "No images to show")
436 (gnus-html-schedule-image-fetching (current-buffer) images)))))
439 (defun gnus-html-prefetch-images (summary)
440 (let (blocked-images urls)
441 (when (buffer-live-p summary)
442 (with-current-buffer summary
443 (setq blocked-images gnus-blocked-images))
445 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
446 (let ((url (match-string 1)))
447 (unless (gnus-html-image-url-blocked-p url blocked-images)
448 (unless (file-exists-p (gnus-html-image-id url))
449 (push (mm-url-decode-entities-string url) urls)
450 (push (gnus-html-image-id url) urls)
453 (apply 'start-process
459 (process-kill-without-query process))))))
463 ;;; gnus-html.el ends here