2010-09-04 00:12:44 +00:00
|
|
|
;;; gnus-html.el --- Render HTML in a buffer.
|
2010-08-30 06:13:50 +00:00
|
|
|
|
2011-01-25 04:08:28 +00:00
|
|
|
;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
|
2010-08-30 06:13:50 +00:00
|
|
|
|
|
|
|
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
|
|
|
|
;; Keywords: html, web
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; The idea is to provide a simple, fast and pretty minimal way to
|
|
|
|
;; render HTML (including links and images) in a buffer, based on an
|
|
|
|
;; external HTML renderer (i.e., w3m).
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2010-08-31 19:47:35 +00:00
|
|
|
(eval-when-compile (require 'cl))
|
2010-09-17 05:38:38 +00:00
|
|
|
|
|
|
|
(require 'gnus-art)
|
2010-11-02 04:08:43 +00:00
|
|
|
(eval-when-compile (require 'mm-decode))
|
|
|
|
|
2010-08-31 01:15:32 +00:00
|
|
|
(require 'mm-url)
|
2010-09-18 23:36:29 +00:00
|
|
|
(require 'url)
|
2010-09-21 11:48:40 +00:00
|
|
|
(require 'url-cache)
|
2010-09-22 12:14:12 +00:00
|
|
|
(require 'xml)
|
2010-09-23 00:30:37 +00:00
|
|
|
(require 'browse-url)
|
2010-10-05 03:48:30 +00:00
|
|
|
(eval-and-compile (unless (featurep 'xemacs) (require 'help-fns)))
|
2010-08-31 01:15:32 +00:00
|
|
|
|
2010-09-21 11:48:40 +00:00
|
|
|
(defcustom gnus-html-image-cache-ttl (days-to-time 7)
|
2010-09-23 00:30:37 +00:00
|
|
|
"Time used to determine if we should use images from the cache."
|
2010-09-01 04:44:25 +00:00
|
|
|
:version "24.1"
|
2010-08-30 06:17:45 +00:00
|
|
|
:group 'gnus-art
|
|
|
|
:type 'integer)
|
|
|
|
|
2010-09-23 00:30:37 +00:00
|
|
|
(defcustom gnus-html-image-automatic-caching t
|
|
|
|
"Whether automatically cache retrieve images."
|
|
|
|
:version "24.1"
|
|
|
|
:group 'gnus-art
|
|
|
|
:type 'boolean)
|
|
|
|
|
2010-08-30 06:17:45 +00:00
|
|
|
(defcustom gnus-html-frame-width 70
|
|
|
|
"What width to use when rendering HTML."
|
2010-09-01 04:44:25 +00:00
|
|
|
:version "24.1"
|
2010-08-30 06:17:45 +00:00
|
|
|
:group 'gnus-art
|
|
|
|
:type 'integer)
|
|
|
|
|
2010-09-23 00:58:01 +00:00
|
|
|
(defcustom gnus-max-image-proportion 0.9
|
2010-09-01 23:53:57 +00:00
|
|
|
"How big pictures displayed are in relation to the window they're in.
|
|
|
|
A value of 0.7 means that they are allowed to take up 70% of the
|
|
|
|
width and height of the window. If they are larger than this,
|
|
|
|
and Emacs supports it, then the images will be rescaled down to
|
|
|
|
fit these criteria."
|
|
|
|
:version "24.1"
|
|
|
|
:group 'gnus-art
|
|
|
|
:type 'float)
|
|
|
|
|
2010-09-04 00:21:34 +00:00
|
|
|
(defvar gnus-html-image-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map "u" 'gnus-article-copy-string)
|
|
|
|
(define-key map "i" 'gnus-html-insert-image)
|
2010-09-21 11:48:40 +00:00
|
|
|
(define-key map "v" 'gnus-html-browse-url)
|
2010-09-04 00:21:34 +00:00
|
|
|
map))
|
|
|
|
|
2010-09-08 23:59:52 +00:00
|
|
|
(defvar gnus-html-displayed-image-map
|
|
|
|
(let ((map (make-sparse-keymap)))
|
|
|
|
(define-key map "a" 'gnus-html-show-alt-text)
|
|
|
|
(define-key map "i" 'gnus-html-browse-image)
|
2010-09-09 00:08:06 +00:00
|
|
|
(define-key map "\r" 'gnus-html-browse-url)
|
|
|
|
(define-key map "u" 'gnus-article-copy-string)
|
|
|
|
(define-key map [tab] 'widget-forward)
|
2010-09-08 23:59:52 +00:00
|
|
|
map))
|
|
|
|
|
2010-09-24 00:38:10 +00:00
|
|
|
(eval-and-compile
|
|
|
|
(defalias 'gnus-html-encode-url-chars
|
|
|
|
(if (fboundp 'browse-url-url-encode-chars)
|
|
|
|
'browse-url-url-encode-chars
|
|
|
|
(lambda (text chars)
|
|
|
|
"URL-encode the chars in TEXT that match CHARS.
|
|
|
|
CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
|
|
|
|
(let ((encoded-text (copy-sequence text))
|
|
|
|
(s 0))
|
|
|
|
(while (setq s (string-match chars encoded-text s))
|
|
|
|
(setq encoded-text
|
|
|
|
(replace-match (format "%%%x"
|
|
|
|
(string-to-char
|
|
|
|
(match-string 0 encoded-text)))
|
|
|
|
t t encoded-text)
|
|
|
|
s (1+ s)))
|
2010-10-04 00:17:16 +00:00
|
|
|
encoded-text)))))
|
2010-09-24 00:38:10 +00:00
|
|
|
|
2010-09-23 00:30:37 +00:00
|
|
|
(defun gnus-html-encode-url (url)
|
|
|
|
"Encode URL."
|
2010-09-24 00:38:10 +00:00
|
|
|
(gnus-html-encode-url-chars url "[)$ ]"))
|
2010-09-23 00:30:37 +00:00
|
|
|
|
2010-09-21 11:48:40 +00:00
|
|
|
(defun gnus-html-cache-expired (url ttl)
|
|
|
|
"Check if URL is cached for more than TTL."
|
|
|
|
(cond (url-standalone-mode
|
|
|
|
(not (file-exists-p (url-cache-create-filename url))))
|
|
|
|
(t (let ((cache-time (url-is-cached url)))
|
|
|
|
(if cache-time
|
|
|
|
(time-less-p
|
|
|
|
(time-add
|
|
|
|
cache-time
|
|
|
|
ttl)
|
|
|
|
(current-time))
|
|
|
|
t)))))
|
|
|
|
|
2010-08-30 06:13:50 +00:00
|
|
|
;;;###autoload
|
2010-09-06 00:41:06 +00:00
|
|
|
(defun gnus-article-html (&optional handle)
|
2010-08-30 06:13:50 +00:00
|
|
|
(let ((article-buffer (current-buffer)))
|
2010-09-06 00:41:06 +00:00
|
|
|
(unless handle
|
|
|
|
(setq handle (mm-dissect-buffer t)))
|
2010-08-30 06:13:50 +00:00
|
|
|
(save-restriction
|
|
|
|
(narrow-to-region (point) (point))
|
|
|
|
(save-excursion
|
2010-08-30 06:17:45 +00:00
|
|
|
(mm-with-part handle
|
|
|
|
(let* ((coding-system-for-read 'utf-8)
|
|
|
|
(coding-system-for-write 'utf-8)
|
|
|
|
(default-process-coding-system
|
2010-09-01 06:04:49 +00:00
|
|
|
(cons coding-system-for-read coding-system-for-write))
|
|
|
|
(charset (mail-content-type-get (mm-handle-type handle)
|
|
|
|
'charset)))
|
|
|
|
(when (and charset
|
|
|
|
(setq charset (mm-charset-to-coding-system charset))
|
|
|
|
(not (eq charset 'ascii)))
|
2010-09-02 03:47:31 +00:00
|
|
|
(insert (prog1
|
|
|
|
(mm-decode-coding-string (buffer-string) charset)
|
|
|
|
(erase-buffer)
|
|
|
|
(mm-enable-multibyte))))
|
2010-08-30 06:17:45 +00:00
|
|
|
(call-process-region (point-min) (point-max)
|
2010-09-02 01:42:32 +00:00
|
|
|
"w3m"
|
2010-08-30 06:17:45 +00:00
|
|
|
nil article-buffer nil
|
|
|
|
"-halfdump"
|
|
|
|
"-no-cookie"
|
2010-08-31 00:23:25 +00:00
|
|
|
"-I" "UTF-8"
|
2010-08-30 06:17:45 +00:00
|
|
|
"-O" "UTF-8"
|
|
|
|
"-o" "ext_halfdump=1"
|
2010-09-20 00:36:54 +00:00
|
|
|
"-o" "display_ins_del=2"
|
2010-08-31 00:23:25 +00:00
|
|
|
"-o" "pre_conv=1"
|
2010-08-30 06:17:45 +00:00
|
|
|
"-t" (format "%s" tab-width)
|
|
|
|
"-cols" (format "%s" gnus-html-frame-width)
|
2010-09-02 01:29:42 +00:00
|
|
|
"-o" "display_image=on"
|
2010-08-30 06:17:45 +00:00
|
|
|
"-T" "text/html"))))
|
2010-08-30 06:13:50 +00:00
|
|
|
(gnus-html-wash-tags))))
|
|
|
|
|
2010-08-31 19:47:35 +00:00
|
|
|
(defvar gnus-article-mouse-face)
|
|
|
|
|
2010-09-09 00:08:06 +00:00
|
|
|
(defun gnus-html-pre-wash ()
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
|
|
|
|
(replace-match "" t t))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(while (re-search-forward "<a name[^\n>]+>" nil t)
|
|
|
|
(replace-match "" t t)))
|
|
|
|
|
2010-09-10 00:07:33 +00:00
|
|
|
(defun gnus-html-wash-images ()
|
2010-09-21 11:48:40 +00:00
|
|
|
"Run through current buffer and replace img tags by images."
|
2010-11-19 04:55:16 +00:00
|
|
|
(let (tag parameters string start end images url alt-text
|
|
|
|
inhibit-images blocked-images)
|
|
|
|
(if (buffer-live-p gnus-summary-buffer)
|
|
|
|
(with-current-buffer gnus-summary-buffer
|
|
|
|
(setq inhibit-images gnus-inhibit-images
|
|
|
|
blocked-images (gnus-blocked-images)))
|
|
|
|
(setq inhibit-images gnus-inhibit-images
|
|
|
|
blocked-images (gnus-blocked-images)))
|
2010-08-30 06:13:50 +00:00
|
|
|
(goto-char (point-min))
|
2010-09-09 00:08:06 +00:00
|
|
|
;; Search for all the images first.
|
|
|
|
(while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
|
|
|
|
(setq parameters (match-string 1)
|
|
|
|
start (match-beginning 0))
|
|
|
|
(delete-region start (point))
|
|
|
|
(when (search-forward "</img_alt>" (line-end-position) t)
|
|
|
|
(delete-region (match-beginning 0) (match-end 0)))
|
|
|
|
(setq end (point))
|
|
|
|
(when (string-match "src=\"\\([^\"]+\\)" parameters)
|
|
|
|
(gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
|
2010-11-18 02:00:00 +00:00
|
|
|
(setq url (gnus-html-encode-url (match-string 1 parameters))
|
|
|
|
alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
|
|
|
|
parameters)
|
|
|
|
(xml-substitute-special (match-string 2 parameters))))
|
|
|
|
(gnus-add-text-properties
|
|
|
|
start end
|
|
|
|
(list 'image-url url
|
|
|
|
'image-displayer `(lambda (url start end)
|
|
|
|
(gnus-html-display-image url start end
|
|
|
|
,alt-text))
|
|
|
|
'gnus-image (list url start end alt-text)))
|
2010-12-05 22:17:34 +00:00
|
|
|
(widget-convert-button
|
|
|
|
'url-link start (point)
|
|
|
|
:help-echo alt-text
|
|
|
|
:keymap gnus-html-image-map
|
|
|
|
url)
|
2010-11-18 02:00:00 +00:00
|
|
|
(if (string-match "\\`cid:" url)
|
2010-09-09 00:08:06 +00:00
|
|
|
;; URLs with cid: have their content stashed in other
|
|
|
|
;; parts of the MIME structure, so just insert them
|
|
|
|
;; immediately.
|
2010-11-18 02:00:00 +00:00
|
|
|
(let* ((handle (mm-get-content-id (substring url (match-end 0))))
|
|
|
|
(image (when (and handle
|
2010-11-19 04:55:16 +00:00
|
|
|
(not inhibit-images))
|
2010-11-18 02:00:00 +00:00
|
|
|
(gnus-create-image
|
2010-11-16 00:04:25 +00:00
|
|
|
(mm-with-part handle (buffer-string))
|
|
|
|
nil t))))
|
2010-11-18 02:00:00 +00:00
|
|
|
(if image
|
2011-05-10 03:14:44 +00:00
|
|
|
(gnus-add-image
|
|
|
|
'cid
|
|
|
|
(gnus-put-image
|
|
|
|
(gnus-rescale-image
|
|
|
|
image (gnus-html-maximum-image-size))
|
|
|
|
(gnus-string-or (prog1
|
|
|
|
(buffer-substring start end)
|
|
|
|
(delete-region start end))
|
|
|
|
"*")
|
|
|
|
'cid))
|
2010-11-18 02:00:00 +00:00
|
|
|
(widget-convert-button
|
|
|
|
'link start end
|
|
|
|
:action 'gnus-html-insert-image
|
|
|
|
:help-echo url
|
|
|
|
:keymap gnus-html-image-map
|
|
|
|
:button-keymap gnus-html-image-map)))
|
2010-09-09 00:08:06 +00:00
|
|
|
;; Normal, external URL.
|
2010-11-19 04:55:16 +00:00
|
|
|
(if (or inhibit-images
|
|
|
|
(gnus-html-image-url-blocked-p url blocked-images))
|
2010-11-18 02:00:00 +00:00
|
|
|
(widget-convert-button
|
|
|
|
'link start end
|
|
|
|
:action 'gnus-html-insert-image
|
|
|
|
:help-echo url
|
|
|
|
:keymap gnus-html-image-map
|
|
|
|
:button-keymap gnus-html-image-map)
|
|
|
|
;; Non-blocked url
|
|
|
|
(let ((width
|
|
|
|
(when (string-match "width=\"?\\([0-9]+\\)" parameters)
|
|
|
|
(string-to-number (match-string 1 parameters))))
|
|
|
|
(height
|
|
|
|
(when (string-match "height=\"?\\([0-9]+\\)" parameters)
|
|
|
|
(string-to-number (match-string 1 parameters)))))
|
|
|
|
;; Don't fetch images that are really small. They're
|
|
|
|
;; probably tracking pictures.
|
|
|
|
(when (and (or (null height)
|
|
|
|
(> height 4))
|
|
|
|
(or (null width)
|
|
|
|
(> width 4)))
|
|
|
|
(gnus-html-display-image url start end alt-text)))))))))
|
2010-09-21 11:48:40 +00:00
|
|
|
|
2010-11-15 23:45:55 +00:00
|
|
|
(defun gnus-html-display-image (url start end &optional alt-text)
|
2010-09-21 11:48:40 +00:00
|
|
|
"Display image at URL on text from START to END.
|
|
|
|
Use ALT-TEXT for the image string."
|
2010-11-18 02:00:00 +00:00
|
|
|
(or alt-text (setq alt-text "*"))
|
|
|
|
(if (string-match "\\`cid:" url)
|
|
|
|
(let ((handle (mm-get-content-id (substring url (match-end 0)))))
|
|
|
|
(when handle
|
|
|
|
(gnus-html-put-image (mm-with-part handle (buffer-string))
|
|
|
|
url alt-text)))
|
|
|
|
(if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
|
|
|
|
;; We don't have it, so schedule it for fetching
|
|
|
|
;; asynchronously.
|
|
|
|
(gnus-html-schedule-image-fetching
|
|
|
|
(current-buffer)
|
|
|
|
(list url alt-text))
|
|
|
|
;; It's already cached, so just insert it.
|
|
|
|
(gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
|
2010-09-10 00:07:33 +00:00
|
|
|
|
|
|
|
(defun gnus-html-wash-tags ()
|
|
|
|
(let (tag parameters string start end images url)
|
|
|
|
(gnus-html-pre-wash)
|
|
|
|
(gnus-html-wash-images)
|
2010-09-09 00:08:06 +00:00
|
|
|
|
2010-09-06 23:58:34 +00:00
|
|
|
(goto-char (point-min))
|
2010-09-09 00:08:06 +00:00
|
|
|
;; Then do the other tags.
|
2010-08-30 06:17:45 +00:00
|
|
|
(while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
|
2010-08-30 06:13:50 +00:00
|
|
|
(setq tag (match-string 1)
|
|
|
|
parameters (match-string 2)
|
2010-08-30 06:17:45 +00:00
|
|
|
start (match-beginning 0))
|
2010-10-03 19:54:31 +00:00
|
|
|
(when (> (length parameters) 0)
|
2010-08-30 06:17:45 +00:00
|
|
|
(set-text-properties 0 (1- (length parameters)) nil parameters))
|
|
|
|
(delete-region start (point))
|
2010-09-10 00:07:33 +00:00
|
|
|
(when (search-forward (concat "</" tag ">") nil t)
|
2010-08-30 06:17:45 +00:00
|
|
|
(delete-region (match-beginning 0) (match-end 0)))
|
|
|
|
(setq end (point))
|
2010-08-30 06:13:50 +00:00
|
|
|
(cond
|
|
|
|
;; Fetch and insert a picture.
|
2010-09-09 00:08:06 +00:00
|
|
|
((equal tag "img_alt"))
|
2010-08-30 06:13:50 +00:00
|
|
|
;; Add a link.
|
2010-09-02 01:02:03 +00:00
|
|
|
((or (equal tag "a")
|
|
|
|
(equal tag "A"))
|
2010-08-30 06:13:50 +00:00
|
|
|
(when (string-match "href=\"\\([^\"]+\\)" parameters)
|
2010-08-31 13:28:02 +00:00
|
|
|
(setq url (match-string 1 parameters))
|
2010-09-02 01:18:45 +00:00
|
|
|
(gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
|
2010-08-30 06:13:50 +00:00
|
|
|
(gnus-article-add-button start end
|
2010-10-05 10:46:15 +00:00
|
|
|
'browse-url (mm-url-decode-entities-string url)
|
2010-08-31 13:28:02 +00:00
|
|
|
url)
|
2010-08-30 06:13:50 +00:00
|
|
|
(let ((overlay (gnus-make-overlay start end)))
|
|
|
|
(gnus-overlay-put overlay 'evaporate t)
|
2010-08-31 13:28:02 +00:00
|
|
|
(gnus-overlay-put overlay 'gnus-button-url url)
|
2010-09-04 00:21:34 +00:00
|
|
|
(gnus-put-text-property start end 'gnus-string url)
|
2010-08-30 06:13:50 +00:00
|
|
|
(when gnus-article-mouse-face
|
|
|
|
(gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
|
2010-09-01 23:40:39 +00:00
|
|
|
;; The upper-case IMG_ALT is apparently just an artifact that
|
|
|
|
;; should be deleted.
|
|
|
|
((equal tag "IMG_ALT")
|
|
|
|
(delete-region start end))
|
2010-09-20 00:36:54 +00:00
|
|
|
;; w3m does not normalize the case
|
|
|
|
((or (equal tag "b")
|
|
|
|
(equal tag "B"))
|
|
|
|
(gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-bold))
|
|
|
|
((or (equal tag "u")
|
|
|
|
(equal tag "U"))
|
|
|
|
(gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
|
|
|
|
((or (equal tag "i")
|
|
|
|
(equal tag "I"))
|
|
|
|
(gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-italic))
|
|
|
|
((or (equal tag "s")
|
|
|
|
(equal tag "S"))
|
|
|
|
(gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-strikethru))
|
|
|
|
((or (equal tag "ins")
|
|
|
|
(equal tag "INS"))
|
|
|
|
(gnus-overlay-put (gnus-make-overlay start end) 'face 'gnus-emphasis-underline))
|
|
|
|
;; Handle different UL types
|
|
|
|
((equal tag "_SYMBOL")
|
|
|
|
(when (string-match "TYPE=\\(.+\\)" parameters)
|
|
|
|
(let ((type (string-to-number (match-string 1 parameters))))
|
|
|
|
(delete-region start end)
|
|
|
|
(cond ((= type 33) (insert " "))
|
|
|
|
((= type 34) (insert " "))
|
|
|
|
((= type 35) (insert " "))
|
|
|
|
((= type 36) (insert " "))
|
|
|
|
((= type 37) (insert " "))
|
|
|
|
((= type 38) (insert " "))
|
|
|
|
((= type 39) (insert " "))
|
|
|
|
((= type 40) (insert " "))
|
|
|
|
((= type 42) (insert " "))
|
|
|
|
((= type 43) (insert " "))
|
|
|
|
(t (insert " "))))))
|
2010-08-30 06:13:50 +00:00
|
|
|
;; Whatever. Just ignore the tag.
|
|
|
|
(t
|
2010-08-30 06:17:45 +00:00
|
|
|
))
|
|
|
|
(goto-char start))
|
|
|
|
(goto-char (point-min))
|
|
|
|
;; The output from -halfdump isn't totally regular, so strip
|
|
|
|
;; off any </pre_int>s that were left over.
|
2010-09-07 00:08:33 +00:00
|
|
|
(while (re-search-forward "</pre_int>\\|</internal>" nil t)
|
2010-08-30 06:17:45 +00:00
|
|
|
(replace-match "" t t))
|
2010-09-04 00:50:02 +00:00
|
|
|
(mm-url-decode-entities)))
|
2010-08-30 06:17:45 +00:00
|
|
|
|
2010-11-18 02:00:00 +00:00
|
|
|
(defun gnus-html-insert-image (&rest args)
|
2010-09-04 00:21:34 +00:00
|
|
|
"Fetch and insert the image under point."
|
|
|
|
(interactive)
|
2010-09-21 11:48:40 +00:00
|
|
|
(apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
|
2010-09-04 00:21:34 +00:00
|
|
|
|
2010-09-08 23:59:52 +00:00
|
|
|
(defun gnus-html-show-alt-text ()
|
|
|
|
"Show the ALT text of the image under point."
|
|
|
|
(interactive)
|
|
|
|
(message "%s" (get-text-property (point) 'gnus-alt-text)))
|
|
|
|
|
|
|
|
(defun gnus-html-browse-image ()
|
|
|
|
"Browse the image under point."
|
|
|
|
(interactive)
|
2010-11-15 23:45:55 +00:00
|
|
|
(browse-url (get-text-property (point) 'image-url)))
|
2010-09-08 23:59:52 +00:00
|
|
|
|
2010-09-09 00:08:06 +00:00
|
|
|
(defun gnus-html-browse-url ()
|
|
|
|
"Browse the image under point."
|
|
|
|
(interactive)
|
|
|
|
(let ((url (get-text-property (point) 'gnus-string)))
|
2010-11-04 22:18:09 +00:00
|
|
|
(cond
|
|
|
|
((not url)
|
|
|
|
(message "No link under point"))
|
|
|
|
((string-match "^mailto:" url)
|
|
|
|
(gnus-url-mailto url))
|
|
|
|
(t
|
|
|
|
(browse-url url)))))
|
2010-09-09 00:08:06 +00:00
|
|
|
|
2010-09-21 11:48:40 +00:00
|
|
|
(defun gnus-html-schedule-image-fetching (buffer image)
|
|
|
|
"Retrieve IMAGE, and place it into BUFFER on arrival."
|
|
|
|
(gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
|
|
|
|
buffer image)
|
2011-05-02 22:41:38 +00:00
|
|
|
(if (fboundp 'url-queue-retrieve)
|
|
|
|
(url-queue-retrieve (car image)
|
|
|
|
'gnus-html-image-fetched
|
|
|
|
(list buffer image) t)
|
2010-10-05 22:43:06 +00:00
|
|
|
(ignore-errors
|
2011-05-02 22:41:38 +00:00
|
|
|
(url-retrieve (car image)
|
|
|
|
'gnus-html-image-fetched
|
|
|
|
(list buffer image)))))
|
2010-08-30 06:17:45 +00:00
|
|
|
|
2010-09-18 23:36:29 +00:00
|
|
|
(defun gnus-html-image-fetched (status buffer image)
|
2010-09-23 00:30:37 +00:00
|
|
|
"Callback function called when image has been fetched."
|
|
|
|
(unless (plist-get status :error)
|
|
|
|
(when gnus-html-image-automatic-caching
|
|
|
|
(url-store-in-cache (current-buffer)))
|
|
|
|
(when (and (or (search-forward "\n\n" nil t)
|
|
|
|
(search-forward "\r\n\r\n" nil t))
|
|
|
|
(buffer-live-p buffer))
|
|
|
|
(let ((data (buffer-substring (point) (point-max))))
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(gnus-html-put-image data (car image) (cadr image)))))))
|
2010-09-21 11:48:40 +00:00
|
|
|
(kill-buffer (current-buffer)))
|
|
|
|
|
|
|
|
(defun gnus-html-get-image-data (url)
|
|
|
|
"Get image data for URL.
|
|
|
|
Return a string with image data."
|
|
|
|
(with-temp-buffer
|
|
|
|
(mm-disable-multibyte)
|
|
|
|
(url-cache-extract (url-cache-create-filename url))
|
2010-09-23 00:30:37 +00:00
|
|
|
(when (or (search-forward "\n\n" nil t)
|
|
|
|
(search-forward "\r\n\r\n" nil t))
|
2010-09-21 11:48:40 +00:00
|
|
|
(buffer-substring (point) (point-max)))))
|
|
|
|
|
2010-10-05 13:19:07 +00:00
|
|
|
(defun gnus-html-maximum-image-size ()
|
|
|
|
"Return the maximum size of an image according to `gnus-max-image-proportion'."
|
|
|
|
(let ((edges (gnus-window-inside-pixel-edges
|
|
|
|
(get-buffer-window (current-buffer)))))
|
|
|
|
;; (width . height)
|
|
|
|
(cons
|
|
|
|
;; Aimed width
|
|
|
|
(truncate
|
|
|
|
(* gnus-max-image-proportion
|
|
|
|
(- (nth 2 edges) (nth 0 edges))))
|
|
|
|
;; Aimed height
|
|
|
|
(truncate (* gnus-max-image-proportion
|
|
|
|
(- (nth 3 edges) (nth 1 edges)))))))
|
|
|
|
|
2010-09-23 00:30:37 +00:00
|
|
|
(defun gnus-html-put-image (data url &optional alt-text)
|
2010-10-05 13:19:07 +00:00
|
|
|
"Put an image with DATA from URL and optional ALT-TEXT."
|
2010-09-03 02:29:03 +00:00
|
|
|
(when (gnus-graphic-display-p)
|
2010-10-03 00:33:27 +00:00
|
|
|
(let* ((start (text-property-any (point-min) (point-max)
|
2010-11-15 23:45:55 +00:00
|
|
|
'image-url url))
|
2010-09-23 00:30:37 +00:00
|
|
|
(end (when start
|
2010-11-15 23:45:55 +00:00
|
|
|
(next-single-property-change start 'image-url))))
|
2010-09-23 00:30:37 +00:00
|
|
|
;; Image found?
|
|
|
|
(when start
|
|
|
|
(let* ((image
|
|
|
|
(ignore-errors
|
|
|
|
(gnus-create-image data nil t)))
|
|
|
|
(size (and image
|
|
|
|
(if (featurep 'xemacs)
|
|
|
|
(cons (glyph-width image) (glyph-height image))
|
|
|
|
(image-size image t)))))
|
|
|
|
(save-excursion
|
|
|
|
(goto-char start)
|
2010-10-03 00:33:27 +00:00
|
|
|
(let ((alt-text (or alt-text
|
2010-11-18 02:00:00 +00:00
|
|
|
(buffer-substring-no-properties start end)))
|
|
|
|
(inhibit-read-only t))
|
2010-09-23 00:30:37 +00:00
|
|
|
(if (and image
|
|
|
|
;; Kludge to avoid displaying 30x30 gif images, which
|
|
|
|
;; seems to be a signal of a broken image.
|
|
|
|
(not (and (if (featurep 'xemacs)
|
|
|
|
(glyphp image)
|
|
|
|
(listp image))
|
|
|
|
(eq (if (featurep 'xemacs)
|
2010-10-03 00:33:27 +00:00
|
|
|
(let ((d (cdadar
|
|
|
|
(specifier-spec-list
|
|
|
|
(glyph-image image)))))
|
2010-09-23 00:30:37 +00:00
|
|
|
(and (vectorp d)
|
|
|
|
(aref d 0)))
|
|
|
|
(plist-get (cdr image) :type))
|
|
|
|
'gif)
|
|
|
|
(= (car size) 30)
|
|
|
|
(= (cdr size) 30))))
|
|
|
|
;; Good image, add it!
|
2010-10-05 13:19:07 +00:00
|
|
|
(let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
|
2010-09-23 00:30:37 +00:00
|
|
|
(delete-region start end)
|
|
|
|
(gnus-put-image image alt-text 'external)
|
2010-12-05 22:17:34 +00:00
|
|
|
(widget-convert-button
|
|
|
|
'url-link start (point)
|
|
|
|
:help-echo alt-text
|
|
|
|
:keymap gnus-html-displayed-image-map
|
|
|
|
url)
|
2010-10-03 00:33:27 +00:00
|
|
|
(gnus-put-text-property start (point)
|
|
|
|
'gnus-alt-text alt-text)
|
2010-09-23 00:30:37 +00:00
|
|
|
(when url
|
2011-05-13 05:35:56 +00:00
|
|
|
(gnus-add-text-properties
|
|
|
|
start (point)
|
|
|
|
`(image-url
|
|
|
|
,url
|
|
|
|
image-displayer
|
|
|
|
(lambda (url start end)
|
2011-05-13 07:53:12 +00:00
|
|
|
(gnus-html-display-image url start end
|
2011-05-13 05:35:56 +00:00
|
|
|
,alt-text)))))
|
2010-09-23 00:30:37 +00:00
|
|
|
(gnus-add-image 'external image)
|
|
|
|
t)
|
|
|
|
;; Bad image, try to show something else
|
|
|
|
(when (fboundp 'find-image)
|
|
|
|
(delete-region start end)
|
2010-10-03 00:33:27 +00:00
|
|
|
(setq image (find-image
|
|
|
|
'((:type xpm :file "lock-broken.xpm"))))
|
2010-09-23 00:30:37 +00:00
|
|
|
(gnus-put-image image alt-text 'internal)
|
|
|
|
(gnus-add-image 'internal image))
|
|
|
|
nil))))))))
|
2010-09-21 11:48:40 +00:00
|
|
|
|
2010-09-02 01:02:03 +00:00
|
|
|
(defun gnus-html-image-url-blocked-p (url blocked-images)
|
2010-09-08 23:59:52 +00:00
|
|
|
"Find out if URL is blocked by BLOCKED-IMAGES."
|
2010-09-02 01:02:03 +00:00
|
|
|
(let ((ret (and blocked-images
|
|
|
|
(string-match blocked-images url))))
|
2010-09-02 01:18:45 +00:00
|
|
|
(if ret
|
|
|
|
(gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
|
|
|
|
url blocked-images)
|
|
|
|
(gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
|
|
|
|
url blocked-images))
|
2010-09-02 00:17:40 +00:00
|
|
|
ret))
|
|
|
|
|
2010-08-30 06:17:45 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defun gnus-html-prefetch-images (summary)
|
2010-09-20 00:36:54 +00:00
|
|
|
(when (buffer-live-p summary)
|
2010-11-19 04:55:16 +00:00
|
|
|
(let (inhibit-images blocked-images)
|
2010-11-19 05:11:59 +00:00
|
|
|
(with-current-buffer summary
|
2010-11-19 04:55:16 +00:00
|
|
|
(setq inhibit-images gnus-inhibit-images
|
|
|
|
blocked-images (gnus-blocked-images)))
|
2010-08-30 06:17:45 +00:00
|
|
|
(save-match-data
|
2010-10-21 03:48:01 +00:00
|
|
|
(while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
|
2010-10-24 09:55:56 +00:00
|
|
|
(let ((url (gnus-html-encode-url
|
|
|
|
(mm-url-decode-entities-string (match-string 1)))))
|
2010-11-19 04:55:16 +00:00
|
|
|
(unless (or inhibit-images
|
2010-11-18 02:00:00 +00:00
|
|
|
(gnus-html-image-url-blocked-p url blocked-images))
|
2010-09-21 11:48:40 +00:00
|
|
|
(when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
|
|
|
|
(gnus-html-schedule-image-fetching nil
|
|
|
|
(list url))))))))))
|
2010-08-30 06:17:45 +00:00
|
|
|
|
|
|
|
(provide 'gnus-html)
|
2010-08-30 06:13:50 +00:00
|
|
|
|
|
|
|
;;; gnus-html.el ends here
|