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 (call-process-region (point-min) (point-max)
72 nil article-buffer nil
79 "-t" (format "%s" tab-width)
80 "-cols" (format "%s" gnus-html-frame-width)
81 "-o" "display_image=off"
83 (gnus-html-wash-tags))))
85 (defvar gnus-article-mouse-face)
87 (defun gnus-html-wash-tags ()
88 (let (tag parameters string start end images url)
89 (mm-url-decode-entities)
90 (goto-char (point-min))
91 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
92 (setq tag (match-string 1)
93 parameters (match-string 2)
94 start (match-beginning 0))
95 (when (plusp (length parameters))
96 (set-text-properties 0 (1- (length parameters)) nil parameters))
97 (delete-region start (point))
98 (when (search-forward (concat "</" tag ">") nil t)
99 (delete-region (match-beginning 0) (match-end 0)))
102 ;; Fetch and insert a picture.
103 ((equal tag "img_alt")
104 (when (string-match "src=\"\\([^\"]+\\)" parameters)
105 (setq url (match-string 1 parameters))
106 (if (string-match "^cid:\\(.*\\)" url)
107 ;; URLs with cid: have their content stashed in other
108 ;; parts of the MIME structure, so just insert them
110 (let ((handle (mm-get-content-id
111 (setq url (match-string 1 url))))
115 (setq image (gnus-create-image (buffer-string)
118 (delete-region start end)
119 (gnus-put-image image)))
120 ;; Normal, external URL.
121 (when (or (null gnus-blocked-images)
122 (not (string-match gnus-blocked-images url)))
123 (let ((file (gnus-html-image-id url)))
124 (if (file-exists-p file)
125 ;; It's already cached, so just insert it.
126 (when (gnus-html-put-image file (point))
127 ;; Delete the ALT text.
128 (delete-region start end))
129 ;; We don't have it, so schedule it for fetching
132 (set-marker (make-marker) start)
137 (when (string-match "href=\"\\([^\"]+\\)" parameters)
138 (setq url (match-string 1 parameters))
139 (gnus-article-add-button start end
142 (let ((overlay (gnus-make-overlay start end)))
143 (gnus-overlay-put overlay 'evaporate t)
144 (gnus-overlay-put overlay 'gnus-button-url url)
145 (when gnus-article-mouse-face
146 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
147 ;; Whatever. Just ignore the tag.
151 (goto-char (point-min))
152 ;; The output from -halfdump isn't totally regular, so strip
153 ;; off any </pre_int>s that were left over.
154 (while (re-search-forward "</pre_int>" nil t)
155 (replace-match "" t t))
157 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
159 (defun gnus-html-schedule-image-fetching (buffer images)
160 (let* ((url (caar images))
161 (process (start-process
166 "-o" (gnus-html-image-id url)
168 (process-kill-without-query process)
169 (set-process-sentinel process 'gnus-html-curl-sentinel)
170 (gnus-set-process-plist process (list 'images images
173 (defun gnus-html-image-id (url)
174 (expand-file-name (sha1 url) gnus-html-cache-directory))
176 (defun gnus-html-curl-sentinel (process event)
177 (when (string-match "finished" event)
178 (let* ((images (gnus-process-get process 'images))
179 (buffer (gnus-process-get process 'buffer))
181 (file (gnus-html-image-id (car spec))))
182 (when (and (buffer-live-p buffer)
183 ;; If the position of the marker is 1, then that
184 ;; means that the text it was in has been deleted;
185 ;; i.e., that the user has selected a different
186 ;; article before the image arrived.
187 (not (= (marker-position (cadr spec)) (point-min))))
188 (with-current-buffer buffer
189 (let ((inhibit-read-only t))
190 (when (gnus-html-put-image file (cadr spec))
191 (delete-region (1+ (cadr spec)) (caddr spec))))))
193 (gnus-html-schedule-image-fetching buffer images)))))
195 (defun gnus-html-put-image (file point)
196 (when (display-graphic-p)
197 (let ((image (ignore-errors
198 (gnus-create-image file))))
202 ;; Kludge to avoid displaying 30x30 gif images, which
203 ;; seems to be a signal of a broken image.
204 (not (and (listp image)
205 (eq (plist-get (cdr image) :type) 'gif)
206 (= (car (image-size image t)) 30)
207 (= (cdr (image-size image t)) 30))))
209 (gnus-put-image image)
211 (when (fboundp 'find-image)
212 (gnus-put-image (find-image
213 '((:type xpm :file "lock-broken.xpm")))))
216 (defun gnus-html-prune-cache ()
219 (dolist (file (directory-files gnus-html-cache-directory t nil t))
220 (let ((attributes (file-attributes file)))
221 (unless (nth 0 attributes)
222 (incf total-size (nth 7 attributes))
223 (push (list (time-to-seconds (nth 5 attributes))
224 (nth 7 attributes) file)
226 (when (> total-size gnus-html-cache-size)
227 (setq files (sort files (lambda (f1 f2)
228 (< (car f1) (car f2)))))
230 (when (> total-size gnus-html-cache-size)
231 (decf total-size (cadr file))
232 (delete-file (nth 2 file)))))))
235 (defun gnus-html-prefetch-images (summary)
236 (let (blocked-images urls)
237 (when (buffer-live-p summary)
238 (with-current-buffer summary
239 (setq blocked-images gnus-blocked-images))
241 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
242 (let ((url (match-string 1)))
243 (when (or (null blocked-images)
244 (not (string-match blocked-images url)))
245 (unless (file-exists-p (gnus-html-image-id url))
247 (push (gnus-html-image-id url) urls)
250 (apply 'start-process
256 (process-kill-without-query process))))))
260 ;;; gnus-html.el ends here