gnus-ems.el: Provide compatibility functions for gnus-set-process-plist by Katsumi...
[emacs.git] / lisp / gnus / gnus-html.el
blobc64b9f5f0d1f682bbc8b251de3f7326522e5e628
1 ;;; gnus-html.el --- Quoted-Printable functions
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html, web
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/>.
23 ;;; Commentary:
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).
29 ;;; Code:
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'mm-decode))
33 (require 'mm-url)
35 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
36 "Where Gnus will cache images it downloads from the web."
37 :group 'gnus-art
38 :type 'directory)
40 (defcustom gnus-html-cache-size 500000000
41 "The size of the Gnus image cache."
42 :group 'gnus-art
43 :type 'integer)
45 (defcustom gnus-html-frame-width 70
46 "What width to use when rendering HTML."
47 :group 'gnus-art
48 :type 'integer)
50 ;;;###autoload
51 (defun gnus-article-html (handle)
52 (let ((article-buffer (current-buffer)))
53 (save-restriction
54 (narrow-to-region (point) (point))
55 (save-excursion
56 (mm-with-part handle
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)
62 "w3m"
63 nil article-buffer nil
64 "-halfdump"
65 "-no-cookie"
66 "-I" "UTF-8"
67 "-O" "UTF-8"
68 "-o" "ext_halfdump=1"
69 "-o" "pre_conv=1"
70 "-t" (format "%s" tab-width)
71 "-cols" (format "%s" gnus-html-frame-width)
72 "-o" "display_image=off"
73 "-T" "text/html"))))
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)))
91 (setq end (point))
92 (cond
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
102 ;; immediately.
103 (let ((handle (mm-get-content-id
104 (setq url (match-string 1 url))))
105 image)
106 (when handle
107 (mm-with-part handle
108 (setq image (gnus-create-image (buffer-string)
109 nil t))))
110 (when image
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
121 ;; asynchronously.
122 (push (list url
123 (set-marker (make-marker) start)
124 (point-marker))
125 images)))))))
126 ;; Add a link.
127 ((equal tag "a")
128 (when (string-match "href=\"\\([^\"]+\\)" parameters)
129 (setq url (match-string 1 parameters))
130 (gnus-article-add-button start end
131 'browse-url url
132 url)
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.
141 (goto-char start))
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))
147 (when images
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
153 "images" nil "curl"
154 "-s" "--create-dirs"
155 "--location"
156 "--max-time" "60"
157 "-o" (gnus-html-image-id url)
158 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
162 'buffer buffer))))
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))
171 (spec (pop images))
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))))))
183 (when images
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))))
190 (save-excursion
191 (goto-char point)
192 (if (and image
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))))
199 (progn
200 (gnus-put-image image)
202 (when (fboundp 'find-image)
203 (gnus-put-image (find-image
204 '((:type xpm :file "lock-broken.xpm")))))
205 nil)))))
207 (defun gnus-html-prune-cache ()
208 (let ((total-size 0)
209 files)
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)
216 files))))
217 (when (> total-size gnus-html-cache-size)
218 (setq files (sort files (lambda (f1 f2)
219 (< (car f1) (car f2)))))
220 (dolist (file files)
221 (when (> total-size gnus-html-cache-size)
222 (decf total-size (cadr file))
223 (delete-file (nth 2 file)))))))
225 ;;;###autoload
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))
231 (save-match-data
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))
237 (push url urls)
238 (push (gnus-html-image-id url) urls)
239 (push "-o" urls)))))
240 (let ((process
241 (apply 'start-process
242 "images" nil "curl"
243 "-s" "--create-dirs"
244 "--location"
245 "--max-time" "60"
246 urls)))
247 (process-kill-without-query process))))))
249 (provide 'gnus-html)
251 ;;; gnus-html.el ends here