Add a new super-simple HTML renderer based on w3m -halfdump.
[gnus] / lisp / gnus-html.el
1 ;;; gnus-html.el --- Quoted-Printable functions
2
3 ;; Copyright (C) 2010  Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html, web
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 ;; The idea is to provide a simple, fast and pretty minimal way to
26 ;; render HTML (including links and images) in a buffer, based on an
27 ;; external HTML renderer (i.e., w3m).
28
29 ;;; Code:
30
31 ;;;###autoload
32 (defun gnus-article-html (handle)
33   (let ((article-buffer (current-buffer)))
34     (save-restriction
35       (narrow-to-region (point) (point))
36       (save-excursion
37         (set-buffer (car handle))
38         (call-process-region (point-min) (point-max)
39                              "w3m" 
40                              nil article-buffer nil
41                              "-halfdump"
42                              "-T" "text/html"))
43       (gnus-html-wash-tags))))
44
45 (defun gnus-html-wash-tags ()
46   (let (tag parameters string start end)
47     ;;(subst-char-in-region (point-min) (point-max) ?_ ? )
48     (goto-char (point-min))
49     (while (re-search-forward "<\\([^ ]+\\)\\([^>]*\\)>\\([^<]*\\)<[^>]*>" nil t)
50       (setq tag (match-string 1)
51             parameters (match-string 2)
52             string (match-string 3)
53             start (match-beginning 0)
54             end (+ start (length string)))
55       (replace-match string)
56       (cond
57        ;; Fetch and insert a picture.
58        ((equal tag "img_alt")
59         ;;
60         )
61        ;; Add a link.
62        ((equal tag "a")
63         (when (string-match "href=\"\\([^\"]+\\)" parameters)
64           (setq parameters (match-string 1 parameters))
65           (gnus-article-add-button start end
66                                    'browse-url parameters)
67           (let ((overlay (gnus-make-overlay start end)))
68             (gnus-overlay-put overlay 'evaporate t)
69             (gnus-overlay-put overlay 'gnus-button-url parameters)
70             (when gnus-article-mouse-face
71               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
72        ;; Whatever.  Just ignore the tag.
73        (t
74         (replace-match string))))))
75
76 ;;; gnus-html.el ends here