Don't delete the space at the start, because this messes up the display.
[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         (set-buffer (car handle))
53         (call-process-region (point-min) (point-max)
54                              "w3m" 
55                              nil article-buffer nil
56                              "-halfdump"
57                              "-t" (format "%s" tab-width)
58                              "-cols" (format "%s" gnus-html-frame-width)
59                              "-o" "display_image=off"
60                              "-T" "text/html"))
61       (gnus-html-wash-tags))))
62
63 (defun gnus-html-wash-tags ()
64   (let (tag parameters string start end images)
65     (mm-url-decode-entities)
66     (goto-char (point-min))
67     (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
68       (setq tag (match-string 1)
69             parameters (match-string 2)
70             start (match-beginning 0))
71       (delete-region start (point))
72       (when (search-forward (concat "</" tag ">") nil t)
73         (delete-region (match-beginning 0) (match-end 0)))
74       (setq end (point))
75       (cond
76        ;; Fetch and insert a picture.
77        ((equal tag "img_alt")
78         (when (string-match "src=\"\\([^\"]+\\)" parameters)
79           (setq parameters (match-string 1 parameters))
80           (when (or (null mm-w3m-safe-url-regexp)
81                     (string-match mm-w3m-safe-url-regexp parameters))
82             (let ((file (gnus-html-image-id parameters)))
83               (if (file-exists-p file)
84                   ;; It's already cached, so just insert it.
85                   (progn
86                     (put-image (create-image file) (point))
87                     ;; Delete the ALT text.
88                     (delete-region start end))
89                 ;; We don't have it, so schedule it for fetching
90                 ;; asynchronously.
91                 (push (list parameters
92                             (set-marker (make-marker) start)
93                             (point-marker))
94                       images))))))
95        ;; Add a link.
96        ((equal tag "a")
97         (when (string-match "href=\"\\([^\"]+\\)" parameters)
98           (setq parameters (match-string 1 parameters))
99           (gnus-article-add-button start end
100                                    'browse-url parameters)
101           (let ((overlay (gnus-make-overlay start end)))
102             (gnus-overlay-put overlay 'evaporate t)
103             (gnus-overlay-put overlay 'gnus-button-url parameters)
104             (when gnus-article-mouse-face
105               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
106        ;; Whatever.  Just ignore the tag.
107        (t
108         ))
109       (goto-char start))
110     (when images
111       (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
112
113 (defun gnus-html-schedule-image-fetching (buffer images)
114   (let* ((url (caar images))
115          (process (start-process
116                    "images" nil "curl"
117                    "-s" "--create-dirs"
118                    "-o" (gnus-html-image-id url)
119                    url)))
120     (set-process-sentinel process 'gnus-html-curl-sentinel)
121     (set-process-plist process (list 'images images
122                                      'buffer buffer))))
123
124 (defun gnus-html-image-id (url)
125   (expand-file-name (sha1 url) gnus-html-cache-directory))
126
127 (defun gnus-html-curl-sentinel (process event)
128   (when (string-match "finished" event)
129     (let* ((images (getf (process-plist process) 'images))
130            (buffer (getf (process-plist process) 'buffer))
131            (spec (pop images))
132            (file (gnus-html-image-id (car spec))))
133       (when (file-exists-p file)
134         (save-excursion
135           (set-buffer buffer)
136           (let ((buffer-read-only nil))
137             (delete-region (cadr spec) (caddr spec))
138             (put-image (create-image file) (cadr spec)))))
139       (when images
140         (gnus-html-schedule-image-fetching buffer images)))))
141
142 (defun gnus-html-prune-cache ()
143   (let ((total-size 0)
144         files)
145     (dolist (file (directory-files gnus-html-cache-directory t nil t))
146       (let ((attributes (file-attributes file)))
147         (unless (nth 0 attributes)
148           (incf total-size (nth 7 attributes))
149           (push (list (time-to-seconds (nth 5 attributes))
150                       (nth 7 attributes) file)
151                 files))))
152     (when (> total-size gnus-html-cache-size)
153       (setq files (sort files (lambda (f1 f2)
154                                 (< (car f1) (car f2)))))
155       (dolist (file files)
156         (when (> total-size gnus-html-cache-size)
157           (decf total-size (cadr file))
158           (delete-file (nth 2 file)))))))
159
160 (defun gnus-html-prefetch-images ()
161   (save-match-data
162     (let (urls)
163       (while (re-search-forward "<img.*src=\"\\([^\"]+\\)" nil t)
164         (let ((url (match-string 1)))
165           (when (or (null mm-w3m-safe-url-regexp)
166                     (string-match mm-w3m-safe-url-regexp url))
167             (unless (file-exists-p (gnus-html-image-id url))
168               (push url urls)
169               (push (gnus-html-image-id url) urls)
170               (push "-o" urls)))))
171       (apply 'start-process 
172              "images" nil "curl"
173              "-s" "--create-dirs"
174              urls))))
175
176 ;;; gnus-html.el ends here