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."
41 (defcustom gnus-html-frame-width 70
42 "What width to use when rendering HTML."
47 (defun gnus-article-html (handle)
48 (let ((article-buffer (current-buffer)))
50 (narrow-to-region (point) (point))
52 (set-buffer (car handle))
53 (call-process-region (point-min) (point-max)
55 nil article-buffer nil
57 "-t" (format "%s" tab-width)
58 "-cols" (format "%s" gnus-html-frame-width)
59 "-o" "display_image=off"
61 (gnus-html-wash-tags))))
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 (when (plusp (length parameters))
72 (set-text-properties 0 (1- (length parameters)) nil parameters))
73 (delete-region start (point))
74 (when (search-forward (concat "</" tag ">") nil t)
75 (delete-region (match-beginning 0) (match-end 0)))
78 ;; Fetch and insert a picture.
79 ((equal tag "img_alt")
80 (when (string-match "src=\"\\([^\"]+\\)" parameters)
81 (setq parameters (match-string 1 parameters))
82 (when (or (null mm-w3m-safe-url-regexp)
83 (string-match mm-w3m-safe-url-regexp parameters))
84 (let ((file (gnus-html-image-id parameters)))
85 (if (file-exists-p file)
86 ;; It's already cached, so just insert it.
88 (put-image (create-image file) (point))
89 ;; Delete the ALT text.
90 (delete-region start end))
91 ;; We don't have it, so schedule it for fetching
93 (push (list parameters
94 (set-marker (make-marker) start)
99 (when (string-match "href=\"\\([^\"]+\\)" parameters)
100 (setq parameters (match-string 1 parameters))
101 (gnus-article-add-button start end
102 'browse-url parameters)
103 (let ((overlay (gnus-make-overlay start end)))
104 (gnus-overlay-put overlay 'evaporate t)
105 (gnus-overlay-put overlay 'gnus-button-url parameters)
106 (when gnus-article-mouse-face
107 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
108 ;; Whatever. Just ignore the tag.
112 (goto-char (point-min))
113 ;; The output from -halfdump isn't totally regular, so strip
114 ;; off any </pre_int>s that were left over.
115 (while (re-search-forward "</pre_int>" nil t)
116 (replace-match "" t t))
118 (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
120 (defun gnus-html-schedule-image-fetching (buffer images)
121 (let* ((url (caar images))
122 (process (start-process
125 "-o" (gnus-html-image-id url)
127 (set-process-sentinel process 'gnus-html-curl-sentinel)
128 (set-process-plist process (list 'images images
131 (defun gnus-html-image-id (url)
132 (expand-file-name (sha1 url) gnus-html-cache-directory))
134 (defun gnus-html-curl-sentinel (process event)
135 (when (string-match "finished" event)
136 (let* ((images (getf (process-plist process) 'images))
137 (buffer (getf (process-plist process) 'buffer))
139 (file (gnus-html-image-id (car spec))))
140 (when (file-exists-p file)
143 (let ((buffer-read-only nil))
144 (delete-region (cadr spec) (caddr spec))
145 (put-image (create-image file) (cadr spec)))))
147 (gnus-html-schedule-image-fetching buffer images)))))
149 (defun gnus-html-prune-cache ()
152 (dolist (file (directory-files gnus-html-cache-directory t nil t))
153 (let ((attributes (file-attributes file)))
154 (unless (nth 0 attributes)
155 (incf total-size (nth 7 attributes))
156 (push (list (time-to-seconds (nth 5 attributes))
157 (nth 7 attributes) file)
159 (when (> total-size gnus-html-cache-size)
160 (setq files (sort files (lambda (f1 f2)
161 (< (car f1) (car f2)))))
163 (when (> total-size gnus-html-cache-size)
164 (decf total-size (cadr file))
165 (delete-file (nth 2 file)))))))
167 (defun gnus-html-prefetch-images ()
170 (while (re-search-forward "<img.*src=\"\\([^\"]+\\)" nil t)
171 (let ((url (match-string 1)))
172 (when (or (null mm-w3m-safe-url-regexp)
173 (string-match mm-w3m-safe-url-regexp url))
174 (unless (file-exists-p (gnus-html-image-id url))
176 (push (gnus-html-image-id url) urls)
178 (apply 'start-process
183 ;;; gnus-html.el ends here