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.
33 (defvar shr-folding-mode nil)
37 (defun shr-transform-dom (dom)
38 (let ((result (list (pop dom))))
39 (dolist (arg (pop dom))
40 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
45 (push (cons :text sub) result)
46 (push (shr-transform-dom sub) result)))
49 (defun shr-insert-document (dom)
50 (setq dom (shr-transform-dom dom))
53 (defun shr-descend (dom)
54 (let ((function (intern (concat "shr-" (symbol-name (car dom))) obarray)))
55 (if (fboundp function)
56 (funcall function (cdr dom))
57 (shr-generic (cdr dom)))))
59 (defun shr-generic (cont)
63 (shr-insert (cdr sub)))
74 (shr-fontize-cont cont 'bold))
77 (shr-fontize-cont cont 'italic))
80 (shr-fontize-cont cont 'underline))
83 (shr-fontize-cont cont 'strikethru))
85 (defun shr-fontize-cont (cont type)
86 (let ((start (point)))
88 (shr-add-font start (point) type)))
90 (defun shr-add-font (start end type)
91 (put-text-property start end 'face type))
95 (let ((start (point-marker)))
96 (let ((alt (or (cdr (assq :alt cont)) "[img]"))
97 (url (cdr (assq :src cont))))
98 (if (url-is-cached url)
99 (shr-put-image (shr-get-image-data url) (point) alt)
101 (url-retrieve url 'shr-image-fetched
102 (list (current-buffer) start (point-marker)))))
103 (shr-ensure-newline)))
105 (defun shr-image-fetched (status buffer start end)
106 (when (and (buffer-name buffer)
107 (not (plist-get status :error)))
108 (url-store-in-cache (current-buffer))
109 (when (or (search-forward "\n\n" nil t)
110 (search-forward "\r\n\r\n" nil t))
111 (let ((data (buffer-substring (point) (point-max))))
112 (with-current-buffer buffer
113 (let ((alt (buffer-substring start end))
114 (inhibit-read-only t))
115 (delete-region start end)
116 (shr-put-image data start alt))))))
117 (kill-buffer (current-buffer)))
119 (defun shr-put-image (data point alt)
120 (if (not (display-graphic-p))
122 (let ((image (create-image data nil t)))
123 (put-image image point alt))))
125 (defun shr-pre (cont)
126 (let ((shr-folding-mode nil))
129 (shr-ensure-newline)))
131 (defun shr-blockquote (cont)
134 (defun shr-ensure-newline ()
135 (unless (zerop (current-column))
138 (defun shr-insert (text)
140 ((eq shr-folding-mode 'none)
144 (dolist (elem (split-string text))
145 (setq column (current-column))
148 (if (> (+ column (length elem) 1) shr-width)
150 (insert " " elem))))))))
152 (defun shr-get-image-data (url)
153 "Get image data for URL.
154 Return a string with image data."
156 (mm-disable-multibyte)
157 (url-cache-extract (url-cache-create-filename url))
158 (when (or (search-forward "\n\n" nil t)
159 (search-forward "\r\n\r\n" nil t))
160 (buffer-substring (point) (point-max)))))