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