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