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 (let ((shr-folding-mode nil))
77 (shr-ensure-newline)))
79 (defun shr-blockquote (cont)
82 (defun shr-ensure-newline ()
83 (unless (zerop (current-column))
86 (defun shr-insert (text)
88 ((eq shr-folding-mode 'none)
92 (dolist (elem (split-string text))
93 (setq column (current-column))
96 (if (> (+ column (length elem) 1) shr-width)
98 (insert " " elem))))))))