The -halfdump format uses nested tags.
[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 (1- (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             start (match-beginning 0))
53       (delete-region start (point))
54       (when (search-forward (concat "</" tag ">"))
55         (delete-region (match-beginning 0) (match-end 0)))
56       (setq end (point))
57       (cond
58        ;; Fetch and insert a picture.
59        ((equal tag "img_alt")
60         ;;
61         )
62        ;; Add a link.
63        ((equal tag "a")
64         (when (string-match "href=\"\\([^\"]+\\)" parameters)
65           (setq parameters (match-string 1 parameters))
66           (gnus-article-add-button start end
67                                    'browse-url parameters)
68           (let ((overlay (gnus-make-overlay start end)))
69             (gnus-overlay-put overlay 'evaporate t)
70             (gnus-overlay-put overlay 'gnus-button-url parameters)
71             (when gnus-article-mouse-face
72               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
73        ;; Whatever.  Just ignore the tag.
74        (t
75         ))
76       (goto-char start))))
77
78 ;;; gnus-html.el ends here