Merge changes made in Gnus trunk.
[bpt/emacs.git] / lisp / gnus / shr.el
CommitLineData
367f7f81
LMI
1;;; shr.el --- Simple HTML Renderer
2
3;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6;; Keywords: html
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;; This package takes a HTML parse tree (as provided by
26;; libxml-parse-html-region) and renders it in the current buffer. It
27;; does not do CSS, JavaScript or anything advanced: It's geared
28;; towards rendering typical short snippets of HTML, like what you'd
29;; find in HTML email and the like.
30
31;;; Code:
32
71e691a5
G
33(require 'browse-url)
34
870409d4
G
35(defgroup shr nil
36 "Simple HTML Renderer"
37 :group 'mail)
38
39(defcustom shr-max-image-proportion 0.9
40 "How big pictures displayed are in relation to the window they're in.
41A value of 0.7 means that they are allowed to take up 70% of the
42width and height of the window. If they are larger than this,
43and Emacs supports it, then the images will be rescaled down to
44fit these criteria."
45 :version "24.1"
46 :group 'shr
47 :type 'float)
48
49(defcustom shr-blocked-images nil
50 "Images that have URLs matching this regexp will be blocked."
51 :version "24.1"
52 :group 'shr
53 :type 'regexp)
54
130e977f
LMI
55(defvar shr-content-function nil
56 "If bound, this should be a function that will return the content.
57This is used for cid: URLs, and the function is called with the
58cid: URL as the argument.")
59
870409d4
G
60(defvar shr-folding-mode nil)
61(defvar shr-state nil)
62(defvar shr-start nil)
a41c2e6d 63(defvar shr-indentation 0)
130e977f 64(defvar shr-inhibit-images nil)
870409d4
G
65
66(defvar shr-width 70)
67
71e691a5
G
68(defvar shr-map
69 (let ((map (make-sparse-keymap)))
70 (define-key map "a" 'shr-show-alt-text)
71 (define-key map "i" 'shr-browse-image)
72 (define-key map "I" 'shr-insert-image)
73 (define-key map "u" 'shr-copy-url)
74 (define-key map "v" 'shr-browse-url)
75 (define-key map "\r" 'shr-browse-url)
76 map))
77
870409d4
G
78(defun shr-transform-dom (dom)
79 (let ((result (list (pop dom))))
80 (dolist (arg (pop dom))
81 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
82 (cdr arg))
83 result))
84 (dolist (sub dom)
85 (if (stringp sub)
86 (push (cons :text sub) result)
87 (push (shr-transform-dom sub) result)))
88 (nreverse result)))
89
90;;;###autoload
91(defun shr-insert-document (dom)
92 (let ((shr-state nil)
93 (shr-start nil))
94 (shr-descend (shr-transform-dom dom))))
95
96(defun shr-descend (dom)
a41c2e6d 97 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
870409d4
G
98 (if (fboundp function)
99 (funcall function (cdr dom))
100 (shr-generic (cdr dom)))))
101
102(defun shr-generic (cont)
103 (dolist (sub cont)
104 (cond
105 ((eq (car sub) :text)
106 (shr-insert (cdr sub)))
a41c2e6d 107 ((listp (cdr sub))
870409d4
G
108 (shr-descend sub)))))
109
a41c2e6d
G
110(defun shr-tag-p (cont)
111 (shr-ensure-paragraph)
870409d4 112 (shr-generic cont)
a41c2e6d
G
113 (shr-ensure-paragraph))
114
115(defun shr-ensure-paragraph ()
116 (unless (bobp)
117 (if (bolp)
71e691a5
G
118 (unless (save-excursion
119 (forward-line -1)
120 (looking-at " *$"))
a41c2e6d
G
121 (insert "\n"))
122 (if (save-excursion
123 (beginning-of-line)
5d2ef6db 124 (looking-at " *$"))
a41c2e6d
G
125 (insert "\n")
126 (insert "\n\n")))))
127
128(defun shr-tag-b (cont)
870409d4
G
129 (shr-fontize-cont cont 'bold))
130
a41c2e6d 131(defun shr-tag-i (cont)
870409d4
G
132 (shr-fontize-cont cont 'italic))
133
562f5ce5
G
134(defun shr-tag-em (cont)
135 (shr-fontize-cont cont 'bold))
136
a41c2e6d 137(defun shr-tag-u (cont)
870409d4
G
138 (shr-fontize-cont cont 'underline))
139
a41c2e6d
G
140(defun shr-tag-s (cont)
141 (shr-fontize-cont cont 'strike-through))
870409d4 142
a41c2e6d 143(defun shr-fontize-cont (cont &rest types)
870409d4
G
144 (let (shr-start)
145 (shr-generic cont)
a41c2e6d
G
146 (dolist (type types)
147 (shr-add-font (or shr-start (point)) (point) type))))
870409d4
G
148
149(defun shr-add-font (start end type)
150 (let ((overlay (make-overlay start end)))
151 (overlay-put overlay 'face type)))
152
a41c2e6d 153(defun shr-tag-a (cont)
870409d4 154 (let ((url (cdr (assq :href cont)))
71e691a5 155 (start (point))
870409d4
G
156 shr-start)
157 (shr-generic cont)
158 (widget-convert-button
71e691a5
G
159 'link (or shr-start start) (point)
160 :help-echo url)
161 (put-text-property (or shr-start start) (point) 'keymap shr-map)
162 (put-text-property (or shr-start start) (point) 'shr-url url)))
163
164(defun shr-browse-url ()
165 "Browse the URL under point."
166 (interactive)
167 (let ((url (get-text-property (point) 'shr-url)))
168 (if (not url)
169 (message "No link under point")
170 (browse-url url))))
171
172(defun shr-copy-url ()
173 "Copy the URL under point to the kill ring.
174If called twice, then try to fetch the URL and see whether it
175redirects somewhere else."
176 (interactive)
177 (let ((url (get-text-property (point) 'shr-url)))
178 (cond
179 ((not url)
180 (message "No URL under point"))
181 ;; Resolve redirected URLs.
182 ((equal url (car kill-ring))
183 (url-retrieve
184 url
185 (lambda (a)
186 (when (and (consp a)
187 (eq (car a) :redirect))
188 (with-temp-buffer
189 (insert (cadr a))
190 (goto-char (point-min))
191 ;; Remove common tracking junk from the URL.
192 (when (re-search-forward ".utm_.*" nil t)
193 (replace-match "" t t))
194 (message "Copied %s" (buffer-string))
195 (copy-region-as-kill (point-min) (point-max)))))))
196 ;; Copy the URL to the kill ring.
197 (t
198 (with-temp-buffer
199 (insert url)
200 (copy-region-as-kill (point-min) (point-max))
201 (message "Copied %s" url))))))
870409d4 202
a41c2e6d
G
203(defun shr-tag-img (cont)
204 (when (and (> (current-column) 0)
205 (not (eq shr-state 'image)))
206 (insert "\n"))
870409d4
G
207 (let ((start (point-marker)))
208 (let ((alt (cdr (assq :alt cont)))
209 (url (cdr (assq :src cont))))
210 (when (zerop (length alt))
211 (setq alt "[img]"))
212 (cond
130e977f
LMI
213 ((and (not shr-inhibit-images)
214 (string-match "\\`cid:" url))
215 (let ((url (substring url (match-end 0)))
216 image)
217 (if (or (not shr-content-function)
218 (not (setq image (funcall shr-content-function url))))
219 (insert alt)
220 (shr-put-image image (point) alt))))
221 ((or shr-inhibit-images
222 (and shr-blocked-images
223 (string-match shr-blocked-images url)))
224 (setq shr-start (point))
225 (let ((shr-state 'space))
226 (if (> (length alt) 8)
227 (shr-insert (substring alt 0 8))
228 (shr-insert alt))))
97ab3539 229 ((url-is-cached (browse-url-url-encode-chars url "[&)$ ]"))
870409d4
G
230 (shr-put-image (shr-get-image-data url) (point) alt))
231 (t
232 (insert alt)
130e977f
LMI
233 (ignore-errors
234 (url-retrieve url 'shr-image-fetched
235 (list (current-buffer) start (point-marker))
236 t))))
870409d4 237 (insert " ")
71e691a5
G
238 (put-text-property start (point) 'keymap shr-map)
239 (put-text-property start (point) 'shr-alt alt)
240 (put-text-property start (point) 'shr-image url)
870409d4
G
241 (setq shr-state 'image))))
242
71e691a5
G
243(defun shr-show-alt-text ()
244 "Show the ALT text of the image under point."
245 (interactive)
246 (let ((text (get-text-property (point) 'shr-alt)))
247 (if (not text)
248 (message "No image under point")
249 (message "%s" text))))
250
251(defun shr-browse-image ()
252 "Browse the image under point."
253 (interactive)
254 (let ((url (get-text-property (point) 'shr-image)))
255 (if (not url)
256 (message "No image under point")
257 (message "Browsing %s..." url)
258 (browse-url url))))
259
870409d4
G
260(defun shr-image-fetched (status buffer start end)
261 (when (and (buffer-name buffer)
262 (not (plist-get status :error)))
263 (url-store-in-cache (current-buffer))
264 (when (or (search-forward "\n\n" nil t)
265 (search-forward "\r\n\r\n" nil t))
266 (let ((data (buffer-substring (point) (point-max))))
267 (with-current-buffer buffer
268 (let ((alt (buffer-substring start end))
269 (inhibit-read-only t))
270 (delete-region start end)
271 (shr-put-image data start alt))))))
272 (kill-buffer (current-buffer)))
273
274(defun shr-put-image (data point alt)
275 (if (not (display-graphic-p))
276 (insert alt)
a41c2e6d
G
277 (let ((image (ignore-errors
278 (shr-rescale-image data))))
279 (when image
280 (put-image image point alt)))))
870409d4
G
281
282(defun shr-rescale-image (data)
283 (if (or (not (fboundp 'imagemagick-types))
284 (not (get-buffer-window (current-buffer))))
285 (create-image data nil t)
286 (let* ((image (create-image data nil t))
a41c2e6d 287 (size (image-size image t))
870409d4
G
288 (width (car size))
289 (height (cdr size))
290 (edges (window-inside-pixel-edges
291 (get-buffer-window (current-buffer))))
292 (window-width (truncate (* shr-max-image-proportion
293 (- (nth 2 edges) (nth 0 edges)))))
294 (window-height (truncate (* shr-max-image-proportion
295 (- (nth 3 edges) (nth 1 edges)))))
296 scaled-image)
297 (when (> height window-height)
298 (setq image (or (create-image data 'imagemagick t
299 :height window-height)
300 image))
301 (setq size (image-size image t)))
302 (when (> (car size) window-width)
303 (setq image (or
304 (create-image data 'imagemagick t
305 :width window-width)
306 image)))
307 image)))
308
a41c2e6d 309(defun shr-tag-pre (cont)
1e463294 310 (let ((shr-folding-mode 'none))
870409d4
G
311 (shr-ensure-newline)
312 (shr-generic cont)
313 (shr-ensure-newline)))
314
a41c2e6d 315(defun shr-tag-blockquote (cont)
8028ed5c 316 (shr-ensure-paragraph)
a41c2e6d 317 (let ((shr-indentation (+ shr-indentation 4)))
71e691a5
G
318 (shr-generic cont))
319 (shr-ensure-paragraph))
870409d4
G
320
321(defun shr-ensure-newline ()
322 (unless (zerop (current-column))
323 (insert "\n")))
324
325(defun shr-insert (text)
326 (when (eq shr-state 'image)
327 (insert "\n")
328 (setq shr-state nil))
329 (cond
330 ((eq shr-folding-mode 'none)
1e463294 331 (insert text))
870409d4 332 (t
a41c2e6d
G
333 (let ((first t)
334 column)
23179cb9 335 (when (and (string-match "\\`[ \t\n]" text)
a41c2e6d
G
336 (not (bolp)))
337 (insert " "))
870409d4
G
338 (dolist (elem (split-string text))
339 (setq column (current-column))
d99a4591 340 (when (> column 0)
a41c2e6d 341 (cond
1e463294
LMI
342 ((and (or (not first)
343 (eq shr-state 'space))
344 (> (+ column (length elem) 1) shr-width))
a41c2e6d
G
345 (insert "\n"))
346 ((not first)
347 (insert " "))))
348 (setq first nil)
349 (when (and (bolp)
350 (> shr-indentation 0))
71e691a5 351 (shr-indent))
870409d4
G
352 ;; The shr-start is a special variable that is used to pass
353 ;; upwards the first point in the buffer where the text really
354 ;; starts.
355 (unless shr-start
356 (setq shr-start (point)))
a41c2e6d 357 (insert elem))
1e463294 358 (setq shr-state nil)
23179cb9 359 (when (and (string-match "[ \t\n]\\'" text)
a41c2e6d 360 (not (bolp)))
1e463294
LMI
361 (insert " ")
362 (setq shr-state 'space))))))
870409d4 363
71e691a5
G
364(defun shr-indent ()
365 (insert (make-string shr-indentation ? )))
366
870409d4
G
367(defun shr-get-image-data (url)
368 "Get image data for URL.
369Return a string with image data."
370 (with-temp-buffer
371 (mm-disable-multibyte)
71e691a5
G
372 (when (ignore-errors
373 (url-cache-extract (url-cache-create-filename url))
374 t)
375 (when (or (search-forward "\n\n" nil t)
376 (search-forward "\r\n\r\n" nil t))
377 (buffer-substring (point) (point-max))))))
870409d4 378
a41c2e6d
G
379(defvar shr-list-mode nil)
380
381(defun shr-tag-ul (cont)
382 (shr-ensure-paragraph)
383 (let ((shr-list-mode 'ul))
384 (shr-generic cont)))
385
386(defun shr-tag-ol (cont)
387 (let ((shr-list-mode 1))
388 (shr-generic cont)))
389
390(defun shr-tag-li (cont)
391 (shr-ensure-newline)
8028ed5c
LMI
392 (let* ((bullet
393 (if (numberp shr-list-mode)
394 (prog1
395 (format "%d " shr-list-mode)
396 (setq shr-list-mode (1+ shr-list-mode)))
397 "* "))
398 (shr-indentation (+ shr-indentation (length bullet))))
399 (insert bullet)
400 (shr-generic cont)))
a41c2e6d
G
401
402(defun shr-tag-br (cont)
1e463294
LMI
403 (unless (bobp)
404 (insert "\n"))
a41c2e6d
G
405 (shr-generic cont))
406
407(defun shr-tag-h1 (cont)
408 (shr-heading cont 'bold 'underline))
409
410(defun shr-tag-h2 (cont)
411 (shr-heading cont 'bold))
412
413(defun shr-tag-h3 (cont)
414 (shr-heading cont 'italic))
415
416(defun shr-tag-h4 (cont)
417 (shr-heading cont))
418
419(defun shr-tag-h5 (cont)
420 (shr-heading cont))
421
422(defun shr-tag-h6 (cont)
423 (shr-heading cont))
424
425(defun shr-heading (cont &rest types)
426 (shr-ensure-paragraph)
427 (apply #'shr-fontize-cont cont types)
428 (shr-ensure-paragraph))
429
71e691a5
G
430(defun shr-tag-table (cont)
431 (shr-ensure-paragraph)
432 (setq cont (or (cdr (assq 'tbody cont))
433 cont))
130e977f
LMI
434 (let* ((shr-inhibit-images t)
435 (columns (shr-column-specs cont))
71e691a5
G
436 (suggested-widths (shr-pro-rate-columns columns))
437 (sketch (shr-make-table cont suggested-widths))
438 (sketch-widths (shr-table-widths sketch (length suggested-widths))))
130e977f
LMI
439 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
440 (dolist (elem (shr-find-elements cont 'img))
441 (shr-tag-img (cdr elem))))
442
443(defun shr-find-elements (cont type)
444 (let (result)
445 (dolist (elem cont)
446 (cond ((eq (car elem) type)
447 (push elem result))
448 ((consp (cdr elem))
449 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
450 (nreverse result)))
71e691a5
G
451
452(defun shr-insert-table (table widths)
453 (shr-insert-table-ruler widths)
454 (dolist (row table)
455 (let ((start (point))
456 (height (let ((max 0))
457 (dolist (column row)
458 (setq max (max max (cadr column))))
459 max)))
460 (dotimes (i height)
461 (shr-indent)
462 (insert "|\n"))
463 (dolist (column row)
464 (goto-char start)
130e977f
LMI
465 (let ((lines (split-string (nth 2 column) "\n"))
466 (overlay-lines (nth 3 column))
467 overlay overlay-line)
71e691a5 468 (dolist (line lines)
130e977f 469 (setq overlay-line (pop overlay-lines))
71e691a5
G
470 (when (> (length line) 0)
471 (end-of-line)
472 (insert line "|")
130e977f
LMI
473 (dolist (overlay overlay-line)
474 (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
475 (- (point) (nth 1 overlay) 1)))
476 (properties (nth 2 overlay)))
477 (while properties
478 (overlay-put o (pop properties) (pop properties)))))
71e691a5
G
479 (forward-line 1)))
480 ;; Add blank lines at padding at the bottom of the TD,
481 ;; possibly.
482 (dotimes (i (- height (length lines)))
483 (end-of-line)
484 (insert (make-string (length (car lines)) ? ) "|")
485 (forward-line 1)))))
486 (shr-insert-table-ruler widths)))
487
488(defun shr-insert-table-ruler (widths)
489 (shr-indent)
490 (insert "+")
491 (dotimes (i (length widths))
492 (insert (make-string (aref widths i) ?-) ?+))
493 (insert "\n"))
494
495(defun shr-table-widths (table length)
496 (let ((widths (make-vector length 0)))
497 (dolist (row table)
498 (let ((i 0))
499 (dolist (column row)
500 (aset widths i (max (aref widths i)
501 (car column)))
502 (incf i))))
503 widths))
504
505(defun shr-make-table (cont widths &optional fill)
506 (let ((trs nil))
507 (dolist (row cont)
508 (when (eq (car row) 'tr)
509 (let ((i 0)
510 (tds nil))
511 (dolist (column (cdr row))
512 (when (memq (car column) '(td th))
513 (push (shr-render-td (cdr column) (aref widths i) fill)
514 tds)
515 (setq i (1+ i))))
516 (push (nreverse tds) trs))))
517 (nreverse trs)))
518
519(defun shr-render-td (cont width fill)
520 (with-temp-buffer
521 (let ((shr-width width)
522 (shr-indentation 0))
523 (shr-generic cont))
524 (while (re-search-backward "\n *$" nil t)
525 (delete-region (match-beginning 0) (match-end 0)))
526 (goto-char (point-min))
527 (let ((max 0))
528 (while (not (eobp))
529 (end-of-line)
530 (setq max (max max (current-column)))
531 (forward-line 1))
532 (when fill
533 (goto-char (point-min))
534 (while (not (eobp))
535 (end-of-line)
536 (when (> (- width (current-column)) 0)
537 (insert (make-string (- width (current-column)) ? )))
538 (forward-line 1)))
130e977f
LMI
539 (list max
540 (count-lines (point-min) (point-max))
541 (buffer-string)
542 (and fill
543 (shr-collect-overlays))))))
544
545(defun shr-collect-overlays ()
546 (save-excursion
547 (goto-char (point-min))
548 (let ((overlays nil))
549 (while (not (eobp))
550 (push (shr-overlays-in-region (point) (line-end-position))
551 overlays)
552 (forward-line 1))
553 (nreverse overlays))))
554
555(defun shr-overlays-in-region (start end)
556 (let (result)
557 (dolist (overlay (overlays-in start end))
558 (push (list (if (> start (overlay-start overlay))
559 (- end start)
560 (- end (overlay-start overlay)))
561 (if (< end (overlay-end overlay))
562 0
563 (- end (overlay-end overlay)))
564 (overlay-properties overlay))
565 result))
566 (nreverse result)))
71e691a5
G
567
568(defun shr-pro-rate-columns (columns)
569 (let ((total-percentage 0)
570 (widths (make-vector (length columns) 0)))
571 (dotimes (i (length columns))
572 (incf total-percentage (aref columns i)))
573 (setq total-percentage (/ 1.0 total-percentage))
574 (dotimes (i (length columns))
575 (aset widths i (max (truncate (* (aref columns i)
576 total-percentage
577 shr-width))
578 10)))
579 widths))
580
581;; Return a summary of the number and shape of the TDs in the table.
582(defun shr-column-specs (cont)
583 (let ((columns (make-vector (shr-max-columns cont) 1)))
584 (dolist (row cont)
585 (when (eq (car row) 'tr)
586 (let ((i 0))
587 (dolist (column (cdr row))
588 (when (memq (car column) '(td th))
589 (let ((width (cdr (assq :width (cdr column)))))
590 (when (and width
591 (string-match "\\([0-9]+\\)%" width))
592 (aset columns i
593 (/ (string-to-number (match-string 1 width))
130e977f
LMI
594 100.0))))
595 (setq i (1+ i)))))))
71e691a5
G
596 columns))
597
598(defun shr-count (cont elem)
599 (let ((i 0))
600 (dolist (sub cont)
601 (when (eq (car sub) elem)
602 (setq i (1+ i))))
603 i))
604
605(defun shr-max-columns (cont)
606 (let ((max 0))
607 (dolist (row cont)
608 (when (eq (car row) 'tr)
130e977f
LMI
609 (setq max (max max (+ (shr-count (cdr row) 'td)
610 (shr-count (cdr row) 'th))))))
71e691a5
G
611 max))
612
f3fd95db 613(provide 'shr)
367f7f81
LMI
614
615;;; shr.el ends here