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))
40 (eval-and-compile (unless (featurep 'xemacs) (require 'help-fns)))
42 (defcustom gnus-html-image-cache-ttl (days-to-time 7)
43 "Time used to determine if we should use images from the cache."
48 (defcustom gnus-html-image-automatic-caching t
49 "Whether automatically cache retrieve images."
54 (defcustom gnus-html-frame-width 70
55 "What width to use when rendering HTML."
60 (defcustom gnus-blocked-images "."
61 "Images that have URLs matching this regexp will be blocked."
66 (defcustom gnus-max-image-proportion 0.9
67 "How big pictures displayed are in relation to the window they're in.
68 A value of 0.7 means that they are allowed to take up 70% of the
69 width and height of the window. If they are larger than this,
70 and Emacs supports it, then the images will be rescaled down to
76 (defvar gnus-html-image-map
77 (let ((map (make-sparse-keymap)))
78 (define-key map "u" 'gnus-article-copy-string)
79 (define-key map "i" 'gnus-html-insert-image)
80 (define-key map "v" 'gnus-html-browse-url)
83 (defvar gnus-html-displayed-image-map
84 (let ((map (make-sparse-keymap)))
85 (define-key map "a" 'gnus-html-show-alt-text)
86 (define-key map "i" 'gnus-html-browse-image)
87 (define-key map "\r" 'gnus-html-browse-url)
88 (define-key map "u" 'gnus-article-copy-string)
89 (define-key map [tab] 'widget-forward)
93 (defalias 'gnus-html-encode-url-chars
94 (if (fboundp 'browse-url-url-encode-chars)
95 'browse-url-url-encode-chars
97 "URL-encode the chars in TEXT that match CHARS.
98 CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
99 (let ((encoded-text (copy-sequence text))
101 (while (setq s (string-match chars encoded-text s))
103 (replace-match (format "%%%x"
105 (match-string 0 encoded-text)))
110 (defun gnus-html-encode-url (url)
112 (gnus-html-encode-url-chars url "[)$ ]"))
114 (defun gnus-html-cache-expired (url ttl)
115 "Check if URL is cached for more than TTL."
116 (cond (url-standalone-mode
117 (not (file-exists-p (url-cache-create-filename url))))
118 (t (let ((cache-time (url-is-cached url)))
128 (defun gnus-article-html (&optional handle)
129 (let ((article-buffer (current-buffer)))
131 (setq handle (mm-dissect-buffer t)))
133 (narrow-to-region (point) (point))
136 (let* ((coding-system-for-read 'utf-8)
137 (coding-system-for-write 'utf-8)
138 (default-process-coding-system
139 (cons coding-system-for-read coding-system-for-write))
140 (charset (mail-content-type-get (mm-handle-type handle)
143 (setq charset (mm-charset-to-coding-system charset))
144 (not (eq charset 'ascii)))
146 (mm-decode-coding-string (buffer-string) charset)
148 (mm-enable-multibyte))))
149 (call-process-region (point-min) (point-max)
151 nil article-buffer nil
156 "-o" "ext_halfdump=1"
157 "-o" "display_ins_del=2"
159 "-t" (format "%s" tab-width)
160 "-cols" (format "%s" gnus-html-frame-width)
161 "-o" "display_image=on"
163 (gnus-html-wash-tags))))
165 (defvar gnus-article-mouse-face)
167 (defun gnus-html-pre-wash ()
168 (goto-char (point-min))
169 (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
170 (replace-match "" t t))
171 (goto-char (point-min))
172 (while (re-search-forward "<a name[^\n>]+>" nil t)
173 (replace-match "" t t)))
175 (defun gnus-html-wash-images ()
176 "Run through current buffer and replace img tags by images."
177 (let (tag parameters string start end images url)
178 (goto-char (point-min))
179 ;; Search for all the images first.
180 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
181 (setq parameters (match-string 1)
182 start (match-beginning 0))
183 (delete-region start (point))
184 (when (search-forward "</img_alt>" (line-end-position) t)
185 (delete-region (match-beginning 0) (match-end 0)))
187 (when (string-match "src=\"\\([^\"]+\\)" parameters)
188 (setq url (gnus-html-encode-url (match-string 1 parameters)))
189 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
190 (if (string-match "^cid:\\(.*\\)" url)
191 ;; URLs with cid: have their content stashed in other
192 ;; parts of the MIME structure, so just insert them
194 (let* ((handle (mm-get-content-id
195 (setq url (match-string 1 url))))
197 (gnus-create-image (mm-with-part handle (buffer-string))
200 (let ((string (buffer-substring start end)))
201 (delete-region start end)
202 (gnus-put-image (gnus-rescale-image image (gnus-html-maximum-image-size))
203 (gnus-string-or string "*") 'cid)
204 (gnus-add-image 'cid image))))
205 ;; Normal, external URL.
206 (let ((alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
208 (xml-substitute-special (match-string 2 parameters)))))
209 (gnus-put-text-property start end 'gnus-image-url url)
210 (if (gnus-html-image-url-blocked-p
212 (if (buffer-live-p gnus-summary-buffer)
213 (with-current-buffer gnus-summary-buffer
215 gnus-blocked-images))
217 (widget-convert-button
219 :action 'gnus-html-insert-image
221 :keymap gnus-html-image-map
222 :button-keymap gnus-html-image-map)
223 (let ((overlay (gnus-make-overlay start end))
224 (spec (list url start end alt-text)))
225 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
226 (gnus-overlay-put overlay 'gnus-image spec)
227 (gnus-put-text-property
232 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
233 (string-to-number (match-string 1 parameters))))
235 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
236 (string-to-number (match-string 1 parameters)))))
237 ;; Don't fetch images that are really small. They're
238 ;; probably tracking pictures.
239 (when (and (or (null height)
243 (gnus-html-display-image url start end alt-text))))))))))
245 (defun gnus-html-display-image (url start end alt-text)
246 "Display image at URL on text from START to END.
247 Use ALT-TEXT for the image string."
248 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
249 ;; We don't have it, so schedule it for fetching
251 (gnus-html-schedule-image-fetching
254 ;; It's already cached, so just insert it.
255 (gnus-html-put-image (gnus-html-get-image-data url) url alt-text)))
257 (defun gnus-html-wash-tags ()
258 (let (tag parameters string start end images url)
260 (gnus-html-wash-images)
262 (goto-char (point-min))
263 ;; Then do the other tags.
264 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
265 (setq tag (match-string 1)
266 parameters (match-string 2)
267 start (match-beginning 0))
268 (when (> (length parameters) 0)
269 (set-text-properties 0 (1- (length parameters)) nil parameters))
270 (delete-region start (point))
271 (when (search-forward (concat "</" tag ">") nil t)
272 (delete-region (match-beginning 0) (match-end 0)))
275 ;; Fetch and insert a picture.
276 ((equal tag "img_alt"))
280 (when (string-match "href=\"\\([^\"]+\\)" parameters)
281 (setq url (match-string 1 parameters))
282 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
283 (gnus-article-add-button start end
284 'browse-url (mm-url-decode-entities-string url)
286 (let ((overlay (gnus-make-overlay start end)))
287 (gnus-overlay-put overlay 'evaporate t)
288 (gnus-overlay-put overlay 'gnus-button-url url)
289 (gnus-put-text-property start end 'gnus-string url)
290 (when gnus-article-mouse-face
291 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
292 ;; The upper-case IMG_ALT is apparently just an artifact that
293 ;; should be deleted.
294 ((equal tag "IMG_ALT")
295 (delete-region start end))
296 ;; w3m does not normalize the case
299 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
302 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
305 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
308 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
309 ((or (equal tag "ins")
311 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
312 ;; Handle different UL types
313 ((equal tag "_SYMBOL")
314 (when (string-match "TYPE=\\(.+\\)" parameters)
315 (let ((type (string-to-number (match-string 1 parameters))))
316 (delete-region start end)
317 (cond ((= type 33) (insert " "))
318 ((= type 34) (insert " "))
319 ((= type 35) (insert " "))
320 ((= type 36) (insert " "))
321 ((= type 37) (insert " "))
322 ((= type 38) (insert " "))
323 ((= type 39) (insert " "))
324 ((= type 40) (insert " "))
325 ((= type 42) (insert " "))
326 ((= type 43) (insert " "))
328 ;; Whatever. Just ignore the tag.
332 (goto-char (point-min))
333 ;; The output from -halfdump isn't totally regular, so strip
334 ;; off any </pre_int>s that were left over.
335 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
336 (replace-match "" t t))
337 (mm-url-decode-entities)))
339 (defun gnus-html-insert-image ()
340 "Fetch and insert the image under point."
342 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
344 (defun gnus-html-show-alt-text ()
345 "Show the ALT text of the image under point."
347 (message "%s" (get-text-property (point) 'gnus-alt-text)))
349 (defun gnus-html-browse-image ()
350 "Browse the image under point."
352 (browse-url (get-text-property (point) 'gnus-image-url)))
354 (defun gnus-html-browse-url ()
355 "Browse the image under point."
357 (let ((url (get-text-property (point) 'gnus-string)))
359 (message "No URL at point")
362 (defun gnus-html-schedule-image-fetching (buffer image)
363 "Retrieve IMAGE, and place it into BUFFER on arrival."
364 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
366 (let ((args (list (car image)
367 'gnus-html-image-fetched
368 (list buffer image))))
369 (when (> (length (if (featurep 'xemacs)
370 (split-string (function-arglist 'url-retrieve))
371 (help-function-arglist 'url-retrieve)))
373 (setq args (nconc args (list t))))
374 (apply #'url-retrieve args)))
376 (defun gnus-html-image-fetched (status buffer image)
377 "Callback function called when image has been fetched."
378 (unless (plist-get status :error)
379 (when gnus-html-image-automatic-caching
380 (url-store-in-cache (current-buffer)))
381 (when (and (or (search-forward "\n\n" nil t)
382 (search-forward "\r\n\r\n" nil t))
383 (buffer-live-p buffer))
384 (let ((data (buffer-substring (point) (point-max))))
385 (with-current-buffer buffer
386 (let ((inhibit-read-only t))
387 (gnus-html-put-image data (car image) (cadr image)))))))
388 (kill-buffer (current-buffer)))
390 (defun gnus-html-get-image-data (url)
391 "Get image data for URL.
392 Return a string with image data."
394 (mm-disable-multibyte)
395 (url-cache-extract (url-cache-create-filename url))
396 (when (or (search-forward "\n\n" nil t)
397 (search-forward "\r\n\r\n" nil t))
398 (buffer-substring (point) (point-max)))))
400 (defun gnus-html-maximum-image-size ()
401 "Return the maximum size of an image according to `gnus-max-image-proportion'."
402 (let ((edges (gnus-window-inside-pixel-edges
403 (get-buffer-window (current-buffer)))))
408 (* gnus-max-image-proportion
409 (- (nth 2 edges) (nth 0 edges))))
411 (truncate (* gnus-max-image-proportion
412 (- (nth 3 edges) (nth 1 edges)))))))
414 (defun gnus-html-put-image (data url &optional alt-text)
415 "Put an image with DATA from URL and optional ALT-TEXT."
416 (when (gnus-graphic-display-p)
417 (let* ((start (text-property-any (point-min) (point-max)
418 'gnus-image-url url))
420 (next-single-property-change start 'gnus-image-url))))
425 (gnus-create-image data nil t)))
427 (if (featurep 'xemacs)
428 (cons (glyph-width image) (glyph-height image))
429 (image-size image t)))))
432 (let ((alt-text (or alt-text
433 (buffer-substring-no-properties start end))))
435 ;; Kludge to avoid displaying 30x30 gif images, which
436 ;; seems to be a signal of a broken image.
437 (not (and (if (featurep 'xemacs)
440 (eq (if (featurep 'xemacs)
443 (glyph-image image)))))
446 (plist-get (cdr image) :type))
450 ;; Good image, add it!
451 (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
452 (delete-region start end)
453 (gnus-put-image image alt-text 'external)
454 (gnus-put-text-property start (point) 'help-echo alt-text)
456 (gnus-make-overlay start (point)) 'local-map
457 gnus-html-displayed-image-map)
458 (gnus-put-text-property start (point)
459 'gnus-alt-text alt-text)
461 (gnus-put-text-property start (point)
462 'gnus-image-url url))
463 (gnus-add-image 'external image)
465 ;; Bad image, try to show something else
466 (when (fboundp 'find-image)
467 (delete-region start end)
468 (setq image (find-image
469 '((:type xpm :file "lock-broken.xpm"))))
470 (gnus-put-image image alt-text 'internal)
471 (gnus-add-image 'internal image))
474 (defun gnus-html-image-url-blocked-p (url blocked-images)
475 "Find out if URL is blocked by BLOCKED-IMAGES."
476 (let ((ret (and blocked-images
477 (string-match blocked-images url))))
479 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
481 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
485 (defun gnus-html-show-images ()
486 "Show any images that are in the HTML-rendered article buffer.
487 This only works if the article in question is HTML."
489 (gnus-with-article-buffer
490 (dolist (overlay (overlays-in (point-min) (point-max)))
491 (let ((o (overlay-get overlay 'gnus-image)))
493 (apply 'gnus-html-display-image o))))))
496 (defun gnus-html-prefetch-images (summary)
497 (when (buffer-live-p summary)
498 (let ((blocked-images (with-current-buffer summary
499 gnus-blocked-images)))
501 (while (re-search-forward "<img[^>]+src=[\"']\\([^\"']+\\)" nil t)
502 (let ((url (gnus-html-encode-url (match-string 1))))
503 (unless (gnus-html-image-url-blocked-p url blocked-images)
504 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
505 (gnus-html-schedule-image-fetching nil
510 ;;; gnus-html.el ends here