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