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).
33 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
34 "Where Gnus will cache images it downloads from the web."
38 (defcustom gnus-html-cache-size 500000000
39 "The size of the Gnus image cache."
43 (defcustom gnus-html-frame-width 70
44 "What width to use when rendering HTML."
49 (defun gnus-article-html (handle)
50 (let ((article-buffer (current-buffer)))
52 (narrow-to-region (point) (point))
55 (let* ((coding-system-for-read 'utf-8)
56 (coding-system-for-write 'utf-8)
57 (default-process-coding-system
58 (cons coding-system-for-read coding-system-for-write)))
59 (call-process-region (point-min) (point-max)
61 nil article-buffer nil
68 "-t" (format "%s" tab-width)
69 "-cols" (format "%s" gnus-html-frame-width)
70 "-o" "display_image=off"
72 (gnus-html-wash-tags))))
74 (defun gnus-html-wash-tags ()
75 (let (tag parameters string start end images url)
76 (mm-url-decode-entities)
77 (goto-char (point-min))
78 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
79 (setq tag (match-string 1)
80 parameters (match-string 2)
81 start (match-beginning 0))
82 (when (plusp (length parameters))
83 (set-text-properties 0 (1- (length parameters)) nil parameters))
84 (delete-region start (point))
85 (when (search-forward (concat "</" tag ">") nil t)
86 (delete-region (match-beginning 0) (match-end 0)))
89 ;; Fetch and insert a picture.
90 ((equal tag "img_alt")
91 (when (string-match "src=\"\\([^\"]+\\)" parameters)
92 (setq url (match-string 1 parameters))
93 (when (or (null mm-w3m-safe-url-regexp)
94 (string-match mm-w3m-safe-url-regexp url))
95 (if (string-match "^cid:\\(.*\\)" url)
96 ;; URLs with cid: have their content stashed in other
97 ;; parts of the MIME structure, so just insert them
99 (let ((handle (mm-get-content-id
100 (setq url (match-string 1 url))))
104 (setq image (gnus-create-image (buffer-string)
107 (delete-region start end)
108 (gnus-put-image image)))
109 ;; Normal, external URL.
110 (let ((file (gnus-html-image-id url)))
111 (if (file-exists-p file)
112 ;; It's already cached, so just insert it.
113 (when (gnus-html-put-image file (point))
114 ;; Delete the ALT text.
115 (delete-region start end))
116 ;; We don't have it, so schedule it for fetching
119 (set-marker (make-marker) start)
124 (when (string-match "href=\"\\([^\"]+\\)" parameters)
125 (setq url (match-string 1 parameters))
126 (gnus-article-add-button start end
129 (let ((overlay (gnus-make-overlay start end)))
130 (gnus-overlay-put overlay 'evaporate t)
131 (gnus-overlay-put overlay 'gnus-button-url url)
132 (when gnus-article-mouse-face
133 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
134 ;; Whatever. Just ignore the tag.
138 (goto-char (point-min))
139 ;; The output from -halfdump isn't totally regular, so strip
140 ;; off any </pre_int>s that were left over.
141 (while (re-search-forward "</pre_int>" nil t)
142 (replace-match "" t t))
144 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
146 (defun gnus-html-schedule-image-fetching (buffer images)
147 (let* ((url (caar images))
148 (process (start-process
153 "-o" (gnus-html-image-id url)
155 (process-kill-without-query process)
156 (set-process-sentinel process 'gnus-html-curl-sentinel)
157 (set-process-plist process (list 'images images
160 (defun gnus-html-image-id (url)
161 (expand-file-name (sha1 url) gnus-html-cache-directory))
163 (defun gnus-html-curl-sentinel (process event)
164 (when (string-match "finished" event)
165 (let* ((images (getf (process-plist process) 'images))
166 (buffer (getf (process-plist process) 'buffer))
168 (file (gnus-html-image-id (car spec))))
169 (when (and (buffer-live-p buffer)
170 ;; If the position of the marker is 1, then that
171 ;; means that the text is was in has been deleted;
172 ;; i.e., that the user has selected a different
173 ;; article before the image arrived.
174 (not (= (marker-position (cadr spec)) 1)))
177 (let ((buffer-read-only nil))
178 (when (gnus-html-put-image file (cadr spec))
179 (delete-region (1+ (cadr spec)) (caddr spec))))))
181 (gnus-html-schedule-image-fetching buffer images)))))
183 (defun gnus-html-put-image (file point)
184 (when (display-graphic-p)
185 (let ((image (ignore-errors
186 (gnus-create-image file))))
190 ;; Kludge to avoid displaying 30x30 gif images, which
191 ;; seems to be a signal of a broken image.
192 (not (and (listp image)
193 (eq (getf (cdr image) :type) 'gif)
194 (= (car (image-size image t)) 30)
195 (= (cdr (image-size image t)) 30))))
197 (gnus-put-image image)
199 (when (fboundp 'find-image)
200 (gnus-put-image (find-image
201 '((:type xpm :file "lock-broken.xpm")))))
204 (defun gnus-html-prune-cache ()
207 (dolist (file (directory-files gnus-html-cache-directory t nil t))
208 (let ((attributes (file-attributes file)))
209 (unless (nth 0 attributes)
210 (incf total-size (nth 7 attributes))
211 (push (list (time-to-seconds (nth 5 attributes))
212 (nth 7 attributes) file)
214 (when (> total-size gnus-html-cache-size)
215 (setq files (sort files (lambda (f1 f2)
216 (< (car f1) (car f2)))))
218 (when (> total-size gnus-html-cache-size)
219 (decf total-size (cadr file))
220 (delete-file (nth 2 file)))))))
223 (defun gnus-html-prefetch-images (summary)
224 (let (safe-url-regexp urls)
225 (when (buffer-live-p summary)
228 (setq safe-url-regexp mm-w3m-safe-url-regexp))
230 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
231 (let ((url (match-string 1)))
232 (when (or (null safe-url-regexp)
233 (string-match safe-url-regexp url))
234 (unless (file-exists-p (gnus-html-image-id url))
236 (push (gnus-html-image-id url) urls)
239 (apply 'start-process
245 (process-kill-without-query process))))))
249 ;;; gnus-html.el ends here