2b26a79fa4bbb4c4564eeb00f51f23ffddfedaf2
[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 (defun shr-insert-document (dom)
70   (setq dom (shr-transform-dom dom))
71   (shr-descend 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       (insert " "))))
144
145 (defun shr-image-fetched (status buffer start end)
146   (when (and (buffer-name buffer)
147              (not (plist-get status :error)))
148     (url-store-in-cache (current-buffer))
149     (when (or (search-forward "\n\n" nil t)
150               (search-forward "\r\n\r\n" nil t))
151       (let ((data (buffer-substring (point) (point-max))))
152         (with-current-buffer buffer
153           (let ((alt (buffer-substring start end))
154                 (inhibit-read-only t))
155             (delete-region start end)
156             (shr-put-image data start alt))))))
157   (kill-buffer (current-buffer)))
158
159 (defun shr-put-image (data point alt)
160   (if (not (display-graphic-p))
161       (insert alt)
162     (let ((image (shr-rescale-image data)))
163       (put-image image point alt))))
164
165 (defun shr-rescale-image (data)
166   (if (or (not (fboundp 'imagemagick-types))
167           (not (get-buffer-window (current-buffer))))
168       (create-image data nil t)
169     (let* ((image (create-image data nil t))
170            (size (image-size image))
171            (width (car size))
172            (height (cdr size))
173            (edges (window-inside-pixel-edges
174                    (get-buffer-window (current-buffer))))
175            (window-width (truncate (* shr-max-image-proportion
176                                       (- (nth 2 edges) (nth 0 edges)))))
177            (window-height (truncate (* shr-max-image-proportion
178                                        (- (nth 3 edges) (nth 1 edges)))))
179            scaled-image)
180       (when (> height window-height)
181         (setq image (or (create-image data 'imagemagick t
182                                       :height window-height)
183                         image))
184         (setq size (image-size image t)))
185       (when (> (car size) window-width)
186         (setq image (or
187                      (create-image data 'imagemagick t
188                                    :width window-width)
189                      image)))
190       image)))
191
192 (defun shr-pre (cont)
193   (let ((shr-folding-mode nil))
194     (shr-ensure-newline)
195     (shr-generic cont)
196     (shr-ensure-newline)))
197
198 (defun shr-blockquote (cont)
199   (shr-pre cont))
200
201 (defun shr-ensure-newline ()
202   (unless (zerop (current-column))
203     (insert "\n")))
204
205 (defun shr-insert (text)
206   (cond
207    ((eq shr-folding-mode 'none)
208     (insert t))
209    (t
210     (let (column)
211       (dolist (elem (split-string text))
212         (setq column (current-column))
213         (if (zerop column)
214             (insert elem)
215           (if (> (+ column (length elem) 1) shr-width)
216               (insert "\n" elem)
217             (insert " " elem))))))))
218
219 (defun shr-get-image-data (url)
220   "Get image data for URL.
221 Return a string with image data."
222   (with-temp-buffer
223     (mm-disable-multibyte)
224     (url-cache-extract (url-cache-create-filename url))
225     (when (or (search-forward "\n\n" nil t)
226               (search-forward "\r\n\r\n" nil t))
227       (buffer-substring (point) (point-max)))))
228
229 (provide 'shr)
230
231 ;;; shr.el ends here