1 ;;; gnus-html.el --- Quoted-Printable functions
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."
60 (defun gnus-article-html (handle)
61 (let ((article-buffer (current-buffer)))
63 (narrow-to-region (point) (point))
66 (let* ((coding-system-for-read 'utf-8
)
67 (coding-system-for-write 'utf-8
)
68 (default-process-coding-system
69 (cons coding-system-for-read coding-system-for-write
))
70 (charset (mail-content-type-get (mm-handle-type handle
)
73 (setq charset
(mm-charset-to-coding-system charset
))
74 (not (eq charset
'ascii
)))
75 (mm-decode-coding-region (point-min) (point-max) charset
))
76 (call-process-region (point-min) (point-max)
78 nil article-buffer nil
85 "-t" (format "%s" tab-width
)
86 "-cols" (format "%s" gnus-html-frame-width
)
87 "-o" "display_image=off"
89 (gnus-html-wash-tags))))
91 (defvar gnus-article-mouse-face
)
93 (defun gnus-html-wash-tags ()
94 (let (tag parameters string start end images url
)
95 (mm-url-decode-entities)
96 (goto-char (point-min))
97 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t
)
98 (setq tag
(match-string 1)
99 parameters
(match-string 2)
100 start
(match-beginning 0))
101 (when (plusp (length parameters
))
102 (set-text-properties 0 (1- (length parameters
)) nil parameters
))
103 (delete-region start
(point))
104 (when (search-forward (concat "</" tag
">") nil t
)
105 (delete-region (match-beginning 0) (match-end 0)))
108 ;; Fetch and insert a picture.
109 ((equal tag
"img_alt")
110 (when (string-match "src=\"\\([^\"]+\\)" parameters
)
111 (setq url
(match-string 1 parameters
))
112 (if (string-match "^cid:\\(.*\\)" url
)
113 ;; URLs with cid: have their content stashed in other
114 ;; parts of the MIME structure, so just insert them
116 (let ((handle (mm-get-content-id
117 (setq url
(match-string 1 url
))))
121 (setq image
(gnus-create-image (buffer-string)
124 (delete-region start end
)
125 (gnus-put-image image
)))
126 ;; Normal, external URL.
127 (when (or (null gnus-blocked-images
)
128 (not (string-match gnus-blocked-images url
)))
129 (let ((file (gnus-html-image-id url
)))
130 (if (file-exists-p file
)
131 ;; It's already cached, so just insert it.
132 (when (gnus-html-put-image file
(point))
133 ;; Delete the ALT text.
134 (delete-region start end
))
135 ;; We don't have it, so schedule it for fetching
138 (set-marker (make-marker) start
)
143 (when (string-match "href=\"\\([^\"]+\\)" parameters
)
144 (setq url
(match-string 1 parameters
))
145 (gnus-article-add-button start end
148 (let ((overlay (gnus-make-overlay start end
)))
149 (gnus-overlay-put overlay
'evaporate t
)
150 (gnus-overlay-put overlay
'gnus-button-url url
)
151 (when gnus-article-mouse-face
152 (gnus-overlay-put overlay
'mouse-face gnus-article-mouse-face
)))))
153 ;; Whatever. Just ignore the tag.
157 (goto-char (point-min))
158 ;; The output from -halfdump isn't totally regular, so strip
159 ;; off any </pre_int>s that were left over.
160 (while (re-search-forward "</pre_int>" nil t
)
161 (replace-match "" t t
))
163 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images
)))))
165 (defun gnus-html-schedule-image-fetching (buffer images
)
166 (let* ((url (caar images
))
167 (process (start-process
172 "-o" (gnus-html-image-id url
)
174 (process-kill-without-query process
)
175 (set-process-sentinel process
'gnus-html-curl-sentinel
)
176 (gnus-set-process-plist process
(list 'images images
179 (defun gnus-html-image-id (url)
180 (expand-file-name (sha1 url
) gnus-html-cache-directory
))
182 (defun gnus-html-curl-sentinel (process event
)
183 (when (string-match "finished" event
)
184 (let* ((images (gnus-process-get process
'images
))
185 (buffer (gnus-process-get process
'buffer
))
187 (file (gnus-html-image-id (car spec
))))
188 (when (and (buffer-live-p buffer
)
189 ;; If the position of the marker is 1, then that
190 ;; means that the text it was in has been deleted;
191 ;; i.e., that the user has selected a different
192 ;; article before the image arrived.
193 (not (= (marker-position (cadr spec
)) (point-min))))
194 (with-current-buffer buffer
195 (let ((inhibit-read-only t
))
196 (when (gnus-html-put-image file
(cadr spec
))
197 (delete-region (1+ (cadr spec
)) (caddr spec
))))))
199 (gnus-html-schedule-image-fetching buffer images
)))))
201 (defun gnus-html-put-image (file point
)
202 (when (display-graphic-p)
203 (let ((image (ignore-errors
204 (gnus-create-image file
))))
208 ;; Kludge to avoid displaying 30x30 gif images, which
209 ;; seems to be a signal of a broken image.
210 (not (and (listp image
)
211 (eq (plist-get (cdr image
) :type
) 'gif
)
212 (= (car (image-size image t
)) 30)
213 (= (cdr (image-size image t
)) 30))))
215 (gnus-put-image image
)
217 (when (fboundp 'find-image
)
218 (gnus-put-image (find-image
219 '((:type xpm
:file
"lock-broken.xpm")))))
222 (defun gnus-html-prune-cache ()
225 (dolist (file (directory-files gnus-html-cache-directory t nil t
))
226 (let ((attributes (file-attributes file
)))
227 (unless (nth 0 attributes
)
228 (incf total-size
(nth 7 attributes
))
229 (push (list (time-to-seconds (nth 5 attributes
))
230 (nth 7 attributes
) file
)
232 (when (> total-size gnus-html-cache-size
)
233 (setq files
(sort files
(lambda (f1 f2
)
234 (< (car f1
) (car f2
)))))
236 (when (> total-size gnus-html-cache-size
)
237 (decf total-size
(cadr file
))
238 (delete-file (nth 2 file
)))))))
241 (defun gnus-html-prefetch-images (summary)
242 (let (blocked-images urls
)
243 (when (buffer-live-p summary
)
244 (with-current-buffer summary
245 (setq blocked-images gnus-blocked-images
))
247 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t
)
248 (let ((url (match-string 1)))
249 (when (or (null blocked-images
)
250 (not (string-match blocked-images url
)))
251 (unless (file-exists-p (gnus-html-image-id url
))
253 (push (gnus-html-image-id url
) urls
)
256 (apply 'start-process
262 (process-kill-without-query process
))))))
266 ;;; gnus-html.el ends here