1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2011 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 (eval-when-compile (require 'cl))
37 "Simple HTML Renderer"
40 (defcustom shr-max-image-proportion 0.9
41 "How big pictures displayed are in relation to the window they're in.
42 A value of 0.7 means that they are allowed to take up 70% of the
43 width and height of the window. If they are larger than this,
44 and Emacs supports it, then the images will be rescaled down to
50 (defcustom shr-blocked-images nil
51 "Images that have URLs matching this regexp will be blocked."
56 (defcustom shr-table-horizontal-line ?-
57 "Character used to draw horizontal table lines."
61 (defcustom shr-table-vertical-line ?|
62 "Character used to draw vertical table lines."
66 (defcustom shr-table-corner ?+
67 "Character used to draw table corners."
71 (defcustom shr-hr-line ?-
72 "Character used to draw hr lines."
76 (defcustom shr-width fill-column
77 "Frame width to use for rendering.
78 May either be an integer specifying a fixed width in characters,
79 or nil, meaning that the full width of the window should be
81 :type '(choice (integer :tag "Fixed width in characters")
82 (const :tag "Use the width of the window" nil))
85 (defvar shr-content-function nil
86 "If bound, this should be a function that will return the content.
87 This is used for cid: URLs, and the function is called with the
88 cid: URL as the argument.")
90 ;;; Internal variables.
92 (defvar shr-folding-mode nil)
93 (defvar shr-state nil)
94 (defvar shr-start nil)
95 (defvar shr-indentation 0)
96 (defvar shr-inhibit-images nil)
97 (defvar shr-list-mode nil)
98 (defvar shr-content-cache nil)
99 (defvar shr-kinsoku-shorten nil)
100 (defvar shr-table-depth 0)
101 (defvar shr-stylesheet nil)
104 (let ((map (make-sparse-keymap)))
105 (define-key map "a" 'shr-show-alt-text)
106 (define-key map "i" 'shr-browse-image)
107 (define-key map "I" 'shr-insert-image)
108 (define-key map "u" 'shr-copy-url)
109 (define-key map "v" 'shr-browse-url)
110 (define-key map "o" 'shr-save-contents)
111 (define-key map "\r" 'shr-browse-url)
114 ;; Public functions and commands.
117 (defun shr-insert-document (dom)
118 (setq shr-content-cache nil)
119 (let ((shr-state nil)
121 (shr-width (or shr-width (window-width))))
122 (shr-descend (shr-transform-dom dom))))
124 (defun shr-copy-url ()
125 "Copy the URL under point to the kill ring.
126 If called twice, then try to fetch the URL and see whether it
127 redirects somewhere else."
129 (let ((url (get-text-property (point) 'shr-url)))
132 (message "No URL under point"))
133 ;; Resolve redirected URLs.
134 ((equal url (car kill-ring))
139 (eq (car a) :redirect))
142 (goto-char (point-min))
143 ;; Remove common tracking junk from the URL.
144 (when (re-search-forward ".utm_.*" nil t)
145 (replace-match "" t t))
146 (message "Copied %s" (buffer-string))
147 (copy-region-as-kill (point-min) (point-max)))))))
148 ;; Copy the URL to the kill ring.
152 (copy-region-as-kill (point-min) (point-max))
153 (message "Copied %s" url))))))
155 (defun shr-show-alt-text ()
156 "Show the ALT text of the image under point."
158 (let ((text (get-text-property (point) 'shr-alt)))