1 ;;; gnus-html.el --- Render HTML in a buffer.
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 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'mm-decode))
41 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
42 "Time in seconds used to cache the image on disk."
47 (defcustom gnus-html-frame-width 70
48 "What width to use when rendering HTML."
53 (defcustom gnus-blocked-images "."
54 "Images that have URLs matching this regexp will be blocked."
59 (defcustom gnus-max-image-proportion 0.7
60 "How big pictures displayed are in relation to the window they're in.
61 A value of 0.7 means that they are allowed to take up 70% of the
62 width and height of the window. If they are larger than this,
63 and Emacs supports it, then the images will be rescaled down to
69 (defvar gnus-html-image-map
70 (let ((map (make-sparse-keymap)))
71 (define-key map "u" 'gnus-article-copy-string)
72 (define-key map "i" 'gnus-html-insert-image)
73 (define-key map "v" 'gnus-html-browse-url)
76 (defvar gnus-html-displayed-image-map
77 (let ((map (make-sparse-keymap)))
78 (define-key map "a" 'gnus-html-show-alt-text)
79 (define-key map "i" 'gnus-html-browse-image)
80 (define-key map "\r" 'gnus-html-browse-url)
81 (define-key map "u" 'gnus-article-copy-string)
82 (define-key map [tab] 'widget-forward)
85 (defun gnus-html-encode-url (url)
87 (browse-url-url-encode-chars url "[)$ ]"))
89 (defun gnus-html-cache-expired (url ttl)
90 "Check if URL is cached for more than TTL."
91 (cond (url-standalone-mode
92 (not (file-exists-p (url-cache-create-filename url))))
93 (t (let ((cache-time (url-is-cached url)))
103 (defun gnus-article-html (&optional handle)
104 (let ((article-buffer (current-buffer)))
106 (setq handle (mm-dissect-buffer t)))
108 (narrow-to-region (point) (point))
111 (let* ((coding-system-for-read 'utf-8)
112 (coding-system-for-write 'utf-8)
113 (default-process-coding-system
114 (cons coding-system-for-read coding-system-for-write))
115 (charset (mail-content-type-get (mm-handle-type handle)
118 (setq charset (mm-charset-to-coding-system charset))
119 (not (eq charset 'ascii)))
121 (mm-decode-coding-string (buffer-string) charset)
123 (mm-enable-multibyte))))
124 (call-process-region (point-min) (point-max)
126 nil article-buffer nil
131 "-o" "ext_halfdump=1"
132 "-o" "display_ins_del=2"
134 "-t" (format "%s" tab-width)
135 "-cols" (format "%s" gnus-html-frame-width)
136 "-o" "display_image=on"
138 (gnus-html-wash-tags))))
140 (defvar gnus-article-mouse-face)
142 (defun gnus-html-pre-wash ()
143 (goto-char (point-min))
144 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
145 (replace-match "" t t))
146 (goto-char (point-min))
147 (while (re-search-forward "<a name[^\n>]+>" nil t)
148 (replace-match "" t t)))
150 (defun gnus-html-wash-images ()
151 "Run through current buffer and replace img tags by images."
152 (let (tag parameters string start end images url)
153 (goto-char (point-min))
154 ;; Search for all the images first.
155 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
156 (setq parameters (match-string 1)
157 start (match-beginning 0))
158 (delete-region start (point))
159 (when (search-forward "</img_alt>" (line-end-position) t)
160 (delete-region (match-beginning 0) (match-end 0)))
162 (when (string-match "src=\"\\([^\"]+\\)" parameters)
163 (setq url (gnus-html-encode-url (match-string 1 parameters)))
164 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
165 (if (string-match "^cid:\\(.*\\)" url)
166 ;; URLs with cid: have their content stashed in other
167 ;; parts of the MIME structure, so just insert them
169 (let ((handle (mm-get-content-id
170 (setq url (match-string 1 url))))
174 (setq image (gnus-create-image (buffer-string)
177 (let ((string (buffer-substring start end)))
178 (delete-region start end)
179 (gnus-put-image image (gnus-string-or string "*") 'cid)
180 (gnus-add-image 'cid image))))
181 ;; Normal, external URL.
182 (let ((alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
184 (xml-substitute-special (match-string 2 parameters)))))
185 (gnus-put-text-property start end 'gnus-image-url url)
186 (if (gnus-html-image-url-blocked-p
188 (if (buffer-live-p gnus-summary-buffer)
189 (with-current-buffer gnus-summary-buffer
191 gnus-blocked-images))
193 (widget-convert-button
195 :action 'gnus-html-insert-image
197 :keymap gnus-html-image-map
198 :button-keymap gnus-html-image-map)
199 (let ((overlay (gnus-make-overlay start end))
200 (spec (list url alt-text)))
201 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
202 (gnus-overlay-put overlay 'gnus-image spec)
203 (gnus-put-text-property
208 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
209 (string-to-number (match-string 1 parameters))))
211 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
212 (string-to-number (match-string 1 parameters)))))
213 ;; Don't fetch images that are really small. They're
214 ;; probably tracking pictures.
215 (when (and (or (null height)
219 (gnus-html-display-image url start end alt-text))))))))))
221 (defun gnus-html-display-image (url start end alt-text)
222 "Display image at URL on text from START to END.
223 Use ALT-TEXT for the image string."
224 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
225 ;; We don't have it, so schedule it for fetching
227 (gnus-html-schedule-image-fetching
230 ;; It's already cached, so just insert it.
231 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text)))
233 (defun gnus-html-wash-tags ()
234 (let (tag parameters string start end images url)
236 (gnus-html-wash-images)
238 (goto-char (point-min))
239 ;; Then do the other tags.
240 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
241 (setq tag (match-string 1)
242 parameters (match-string 2)
243 start (match-beginning 0))
244 (when (plusp (length parameters))
245 (set-text-properties 0 (1- (length parameters)) nil parameters))
246 (delete-region start (point))
247 (when (search-forward (concat "</" tag ">") nil t)
248 (delete-region (match-beginning 0) (match-end 0)))
251 ;; Fetch and insert a picture.
252 ((equal tag "img_alt"))
256 (when (string-match "href=\"\\([^\"]+\\)" parameters)
257 (setq url (match-string 1 parameters))
258 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
259 (gnus-article-add-button start end
262 (let ((overlay (gnus-make-overlay start end)))
263 (gnus-overlay-put overlay 'evaporate t)
264 (gnus-overlay-put overlay 'gnus-button-url url)
265 (gnus-put-text-property start end 'gnus-string url)
266 (when gnus-article-mouse-face
267 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
268 ;; The upper-case IMG_ALT is apparently just an artifact that
269 ;; should be deleted.
270 ((equal tag "IMG_ALT")
271 (delete-region start end))
272 ;; w3m does not normalize the case
275 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
278 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
281 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
284 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
285 ((or (equal tag "ins")
287 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
288 ;; Handle different UL types
289 ((equal tag "_SYMBOL")
290 (when (string-match "TYPE=\\(.+\\)" parameters)
291 (let ((type (string-to-number (match-string 1 parameters))))
292 (delete-region start end)
293 (cond ((= type 33) (insert " "))
294 ((= type 34) (insert " "))
295 ((= type 35) (insert " "))
296 ((= type 36) (insert " "))
297 ((= type 37) (insert " "))
298 ((= type 38) (insert " "))
299 ((= type 39) (insert " "))
300 ((= type 40) (insert " "))
301 ((= type 42) (insert " "))
302 ((= type 43) (insert " "))
304 ;; Whatever. Just ignore the tag.
308 (goto-char (point-min))
309 ;; The output from -halfdump isn't totally regular, so strip
310 ;; off any </pre_int>s that were left over.
311 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
312 (replace-match "" t t))
313 (mm-url-decode-entities)))
315 (defun gnus-html-insert-image ()
316 "Fetch and insert the image under point."
318 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
320 (defun gnus-html-show-alt-text ()
321 "Show the ALT text of the image under point."
323 (message "%s" (get-text-property (point) 'gnus-alt-text)))
325 (defun gnus-html-browse-image ()
326 "Browse the image under point."
328 (browse-url (get-text-property (point) 'gnus-image-url)))
330 (defun gnus-html-browse-url ()
331 "Browse the image under point."
333 (let ((url (get-text-property (point) 'gnus-string)))
335 (message "No URL at point")
338 (defun gnus-html-schedule-image-fetching (buffer image)
339 "Retrieve IMAGE, and place it into BUFFER on arrival."
340 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
343 (url-retrieve (car image)
344 'gnus-html-image-fetched
345 (list buffer image))))
347 (defun gnus-html-image-fetched (status buffer image)
348 (url-store-in-cache (current-buffer))
349 (when (and (or (search-forward "\n\n" nil t)
350 (search-forward "\r\n\r\n" nil t))
351 (buffer-live-p buffer))
352 (let ((data (buffer-substring (point) (point-max))))
353 (with-current-buffer buffer
354 (let ((inhibit-read-only t))
355 (gnus-html-put-image data (car image) (cadr image))))))
356 (kill-buffer (current-buffer)))
358 (defun gnus-html-get-image-data (url)
359 "Get image data for URL.
360 Return a string with image data."
362 (mm-disable-multibyte)
363 (url-cache-extract (url-cache-create-filename url))
364 (when (or (search-forward "\n\n" nil t)
365 (search-forward "\r\n\r\n" nil t))
366 (buffer-substring (point) (point-max)))))
368 (defun gnus-html-put-image (data url &optional alt-text)
369 (when (gnus-graphic-display-p)
370 (let* ((start (text-property-any (point-min) (point-max) 'gnus-image-url url))
372 (next-single-property-change start 'gnus-image-url))))
377 (gnus-create-image data nil t)))
379 (if (featurep 'xemacs)
380 (cons (glyph-width image) (glyph-height image))
381 (image-size image t)))))
384 (let ((alt-text (or alt-text (buffer-substring-no-properties start end))))
386 ;; Kludge to avoid displaying 30x30 gif images, which
387 ;; seems to be a signal of a broken image.
388 (not (and (if (featurep 'xemacs)
391 (eq (if (featurep 'xemacs)
392 (let ((d (cdadar (specifier-spec-list
393 (glyph-image image)))))
396 (plist-get (cdr image) :type))
400 ;; Good image, add it!
401 (let ((image (gnus-html-rescale-image image data size)))
402 (delete-region start end)
403 (gnus-put-image image alt-text 'external)
404 (gnus-put-text-property start (point) 'help-echo alt-text)
405 (gnus-overlay-put (gnus-make-overlay start (point)) 'local-map
406 gnus-html-displayed-image-map)
407 (gnus-put-text-property start (point) 'gnus-alt-text alt-text)
409 (gnus-put-text-property start (point) 'gnus-image-url url))
410 (gnus-add-image 'external image)
412 ;; Bad image, try to show something else
413 (when (fboundp 'find-image)
414 (delete-region start end)
415 (setq image (find-image '((:type xpm :file "lock-broken.xpm"))))
416 (gnus-put-image image alt-text 'internal)
417 (gnus-add-image 'internal image))
420 (defun gnus-html-rescale-image (image data size)
421 (if (or (not (fboundp 'imagemagick-types))
422 (not (get-buffer-window (current-buffer))))
424 (let* ((width (car size))
426 (edges (window-inside-pixel-edges (get-buffer-window (current-buffer))))
427 (window-width (truncate (* gnus-max-image-proportion
428 (- (nth 2 edges) (nth 0 edges)))))
429 (window-height (truncate (* gnus-max-image-proportion
430 (- (nth 3 edges) (nth 1 edges)))))
432 (when (> height window-height)
433 (setq image (or (create-image data 'imagemagick t
434 :height window-height)
436 (setq size (image-size image t)))
437 (when (> (car size) window-width)
439 (create-image data 'imagemagick t
444 (defun gnus-html-image-url-blocked-p (url blocked-images)
445 "Find out if URL is blocked by BLOCKED-IMAGES."
446 (let ((ret (and blocked-images
447 (string-match blocked-images url))))
449 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
451 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
455 (defun gnus-html-show-images ()
456 "Show any images that are in the HTML-rendered article buffer.
457 This only works if the article in question is HTML."
459 (gnus-with-article-buffer
460 (dolist (overlay (overlays-in (point-min) (point-max)))
461 (let ((o (overlay-get overlay 'gnus-image)))
463 (apply 'gnus-html-display-image o))))))
466 (defun gnus-html-prefetch-images (summary)
467 (when (buffer-live-p summary)
468 (let ((blocked-images (with-current-buffer summary
469 gnus-blocked-images)))
471 (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
472 (let ((url (gnus-html-encode-url (match-string 1))))
473 (unless (gnus-html-image-url-blocked-p url blocked-images)
474 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
475 (gnus-html-schedule-image-fetching nil
480 ;;; gnus-html.el ends here