542d1401a80701455d4e648a41162fe5406338a2
[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 (require 'mm-url)
32
33 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
34   "Where Gnus will cache images it downloads from the web."
35   :group 'gnus-art
36   :type 'directory)
37
38 (defcustom gnus-html-cache-size 500000000
39   "The size of the Gnus image cache."
40   :group 'gnus-art
41   :type 'integer)
42
43 (defcustom gnus-html-frame-width 70
44   "What width to use when rendering HTML."
45   :group 'gnus-art
46   :type 'integer)
47
48 ;;;###autoload
49 (defun gnus-article-html (handle)
50   (let ((article-buffer (current-buffer)))
51     (save-restriction
52       (narrow-to-region (point) (point))
53       (save-excursion
54         (mm-with-part handle
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)
60                                  "w3m" 
61                                  nil article-buffer nil
62                                  "-halfdump"
63                                  "-no-cookie"
64                                  "-I" "UTF-8"
65                                  "-O" "UTF-8"
66                                  "-o" "ext_halfdump=1"
67                                  "-o" "pre_conv=1"
68                                  "-t" (format "%s" tab-width)
69                                  "-cols" (format "%s" gnus-html-frame-width)
70                                  "-o" "display_image=off"
71                                  "-T" "text/html"))))
72       (gnus-html-wash-tags))))
73
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)))
87       (setq end (point))
88       (cond
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
98                 ;; immediately.
99                 (let ((handle (mm-get-content-id
100                                (setq url (match-string 1 url))))
101                       image)
102                   (when handle
103                     (mm-with-part handle
104                       (setq image (gnus-create-image (buffer-string)
105                                                      nil t))))
106                   (when image
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
117                   ;; asynchronously.
118                   (push (list url
119                               (set-marker (make-marker) start)
120                               (point-marker))
121                         images)))))))
122        ;; Add a link.
123        ((equal tag "a")
124         (when (string-match "href=\"\\([^\"]+\\)" parameters)
125           (setq url (match-string 1 parameters))
126           (gnus-article-add-button start end
127                                    'browse-url url
128                                    url)
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.
135        (t
136         ))
137       (goto-char start))
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))
143     (when images
144       (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
145
146 (defun gnus-html-schedule-image-fetching (buffer images)
147   (let* ((url (caar images))
148          (process (start-process
149                    "images" nil "curl"
150                    "-s" "--create-dirs"
151                    "--location"
152                    "--max-time" "60"
153                    "-o" (gnus-html-image-id url)
154                    url)))
155     (process-kill-without-query process)
156     (set-process-sentinel process 'gnus-html-curl-sentinel)
157     (set-process-plist process (list 'images images
158                                      'buffer buffer))))
159
160 (defun gnus-html-image-id (url)
161   (expand-file-name (sha1 url) gnus-html-cache-directory))
162
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))
167            (spec (pop images))
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)))
175         (save-excursion
176           (set-buffer buffer)
177           (let ((buffer-read-only nil))
178             (when (gnus-html-put-image file (cadr spec))
179               (delete-region (1+ (cadr spec)) (caddr spec))))))
180       (when images
181         (gnus-html-schedule-image-fetching buffer images)))))
182
183 (defun gnus-html-put-image (file point)
184   (when (display-graphic-p)
185     (let ((image (ignore-errors
186                    (gnus-create-image file))))
187       (save-excursion
188         (goto-char point)
189         (if (and image
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))))
196             (progn
197               (gnus-put-image image)
198               t)
199           (when (fboundp 'find-image)
200             (gnus-put-image (find-image
201                              '((:type xpm :file "lock-broken.xpm")))))
202           nil)))))
203
204 (defun gnus-html-prune-cache ()
205   (let ((total-size 0)
206         files)
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)
213                 files))))
214     (when (> total-size gnus-html-cache-size)
215       (setq files (sort files (lambda (f1 f2)
216                                 (< (car f1) (car f2)))))
217       (dolist (file files)
218         (when (> total-size gnus-html-cache-size)
219           (decf total-size (cadr file))
220           (delete-file (nth 2 file)))))))
221
222 ;;;###autoload
223 (defun gnus-html-prefetch-images (summary)
224   (let (safe-url-regexp urls)
225     (when (buffer-live-p summary)
226       (save-excursion
227         (set-buffer summary)
228         (setq safe-url-regexp mm-w3m-safe-url-regexp))
229       (save-match-data
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))
235                 (push url urls)
236                 (push (gnus-html-image-id url) urls)
237                 (push "-o" urls)))))
238         (let ((process
239                (apply 'start-process 
240                       "images" nil "curl"
241                       "-s" "--create-dirs"
242                       "--location"
243                       "--max-time" "60"
244                       urls)))
245           (process-kill-without-query process))))))
246
247 (provide 'gnus-html)
248
249 ;;; gnus-html.el ends here