Tell w3m that the input is UTF-8.
[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 (defcustom gnus-html-cache-directory (nnheader-concat gnus-directory "html-cache/")
32   "Where Gnus will cache images it downloads from the web."
33   :group 'gnus-art
34   :type 'directory)
35
36 (defcustom gnus-html-cache-size 500000000
37   "The size of the Gnus image cache."
38   :group 'gnus-art
39   :type 'integer)
40
41 (defcustom gnus-html-frame-width 70
42   "What width to use when rendering HTML."
43   :group 'gnus-art
44   :type 'integer)
45
46 ;;;###autoload
47 (defun gnus-article-html (handle)
48   (let ((article-buffer (current-buffer)))
49     (save-restriction
50       (narrow-to-region (point) (point))
51       (save-excursion
52         (mm-with-part handle
53           (let* ((coding-system-for-read 'utf-8)
54                  (coding-system-for-write 'utf-8)
55                  (default-process-coding-system
56                    (cons coding-system-for-read coding-system-for-write)))
57             (call-process-region (point-min) (point-max)
58                                  "w3m" 
59                                  nil article-buffer nil
60                                  "-halfdump"
61                                  "-no-cookie"
62                                  "-I" "UTF-8"
63                                  "-O" "UTF-8"
64                                  "-o" "ext_halfdump=1"
65                                  "-o" "pre_conv=1"
66                                  "-t" (format "%s" tab-width)
67                                  "-cols" (format "%s" gnus-html-frame-width)
68                                  "-o" "display_image=off"
69                                  "-T" "text/html"))))
70       (gnus-html-wash-tags))))
71
72 (defun gnus-html-wash-tags ()
73   (let (tag parameters string start end images)
74     (mm-url-decode-entities)
75     (goto-char (point-min))
76     (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
77       (setq tag (match-string 1)
78             parameters (match-string 2)
79             start (match-beginning 0))
80       (when (plusp (length parameters))
81         (set-text-properties 0 (1- (length parameters)) nil parameters))
82       (delete-region start (point))
83       (when (search-forward (concat "</" tag ">") nil t)
84         (delete-region (match-beginning 0) (match-end 0)))
85       (setq end (point))
86       (cond
87        ;; Fetch and insert a picture.
88        ((equal tag "img_alt")
89         (when (string-match "src=\"\\([^\"]+\\)" parameters)
90           (setq parameters (match-string 1 parameters))
91           (when (or (null mm-w3m-safe-url-regexp)
92                     (string-match mm-w3m-safe-url-regexp parameters))
93             (let ((file (gnus-html-image-id parameters)))
94               (if (file-exists-p file)
95                   ;; It's already cached, so just insert it.
96                   (when (gnus-html-put-image file (point))
97                     ;; Delete the ALT text.
98                     (delete-region start end))
99                 ;; We don't have it, so schedule it for fetching
100                 ;; asynchronously.
101                 (push (list parameters
102                             (set-marker (make-marker) start)
103                             (point-marker))
104                       images))))))
105        ;; Add a link.
106        ((equal tag "a")
107         (when (string-match "href=\"\\([^\"]+\\)" parameters)
108           (setq parameters (match-string 1 parameters))
109           (gnus-article-add-button start end
110                                    'browse-url parameters
111                                    parameters)
112           (let ((overlay (gnus-make-overlay start end)))
113             (gnus-overlay-put overlay 'evaporate t)
114             (gnus-overlay-put overlay 'gnus-button-url parameters)
115             (when gnus-article-mouse-face
116               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
117        ;; Whatever.  Just ignore the tag.
118        (t
119         ))
120       (goto-char start))
121     (goto-char (point-min))
122     ;; The output from -halfdump isn't totally regular, so strip
123     ;; off any </pre_int>s that were left over.
124     (while (re-search-forward "</pre_int>" nil t)
125       (replace-match "" t t))
126     (when images
127       (gnus-html-schedule-image-fetching (current-buffer) (nreverse images)))))
128
129 (defun gnus-html-schedule-image-fetching (buffer images)
130   (let* ((url (caar images))
131          (process (start-process
132                    "images" nil "curl"
133                    "-s" "--create-dirs"
134                    "--location"
135                    "--max-time" "60"
136                    "-o" (gnus-html-image-id url)
137                    url)))
138     (process-kill-without-query process)
139     (set-process-sentinel process 'gnus-html-curl-sentinel)
140     (set-process-plist process (list 'images images
141                                      'buffer buffer))))
142
143 (defun gnus-html-image-id (url)
144   (expand-file-name (sha1 url) gnus-html-cache-directory))
145
146 (defun gnus-html-curl-sentinel (process event)
147   (when (string-match "finished" event)
148     (let* ((images (getf (process-plist process) 'images))
149            (buffer (getf (process-plist process) 'buffer))
150            (spec (pop images))
151            (file (gnus-html-image-id (car spec))))
152       (when (and (buffer-live-p buffer)
153                  ;; If the position of the marker is 1, then that
154                  ;; means that the text is was in has been deleted;
155                  ;; i.e., that the user has selected a different
156                  ;; article before the image arrived.
157                  (not (= (marker-position (cadr spec)) 1)))
158         (save-excursion
159           (set-buffer buffer)
160           (let ((buffer-read-only nil))
161             (when (gnus-html-put-image file (cadr spec))
162               (delete-region (1+ (cadr spec)) (caddr spec))))))
163       (when images
164         (gnus-html-schedule-image-fetching buffer images)))))
165
166 (defun gnus-html-put-image (file point)
167   (when (display-graphic-p)
168     (let ((image (ignore-errors
169                    (gnus-create-image file))))
170       (save-excursion
171         (goto-char point)
172         (if (and image
173                  ;; Kludge to avoid displaying 30x30 gif images, which
174                  ;; seems to be a signal of a broken image.
175                  (not (and (listp image)
176                            (eq (getf (cdr image) :type) 'gif)
177                            (= (car (image-size image t)) 30)
178                            (= (cdr (image-size image t)) 30))))
179             (progn
180               (gnus-put-image image)
181               t)
182           (when (fboundp 'find-image)
183             (gnus-put-image (find-image
184                              '((:type xpm :file "lock-broken.xpm")))))
185           nil)))))
186
187 (defun gnus-html-prune-cache ()
188   (let ((total-size 0)
189         files)
190     (dolist (file (directory-files gnus-html-cache-directory t nil t))
191       (let ((attributes (file-attributes file)))
192         (unless (nth 0 attributes)
193           (incf total-size (nth 7 attributes))
194           (push (list (time-to-seconds (nth 5 attributes))
195                       (nth 7 attributes) file)
196                 files))))
197     (when (> total-size gnus-html-cache-size)
198       (setq files (sort files (lambda (f1 f2)
199                                 (< (car f1) (car f2)))))
200       (dolist (file files)
201         (when (> total-size gnus-html-cache-size)
202           (decf total-size (cadr file))
203           (delete-file (nth 2 file)))))))
204
205 ;;;###autoload
206 (defun gnus-html-prefetch-images (summary)
207   (let (safe-url-regexp urls)
208     (when (buffer-live-p summary)
209       (save-excursion
210         (set-buffer summary)
211         (setq safe-url-regexp mm-w3m-safe-url-regexp))
212       (save-match-data
213         (while (re-search-forward "<img.*src=[\"']\\([^\"']+\\)" nil t)
214           (let ((url (match-string 1)))
215             (when (or (null safe-url-regexp)
216                       (string-match safe-url-regexp url))
217               (unless (file-exists-p (gnus-html-image-id url))
218                 (push url urls)
219                 (push (gnus-html-image-id url) urls)
220                 (push "-o" urls)))))
221         (let ((process
222                (apply 'start-process 
223                       "images" nil "curl"
224                       "-s" "--create-dirs"
225                       "--location"
226                       "--max-time" "60"
227                       urls)))
228           (process-kill-without-query process))))))
229
230 (provide 'gnus-html)
231
232 ;;; gnus-html.el ends here