8a7fae1fe9ae4b911332289d1d9a7b7ae6a14c94
[bpt/emacs.git] / lisp / gnus / gnus-html.el
1 ;;; gnus-html.el --- Quoted-Printable functions
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html, web
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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).
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'mm-decode))
33 (require 'mm-url)
34
35 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
36 "Where Gnus will cache images it downloads from the web."
37 :version "24.1"
38 :group 'gnus-art
39 :type 'directory)
40
41 (defcustom gnus-html-cache-size 500000000
42 "The size of the Gnus image cache."
43 :version "24.1"
44 :group 'gnus-art
45 :type 'integer)
46
47 (defcustom gnus-html-frame-width 70
48 "What width to use when rendering HTML."
49 :version "24.1"
50 :group 'gnus-art
51 :type 'integer)
52
53 (defcustom gnus-blocked-images "."
54 "Images that have URLs matching this regexp will be blocked."
55 :version "24.1"
56 :group 'gnus-art
57 :type 'regexp)
58
59 ;;;###autoload
60 (defun gnus-article-html (handle)
61 (let ((article-buffer (current-buffer)))
62 (save-restriction
63 (narrow-to-region (point) (point))
64 (save-excursion
65 (mm-with-part handle
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)
71 'charset)))
72 (when (and charset
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)
77 "w3m"
78 nil article-buffer nil
79 "-halfdump"
80 "-no-cookie"
81 "-I" "UTF-8"
82 "-O" "UTF-8"
83 "-o" "ext_halfdump=1"
84 "-o" "pre_conv=1"
85 "-t" (format "%s" tab-width)
86 "-cols" (format "%s" gnus-html-frame-width)
87 "-o" "display_image=off"
88 "-T" "text/html"))))
89 (gnus-html-wash-tags))))
90
91 (defvar gnus-article-mouse-face)
92
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)))
106 (setq end (point))
107 (cond
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
115 ;; immediately.
116 (let ((handle (mm-get-content-id
117 (setq url (match-string 1 url))))
118 image)
119 (when handle
120 (mm-with-part handle
121 (setq image (gnus-create-image (buffer-string)
122 nil t))))
123 (when image
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
136 ;; asynchronously.
137 (push (list url
138 (set-marker (make-marker) start)
139 (point-marker))
140 images)))))))
141 ;; Add a link.
142 ((equal tag "a")
143 (when (string-match "href=\"\\([^\"]+\\)" parameters)
144 (setq url (match-string 1 parameters))
145 (gnus-article-add-button start end
146 'browse-url url
147 url)
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.
154 (t
155 ))
156 (goto-char start))
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))
162 (when images
163 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
164
165 (defun gnus-html-schedule-image-fetching (buffer images)
166 (let* ((url (caar images))
167 (process (start-process
168 "images" nil "curl"
169 "-s" "--create-dirs"
170 "--location"
171 "--max-time" "60"
172 "-o" (gnus-html-image-id url)
173 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
177 'buffer buffer))))
178
179 (defun gnus-html-image-id (url)
180 (expand-file-name (sha1 url) gnus-html-cache-directory))
181
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))
186 (spec (pop images))
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))))))
198 (when images
199 (gnus-html-schedule-image-fetching buffer images)))))
200
201 (defun gnus-html-put-image (file point)
202 (when (display-graphic-p)
203 (let ((image (ignore-errors
204 (gnus-create-image file))))
205 (save-excursion
206 (goto-char point)
207 (if (and image
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))))
214 (progn
215 (gnus-put-image image)
216 t)
217 (when (fboundp 'find-image)
218 (gnus-put-image (find-image
219 '((:type xpm :file "lock-broken.xpm")))))
220 nil)))))
221
222 (defun gnus-html-prune-cache ()
223 (let ((total-size 0)
224 files)
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)
231 files))))
232 (when (> total-size gnus-html-cache-size)
233 (setq files (sort files (lambda (f1 f2)
234 (< (car f1) (car f2)))))
235 (dolist (file files)
236 (when (> total-size gnus-html-cache-size)
237 (decf total-size (cadr file))
238 (delete-file (nth 2 file)))))))
239
240 ;;;###autoload
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))
246 (save-match-data
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))
252 (push url urls)
253 (push (gnus-html-image-id url) urls)
254 (push "-o" urls)))))
255 (let ((process
256 (apply 'start-process
257 "images" nil "curl"
258 "-s" "--create-dirs"
259 "--location"
260 "--max-time" "60"
261 urls)))
262 (process-kill-without-query process))))))
263
264 (provide 'gnus-html)
265
266 ;;; gnus-html.el ends here