Continue implementation.
[gnus] / lisp / shr.el
1 ;;; shr.el --- Simple HTML Renderer
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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.
30
31 ;;; Code:
32
33 (defvar shr-folding-mode nil)
34
35 (defvar shr-width 70)
36
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)
41                   (cdr arg))
42             result))
43     (dolist (sub dom)
44       (if (stringp sub)
45           (push (cons :text sub) result)
46         (push (shr-transform-dom sub) result)))
47     (nreverse result)))
48
49 (defun shr-insert-document (dom)
50   (setq dom (shr-transform-dom dom))
51   (shr-descend dom))
52
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)))))
58
59 (defun shr-generic (cont)
60   (dolist (sub cont)
61     (cond
62      ((eq (car sub) :text)
63       (shr-insert (cdr sub)))
64      ((consp (cdr sub))
65       (shr-descend sub)))))
66
67 (defun shr-p (cont)
68   (shr-ensure-newline)
69   (insert "\n")
70   (shr-generic cont)
71   (insert "\n"))
72
73 (defun shr-b (cont)
74   (shr-fontize-cont cont 'bold))
75
76 (defun shr-i (cont)
77   (shr-fontize-cont cont 'italic))
78
79 (defun shr-u (cont)
80   (shr-fontize-cont cont 'underline))
81
82 (defun shr-s (cont)
83   (shr-fontize-cont cont 'strikethru))
84
85 (defun shr-fontize-cont (cont type)
86   (let ((start (point)))
87     (shr-generic cont)
88     (shr-add-font start (point) type)))
89
90 (defun shr-add-font (start end type)
91   (put-text-property start end 'face type))
92
93 (defun shr-img (cont)
94   (shr-ensure-newline)
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)
100         (insert alt)
101         (url-retrieve url 'shr-image-fetched
102                       (list (current-buffer) start (point-marker)))))
103     (shr-ensure-newline)))
104
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)))
118
119 (defun shr-put-image (data point alt)
120   (if (not (display-graphic-p))
121       (insert alt)
122     (let ((image (create-image data nil t)))
123       (put-image image point alt))))
124
125 (defun shr-pre (cont)
126   (let ((shr-folding-mode nil))
127     (shr-ensure-newline)
128     (shr-generic cont)
129     (shr-ensure-newline)))
130
131 (defun shr-blockquote (cont)
132   (shr-pre cont))
133
134 (defun shr-ensure-newline ()
135   (unless (zerop (current-column))
136     (insert "\n")))
137
138 (defun shr-insert (text)
139   (cond
140    ((eq shr-folding-mode 'none)
141     (insert t))
142    (t
143     (let (column)
144       (dolist (elem (split-string text))
145         (setq column (current-column))
146         (if (zerop column)
147             (insert elem)
148           (if (> (+ column (length elem) 1) shr-width)
149               (insert "\n" elem)
150             (insert " " elem))))))))
151
152 (defun shr-get-image-data (url)
153   "Get image data for URL.
154 Return a string with image data."
155   (with-temp-buffer
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)))))
161
162 (provide 'shr)
163
164 ;;; shr.el ends here