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."
40 (defcustom gnus-html-cache-size
500000000
41 "The size of the Gnus image cache."
45 (defcustom gnus-html-frame-width
70
46 "What width to use when rendering HTML."
51 (defun gnus-article-html (handle)
52 (let ((article-buffer (current-buffer)))
54 (narrow-to-region (point) (point))
57 (let* ((coding-system-for-read 'utf-8
)
58 (coding-system-for-write 'utf-8
)
59 (default-process-coding-system
60 (cons coding-system-for-read coding-system-for-write
)))
61 (call-process-region (point-min) (point-max)
63 nil article-buffer nil
70 "-t" (format "%s" tab-width
)
71 "-cols" (format "%s" gnus-html-frame-width
)
72 "-o" "display_image=off"
74 (gnus-html-wash-tags))))
76 (defvar gnus-article-mouse-face
)
78 (defun gnus-html-wash-tags ()
79 (let (tag parameters string start end images url
)
80 (mm-url-decode-entities)
81 (goto-char (point-min))
82 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t
)
83 (setq tag
(match-string 1)
84 parameters
(match-string 2)
85 start
(match-beginning 0))
86 (when (plusp (length parameters
))
87 (set-text-properties 0 (1- (length parameters
)) nil parameters
))
88 (delete-region start
(point))
89 (when (search-forward (concat "</" tag
">") nil t
)
90 (delete-region (match-beginning 0) (match-end 0)))
93 ;; Fetch and insert a picture.
94 ((equal tag
"img_alt")
95 (when (string-match "src=\"\\([^\"]+\\)" parameters
)
96 (setq url
(match-string 1 parameters
))
97 (when (or (null mm-w3m-safe-url-regexp
)
98 (string-match mm-w3m-safe-url-regexp url
))
99 (if (string-match "^cid:\\(.*\\)" url
)
100 ;; URLs with cid: have their content stashed in other
101 ;; parts of the MIME structure, so just insert them
103 (let ((handle (mm-get-content-id
104 (setq url
(match-string 1 url
))))
108 (setq image
(gnus-create-image (buffer-string)
111 (delete-region start end
)
112 (gnus-put-image image
)))
113 ;; Normal, external URL.
114 (let ((file (gnus-html-image-id url
)))
115 (if (file-exists-p file
)
116 ;; It's already cached, so just insert it.
117 (when (gnus-html-put-image file
(point))
118 ;; Delete the ALT text.
119 (delete-region start end
))
120 ;; We don't have it, so schedule it for fetching
123 (set-marker (make-marker) start
)
128 (when (string-match "href=\"\\([^\"]+\\)" parameters
)
129 (setq url
(match-string 1 parameters
))
130 (gnus-article-add-button start end
133 (let ((overlay (gnus-make-overlay start end
)))
134 (gnus-overlay-put overlay
'evaporate t
)
135 (gnus-overlay-put overlay
'gnus-button-url url
)
136 (when gnus-article-mouse-face
137 (gnus-overlay-put overlay
'mouse-face gnus-article-mouse-face
)))))
138 ;; Whatever. Just ignore the tag.
142 (goto-char (point-min))
143 ;; The output from -halfdump isn't totally regular, so strip
144 ;; off any </pre_int>s that were left over.
145 (while (re-search-forward "</pre_int>" nil t
)
146 (replace-match "" t t
))
148 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images
)))))
150 (defun gnus-html-schedule-image-fetching (buffer images
)
151 (let* ((url (caar images
))
152 (process (start-process
157 "-o" (gnus-html-image-id url
)
159 (process-kill-without-query process
)
160 (set-process-sentinel process
'gnus-html-curl-sentinel
)
161 (gnus-set-process-plist process
(list 'images images
164 (defun gnus-html-image-id (url)
165 (expand-file-name (sha1 url
) gnus-html-cache-directory
))
167 (defun gnus-html-curl-sentinel (process event
)
168 (when (string-match "finished" event
)
169 (let* ((images (gnus-process-get process
'images
))
170 (buffer (gnus-process-get process
'buffer
))
172 (file (gnus-html-image-id (car spec
))))
173 (when (and (buffer-live-p buffer
)
174 ;; If the position of the marker is 1, then that
175 ;; means that the text it was in has been deleted;
176 ;; i.e., that the user has selected a different
177 ;; article before the image arrived.
178 (not (= (marker-position (cadr spec
)) (point-min))))
179 (with-current-buffer buffer
180 (let ((inhibit-read-only t
))
181 (when (gnus-html-put-image file
(cadr spec
))
182 (delete-region (1+ (cadr spec
)) (caddr spec
))))))
184 (gnus-html-schedule-image-fetching buffer images
)))))
186 (defun gnus-html-put-image (file point
)
187 (when (display-graphic-p)
188 (let ((image (ignore-errors
189 (gnus-create-image file
))))
193 ;; Kludge to avoid displaying 30x30 gif images, which
194 ;; seems to be a signal of a broken image.
195 (not (and (listp image
)
196 (eq (plist-get (cdr image
) :type
) 'gif
)
197 (= (car (image-size image t
)) 30)
198 (= (cdr (image-size image t
)) 30))))
200 (gnus-put-image image
)
202 (when (fboundp 'find-image
)
203 (gnus-put-image (find-image
204 '((:type xpm
:file
"lock-broken.xpm")))))
207 (defun gnus-html-prune-cache ()
210 (dolist (file (directory-files gnus-html-cache-directory t nil t
))
211 (let ((attributes (file-attributes file
)))
212 (unless (nth 0 attributes
)
213 (incf total-size
(nth 7 attributes
))
214 (push (list (time-to-seconds (nth 5 attributes
))
215 (nth 7 attributes
) file
)
217 (when (> total-size gnus-html-cache-size
)
218 (setq files
(sort files
(lambda (f1 f2
)
219 (< (car f1
) (car f2
)))))
221 (when (> total-size gnus-html-cache-size
)
222 (decf total-size
(cadr file
))
223 (delete-file (nth 2 file
)))))))
226 (defun gnus-html-prefetch-images (summary)
227 (let (safe-url-regexp urls
)
228 (when (buffer-live-p summary
)
229 (with-current-buffer summary
230 (setq safe-url-regexp mm-w3m-safe-url-regexp
))
232 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t
)
233 (let ((url (match-string 1)))
234 (when (or (null safe-url-regexp
)
235 (string-match safe-url-regexp url
))
236 (unless (file-exists-p (gnus-html-image-id url
))
238 (push (gnus-html-image-id url
) urls
)
241 (apply 'start-process
247 (process-kill-without-query process
))))))
251 ;;; gnus-html.el ends here