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 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
32 "Where Gnus will cache images it downloads from the web."
36 (defcustom gnus-html-cache-size 500000000
37 "The size of the Gnus image cache."
41 (defcustom gnus-html-frame-width 70
42 "What width to use when rendering HTML."
47 (defun gnus-article-html (handle)
48 (let ((article-buffer (current-buffer)))
50 (narrow-to-region (point) (point))
53 (let* ((coding-system-for-read 'utf-8)
54 (coding-system-for-write 'utf-8)
55 (default-process-coding-system
56 (cons coding-system-for-read coding-system-for-write)))
57 (call-process-region (point-min) (point-max)
59 nil article-buffer nil
66 "-t" (format "%s" tab-width)
67 "-cols" (format "%s" gnus-html-frame-width)
68 "-o" "display_image=off"
70 (gnus-html-wash-tags))))
72 (defun gnus-html-wash-tags ()
73 (let (tag parameters string start end images)
74 (mm-url-decode-entities)
75 (goto-char (point-min))
76 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
77 (setq tag (match-string 1)
78 parameters (match-string 2)
79 start (match-beginning 0))
80 (when (plusp (length parameters))
81 (set-text-properties 0 (1- (length parameters)) nil parameters))
82 (delete-region start (point))
83 (when (search-forward (concat "</" tag ">") nil t)
84 (delete-region (match-beginning 0) (match-end 0)))
87 ;; Fetch and insert a picture.
88 ((equal tag "img_alt")
89 (when (string-match "src=\"\\([^\"]+\\)" parameters)
90 (setq parameters (match-string 1 parameters))
91 (when (or (null mm-w3m-safe-url-regexp)
92 (string-match mm-w3m-safe-url-regexp parameters))
93 (let ((file (gnus-html-image-id parameters)))
94 (if (file-exists-p file)
95 ;; It's already cached, so just insert it.
96 (when (gnus-html-put-image file (point))
97 ;; Delete the ALT text.
98 (delete-region start end))
99 ;; We don't have it, so schedule it for fetching
101 (push (list parameters
102 (set-marker (make-marker) start)
107 (when (string-match "href=\"\\([^\"]+\\)" parameters)
108 (setq parameters (match-string 1 parameters))
109 (gnus-article-add-button start end
110 'browse-url parameters
112 (let ((overlay (gnus-make-overlay start end)))
113 (gnus-overlay-put overlay 'evaporate t)
114 (gnus-overlay-put overlay 'gnus-button-url parameters)
115 (when gnus-article-mouse-face
116 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
117 ;; Whatever. Just ignore the tag.
121 (goto-char (point-min))
122 ;; The output from -halfdump isn't totally regular, so strip
123 ;; off any </pre_int>s that were left over.
124 (while (re-search-forward "</pre_int>" nil t)
125 (replace-match "" t t))
127 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
129 (defun gnus-html-schedule-image-fetching (buffer images)
130 (let* ((url (caar images))
131 (process (start-process
136 "-o" (gnus-html-image-id url)
138 (process-kill-without-query process)
139 (set-process-sentinel process 'gnus-html-curl-sentinel)
140 (set-process-plist process (list 'images images
143 (defun gnus-html-image-id (url)
144 (expand-file-name (sha1 url) gnus-html-cache-directory))
146 (defun gnus-html-curl-sentinel (process event)
147 (when (string-match "finished" event)
148 (let* ((images (getf (process-plist process) 'images))
149 (buffer (getf (process-plist process) 'buffer))
151 (file (gnus-html-image-id (car spec))))
152 (when (and (buffer-live-p buffer)
153 ;; If the position of the marker is 1, then that
154 ;; means that the text is was in has been deleted;
155 ;; i.e., that the user has selected a different
156 ;; article before the image arrived.
157 (not (= (marker-position (cadr spec)) 1)))
160 (let ((buffer-read-only nil))
161 (when (gnus-html-put-image file (cadr spec))
162 (delete-region (1+ (cadr spec)) (caddr spec))))))
164 (gnus-html-schedule-image-fetching buffer images)))))
166 (defun gnus-html-put-image (file point)
167 (when (display-graphic-p)
168 (let ((image (ignore-errors
169 (gnus-create-image file))))
173 ;; Kludge to avoid displaying 30x30 gif images, which
174 ;; seems to be a signal of a broken image.
175 (not (and (listp image)
176 (eq (getf (cdr image) :type) 'gif)
177 (= (car (image-size image t)) 30)
178 (= (cdr (image-size image t)) 30))))
180 (gnus-put-image image)
182 (when (fboundp 'find-image)
183 (gnus-put-image (find-image
184 '((:type xpm :file "lock-broken.xpm")))))
187 (defun gnus-html-prune-cache ()
190 (dolist (file (directory-files gnus-html-cache-directory t nil t))
191 (let ((attributes (file-attributes file)))
192 (unless (nth 0 attributes)
193 (incf total-size (nth 7 attributes))
194 (push (list (time-to-seconds (nth 5 attributes))
195 (nth 7 attributes) file)
197 (when (> total-size gnus-html-cache-size)
198 (setq files (sort files (lambda (f1 f2)
199 (< (car f1) (car f2)))))
201 (when (> total-size gnus-html-cache-size)
202 (decf total-size (cadr file))
203 (delete-file (nth 2 file)))))))
206 (defun gnus-html-prefetch-images (summary)
207 (let (safe-url-regexp urls)
208 (when (buffer-live-p summary)
211 (setq safe-url-regexp mm-w3m-safe-url-regexp))
213 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
214 (let ((url (match-string 1)))
215 (when (or (null safe-url-regexp)
216 (string-match safe-url-regexp url))
217 (unless (file-exists-p (gnus-html-image-id url))
219 (push (gnus-html-image-id url) urls)
222 (apply 'start-process
228 (process-kill-without-query process))))))
232 ;;; gnus-html.el ends here