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))
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)
173 (goto-char (point-min))
174 ;; Search for all the images first.
175 (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
176 (setq parameters (match-string 1)
177 start (match-beginning 0))
178 (delete-region start (point))
179 (when (search-forward "</img_alt>" (line-end-position) t)
180 (delete-region (match-beginning 0) (match-end 0)))
182 (when (string-match "src=\"\\([^\"]+\\)" parameters)
183 (setq url (gnus-html-encode-url (match-string 1 parameters)))
184 (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
185 (if (string-match "^cid:\\(.*\\)" url)
186 ;; URLs with cid: have their content stashed in other
187 ;; parts of the MIME structure, so just insert them
189 (let* ((handle (mm-get-content-id
190 (setq url (match-string 1 url))))
193 (mm-with-part handle (buffer-string))
196 (let ((string (buffer-substring start end)))
197 (delete-region start end)
198 (gnus-put-image (gnus-rescale-image
199 image (gnus-html-maximum-image-size))
200 (gnus-string-or string "*") 'cid)
201 (gnus-add-image 'cid image))))
202 ;; Normal, external URL.
204 (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
206 (xml-substitute-special (match-string 2 parameters)))))
207 (gnus-put-text-property start end 'image-url url)
208 (gnus-put-text-property
209 start end 'image-displayer
210 (lambda (url start end)
211 (gnus-html-display-image url start end)))
212 (if (gnus-html-image-url-blocked-p
214 (if (buffer-live-p gnus-summary-buffer)
215 (with-current-buffer gnus-summary-buffer
216 (gnus-blocked-images))
217 (gnus-blocked-images)))
219 (widget-convert-button
221 :action 'gnus-html-insert-image
223 :keymap gnus-html-image-map
224 :button-keymap gnus-html-image-map)
225 (let ((overlay (gnus-make-overlay start end))
226 (spec (list url start end alt-text)))
227 (gnus-overlay-put overlay 'local-map gnus-html-image-map)
228 (gnus-overlay-put overlay 'gnus-image spec)
229 (gnus-put-text-property
234 (when (string-match "width=\"?\\([0-9]+\\)" parameters)
235 (string-to-number (match-string 1 parameters))))
237 (when (string-match "height=\"?\\([0-9]+\\)" parameters)
238 (string-to-number (match-string 1 parameters)))))
239 ;; Don't fetch images that are really small. They're
240 ;; probably tracking pictures.
241 (when (and (or (null height)
245 (gnus-html-display-image url start end alt-text))))))))))
247 (defun gnus-html-display-image (url start end &optional alt-text)
248 "Display image at URL on text from START to END.
249 Use ALT-TEXT for the image string."
250 (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
251 ;; We don't have it, so schedule it for fetching
253 (gnus-html-schedule-image-fetching
256 ;; It's already cached, so just insert it.
257 (gnus-html-put-image (gnus-html-get-image-data url) url (or alt-text "*"))))
259 (defun gnus-html-wash-tags ()
260 (let (tag parameters string start end images url)
262 (gnus-html-wash-images)
264 (goto-char (point-min))
265 ;; Then do the other tags.
266 (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
267 (setq tag (match-string 1)
268 parameters (match-string 2)
269 start (match-beginning 0))
270 (when (> (length parameters) 0)
271 (set-text-properties 0 (1- (length parameters)) nil parameters))
272 (delete-region start (point))
273 (when (search-forward (concat "</" tag ">") nil t)
274 (delete-region (match-beginning 0) (match-end 0)))
277 ;; Fetch and insert a picture.
278 ((equal tag "img_alt"))
282 (when (string-match "href=\"\\([^\"]+\\)" parameters)
283 (setq url (match-string 1 parameters))
284 (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
285 (gnus-article-add-button start end
286 'browse-url (mm-url-decode-entities-string url)
288 (let ((overlay (gnus-make-overlay start end)))
289 (gnus-overlay-put overlay 'evaporate t)
290 (gnus-overlay-put overlay 'gnus-button-url url)
291 (gnus-put-text-property start end 'gnus-string url)
292 (when gnus-article-mouse-face
293 (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
294 ;; The upper-case IMG_ALT is apparently just an artifact that
295 ;; should be deleted.
296 ((equal tag "IMG_ALT")
297 (delete-region start end))
298 ;; w3m does not normalize the case
301 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
304 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
307 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
310 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
311 ((or (equal tag "ins")
313 (gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
314 ;; Handle different UL types
315 ((equal tag "_SYMBOL")
316 (when (string-match "TYPE=\\(.+\\)" parameters)
317 (let ((type (string-to-number (match-string 1 parameters))))
318 (delete-region start end)
319 (cond ((= type 33) (insert " "))
320 ((= type 34) (insert " "))
321 ((= type 35) (insert " "))
322 ((= type 36) (insert " "))
323 ((= type 37) (insert " "))
324 ((= type 38) (insert " "))
325 ((= type 39) (insert " "))
326 ((= type 40) (insert " "))
327 ((= type 42) (insert " "))
328 ((= type 43) (insert " "))
330 ;; Whatever. Just ignore the tag.
334 (goto-char (point-min))
335 ;; The output from -halfdump isn't totally regular, so strip
336 ;; off any </pre_int>s that were left over.
337 (while (re-search-forward "</pre_int>\\|</internal>" nil t)
338 (replace-match "" t t))
339 (mm-url-decode-entities)))
341 (defun gnus-html-insert-image ()
342 "Fetch and insert the image under point."
344 (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
346 (defun gnus-html-show-alt-text ()
347 "Show the ALT text of the image under point."
349 (message "%s" (get-text-property (point) 'gnus-alt-text)))
351 (defun gnus-html-browse-image ()
352 "Browse the image under point."
354 (browse-url (get-text-property (point) 'image-url)))
356 (defun gnus-html-browse-url ()
357 "Browse the image under point."
359 (let ((url (get-text-property (point) 'gnus-string)))
362 (message "No link under point"))
363 ((string-match "^mailto:" url)
364 (gnus-url-mailto url))
368 (defun gnus-html-schedule-image-fetching (buffer image)
369 "Retrieve IMAGE, and place it into BUFFER on arrival."
370 (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
372 (let ((args (list (car image)
373 'gnus-html-image-fetched
374 (list buffer image))))
375 (when (> (length (if (featurep 'xemacs)
376 (cdr (split-string (function-arglist 'url-retrieve)))
377 (help-function-arglist 'url-retrieve)))
379 (setq args (nconc args (list t))))
381 (apply #'url-retrieve args))))
383 (defun gnus-html-image-fetched (status buffer image)
384 "Callback function called when image has been fetched."
385 (unless (plist-get status :error)
386 (when gnus-html-image-automatic-caching
387 (url-store-in-cache (current-buffer)))
388 (when (and (or (search-forward "\n\n" nil t)
389 (search-forward "\r\n\r\n" nil t))
390 (buffer-live-p buffer))
391 (let ((data (buffer-substring (point) (point-max))))
392 (with-current-buffer buffer
393 (let ((inhibit-read-only t))
394 (gnus-html-put-image data (car image) (cadr image)))))))
395 (kill-buffer (current-buffer)))
397 (defun gnus-html-get-image-data (url)
398 "Get image data for URL.
399 Return a string with image data."
401 (mm-disable-multibyte)
402 (url-cache-extract (url-cache-create-filename url))
403 (when (or (search-forward "\n\n" nil t)
404 (search-forward "\r\n\r\n" nil t))
405 (buffer-substring (point) (point-max)))))
407 (defun gnus-html-maximum-image-size ()
408 "Return the maximum size of an image according to `gnus-max-image-proportion'."
409 (let ((edges (gnus-window-inside-pixel-edges
410 (get-buffer-window (current-buffer)))))
415 (* gnus-max-image-proportion
416 (- (nth 2 edges) (nth 0 edges))))
418 (truncate (* gnus-max-image-proportion
419 (- (nth 3 edges) (nth 1 edges)))))))
421 (defun gnus-html-put-image (data url &optional alt-text)
422 "Put an image with DATA from URL and optional ALT-TEXT."
423 (when (gnus-graphic-display-p)
424 (let* ((start (text-property-any (point-min) (point-max)
427 (next-single-property-change start 'image-url))))
432 (gnus-create-image data nil t)))
434 (if (featurep 'xemacs)
435 (cons (glyph-width image) (glyph-height image))
436 (image-size image t)))))
439 (let ((alt-text (or alt-text
440 (buffer-substring-no-properties start end))))
442 ;; Kludge to avoid displaying 30x30 gif images, which
443 ;; seems to be a signal of a broken image.
444 (not (and (if (featurep 'xemacs)
447 (eq (if (featurep 'xemacs)
450 (glyph-image image)))))
453 (plist-get (cdr image) :type))
457 ;; Good image, add it!
458 (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
459 (delete-region start end)
460 (gnus-put-image image alt-text 'external)
461 (gnus-put-text-property start (point) 'help-echo alt-text)
463 (gnus-make-overlay start (point)) 'local-map
464 gnus-html-displayed-image-map)
465 (gnus-put-text-property start (point)
466 'gnus-alt-text alt-text)
468 (gnus-put-text-property start (point)
470 (gnus-add-image 'external image)
472 ;; Bad image, try to show something else
473 (when (fboundp 'find-image)
474 (delete-region start end)
475 (setq image (find-image
476 '((:type xpm :file "lock-broken.xpm"))))
477 (gnus-put-image image alt-text 'internal)
478 (gnus-add-image 'internal image))
481 (defun gnus-html-image-url-blocked-p (url blocked-images)
482 "Find out if URL is blocked by BLOCKED-IMAGES."
483 (let ((ret (and blocked-images
484 (string-match blocked-images url))))
486 (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
488 (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
493 (defun gnus-html-prefetch-images (summary)
494 (when (buffer-live-p summary)
495 (let ((blocked-images (with-current-buffer summary
496 (gnus-blocked-images))))
498 (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
499 (let ((url (gnus-html-encode-url
500 (mm-url-decode-entities-string (match-string 1)))))
501 (unless (gnus-html-image-url-blocked-p url blocked-images)
502 (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
503 (gnus-html-schedule-image-fetching nil
508 ;;; gnus-html.el ends here