Switch to using shr.el for HTML rendering.
[gnus] / lisp / shr.el
1 ;;; shr.el --- Simple HTML Renderer
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; This package takes a HTML parse tree (as provided by
26 ;; libxml-parse-html-region) and renders it in the current buffer.  It
27 ;; does not do CSS, JavaScript or anything advanced: It's geared
28 ;; towards rendering typical short snippets of HTML, like what you'd
29 ;; find in HTML email and the like.
30
31 ;;; Code:
32
33 (defgroup shr nil
34   "Simple HTML Renderer"
35   :group 'mail)
36
37 (defcustom shr-max-image-proportion 0.9
38   "How big pictures displayed are in relation to the window they're in.
39 A value of 0.7 means that they are allowed to take up 70% of the
40 width and height of the window.  If they are larger than this,
41 and Emacs supports it, then the images will be rescaled down to
42 fit these criteria."
43   :version "24.1"
44   :group 'shr
45   :type 'float)
46
47 (defcustom shr-blocked-images nil
48   "Images that have URLs matching this regexp will be blocked."
49   :version "24.1"
50   :group 'shr
51   :type 'regexp)
52
53 (defvar shr-folding-mode nil)
54
55 (defvar shr-width 70)
56
57 (defun shr-transform-dom (dom)
58   (let ((result (list (pop dom))))
59     (dolist (arg (pop dom))
60       (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
61                   (cdr arg))
62             result))
63     (dolist (sub dom)
64       (if (stringp sub)
65           (push (cons :text sub) result)
66         (push (shr-transform-dom sub) result)))
67     (nreverse result)))
68
69 ;;;###autoload
70 (defun shr-insert-document (dom)
71   (shr-descend (shr-transform-dom dom)))
72
73 (defun shr-descend (dom)
74   (let ((function (intern (concat "shr-" (symbol-name (car dom))) obarray)))
75     (if (fboundp function)
76         (funcall function (cdr dom))
77       (shr-generic (cdr dom)))))
78
79 (defun shr-generic (cont)
80   (dolist (sub cont)
81     (cond
82      ((eq (car sub) :text)
83       (shr-insert (cdr sub)))
84      ((consp (cdr sub))
85       (shr-descend sub)))))
86
87 (defun shr-p (cont)
88   (shr-ensure-newline)
89   (insert "\n")
90   (shr-generic cont)
91   (insert "\n"))
92
93 (defun shr-b (cont)
94   (shr-fontize-cont cont 'bold))
95
96 (defun shr-i (cont)
97   (shr-fontize-cont cont 'italic))
98
99 (defun shr-u (cont)
100   (shr-fontize-cont cont 'underline))
101
102 (defun shr-s (cont)
103   (shr-fontize-cont cont 'strikethru))
104
105 (defun shr-fontize-cont (cont type)
106   (let ((start (point)))
107     (shr-generic cont)
108     (shr-add-font start (point) type)))
109
110 (defun shr-add-font (start end type)
111   (put-text-property start end 'face type))
112
113 (defun shr-a (cont)
114   (let ((start (point))
115         (url (cdr (assq :href cont))))
116     (shr-generic cont)
117     (widget-convert-button
118      'link start (point)
119      :action 'shr-browse-url
120      :url url
121      :keymap widget-keymap
122      :help-echo url)))
123
124 (defun shr-browse-url (widget &rest stuff)
125   (browse-url (widget-get widget :url)))
126
127 (defun shr-img (cont)
128   (let ((start (point-marker)))
129     (let ((alt (cdr (assq :alt cont)))
130           (url (cdr (assq :src cont))))
131       (when (zerop (length alt))
132         (setq alt "[img]"))
133       (cond
134        ((and shr-blocked-images
135              (string-match shr-blocked-images url))
136         (insert alt))
137        ((url-is-cached url)
138         (shr-put-image (shr-get-image-data url) (point) alt))
139        (t
140         (insert alt)
141         (url-retrieve url 'shr-image-fetched
142                       (list (current-buffer) start (point-marker))
143                       t)))
144       (insert " "))))
145
146 (defun shr-image-fetched (status buffer start end)
147   (when (and (buffer-name buffer)
148              (not (plist-get status :error)))
149     (url-store-in-cache (current-buffer))
150     (when (or (search-forward "\n\n" nil t)
151               (search-forward "\r\n\r\n" nil t))
152       (let ((data (buffer-substring (point) (point-max))))
153         (with-current-buffer buffer
154           (let ((alt (buffer-substring start end))
155                 (inhibit-read-only t))
156             (delete-region start end)
157             (shr-put-image data start alt))))))
158   (kill-buffer (current-buffer)))
159
160 (defun shr-put-image (data point alt)
161   (if (not (display-graphic-p))
162       (insert alt)
163     (let ((image (shr-rescale-image data)))
164       (put-image image point alt))))
165
166 (defun shr-rescale-image (data)
167   (if (or (not (fboundp 'imagemagick-types))
168           (not (get-buffer-window (current-buffer))))
169       (create-image data nil t)
170     (let* ((image (create-image data nil t))
171            (size (image-size image))
172            (width (car size))
173            (height (cdr size))
174            (edges (window-inside-pixel-edges
175                    (get-buffer-window (current-buffer))))
176            (window-width (truncate (* shr-max-image-proportion
177                                       (- (nth 2 edges) (nth 0 edges)))))
178            (window-height (truncate (* shr-max-image-proportion
179                                        (- (nth 3 edges) (nth 1 edges)))))
180            scaled-image)
181       (when (> height window-height)
182         (setq image (or (create-image data 'imagemagick t
183                                       :height window-height)
184                         image))
185         (setq size (image-size image t)))
186       (when (> (car size) window-width)
187         (setq image (or
188                      (create-image data 'imagemagick t
189                                    :width window-width)
190                      image)))
191       image)))
192
193 (defun shr-pre (cont)
194   (let ((shr-folding-mode nil))
195     (shr-ensure-newline)
196     (shr-generic cont)
197     (shr-ensure-newline)))
198
199 (defun shr-blockquote (cont)
200   (shr-pre cont))
201
202 (defun shr-ensure-newline ()
203   (unless (zerop (current-column))
204     (insert "\n")))
205
206 (defun shr-insert (text)
207   (cond
208    ((eq shr-folding-mode 'none)
209     (insert t))
210    (t
211     (let (column)
212       (dolist (elem (split-string text))
213         (setq column (current-column))
214         (if (zerop column)
215             (insert elem)
216           (if (> (+ column (length elem) 1) shr-width)
217               (insert "\n" elem)
218             (insert " " elem))))))))
219
220 (defun shr-get-image-data (url)
221   "Get image data for URL.
222 Return a string with image data."
223   (with-temp-buffer
224     (mm-disable-multibyte)
225     (url-cache-extract (url-cache-create-filename url))
226     (when (or (search-forward "\n\n" nil t)
227               (search-forward "\r\n\r\n" nil t))
228       (buffer-substring (point) (point-max)))))
229
230 (provide 'shr)
231
232 ;;; shr.el ends here