(shr-add-font): Use overlays for combining faces.
[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 (defvar shr-state nil)
55
56 (defvar shr-width 70)
57
58 (defun shr-transform-dom (dom)
59   (let ((result (list (pop dom))))
60     (dolist (arg (pop dom))
61       (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
62                   (cdr arg))
63             result))
64     (dolist (sub dom)
65       (if (stringp sub)
66           (push (cons :text sub) result)
67         (push (shr-transform-dom sub) result)))
68     (nreverse result)))
69
70 ;;;###autoload
71 (defun shr-insert-document (dom)
72   (let ((shr-state nil))
73     (shr-descend (shr-transform-dom dom))))
74
75 (defun shr-descend (dom)
76   (let ((function (intern (concat "shr-" (symbol-name (car dom))) obarray)))
77     (if (fboundp function)
78         (funcall function (cdr dom))
79       (shr-generic (cdr dom)))))
80
81 (defun shr-generic (cont)
82   (dolist (sub cont)
83     (cond
84      ((eq (car sub) :text)
85       (shr-insert (cdr sub)))
86      ((consp (cdr sub))
87       (shr-descend sub)))))
88
89 (defun shr-p (cont)
90   (shr-ensure-newline)
91   (insert "\n")
92   (shr-generic cont)
93   (insert "\n"))
94
95 (defun shr-b (cont)
96   (shr-fontize-cont cont 'bold))
97
98 (defun shr-i (cont)
99   (shr-fontize-cont cont 'italic))
100
101 (defun shr-u (cont)
102   (shr-fontize-cont cont 'underline))
103
104 (defun shr-s (cont)
105   (shr-fontize-cont cont 'strikethru))
106
107 (defun shr-fontize-cont (cont type)
108   (let ((start (point)))
109     (shr-generic cont)
110     (shr-add-font start (point) type)))
111
112 (defun shr-add-font (start end type)
113   (let ((overlay (make-overlay start end)))
114     (overlay-put overlay 'face type)))
115
116 (defun shr-a (cont)
117   (let ((start (point))
118         (url (cdr (assq :href cont))))
119     (shr-generic cont)
120     (widget-convert-button
121      'link start (point)
122      :action 'shr-browse-url
123      :url url
124      :keymap widget-keymap
125      :help-echo url)))
126
127 (defun shr-browse-url (widget &rest stuff)
128   (browse-url (widget-get widget :url)))
129
130 (defun shr-img (cont)
131   (let ((start (point-marker)))
132     (let ((alt (cdr (assq :alt cont)))
133           (url (cdr (assq :src cont))))
134       (when (zerop (length alt))
135         (setq alt "[img]"))
136       (cond
137        ((and shr-blocked-images
138              (string-match shr-blocked-images url))
139         (insert alt))
140        ((url-is-cached (browse-url-url-encode-chars url "[&)$ ]"))
141         (shr-put-image (shr-get-image-data url) (point) alt))
142        (t
143         (insert alt)
144         (url-retrieve url 'shr-image-fetched
145                       (list (current-buffer) start (point-marker))
146                       t)))
147       (insert " ")
148       (setq shr-state 'image))))
149
150 (defun shr-image-fetched (status buffer start end)
151   (when (and (buffer-name buffer)
152              (not (plist-get status :error)))
153     (url-store-in-cache (current-buffer))
154     (when (or (search-forward "\n\n" nil t)
155               (search-forward "\r\n\r\n" nil t))
156       (let ((data (buffer-substring (point) (point-max))))
157         (with-current-buffer buffer
158           (let ((alt (buffer-substring start end))
159                 (inhibit-read-only t))
160             (delete-region start end)
161             (shr-put-image data start alt))))))
162   (kill-buffer (current-buffer)))
163
164 (defun shr-put-image (data point alt)
165   (if (not (display-graphic-p))
166       (insert alt)
167     (let ((image (shr-rescale-image data)))
168       (put-image image point alt))))
169
170 (defun shr-rescale-image (data)
171   (if (or (not (fboundp 'imagemagick-types))
172           (not (get-buffer-window (current-buffer))))
173       (create-image data nil t)
174     (let* ((image (create-image data nil t))
175            (size (image-size image))
176            (width (car size))
177            (height (cdr size))
178            (edges (window-inside-pixel-edges
179                    (get-buffer-window (current-buffer))))
180            (window-width (truncate (* shr-max-image-proportion
181                                       (- (nth 2 edges) (nth 0 edges)))))
182            (window-height (truncate (* shr-max-image-proportion
183                                        (- (nth 3 edges) (nth 1 edges)))))
184            scaled-image)
185       (when (> height window-height)
186         (setq image (or (create-image data 'imagemagick t
187                                       :height window-height)
188                         image))
189         (setq size (image-size image t)))
190       (when (> (car size) window-width)
191         (setq image (or
192                      (create-image data 'imagemagick t
193                                    :width window-width)
194                      image)))
195       image)))
196
197 (defun shr-pre (cont)
198   (let ((shr-folding-mode nil))
199     (shr-ensure-newline)
200     (shr-generic cont)
201     (shr-ensure-newline)))
202
203 (defun shr-blockquote (cont)
204   (shr-pre cont))
205
206 (defun shr-ensure-newline ()
207   (unless (zerop (current-column))
208     (insert "\n")))
209
210 (defun shr-insert (text)
211   (when (eq shr-state 'image)
212     (insert "\n")
213     (setq shr-state nil))
214   (cond
215    ((eq shr-folding-mode 'none)
216     (insert t))
217    (t
218     (let (column)
219       (dolist (elem (split-string text))
220         (setq column (current-column))
221         (if (zerop column)
222             (insert elem)
223           (if (> (+ column (length elem) 1) shr-width)
224               (insert "\n" elem)
225             (insert " " elem))))))))
226
227 (defun shr-get-image-data (url)
228   "Get image data for URL.
229 Return a string with image data."
230   (with-temp-buffer
231     (mm-disable-multibyte)
232     (url-cache-extract (url-cache-create-filename url))
233     (when (or (search-forward "\n\n" nil t)
234               (search-forward "\r\n\r\n" nil t))
235       (buffer-substring (point) (point-max)))))
236
237 (provide 'shr)
238
239 ;;; shr.el ends here