a5a0ae2387fe4b92b0eabe1e1bbac98cf01ae4c8
[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 images)
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         (when (string-match "src=\"\\([^\"]+\\)" parameters)
61           (setq parameters (match-string 1 parameters))
62           (let ((file (gnus-html-image-id parameters)))
63             (if (file-exists-p file)
64                 ;; It's already cached, so just insert it.
65                 (progn
66                   (put-image (create-image file) (point))
67                   ;; Delete the ALT text.
68                   (delete-region start end))
69               ;; We don't have it, so schedule it for fetching
70               ;; asynchronously.
71               (push (list parameters
72                           (set-marker (make-marker) start)
73                           (point-marker))
74                     images)))))
75        ;; Add a link.
76        ((equal tag "a")
77         (when (string-match "href=\"\\([^\"]+\\)" parameters)
78           (setq parameters (match-string 1 parameters))
79           (gnus-article-add-button start end
80                                    'browse-url parameters)
81           (let ((overlay (gnus-make-overlay start end)))
82             (gnus-overlay-put overlay 'evaporate t)
83             (gnus-overlay-put overlay 'gnus-button-url parameters)
84             (when gnus-article-mouse-face
85               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
86        ;; Whatever.  Just ignore the tag.
87        (t
88         ))
89       (goto-char start))
90     (when images
91       (gnus-html-schedule-image-fetching (current-buffer) images))))
92
93 (defun gnus-html-schedule-image-fetching (buffer images)
94   (let* ((url (caar images))
95          (process (start-process
96                    "images" nil "curl"
97                    "-s" "--create-dirs"
98                    "-o" (gnus-html-image-id url)
99                    url)))
100     (set-process-sentinel process 'gnus-html-curl-sentinel)
101     (set-process-plist process (list 'images images
102                                      'buffer buffer))))
103
104 (defun gnus-html-image-id (url)
105   (expand-file-name (sha1 url) "~/News/html-cache/"))
106
107 (defun gnus-html-curl-sentinel (process event)
108   (when (string-match "finished" event)
109     (let* ((images (getf (process-plist process) 'images))
110            (buffer (getf (process-plist process) 'buffer))
111            (spec (pop images))
112            (file (gnus-html-image-id (car spec))))
113       (when (file-exists-p file)
114         (save-excursion
115           (set-buffer buffer)
116           (let ((buffer-read-only nil))
117             (delete-region (cadr spec) (caddr spec))
118             (put-image (create-image file) (cadr spec)))))
119       (when images
120         (gnus-html-schedule-image-fetching buffer images)))))
121
122 ;;; gnus-html.el ends here