Don't bug out on non-Emacs images.
[gnus] / lisp / 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 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
32   "Where Gnus will cache images it downloads from the web."
33   :group 'gnus-art
34   :type 'directory)
35
36 (defcustom gnus-html-cache-size 500000000
37   "The size of the Gnus image cache."
38   :group 'gnus-art
39   :type 'integer)
40
41 (defcustom gnus-html-frame-width 70
42   "What width to use when rendering HTML."
43   :group 'gnus-art
44   :type 'integer)
45
46 ;;;###autoload
47 (defun gnus-article-html (handle)
48   (let ((article-buffer (current-buffer)))
49     (save-restriction
50       (narrow-to-region (point) (point))
51       (save-excursion
52         (mm-with-part handle
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)
58                                  "w3m" 
59                                  nil article-buffer nil
60                                  "-halfdump"
61                                  "-no-cookie"
62                                  "-O" "UTF-8"
63                                  "-o" "ext_halfdump=1"
64                                  "-t" (format "%s" tab-width)
65                                  "-cols" (format "%s" gnus-html-frame-width)
66                                  "-o" "display_image=off"
67                                  "-T" "text/html"))))
68       (gnus-html-wash-tags))))
69
70 (defun gnus-html-wash-tags ()
71   (let (tag parameters string start end images)
72     (mm-url-decode-entities)
73     (goto-char (point-min))
74     (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
75       (setq tag (match-string 1)
76             parameters (match-string 2)
77             start (match-beginning 0))
78       (when (plusp (length parameters))
79         (set-text-properties 0 (1- (length parameters)) nil parameters))
80       (delete-region start (point))
81       (when (search-forward (concat "</" tag ">") nil t)
82         (delete-region (match-beginning 0) (match-end 0)))
83       (setq end (point))
84       (cond
85        ;; Fetch and insert a picture.
86        ((equal tag "img_alt")
87         (when (string-match "src=\"\\([^\"]+\\)" parameters)
88           (setq parameters (match-string 1 parameters))
89           (when (or (null mm-w3m-safe-url-regexp)
90                     (string-match mm-w3m-safe-url-regexp parameters))
91             (let ((file (gnus-html-image-id parameters)))
92               (if (file-exists-p file)
93                   ;; It's already cached, so just insert it.
94                   (when (gnus-html-put-image file (point))
95                     ;; Delete the ALT text.
96                     (delete-region start end))
97                 ;; We don't have it, so schedule it for fetching
98                 ;; asynchronously.
99                 (push (list parameters
100                             (set-marker (make-marker) start)
101                             (point-marker))
102                       images))))))
103        ;; Add a link.
104        ((equal tag "a")
105         (when (string-match "href=\"\\([^\"]+\\)" parameters)
106           (setq parameters (match-string 1 parameters))
107           (gnus-article-add-button start end
108                                    'browse-url parameters
109                                    parameters)
110           (let ((overlay (gnus-make-overlay start end)))
111             (gnus-overlay-put overlay 'evaporate t)
112             (gnus-overlay-put overlay 'gnus-button-url parameters)
113             (when gnus-article-mouse-face
114               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
115        ;; Whatever.  Just ignore the tag.
116        (t
117         ))
118       (goto-char start))
119     (goto-char (point-min))
120     ;; The output from -halfdump isn't totally regular, so strip
121     ;; off any </pre_int>s that were left over.
122     (while (re-search-forward "</pre_int>" nil t)
123       (replace-match "" t t))
124     (when images
125       (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
126
127 (defun gnus-html-schedule-image-fetching (buffer images)
128   (let* ((url (caar images))
129          (process (start-process
130                    "images" nil "curl"
131                    "-s" "--create-dirs"
132                    "--location"
133                    "--max-time" "60"
134                    "-o" (gnus-html-image-id url)
135                    url)))
136     (process-kill-without-query process)
137     (set-process-sentinel process 'gnus-html-curl-sentinel)
138     (set-process-plist process (list 'images images
139                                      'buffer buffer))))
140
141 (defun gnus-html-image-id (url)
142   (expand-file-name (sha1 url) gnus-html-cache-directory))
143
144 (defun gnus-html-curl-sentinel (process event)
145   (when (string-match "finished" event)
146     (let* ((images (getf (process-plist process) 'images))
147            (buffer (getf (process-plist process) 'buffer))
148            (spec (pop images))
149            (file (gnus-html-image-id (car spec))))
150       (when (and (buffer-live-p buffer)
151                  ;; If the position of the marker is 1, then that
152                  ;; means that the text is was in has been deleted;
153                  ;; i.e., that the user has selected a different
154                  ;; article before the image arrived.
155                  (not (= (marker-position (cadr spec)) 1)))
156         (save-excursion
157           (set-buffer buffer)
158           (let ((buffer-read-only nil))
159             (when (gnus-html-put-image file (cadr spec))
160               (delete-region (1+ (cadr spec)) (caddr spec))))))
161       (when images
162         (gnus-html-schedule-image-fetching buffer images)))))
163
164 (defun gnus-html-put-image (file point)
165   (when (display-graphic-p)
166     (let ((image (ignore-errors
167                    (gnus-create-image file))))
168       (save-excursion
169         (goto-char point)
170         (if (and image
171                  ;; Kludge to avoid displaying 30x30 gif images, which
172                  ;; seems to be a signal of a broken image.
173                  (not (and (listp image)
174                            (eq (getf (cdr image) :type) 'gif)
175                            (= (car (image-size image t)) 30)
176                            (= (cdr (image-size image t)) 30))))
177             (progn
178               (gnus-put-image image)
179               t)
180           (when (fboundp 'find-image)
181             (gnus-put-image (find-image
182                              '((:type xpm :file "lock-broken.xpm")))))
183           nil)))))
184
185 (defun gnus-html-prune-cache ()
186   (let ((total-size 0)
187         files)
188     (dolist (file (directory-files gnus-html-cache-directory t nil t))
189       (let ((attributes (file-attributes file)))
190         (unless (nth 0 attributes)
191           (incf total-size (nth 7 attributes))
192           (push (list (time-to-seconds (nth 5 attributes))
193                       (nth 7 attributes) file)
194                 files))))
195     (when (> total-size gnus-html-cache-size)
196       (setq files (sort files (lambda (f1 f2)
197                                 (< (car f1) (car f2)))))
198       (dolist (file files)
199         (when (> total-size gnus-html-cache-size)
200           (decf total-size (cadr file))
201           (delete-file (nth 2 file)))))))
202
203 ;;;###autoload
204 (defun gnus-html-prefetch-images (summary)
205   (let (safe-url-regexp urls)
206     (when (buffer-live-p summary)
207       (save-excursion
208         (set-buffer summary)
209         (setq safe-url-regexp mm-w3m-safe-url-regexp))
210       (save-match-data
211         (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
212           (let ((url (match-string 1)))
213             (when (or (null safe-url-regexp)
214                       (string-match safe-url-regexp url))
215               (unless (file-exists-p (gnus-html-image-id url))
216                 (push url urls)
217                 (push (gnus-html-image-id url) urls)
218                 (push "-o" urls)))))
219         (let ((process
220                (apply 'start-process 
221                       "images" nil "curl"
222                       "-s" "--create-dirs"
223                       "--location"
224                       "--max-time" "60"
225                       urls)))
226           (process-kill-without-query process))))))
227
228 (provide 'gnus-html)
229
230 ;;; gnus-html.el ends here