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."
42 (defun gnus-article-html (handle)
43 (let ((article-buffer (current-buffer)))
45 (narrow-to-region (1- (point)) (point))
47 (set-buffer (car handle))
48 (call-process-region (point-min) (point-max)
50 nil article-buffer nil
53 (gnus-html-wash-tags))))
55 (defun gnus-html-wash-tags ()
56 (let (tag parameters string start end images)
57 ;;(subst-char-in-region (point-min) (point-max) ?_ ? )
58 (goto-char (point-min))
59 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
60 (setq tag (match-string 1)
61 parameters (match-string 2)
62 start (match-beginning 0))
63 (delete-region start (point))
64 (when (search-forward (concat "</" tag ">"))
65 (delete-region (match-beginning 0) (match-end 0)))
68 ;; Fetch and insert a picture.
69 ((equal tag "img_alt")
70 (when (string-match "src=\"\\([^\"]+\\)" parameters)
71 (setq parameters (match-string 1 parameters))
72 (when (or (null mm-w3m-safe-url-regexp)
73 (string-match mm-w3m-safe-url-regexp parameters))
74 (let ((file (gnus-html-image-id parameters)))
75 (if (file-exists-p file)
76 ;; It's already cached, so just insert it.
78 (put-image (create-image file) (point))
79 ;; Delete the ALT text.
80 (delete-region start end))
81 ;; We don't have it, so schedule it for fetching
83 (push (list parameters
84 (set-marker (make-marker) start)
89 (when (string-match "href=\"\\([^\"]+\\)" parameters)
90 (setq parameters (match-string 1 parameters))
91 (gnus-article-add-button start end
92 'browse-url parameters)
93 (let ((overlay (gnus-make-overlay start end)))
94 (gnus-overlay-put overlay 'evaporate t)
95 (gnus-overlay-put overlay 'gnus-button-url parameters)
96 (when gnus-article-mouse-face
97 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
98 ;; Whatever. Just ignore the tag.
103 (gnus-html-schedule-image-fetching (current-buffer) images))))
105 (defun gnus-html-schedule-image-fetching (buffer images)
106 (let* ((url (caar images))
107 (process (start-process
110 "-o" (gnus-html-image-id url)
112 (set-process-sentinel process 'gnus-html-curl-sentinel)
113 (set-process-plist process (list 'images images
116 (defun gnus-html-image-id (url)
117 (expand-file-name (sha1 url) gnus-html-cache-directory))
119 (defun gnus-html-curl-sentinel (process event)
120 (when (string-match "finished" event)
121 (let* ((images (getf (process-plist process) 'images))
122 (buffer (getf (process-plist process) 'buffer))
124 (file (gnus-html-image-id (car spec))))
125 (when (file-exists-p file)
128 (let ((buffer-read-only nil))
129 (delete-region (cadr spec) (caddr spec))
130 (put-image (create-image file) (cadr spec)))))
132 (gnus-html-schedule-image-fetching buffer images)))))
134 (defun gnus-html-prune-cache ()
137 (dolist (file (directory-files gnus-html-cache-directory t nil t))
138 (let ((attributes (file-attributes file)))
139 (unless (nth 0 attributes)
140 (incf total-size (nth 7 attributes))
141 (push (list (time-to-seconds (nth 5 attributes))
142 (nth 7 attributes) file)
144 (when (> total-size gnus-html-cache-size)
145 (setq files (sort files (lambda (f1 f2)
146 (< (car f1) (car f2)))))
148 (when (> total-size gnus-html-cache-size)
149 (decf total-size (cadr file))
150 (delete-file (nth 2 file)))))))
152 ;;; gnus-html.el ends here