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))
38 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
39 "Where Gnus will cache images it downloads from the web."
44 (defcustom gnus-html-cache-size 500000000
45 "The size of the Gnus image cache."
50 (defcustom gnus-html-frame-width 70
51 "What width to use when rendering HTML."
56 (defcustom gnus-blocked-images "."
57 "Images that have URLs matching this regexp will be blocked."
62 (defcustom gnus-max-image-proportion 0.7
63 "How big pictures displayed are in relation to the window they're in.
64 A value of 0.7 means that they are allowed to take up 70% of the
65 width and height of the window. If they are larger than this,
66 and Emacs supports it, then the images will be rescaled down to
72 (defvar gnus-html-image-map
73 (let ((map (make-sparse-keymap)))
74 (define-key map "u" 'gnus-article-copy-string)
75 (define-key map "i" 'gnus-html-insert-image)
78 (defvar gnus-html-displayed-image-map
79 (let ((map (make-sparse-keymap)))
80 (define-key map "a" 'gnus-html-show-alt-text)
81 (define-key map "i" 'gnus-html-browse-image)
82 (define-key map "\r" 'gnus-html-browse-url)
83 (define-key map "u" 'gnus-article-copy-string)
84 (define-key map [tab] 'widget-forward)
88 (defun gnus-article-html (&optional handle)
89 (let ((article-buffer (current-buffer)))
91 (setq handle (mm-dissect-buffer t)))
93 (narrow-to-region (point) (point))
96 (let* ((coding-system-for-read 'utf-8)
97 (coding-system-for-write 'utf-8)
98 (default-process-coding-system
99 (cons coding-system-for-read coding-system-for-write))
100 (charset (mail-content-type-get (mm-handle-type handle)
103 (setq charset (mm-charset-to-coding-system charset))
104 (not (eq charset 'ascii)))
106 (mm-decode-coding-string (buffer-string) charset)
108 (mm-enable-multibyte))))
109 (call-process-region (point-min) (point-max)
111 nil article-buffer nil
116 "-o" "ext_halfdump=1"
118 "-t" (format "%s" tab-width)
119 "-cols" (format "%s" gnus-html-frame-width)
120 "-o" "display_image=on"
122 (gnus-html-wash-tags))))
124 (defvar gnus-article-mouse-face)
126 (defun gnus-html-pre-wash ()
127 (goto-char (point-min))
128 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
129 (replace-match "" t t))
130 (goto-char (point-min))
131 (while (re-search-forward "<a name[^\n>]+>" nil t)
132 (replace-match "" t t)))
134 (defun gnus-html-wash-images ()
135 (let (tag parameters string start end images url)
136 (goto-char (point-min))
137 ;; Search for all the images first.
138 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
139 (setq parameters (match-string 1)
140 start (match-beginning 0))
141 (delete-region start (point))
142 (when (search-forward "</img_alt>" (line-end-position) t)
143 (delete-region (match-beginning 0) (match-end 0)))
145 (when (string-match "src=\"\\([^\"]+\\)" parameters)
146 (setq url (match-string 1 parameters))
147 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
148 (if (string-match "^cid:\\(.*\\)" url)
149 ;; URLs with cid: have their content stashed in other
150 ;; parts of the MIME structure, so just insert them
152 (let ((handle (mm-get-content-id
153 (setq url (match-string 1 url))))
157 (setq image (gnus-create-image (buffer-string)
160 (let ((string (buffer-substring start end)))
161 (delete-region start end)
162 (gnus-put-image image (gnus-string-or string "*") 'cid)
163 (gnus-add-image 'cid image))))
164 ;; Normal, external URL.
165 (if (gnus-html-image-url-blocked-p
167 (if (buffer-live-p gnus-summary-buffer)
168 (with-current-buffer gnus-summary-buffer
170 gnus-blocked-images))
172 (widget-convert-button
174 :action 'gnus-html-insert-image
176 :keymap gnus-html-image-map
177 :button-keymap gnus-html-image-map)
178 (let ((overlay (gnus-make-overlay start end))
180 (set-marker (make-marker) start)
181 (set-marker (make-marker) end))))
182 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
183 (gnus-overlay-put overlay 'gnus-image spec)
184 (gnus-put-text-property
187 (let ((file (gnus-html-image-id url))
188 width height alt-text)
189 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
190 (setq height (string-to-number (match-string 1 parameters))))
191 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
192 (setq width (string-to-number (match-string 1 parameters))))
193 (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
195 (setq alt-text (match-string 2 parameters)))
196 ;; Don't fetch images that are really small. They're
197 ;; probably tracking pictures.
198 (when (and (or (null height)
202 (if (file-exists-p file)
203 ;; It's already cached, so just insert it.
204 (let ((string (buffer-substring start end)))
205 ;; Delete the IMG text.
206 (delete-region start end)
207 (gnus-html-put-image file (point) string url alt-text))
208 ;; We don't have it, so schedule it for fetching
211 (set-marker (make-marker) start)
215 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
217 (defun gnus-html-wash-tags ()
218 (let (tag parameters string start end images url)
220 (gnus-html-wash-images)
222 (goto-char (point-min))
223 ;; Then do the other tags.
224 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
225 (setq tag (match-string 1)
226 parameters (match-string 2)
227 start (match-beginning 0))
228 (when (plusp (length parameters))
229 (set-text-properties 0 (1- (length parameters)) nil parameters))
230 (delete-region start (point))
231 (when (search-forward (concat "</" tag ">") nil t)
232 (delete-region (match-beginning 0) (match-end 0)))
235 ;; Fetch and insert a picture.
236 ((equal tag "img_alt"))
240 (when (string-match "href=\"\\([^\"]+\\)" parameters)
241 (setq url (match-string 1 parameters))
242 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
243 (gnus-article-add-button start end
246 (let ((overlay (gnus-make-overlay start end)))
247 (gnus-overlay-put overlay 'evaporate t)
248 (gnus-overlay-put overlay 'gnus-button-url url)
249 (gnus-put-text-property start end 'gnus-string url)
250 (when gnus-article-mouse-face
251 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
252 ;; The upper-case IMG_ALT is apparently just an artifact that
253 ;; should be deleted.
254 ((equal tag "IMG_ALT")
255 (delete-region start end))
256 ;; Whatever. Just ignore the tag.
258 (gnus-overlay-put (gnus-make-overlay start end) 'face 'bold))
260 (gnus-overlay-put (gnus-make-overlay start end) 'face 'underline))
262 (gnus-overlay-put (gnus-make-overlay start end) 'face 'italic))
266 (goto-char (point-min))
267 ;; The output from -halfdump isn't totally regular, so strip
268 ;; off any </pre_int>s that were left over.
269 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
270 (replace-match "" t t))
271 (mm-url-decode-entities)))
273 (defun gnus-html-insert-image ()
274 "Fetch and insert the image under point."
276 (gnus-html-schedule-image-fetching
277 (current-buffer) (list (get-text-property (point) 'gnus-image))))
279 (defun gnus-html-show-alt-text ()
280 "Show the ALT text of the image under point."
282 (message "%s" (get-text-property (point) 'gnus-alt-text)))
284 (defun gnus-html-browse-image ()
285 "Browse the image under point."
287 (browse-url (get-text-property (point) 'gnus-image)))
289 (defun gnus-html-browse-url ()
290 "Browse the image under point."
292 (let ((url (get-text-property (point) 'gnus-string)))
294 (message "No URL at point")
297 (defun gnus-html-schedule-image-fetching (buffer images)
298 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, images %s"
300 (url-retrieve (caar images)
301 'gnus-html-image-fetched
302 (list buffer images)))
304 (defun gnus-html-image-id (url)
305 (expand-file-name (sha1 url) gnus-html-cache-directory))
307 (defun gnus-html-image-fetched (status buffer images)
308 (let ((spec (pop images)))
309 (when (and (buffer-live-p buffer)
310 ;; If the position of the marker is 1, then that
311 ;; means that the text it was in has been deleted;
312 ;; i.e., that the user has selected a different
313 ;; article before the image arrived.
314 (not (= (marker-position (cadr spec)) (point-min))))
315 (let ((file (gnus-html-image-id (car spec))))
316 ;; Search the start of the image data
317 (search-forward "\n\n")
318 ;; Write region (image) silently
319 (write-region (point) (point-max) file nil 1)
321 (with-current-buffer buffer
322 (let ((inhibit-read-only t)
323 (string (buffer-substring (cadr spec) (caddr spec))))
324 (delete-region (cadr spec) (caddr spec))
325 (gnus-html-put-image file (cadr spec) string))))
327 (gnus-html-schedule-image-fetching buffer images)))))
329 (defun gnus-html-put-image (file point string &optional url alt-text)
330 (when (gnus-graphic-display-p)
331 (let* ((image (ignore-errors
332 (gnus-create-image file)))
334 (if (featurep 'xemacs)
335 (cons (glyph-width image) (glyph-height image))
336 (image-size image t)))))
340 ;; Kludge to avoid displaying 30x30 gif images, which
341 ;; seems to be a signal of a broken image.
342 (not (and (if (featurep 'xemacs)
345 (eq (if (featurep 'xemacs)
346 (let ((data (cdadar (specifier-spec-list
347 (glyph-image image)))))
350 (plist-get (cdr image) :type))
354 (let ((start (point)))
355 (setq image (gnus-html-rescale-image image file size))
356 (gnus-put-image image
357 (gnus-string-or string "*")
359 (let ((overlay (gnus-make-overlay start (point))))
360 (gnus-overlay-put overlay 'local-map
361 gnus-html-displayed-image-map)
362 (gnus-put-text-property start (point) 'gnus-alt-text alt-text)
364 (gnus-put-text-property start (point) 'gnus-image url)))
365 (gnus-add-image 'external image)
368 (when (fboundp 'find-image)
369 (setq image (find-image '((:type xpm :file "lock-broken.xpm"))))
370 (gnus-put-image image
371 (gnus-string-or string "*")
373 (gnus-add-image 'internal image))
376 (defun gnus-html-rescale-image (image file size)
377 (if (or (not (fboundp 'imagemagick-types))
378 (not (get-buffer-window (current-buffer))))
380 (let* ((width (car size))
382 (edges (window-pixel-edges (get-buffer-window (current-buffer))))
383 (window-width (truncate (* gnus-max-image-proportion
384 (- (nth 2 edges) (nth 0 edges)))))
385 (window-height (truncate (* gnus-max-image-proportion
386 (- (nth 3 edges) (nth 1 edges)))))
388 (when (> height window-height)
389 (setq image (or (create-image file 'imagemagick nil
390 :height window-height)
392 (setq size (image-size image t)))
393 (when (> (car size) window-width)
395 (create-image file 'imagemagick nil
400 (defun gnus-html-prune-cache ()
403 (dolist (file (directory-files gnus-html-cache-directory t nil t))
404 (let ((attributes (file-attributes file)))
405 (unless (nth 0 attributes)
406 (incf total-size (nth 7 attributes))
407 (push (list (time-to-seconds (nth 5 attributes))
408 (nth 7 attributes) file)
410 (when (> total-size gnus-html-cache-size)
411 (setq files (sort files (lambda (f1 f2)
412 (< (car f1) (car f2)))))
414 (when (> total-size gnus-html-cache-size)
415 (decf total-size (cadr file))
416 (delete-file (nth 2 file)))))))
418 (defun gnus-html-image-url-blocked-p (url blocked-images)
419 "Find out if URL is blocked by BLOCKED-IMAGES."
420 (let ((ret (and blocked-images
421 (string-match blocked-images url))))
423 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
425 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
429 (defun gnus-html-show-images ()
430 "Show any images that are in the HTML-rendered article buffer.
431 This only works if the article in question is HTML."
433 (gnus-with-article-buffer
434 (let ((overlays (overlays-in (point-min) (point-max)))
436 (while (setq overlay (pop overlays))
437 (when (overlay-get overlay 'gnus-image)
438 (push (overlay-get overlay 'gnus-image) images)))
440 (message "No images to show")
441 (gnus-html-schedule-image-fetching (current-buffer) images)))))
444 (defun gnus-html-prefetch-images (summary)
445 (let (blocked-images urls)
446 (when (and (buffer-live-p summary)
447 (executable-find "curl"))
448 (with-current-buffer summary
449 (setq blocked-images gnus-blocked-images))
451 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
452 (let ((url (match-string 1)))
453 (unless (gnus-html-image-url-blocked-p url blocked-images)
454 (unless (file-exists-p (gnus-html-image-id url))
455 (push (mm-url-decode-entities-string url) urls)
456 (push (gnus-html-image-id url) urls)
459 (apply 'start-process
465 (gnus-set-process-query-on-exit-flag process nil))))))
469 ;;; gnus-html.el ends here