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."
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
70 (defun gnus-article-html (handle)
71 (let ((article-buffer (current-buffer)))
73 (narrow-to-region (point) (point))
76 (let* ((coding-system-for-read 'utf-8)
77 (coding-system-for-write 'utf-8)
78 (default-process-coding-system
79 (cons coding-system-for-read coding-system-for-write))
80 (charset (mail-content-type-get (mm-handle-type handle)
83 (setq charset (mm-charset-to-coding-system charset))
84 (not (eq charset 'ascii)))
85 (mm-decode-coding-region (point-min) (point-max) charset))
86 (call-process-region (point-min) (point-max)
88 nil article-buffer nil
95 "-t" (format "%s" tab-width)
96 "-cols" (format "%s" gnus-html-frame-width)
97 "-o" "display_image=off"
99 (gnus-html-wash-tags))))
101 (defvar gnus-article-mouse-face)
103 (defun gnus-html-wash-tags ()
104 (let (tag parameters string start end images url)
105 (mm-url-decode-entities)
106 (goto-char (point-min))
107 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
108 (setq tag (match-string 1)
109 parameters (match-string 2)
110 start (match-beginning 0))
111 (when (plusp (length parameters))
112 (set-text-properties 0 (1- (length parameters)) nil parameters))
113 (delete-region start (point))
114 (when (search-forward (concat "</" tag ">") nil t)
115 (delete-region (match-beginning 0) (match-end 0)))
118 ;; Fetch and insert a picture.
119 ((equal tag "img_alt")
120 (when (string-match "src=\"\\([^\"]+\\)" parameters)
121 (setq url (match-string 1 parameters))
122 (gnus-message 8 "Fetching image URL %s" url)
123 (if (string-match "^cid:\\(.*\\)" url)
124 ;; URLs with cid: have their content stashed in other
125 ;; parts of the MIME structure, so just insert them
127 (let ((handle (mm-get-content-id
128 (setq url (match-string 1 url))))
132 (setq image (gnus-create-image (buffer-string)
135 (let ((string (buffer-substring start end)))
136 (delete-region start end)
137 (gnus-put-image image (gnus-string-or string "*")))))
138 ;; Normal, external URL.
139 (unless (gnus-html-image-url-blocked-p url)
140 (let ((file (gnus-html-image-id url)))
141 (if (file-exists-p file)
142 ;; It's already cached, so just insert it.
143 (let ((string (buffer-substring start end)))
144 ;; Delete the ALT text.
145 (delete-region start end)
146 (gnus-html-put-image file (point) string))
147 ;; We don't have it, so schedule it for fetching
150 (set-marker (make-marker) start)
155 (when (string-match "href=\"\\([^\"]+\\)" parameters)
156 (setq url (match-string 1 parameters))
157 (gnus-message 8 "Fetching link URL %s" url)
158 (gnus-article-add-button start end
161 (let ((overlay (gnus-make-overlay start end)))
162 (gnus-overlay-put overlay 'evaporate t)
163 (gnus-overlay-put overlay 'gnus-button-url url)
164 (when gnus-article-mouse-face
165 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
166 ;; The upper-case IMG_ALT is apparently just an artifact that
167 ;; should be deleted.
168 ((equal tag "IMG_ALT")
169 (delete-region start end))
170 ;; Whatever. Just ignore the tag.
174 (goto-char (point-min))
175 ;; The output from -halfdump isn't totally regular, so strip
176 ;; off any </pre_int>s that were left over.
177 (while (re-search-forward "</pre_int>" nil t)
178 (replace-match "" t t))
180 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
182 (defun gnus-html-schedule-image-fetching (buffer images)
183 (gnus-message 8 "Scheduling image fetching in buffer %s, images %s" buffer images)
184 (let* ((url (caar images))
185 (process (start-process
190 "-o" (gnus-html-image-id url)
192 (process-kill-without-query process)
193 (set-process-sentinel process 'gnus-html-curl-sentinel)
194 (gnus-set-process-plist process (list 'images images
197 (defun gnus-html-image-id (url)
198 (expand-file-name (sha1 url) gnus-html-cache-directory))
200 (defun gnus-html-curl-sentinel (process event)
201 (when (string-match "finished" event)
202 (let* ((images (gnus-process-get process 'images))
203 (buffer (gnus-process-get process 'buffer))
205 (file (gnus-html-image-id (car spec))))
206 (when (and (buffer-live-p buffer)
207 ;; If the position of the marker is 1, then that
208 ;; means that the text it was in has been deleted;
209 ;; i.e., that the user has selected a different
210 ;; article before the image arrived.
211 (not (= (marker-position (cadr spec)) (point-min))))
212 (with-current-buffer buffer
213 (let ((inhibit-read-only t)
214 (string (buffer-substring (cadr spec) (caddr spec))))
215 (delete-region (cadr spec) (caddr spec))
216 (gnus-html-put-image file (cadr spec) string))))
218 (gnus-html-schedule-image-fetching buffer images)))))
220 (defun gnus-html-put-image (file point string)
221 (when (display-graphic-p)
222 (let ((image (ignore-errors
223 (gnus-create-image file))))
227 ;; Kludge to avoid displaying 30x30 gif images, which
228 ;; seems to be a signal of a broken image.
229 (not (and (listp image)
230 (eq (plist-get (cdr image) :type) 'gif)
231 (= (car (image-size image t)) 30)
232 (= (cdr (image-size image t)) 30))))
234 (gnus-put-image (gnus-html-rescale-image image)
235 (gnus-string-or string "*"))
238 (when (fboundp 'find-image)
239 (gnus-put-image (find-image
240 '((:type xpm :file "lock-broken.xpm")))
241 (gnus-string-or string "*")))
244 (defun gnus-html-rescale-image (image)
245 (if (or (not (fboundp 'imagemagick-types))
246 (not (get-buffer-window (current-buffer))))
248 (let* ((width (car (image-size image t)))
249 (height (cdr (image-size image t)))
250 (edges (window-pixel-edges (get-buffer-window (current-buffer))))
251 (window-width (truncate (* gnus-max-image-proportion
252 (- (nth 2 edges) (nth 0 edges)))))
253 (window-height (truncate (* gnus-max-image-proportion
254 (- (nth 3 edges) (nth 1 edges)))))
256 (when (> width window-width)
257 (setq window-height (truncate (* window-height
258 (/ (* 1.0 window-width) width)))))
260 (cond ((> height window-height)
261 (create-image file 'imagemagick nil
262 :height window-height))
263 ((> width window-width)
264 (create-image file 'imagemagick nil
265 :width window-width)))
268 (defun gnus-html-prune-cache ()
271 (dolist (file (directory-files gnus-html-cache-directory t nil t))
272 (let ((attributes (file-attributes file)))
273 (unless (nth 0 attributes)
274 (incf total-size (nth 7 attributes))
275 (push (list (time-to-seconds (nth 5 attributes))
276 (nth 7 attributes) file)
278 (when (> total-size gnus-html-cache-size)
279 (setq files (sort files (lambda (f1 f2)
280 (< (car f1) (car f2)))))
282 (when (> total-size gnus-html-cache-size)
283 (decf total-size (cadr file))
284 (delete-file (nth 2 file)))))))
287 (defun gnus-html-image-url-blocked-p (url)
288 "Find out if URL is blocked by `gnus-blocked-images'."
289 (let ((ret (and gnus-blocked-images
290 (string-match gnus-blocked-images url))))
292 (gnus-message 8 "Image URL %s is blocked by gnus-blocked-images regex %s" url gnus-blocked-images))
296 (defun gnus-html-prefetch-images (summary)
297 (let (blocked-images urls)
298 (when (buffer-live-p summary)
299 (with-current-buffer summary
300 (setq blocked-images gnus-blocked-images))
302 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
303 (let ((url (match-string 1)))
304 (unless (gnus-html-image-url-blocked-p url)
305 (unless (file-exists-p (gnus-html-image-id url))
307 (push (gnus-html-image-id url) urls)
310 (apply 'start-process
316 (process-kill-without-query process))))))
320 ;;; gnus-html.el ends here