1 ;;; gnus-html.el --- Render HTML in a buffer.
3 ;; Copyright (C) 2010-2011 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 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'mm-decode))
41 (eval-and-compile (unless (featurep 'xemacs) (require 'help-fns)))
43 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
44 "Time used to determine if we should use images from the cache."
49 (defcustom gnus-html-image-automatic-caching t
50 "Whether automatically cache retrieve images."
55 (defcustom gnus-html-frame-width 70
56 "What width to use when rendering HTML."
61 (defcustom gnus-max-image-proportion 0.9
62 "How big pictures displayed are in relation to the window they're in.
63 A value of 0.7 means that they are allowed to take up 70% of the
64 width and height of the window. If they are larger than this,
65 and Emacs supports it, then the images will be rescaled down to
71 (defvar gnus-html-image-map
72 (let ((map (make-sparse-keymap)))
73 (define-key map "u" 'gnus-article-copy-string)
74 (define-key map "i" 'gnus-html-insert-image)
75 (define-key map "v" 'gnus-html-browse-url)
78 (defvar gnus-html-displayed-image-map
79 (let ((map (make-sparse-keymap)))
80 (define-key map "a" 'gnus-html-show-alt-text)
81 (define-key map "i" 'gnus-html-browse-image)
82 (define-key map "\r" 'gnus-html-browse-url)
83 (define-key map "u" 'gnus-article-copy-string)
84 (define-key map [tab] 'widget-forward)
88 (defalias 'gnus-html-encode-url-chars
89 (if (fboundp 'browse-url-url-encode-chars)
90 'browse-url-url-encode-chars
92 "URL-encode the chars in TEXT that match CHARS.
93 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
94 (let ((encoded-text (copy-sequence text))
96 (while (setq s (string-match chars encoded-text s))
98 (replace-match (format "%%%x"
100 (match-string 0 encoded-text)))
105 (defun gnus-html-encode-url (url)
107 (gnus-html-encode-url-chars url "[)$ ]"))
109 (defun gnus-html-cache-expired (url ttl)
110 "Check if URL is cached for more than TTL."
111 (cond (url-standalone-mode
112 (not (file-exists-p (url-cache-create-filename url))))
113 (t (let ((cache-time (url-is-cached url)))
123 (defun gnus-article-html (&optional handle)
124 (let ((article-buffer (current-buffer)))
126 (setq handle (mm-dissect-buffer t)))
128 (narrow-to-region (point) (point))
131 (let* ((coding-system-for-read 'utf-8)
132 (coding-system-for-write 'utf-8)
133 (default-process-coding-system
134 (cons coding-system-for-read coding-system-for-write))
135 (charset (mail-content-type-get (mm-handle-type handle)
138 (setq charset (mm-charset-to-coding-system charset))
139 (not (eq charset 'ascii)))
141 (mm-decode-coding-string (buffer-string) charset)
143 (mm-enable-multibyte))))
144 (call-process-region (point-min) (point-max)
146 nil article-buffer nil
151 "-o" "ext_halfdump=1"
152 "-o" "display_ins_del=2"
154 "-t" (format "%s" tab-width)
155 "-cols" (format "%s" gnus-html-frame-width)
156 "-o" "display_image=on"
158 (gnus-html-wash-tags))))
160 (defvar gnus-article-mouse-face)
162 (defun gnus-html-pre-wash ()
163 (goto-char (point-min))
164 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
165 (replace-match "" t t))
166 (goto-char (point-min))
167 (while (re-search-forward "<a name[^\n>]+>" nil t)
168 (replace-match "" t t)))
170 (defun gnus-html-wash-images ()
171 "Run through current buffer and replace img tags by images."
172 (let (tag parameters string start end images url alt-text
173 inhibit-images blocked-images)
174 (if (buffer-live-p gnus-summary-buffer)
175 (with-current-buffer gnus-summary-buffer
176 (setq inhibit-images gnus-inhibit-images
177 blocked-images (gnus-blocked-images)))
178 (setq inhibit-images gnus-inhibit-images
179 blocked-images (gnus-blocked-images)))
180 (goto-char (point-min))
181 ;; Search for all the images first.
182 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
183 (setq parameters (match-string 1)
184 start (match-beginning 0))
185 (delete-region start (point))
186 (when (search-forward "</img_alt>" (line-end-position) t)
187 (delete-region (match-beginning 0) (match-end 0)))
189 (when (string-match "src=\"\\([^\"]+\\)" parameters)
190 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
191 (setq url (gnus-html-encode-url (match-string 1 parameters))
192 alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
194 (xml-substitute-special (match-string 2 parameters))))
195 (gnus-add-text-properties
198 'image-displayer `(lambda (url start end)
199 (gnus-html-display-image url start end
201 'gnus-image (list url start end alt-text)))
202 (widget-convert-button
203 'url-link start (point)
205 :keymap gnus-html-image-map
207 (if (string-match "\\`cid:" url)
208 ;; URLs with cid: have their content stashed in other
209 ;; parts of the MIME structure, so just insert them
211 (let* ((handle (mm-get-content-id (substring url (match-end 0))))
212 (image (when (and handle
213 (not inhibit-images))
215 (mm-with-part handle (buffer-string))
222 image (gnus-html-maximum-image-size))
223 (gnus-string-or (prog1
224 (buffer-substring start end)
225 (delete-region start end))
228 (widget-convert-button
230 :action 'gnus-html-insert-image
232 :keymap gnus-html-image-map
233 :button-keymap gnus-html-image-map)))
234 ;; Normal, external URL.
235 (if (or inhibit-images
236 (gnus-html-image-url-blocked-p url blocked-images))
237 (widget-convert-button
239 :action 'gnus-html-insert-image
241 :keymap gnus-html-image-map
242 :button-keymap gnus-html-image-map)
245 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
246 (string-to-number (match-string 1 parameters))))
248 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
249 (string-to-number (match-string 1 parameters)))))
250 ;; Don't fetch images that are really small. They're
251 ;; probably tracking pictures.
252 (when (and (or (null height)
256 (gnus-html-display-image url start end alt-text)))))))))
258 (defun gnus-html-display-image (url start end &optional alt-text)
259 "Display image at URL on text from START to END.
260 Use ALT-TEXT for the image string."
261 (or alt-text (setq alt-text "*"))
262 (if (string-match "\\`cid:" url)
263 (let ((handle (mm-get-content-id (substring url (match-end 0)))))
265 (gnus-html-put-image (mm-with-part handle (buffer-string))
267 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
268 ;; We don't have it, so schedule it for fetching
270 (gnus-html-schedule-image-fetching
273 ;; It's already cached, so just insert it.
274 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
276 (defun gnus-html-wash-tags ()
277 (let (tag parameters string start end images url)
279 (gnus-html-wash-images)
281 (goto-char (point-min))
282 ;; Then do the other tags.
283 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
284 (setq tag (match-string 1)
285 parameters (match-string 2)
286 start (match-beginning 0))
287 (when (> (length parameters) 0)
288 (set-text-properties 0 (1- (length parameters)) nil parameters))
289 (delete-region start (point))
290 (when (search-forward (concat "</" tag ">") nil t)
291 (delete-region (match-beginning 0) (match-end 0)))
294 ;; Fetch and insert a picture.
295 ((equal tag "img_alt"))
299 (when (string-match "href=\"\\([^\"]+\\)" parameters)
300 (setq url (match-string 1 parameters))
301 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
302 (gnus-article-add-button start end
303 'browse-url (mm-url-decode-entities-string url)
305 (let ((overlay (gnus-make-overlay start end)))
306 (gnus-overlay-put overlay 'evaporate t)
307 (gnus-overlay-put overlay 'gnus-button-url url)
308 (gnus-put-text-property start end 'gnus-string url)
309 (when gnus-article-mouse-face
310 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
311 ;; The upper-case IMG_ALT is apparently just an artifact that
312 ;; should be deleted.
313 ((equal tag "IMG_ALT")
314 (delete-region start end))
315 ;; w3m does not normalize the case
318 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
321 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))