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