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