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)
57 (defun shr-transform-dom (dom)
58 (let ((result (list (pop dom))))
59 (dolist (arg (pop dom))
60 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
65 (push (cons :text sub) result)
66 (push (shr-transform-dom sub) result)))
70 (defun shr-insert-document (dom)
71 (shr-descend (shr-transform-dom dom)))
73 (defun shr-descend (dom)
74 (let ((function (intern (concat "shr-" (symbol-name (car dom))) obarray)))
75 (if (fboundp function)
76 (funcall function (cdr dom))
77 (shr-generic (cdr dom)))))
79 (defun shr-generic (cont)
83 (shr-insert (cdr sub)))
94 (shr-fontize-cont cont 'bold))
97 (shr-fontize-cont cont 'italic))
100 (shr-fontize-cont cont 'underline))
103 (shr-fontize-cont cont 'strikethru))
105 (defun shr-fontize-cont (cont type)
106 (let ((start (point)))
108 (shr-add-font start (point) type)))
110 (defun shr-add-font (start end type)
111 (put-text-property start end 'face type))
114 (let ((start (point))
115 (url (cdr (assq :href cont))))
117 (widget-convert-button
119 :action 'shr-browse-url
121 :keymap widget-keymap
124 (defun shr-browse-url (widget &rest stuff)
125 (browse-url (widget-get widget :url)))
127 (defun shr-img (cont)
128 (let ((start (point-marker)))
129 (let ((alt (cdr (assq :alt cont)))
130 (url (cdr (assq :src cont))))
131 (when (zerop (length alt))
134 ((and shr-blocked-images
135 (string-match shr-blocked-images url))
138 (shr-put-image (shr-get-image-data url) (point) alt))
141 (url-retrieve url 'shr-image-fetched
142 (list (current-buffer) start (point-marker))
146 (defun shr-image-fetched (status buffer start end)
147 (when (and (buffer-name buffer)
148 (not (plist-get status :error)))
149 (url-store-in-cache (current-buffer))
150 (when (or (search-forward "\n\n" nil t)
151 (search-forward "\r\n\r\n" nil t))
152 (let ((data (buffer-substring (point) (point-max))))
153 (with-current-buffer buffer
154 (let ((alt (buffer-substring start end))
155 (inhibit-read-only t))
156 (delete-region start end)
157 (shr-put-image data start alt))))))
158 (kill-buffer (current-buffer)))
160 (defun shr-put-image (data point alt)
161 (if (not (display-graphic-p))
163 (let ((image (shr-rescale-image data)))
164 (put-image image point alt))))
166 (defun shr-rescale-image (data)
167 (if (or (not (fboundp 'imagemagick-types))
168 (not (get-buffer-window (current-buffer))))
169 (create-image data nil t)
170 (let* ((image (create-image data nil t))
171 (size (image-size image))
174 (edges (window-inside-pixel-edges
175 (get-buffer-window (current-buffer))))
176 (window-width (truncate (* shr-max-image-proportion
177 (- (nth 2 edges) (nth 0 edges)))))
178 (window-height (truncate (* shr-max-image-proportion
179 (- (nth 3 edges) (nth 1 edges)))))
181 (when (> height window-height)
182 (setq image (or (create-image data 'imagemagick t
183 :height window-height)
185 (setq size (image-size image t)))
186 (when (> (car size) window-width)
188 (create-image data 'imagemagick t
193 (defun shr-pre (cont)
194 (let ((shr-folding-mode nil))
197 (shr-ensure-newline)))
199 (defun shr-blockquote (cont)
202 (defun shr-ensure-newline ()
203 (unless (zerop (current-column))
206 (defun shr-insert (text)
208 ((eq shr-folding-mode 'none)
212 (dolist (elem (split-string text))
213 (setq column (current-column))
216 (if (> (+ column (length elem) 1) shr-width)
218 (insert " " elem))))))))
220 (defun shr-get-image-data (url)
221 "Get image data for URL.
222 Return a string with image data."
224 (mm-disable-multibyte)
225 (url-cache-extract (url-cache-create-filename url))
226 (when (or (search-forward "\n\n" nil t)
227 (search-forward "\r\n\r\n" nil t))
228 (buffer-substring (point) (point-max)))))