1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
34 "Simple HTML Renderer"
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
47 (defcustom shr-blocked-images nil
48 "Images that have URLs matching this regexp will be blocked."
53 (defvar shr-folding-mode nil)
54 (defvar shr-state nil)
55 (defvar shr-start nil)
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)
67 (push (cons :text sub) result)
68 (push (shr-transform-dom sub) result)))
72 (defun shr-insert-document (dom)
75 (shr-descend (shr-transform-dom dom))))
77 (defun shr-descend (dom)
78 (let ((function (intern (concat "shr-" (symbol-name (car dom))) obarray)))
79 (if (fboundp function)
80 (funcall function (cdr dom))
81 (shr-generic (cdr dom)))))
83 (defun shr-generic (cont)
87 (shr-insert (cdr sub)))
98 (shr-fontize-cont cont 'bold))
101 (shr-fontize-cont cont 'italic))
104 (shr-fontize-cont cont 'underline))
107 (shr-fontize-cont cont 'strikethru))
109 (defun shr-fontize-cont (cont type)
112 (shr-add-font shr-start (point) type)))
114 (defun shr-add-font (start end type)
115 (let ((overlay (make-overlay start end)))
116 (overlay-put overlay 'face type)))
119 (let ((url (cdr (assq :href cont)))
122 (widget-convert-button
123 'link shr-start (point)
124 :action 'shr-browse-url
126 :keymap widget-keymap
129 (defun shr-browse-url (widget &rest stuff)
130 (browse-url (widget-get widget :url)))
132 (defun shr-img (cont)
133 (let ((start (point-marker)))
134 (let ((alt (cdr (assq :alt cont)))
135 (url (cdr (assq :src cont))))
136 (when (zerop (length alt))
139 ((and shr-blocked-images
140 (string-match shr-blocked-images url))
142 ((url-is-cached (browse-url-url-encode-chars url "[&)$ ]"))
143 (shr-put-image (shr-get-image-data url) (point) alt))
146 (url-retrieve url 'shr-image-fetched
147 (list (current-buffer) start (point-marker))
150 (setq shr-state 'image))))
152 (defun shr-image-fetched (status buffer start end)
153 (when (and (buffer-name buffer)
154 (not (plist-get status :error)))
155 (url-store-in-cache (current-buffer))
156 (when (or (search-forward "\n\n" nil t)
157 (search-forward "\r\n\r\n" nil t))
158 (let ((data (buffer-substring (point) (point-max))))
159 (with-current-buffer buffer
160 (let ((alt (buffer-substring start end))
161 (inhibit-read-only t))
162 (delete-region start end)
163 (shr-put-image data start alt))))))
164 (kill-buffer (current-buffer)))
166 (defun shr-put-image (data point alt)
167 (if (not (display-graphic-p))
169 (let ((image (shr-rescale-image data)))
170 (put-image image point alt))))
172 (defun shr-rescale-image (data)
173 (if (or (not (fboundp 'imagemagick-types))
174 (not (get-buffer-window (current-buffer))))
175 (create-image data nil t)
176 (let* ((image (create-image data nil t))
177 (size (image-size image))
180 (edges (window-inside-pixel-edges
181 (get-buffer-window (current-buffer))))
182 (window-width (truncate (* shr-max-image-proportion
183 (- (nth 2 edges) (nth 0 edges)))))
184 (window-height (truncate (* shr-max-image-proportion
185 (- (nth 3 edges) (nth 1 edges)))))
187 (when (> height window-height)
188 (setq image (or (create-image data 'imagemagick t
189 :height window-height)
191 (setq size (image-size image t)))
192 (when (> (car size) window-width)
194 (create-image data 'imagemagick t
199 (defun shr-pre (cont)
200 (let ((shr-folding-mode nil))
203 (shr-ensure-newline)))
205 (defun shr-blockquote (cont)
208 (defun shr-ensure-newline ()
209 (unless (zerop (current-column))
212 (defun shr-insert (text)
213 (when (eq shr-state 'image)
215 (setq shr-state nil))
217 ((eq shr-folding-mode 'none)
221 (dolist (elem (split-string text))
222 (setq column (current-column))
224 (if (> (+ column (length elem) 1) shr-width)
227 ;; The shr-start is a special variable that is used to pass
228 ;; upwards the first point in the buffer where the text really
231 (setq shr-start (point)))
234 (defun shr-get-image-data (url)
235 "Get image data for URL.
236 Return a string with image data."
238 (mm-disable-multibyte)
239 (url-cache-extract (url-cache-create-filename url))
240 (when (or (search-forward "\n\n" nil t)
241 (search-forward "\r\n\r\n" nil t))
242 (buffer-substring (point) (point-max)))))