1 ;;; gnus-html.el --- Quoted-Printable functions
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 ;; 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).
32 (defun gnus-article-html (handle)
33 (let ((article-buffer (current-buffer)))
35 (narrow-to-region (1- (point)) (point))
37 (set-buffer (car handle))
38 (call-process-region (point-min) (point-max)
40 nil article-buffer nil
43 (gnus-html-wash-tags))))
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)))
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.
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
71 (push (list parameters
72 (set-marker (make-marker) start)
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.
91 (gnus-html-schedule-image-fetching (current-buffer) images))))
93 (defun gnus-html-schedule-image-fetching (buffer images)
94 (let* ((url (caar images))
95 (process (start-process
98 "-o" (gnus-html-image-id url)
100 (set-process-sentinel process 'gnus-html-curl-sentinel)
101 (set-process-plist process (list 'images images
104 (defun gnus-html-image-id (url)
105 (expand-file-name (sha1 url) "~/News/html-cache/"))
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))
112 (file (gnus-html-image-id (car spec))))
113 (when (file-exists-p file)
116 (let ((buffer-read-only nil))
117 (delete-region (cadr spec) (caddr spec))
118 (put-image (create-image file) (cadr spec)))))
120 (gnus-html-schedule-image-fetching buffer images)))))
122 ;;; gnus-html.el ends here