* newcomment.el (comment-search-forward, comment-search-backward): Doc fix.
[bpt/emacs.git] / lisp / gnus / shr.el
1 ;;; shr.el --- Simple HTML Renderer
2
3 ;; Copyright (C) 2010-2013 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
33 (eval-when-compile (require 'cl))
34 (eval-when-compile (require 'url)) ;For url-filename's setf handler.
35 (require 'browse-url)
36
37 (defgroup shr nil
38 "Simple HTML Renderer"
39 :version "24.1"
40 :group 'mail)
41
42 (defcustom shr-max-image-proportion 0.9
43 "How big pictures displayed are in relation to the window they're in.
44 A value of 0.7 means that they are allowed to take up 70% of the
45 width and height of the window. If they are larger than this,
46 and Emacs supports it, then the images will be rescaled down to
47 fit these criteria."
48 :version "24.1"
49 :group 'shr
50 :type 'float)
51
52 (defcustom shr-blocked-images nil
53 "Images that have URLs matching this regexp will be blocked."
54 :version "24.1"
55 :group 'shr
56 :type '(choice (const nil) regexp))
57
58 (defcustom shr-table-horizontal-line ?\s
59 "Character used to draw horizontal table lines."
60 :group 'shr
61 :type 'character)
62
63 (defcustom shr-table-vertical-line ?\s
64 "Character used to draw vertical table lines."
65 :group 'shr
66 :type 'character)
67
68 (defcustom shr-table-corner ?\s
69 "Character used to draw table corners."
70 :group 'shr
71 :type 'character)
72
73 (defcustom shr-hr-line ?-
74 "Character used to draw hr lines."
75 :group 'shr
76 :type 'character)
77
78 (defcustom shr-width fill-column
79 "Frame width to use for rendering.
80 May either be an integer specifying a fixed width in characters,
81 or nil, meaning that the full width of the window should be
82 used."
83 :type '(choice (integer :tag "Fixed width in characters")
84 (const :tag "Use the width of the window" nil))
85 :group 'shr)
86
87 (defcustom shr-bullet "* "
88 "Bullet used for unordered lists.
89 Alternative suggestions are:
90 - \" \"
91 - \" \""
92 :type 'string
93 :group 'shr)
94
95 (defvar shr-content-function nil
96 "If bound, this should be a function that will return the content.
97 This is used for cid: URLs, and the function is called with the
98 cid: URL as the argument.")
99
100 (defvar shr-put-image-function 'shr-put-image
101 "Function called to put image and alt string.")
102
103 (defface shr-strike-through '((t (:strike-through t)))
104 "Font for <s> elements."
105 :group 'shr)
106
107 (defface shr-link
108 '((t (:inherit link)))
109 "Font for link elements."
110 :group 'shr)
111
112 ;;; Internal variables.
113
114 (defvar shr-folding-mode nil)
115 (defvar shr-state nil)
116 (defvar shr-start nil)
117 (defvar shr-indentation 0)
118 (defvar shr-inhibit-images nil)
119 (defvar shr-list-mode nil)
120 (defvar shr-content-cache nil)
121 (defvar shr-kinsoku-shorten nil)
122 (defvar shr-table-depth 0)
123 (defvar shr-stylesheet nil)
124 (defvar shr-base nil)
125 (defvar shr-ignore-cache nil)
126 (defvar shr-external-rendering-functions nil)
127 (defvar shr-target-id nil)
128 (defvar shr-inhibit-decoration nil)
129
130 (defvar shr-map
131 (let ((map (make-sparse-keymap)))
132 (define-key map "a" 'shr-show-alt-text)
133 (define-key map "i" 'shr-browse-image)
134 (define-key map "z" 'shr-zoom-image)
135 (define-key map [tab] 'shr-next-link)
136 (define-key map [backtab] 'shr-previous-link)
137 (define-key map "I" 'shr-insert-image)
138 (define-key map "u" 'shr-copy-url)
139 (define-key map "v" 'shr-browse-url)
140 (define-key map "o" 'shr-save-contents)
141 (define-key map "\r" 'shr-browse-url)
142 map))
143
144 ;; Public functions and commands.
145 (declare-function libxml-parse-html-region "xml.c"
146 (start end &optional base-url))
147
148 (defun shr-render-buffer (buffer)
149 "Display the HTML rendering of the current buffer."
150 (interactive (list (current-buffer)))
151 (or (fboundp 'libxml-parse-html-region)
152 (error "This function requires Emacs to be compiled with libxml2"))
153 (pop-to-buffer "*html*")
154 (erase-buffer)
155 (shr-insert-document
156 (with-current-buffer buffer
157 (libxml-parse-html-region (point-min) (point-max))))
158 (goto-char (point-min)))
159
160 (defun shr-visit-file (file)
161 "Parse FILE as an HTML document, and render it in a new buffer."
162 (interactive "fHTML file name: ")
163 (with-temp-buffer
164 (insert-file-contents file)
165 (shr-render-buffer (current-buffer))))
166
167 ;;;###autoload
168 (defun shr-insert-document (dom)
169 "Render the parsed document DOM into the current buffer.
170 DOM should be a parse tree as generated by
171 `libxml-parse-html-region' or similar."
172 (setq shr-content-cache nil)
173 (let ((start (point))
174 (shr-state nil)
175 (shr-start nil)
176 (shr-base nil)
177 (shr-preliminary-table-render 0)
178 (shr-width (or shr-width (window-width))))
179 (shr-descend (shr-transform-dom dom))
180 (shr-remove-trailing-whitespace start (point))))
181
182 (defun shr-remove-trailing-whitespace (start end)
183 (let ((width (window-width)))
184 (save-restriction
185 (narrow-to-region start end)
186 (goto-char start)
187 (while (not (eobp))
188 (end-of-line)
189 (when (> (shr-previous-newline-padding-width (current-column)) width)
190 (dolist (overlay (overlays-at (point)))
191 (when (overlay-get overlay 'before-string)
192 (overlay-put overlay 'before-string nil))))
193 (forward-line 1)))))
194
195 (defun shr-copy-url ()
196 "Copy the URL under point to the kill ring.
197 If called twice, then try to fetch the URL and see whether it
198 redirects somewhere else."
199 (interactive)
200 (let ((url (get-text-property (point) 'shr-url)))
201 (cond
202 ((not url)
203 (message "No URL under point"))
204 ;; Resolve redirected URLs.
205 ((equal url (car kill-ring))
206 (url-retrieve
207 url
208 (lambda (a)
209 (when (and (consp a)
210 (eq (car a) :redirect))
211 (with-temp-buffer
212 (insert (cadr a))
213 (goto-char (point-min))
214 ;; Remove common tracking junk from the URL.
215 (when (re-search-forward ".utm_.*" nil t)
216 (replace-match "" t t))
217 (message "Copied %s" (buffer-string))
218 (copy-region-as-kill (point-min) (point-max)))))
219 nil t))
220 ;; Copy the URL to the kill ring.
221 (t
222 (with-temp-buffer
223 (insert url)
224 (copy-region-as-kill (point-min) (point-max))
225 (message "Copied %s" url))))))
226
227 (defun shr-next-link ()
228 "Skip to the next link."
229 (interactive)
230 (let ((skip (text-property-any (point) (point-max) 'help-echo nil)))
231 (if (not (setq skip (text-property-not-all skip (point-max)
232 'help-echo nil)))
233 (message "No next link")
234 (goto-char skip)
235 (message "%s" (get-text-property (point) 'help-echo)))))
236
237 (defun shr-previous-link ()
238 "Skip to the previous link."
239 (interactive)
240 (let ((start (point))
241 (found nil))
242 ;; Skip past the current link.
243 (while (and (not (bobp))
244 (get-text-property (point) 'help-echo))
245 (forward-char -1))
246 ;; Find the previous link.
247 (while (and (not (bobp))
248 (not (setq found (get-text-property (point) 'help-echo))))
249 (forward-char -1))
250 (if (not found)
251 (progn
252 (message "No previous link")
253 (goto-char start))
254 ;; Put point at the start of the link.
255 (while (and (not (bobp))
256 (get-text-property (point) 'help-echo))
257 (forward-char -1))
258 (forward-char 1)
259 (message "%s" (get-text-property (point) 'help-echo)))))
260
261 (defun shr-show-alt-text ()
262 "Show the ALT text of the image under point."
263 (interactive)
264 (let ((text (get-text-property (point) 'shr-alt)))
265 (if (not text)
266 (message "No image under point")
267 (message "%s" text))))
268
269 (defun shr-browse-image (&optional copy-url)
270 "Browse the image under point.
271 If COPY-URL (the prefix if called interactively) is non-nil, copy
272 the URL of the image to the kill buffer instead."
273 (interactive "P")
274 (let ((url (get-text-property (point) 'image-url)))
275 (cond
276 ((not url)
277 (message "No image under point"))
278 (copy-url
279 (with-temp-buffer
280 (insert url)
281 (copy-region-as-kill (point-min) (point-max))
282 (message "Copied %s" url)))
283 (t
284 (message "Browsing %s..." url)
285 (browse-url url)))))
286
287 (defun shr-insert-image ()
288 "Insert the image under point into the buffer."
289 (interactive)
290 (let ((url (get-text-property (point) 'image-url)))
291 (if (not url)
292 (message "No image under point")
293 (message "Inserting %s..." url)
294 (url-retrieve url 'shr-image-fetched
295 (list (current-buffer) (1- (point)) (point-marker))
296 t t))))
297
298 (defun shr-zoom-image ()
299 "Toggle the image size.
300 The size will be rotated between the default size, the original
301 size, and full-buffer size."
302 (interactive)
303 (let ((url (get-text-property (point) 'image-url))
304 (size (get-text-property (point) 'image-size))
305 (buffer-read-only nil))
306 (if (not url)
307 (message "No image under point")
308 ;; Delete the old picture.
309 (while (get-text-property (point) 'image-url)
310 (forward-char -1))
311 (forward-char 1)
312 (let ((start (point)))
313 (while (get-text-property (point) 'image-url)
314 (forward-char 1))
315 (forward-char -1)
316 (put-text-property start (point) 'display nil)
317 (when (> (- (point) start) 2)
318 (delete-region start (1- (point)))))
319 (message "Inserting %s..." url)
320 (url-retrieve url 'shr-image-fetched
321 (list (current-buffer) (1- (point)) (point-marker)
322 (list (cons 'size
323 (cond ((or (eq size 'default)
324 (null size))
325 'original)
326 ((eq size 'original)
327 'full)
328 ((eq size 'full)
329 'default)))))
330 t))))
331
332 ;;; Utility functions.
333
334 (defun shr-transform-dom (dom)
335 (let ((result (list (pop dom))))
336 (dolist (arg (pop dom))
337 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
338 (cdr arg))
339 result))
340 (dolist (sub dom)
341 (if (stringp sub)
342 (push (cons 'text sub) result)
343 (push (shr-transform-dom sub) result)))
344 (nreverse result)))
345
346 (defun shr-descend (dom)
347 (let ((function
348 (or
349 ;; Allow other packages to override (or provide) rendering
350 ;; of elements.
351 (cdr (assq (car dom) shr-external-rendering-functions))
352 (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
353 (style (cdr (assq :style (cdr dom))))
354 (shr-stylesheet shr-stylesheet)
355 (start (point)))
356 (when style
357 (if (string-match "color\\|display\\|border-collapse" style)
358 (setq shr-stylesheet (nconc (shr-parse-style style)
359 shr-stylesheet))
360 (setq style nil)))
361 ;; If we have a display:none, then just ignore this part of the
362 ;; DOM.
363 (unless (equal (cdr (assq 'display shr-stylesheet)) "none")
364 (if (fboundp function)
365 (funcall function (cdr dom))
366 (shr-generic (cdr dom)))
367 (when (and shr-target-id
368 (equal (cdr (assq :id (cdr dom))) shr-target-id))
369 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
370 ;; If style is set, then this node has set the color.
371 (when style
372 (shr-colorize-region start (point)
373 (cdr (assq 'color shr-stylesheet))
374 (cdr (assq 'background-color shr-stylesheet)))))))
375
376 (defun shr-generic (cont)
377 (dolist (sub cont)
378 (cond
379 ((eq (car sub) 'text)
380 (shr-insert (cdr sub)))
381 ((listp (cdr sub))
382 (shr-descend sub)))))
383
384 (defmacro shr-char-breakable-p (char)
385 "Return non-nil if a line can be broken before and after CHAR."
386 `(aref fill-find-break-point-function-table ,char))
387 (defmacro shr-char-nospace-p (char)
388 "Return non-nil if no space is required before and after CHAR."
389 `(aref fill-nospace-between-words-table ,char))
390
391 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
392 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
393 ;; parentheses, and so on, that should not be placed in the beginning
394 ;; of a line or the end of a line.
395 (defmacro shr-char-kinsoku-bol-p (char)
396 "Return non-nil if a line ought not to begin with CHAR."
397 `(aref (char-category-set ,char) ?>))
398 (defmacro shr-char-kinsoku-eol-p (char)
399 "Return non-nil if a line ought not to end with CHAR."
400 `(aref (char-category-set ,char) ?<))
401 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
402 (load "kinsoku" nil t))
403
404 (defun shr-insert (text)
405 (when (and (eq shr-state 'image)
406 (not (bolp))
407 (not (string-match "\\`[ \t\n]+\\'" text)))
408 (insert "\n")
409 (setq shr-state nil))
410 (cond
411 ((eq shr-folding-mode 'none)
412 (insert text))
413 (t
414 (when (and (string-match "\\`[ \t\n ]" text)
415 (not (bolp))
416 (not (eq (char-after (1- (point))) ? )))
417 (insert " "))
418 (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t))
419 (when (and (bolp)
420 (> shr-indentation 0))
421 (shr-indent))
422 ;; No space is needed behind a wide character categorized as
423 ;; kinsoku-bol, between characters both categorized as nospace,
424 ;; or at the beginning of a line.
425 (let (prev)
426 (when (and (> (current-column) shr-indentation)
427 (eq (preceding-char) ? )
428 (or (= (line-beginning-position) (1- (point)))
429 (and (shr-char-breakable-p
430 (setq prev (char-after (- (point) 2))))
431 (shr-char-kinsoku-bol-p prev))
432 (and (shr-char-nospace-p prev)
433 (shr-char-nospace-p (aref elem 0)))))
434 (delete-char -1)))
435 ;; The shr-start is a special variable that is used to pass
436 ;; upwards the first point in the buffer where the text really
437 ;; starts.
438 (unless shr-start
439 (setq shr-start (point)))
440 (insert elem)
441 (setq shr-state nil)
442 (let (found)
443 (while (and (> (current-column) shr-width)
444 (progn
445 (setq found (shr-find-fill-point))
446 (not (eolp))))
447 (when (eq (preceding-char) ? )
448 (delete-char -1))
449 (insert "\n")
450 (unless found
451 ;; No space is needed at the beginning of a line.
452 (when (eq (following-char) ? )
453 (delete-char 1)))
454 (when (> shr-indentation 0)
455 (shr-indent))
456 (end-of-line))
457 (insert " ")))
458 (unless (string-match "[ \t\r\n ]\\'" text)
459 (delete-char -1)))))
460
461 (defun shr-find-fill-point ()
462 (when (> (move-to-column shr-width) shr-width)
463 (backward-char 1))
464 (let ((bp (point))
465 failed)
466 (while (not (or (setq failed (= (current-column) shr-indentation))
467 (eq (preceding-char) ? )
468 (eq (following-char) ? )
469 (shr-char-breakable-p (preceding-char))
470 (shr-char-breakable-p (following-char))
471 (if (eq (preceding-char) ?')
472 (not (memq (char-after (- (point) 2))
473 (list nil ?\n ? )))
474 (and (shr-char-kinsoku-bol-p (preceding-char))
475 (shr-char-breakable-p (following-char))
476 (not (shr-char-kinsoku-bol-p (following-char)))))
477 (shr-char-kinsoku-eol-p (following-char))))
478 (backward-char 1))
479 (if (and (not (or failed (eolp)))
480 (eq (preceding-char) ?'))
481 (while (not (or (setq failed (eolp))
482 (eq (following-char) ? )
483 (shr-char-breakable-p (following-char))
484 (shr-char-kinsoku-eol-p (following-char))))
485 (forward-char 1)))
486 (if failed
487 ;; There's no breakable point, so we give it up.
488 (let (found)
489 (goto-char bp)
490 (unless shr-kinsoku-shorten
491 (while (and (setq found (re-search-forward
492 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
493 (line-end-position) 'move))
494 (eq (preceding-char) ?')))
495 (if (and found (not (match-beginning 1)))
496 (goto-char (match-beginning 0)))))
497 (or
498 (eolp)
499 ;; Don't put kinsoku-bol characters at the beginning of a line,
500 ;; or kinsoku-eol characters at the end of a line.
501 (cond
502 (shr-kinsoku-shorten
503 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
504 (shr-char-kinsoku-eol-p (preceding-char)))
505 (backward-char 1))
506 (when (setq failed (= (current-column) shr-indentation))
507 ;; There's no breakable point that doesn't violate kinsoku,
508 ;; so we look for the second best position.
509 (while (and (progn
510 (forward-char 1)
511 (<= (current-column) shr-width))
512 (progn
513 (setq bp (point))
514 (shr-char-kinsoku-eol-p (following-char)))))
515 (goto-char bp)))
516 ((shr-char-kinsoku-eol-p (preceding-char))
517 ;; Find backward the point where kinsoku-eol characters begin.
518 (let ((count 4))
519 (while
520 (progn
521 (backward-char 1)
522 (and (> (setq count (1- count)) 0)
523 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
524 (or (shr-char-kinsoku-eol-p (preceding-char))
525 (shr-char-kinsoku-bol-p (following-char)))))))
526 (if (setq failed (= (current-column) shr-indentation))
527 ;; There's no breakable point that doesn't violate kinsoku,
528 ;; so we go to the second best position.
529 (if (looking-at "\\(\\c<+\\)\\c<")
530 (goto-char (match-end 1))
531 (forward-char 1))))
532 ((shr-char-kinsoku-bol-p (following-char))
533 ;; Find forward the point where kinsoku-bol characters end.
534 (let ((count 4))
535 (while (progn
536 (forward-char 1)
537 (and (>= (setq count (1- count)) 0)
538 (shr-char-kinsoku-bol-p (following-char))
539 (shr-char-breakable-p (following-char))))))))
540 (when (eq (following-char) ? )
541 (forward-char 1))))
542 (not failed)))
543
544 (defun shr-parse-base (url)
545 ;; Always chop off anchors.
546 (when (string-match "#.*" url)
547 (setq url (substring url 0 (match-beginning 0))))
548 (let* ((parsed (url-generic-parse-url url))
549 (local (url-filename parsed)))
550 (setf (url-filename parsed) "")
551 ;; Chop off the bit after the last slash.
552 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local)
553 (setq local (match-string 1 local)))
554 ;; Always make the local bit end with a slash.
555 (when (and (not (zerop (length local)))
556 (not (eq (aref local (1- (length local))) ?/)))
557 (setq local (concat local "/")))
558 (list (url-recreate-url parsed)
559 local
560 (url-type parsed)
561 url)))
562
563 (defun shr-expand-url (url &optional base)
564 (setq base
565 (if base
566 (shr-parse-base base)
567 ;; Bound by the parser.
568 shr-base))
569 (when (zerop (length url))
570 (setq url nil))
571 (cond ((or (not url)
572 (not base)
573 (string-match "\\`[a-z]*:" url))
574 ;; Absolute URL.
575 (or url (car base)))
576 ((eq (aref url 0) ?/)
577 (if (and (> (length url) 1)
578 (eq (aref url 1) ?/))
579 ;; //host...; just use the protocol
580 (concat (nth 2 base) ":" url)
581 ;; Just use the host name part.
582 (concat (car base) url)))
583 ((eq (aref url 0) ?#)
584 ;; A link to an anchor.
585 (concat (nth 3 base) url))
586 (t
587 ;; Totally relative.
588 (concat (car base) (cadr base) url))))
589
590 (defun shr-ensure-newline ()
591 (unless (zerop (current-column))
592 (insert "\n")))
593
594 (defun shr-ensure-paragraph ()
595 (unless (bobp)
596 (if (<= (current-column) shr-indentation)
597 (unless (save-excursion
598 (forward-line -1)
599 (looking-at " *$"))
600 (insert "\n"))
601 (if (save-excursion
602 (beginning-of-line)
603 ;; If the current line is totally blank, and doesn't even
604 ;; have any face properties set, then delete the blank
605 ;; space.
606 (and (looking-at " *$")
607 (not (get-text-property (point) 'face))
608 (not (= (next-single-property-change (point) 'face nil
609 (line-end-position))
610 (line-end-position)))))
611 (delete-region (match-beginning 0) (match-end 0))
612 (insert "\n\n")))))
613
614 (defun shr-indent ()
615 (when (> shr-indentation 0)
616 (insert (make-string shr-indentation ? ))))
617
618 (defun shr-fontize-cont (cont &rest types)
619 (let (shr-start)
620 (shr-generic cont)
621 (dolist (type types)
622 (shr-add-font (or shr-start (point)) (point) type))))
623
624 ;; Add face to the region, but avoid putting the font properties on
625 ;; blank text at the start of the line, and the newline at the end, to
626 ;; avoid ugliness.
627 (defun shr-add-font (start end type)
628 (unless shr-inhibit-decoration
629 (save-excursion
630 (goto-char start)
631 (while (< (point) end)
632 (when (bolp)
633 (skip-chars-forward " "))
634 (add-face-text-property (point) (min (line-end-position) end) type t)
635 (if (< (line-end-position) end)
636 (forward-line 1)
637 (goto-char end))))))
638
639 (defun shr-browse-url ()
640 "Browse the URL under point."
641 (interactive)
642 (let ((url (get-text-property (point) 'shr-url)))
643 (cond
644 ((not url)
645 (message "No link under point"))
646 ((string-match "^mailto:" url)
647 (browse-url-mail url))
648 (t
649 (browse-url url)))))
650
651 (defun shr-save-contents (directory)
652 "Save the contents from URL in a file."
653 (interactive "DSave contents of URL to directory: ")
654 (let ((url (get-text-property (point) 'shr-url)))
655 (if (not url)
656 (message "No link under point")
657 (url-retrieve (shr-encode-url url)
658 'shr-store-contents (list url directory)
659 nil t))))
660
661 (defun shr-store-contents (status url directory)
662 (unless (plist-get status :error)
663 (when (or (search-forward "\n\n" nil t)
664 (search-forward "\r\n\r\n" nil t))
665 (write-region (point) (point-max)
666 (expand-file-name (file-name-nondirectory url)
667 directory)))))
668
669 (defun shr-image-fetched (status buffer start end &optional flags)
670 (let ((image-buffer (current-buffer)))
671 (when (and (buffer-name buffer)
672 (not (plist-get status :error)))
673 (url-store-in-cache image-buffer)
674 (when (or (search-forward "\n\n" nil t)
675 (search-forward "\r\n\r\n" nil t))
676 (let ((data (buffer-substring (point) (point-max))))
677 (with-current-buffer buffer
678 (save-excursion
679 (let ((alt (buffer-substring start end))
680 (properties (text-properties-at start))
681 (inhibit-read-only t))
682 (delete-region start end)
683 (goto-char start)
684 (funcall shr-put-image-function data alt flags)
685 (while properties
686 (let ((type (pop properties))
687 (value (pop properties)))
688 (unless (memq type '(display image-size))
689 (put-text-property start (point) type value))))))))))
690 (kill-buffer image-buffer)))
691
692 (defun shr-image-from-data (data)
693 "Return an image from the data: URI content DATA."
694 (when (string-match
695 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
696 data)
697 (let ((param (match-string 4 data))
698 (payload (url-unhex-string (match-string 5 data))))
699 (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
700 (setq payload (base64-decode-string payload)))
701 payload)))
702
703 (defun shr-put-image (data alt &optional flags)
704 "Put image DATA with a string ALT. Return image."
705 (if (display-graphic-p)
706 (let* ((size (cdr (assq 'size flags)))
707 (start (point))
708 (image (cond
709 ((eq size 'original)
710 (create-image data nil t :ascent 100))
711 ((eq size 'full)
712 (ignore-errors
713 (shr-rescale-image data t)))
714 (t
715 (ignore-errors
716 (shr-rescale-image data))))))
717 (when image
718 ;; When inserting big-ish pictures, put them at the
719 ;; beginning of the line.
720 (when (and (> (current-column) 0)
721 (> (car (image-size image t)) 400))
722 (insert "\n"))
723 (if (eq size 'original)
724 (insert-sliced-image image (or alt "*") nil 20 1)
725 (insert-image image (or alt "*")))
726 (put-text-property start (point) 'image-size size)
727 (when (cond ((fboundp 'image-multi-frame-p)
728 ;; Only animate multi-frame things that specify a
729 ;; delay; eg animated gifs as opposed to
730 ;; multi-page tiffs. FIXME?
731 (cdr (image-multi-frame-p image)))
732 ((fboundp 'image-animated-p)
733 (image-animated-p image)))
734 (image-animate image nil 60)))
735 image)
736 (insert alt)))
737
738 (defun shr-rescale-image (data &optional force)
739 "Rescale DATA, if too big, to fit the current buffer.
740 If FORCE, rescale the image anyway."
741 (let ((image (create-image data nil t :ascent 100)))
742 (if (or (not (fboundp 'imagemagick-types))
743 (not (get-buffer-window (current-buffer))))
744 image
745 (let* ((size (image-size image t))
746 (width (car size))
747 (height (cdr size))
748 (edges (window-inside-pixel-edges
749 (get-buffer-window (current-buffer))))
750 (window-width (truncate (* shr-max-image-proportion
751 (- (nth 2 edges) (nth 0 edges)))))
752 (window-height (truncate (* shr-max-image-proportion
753 (- (nth 3 edges) (nth 1 edges)))))
754 scaled-image)
755 (when (or force
756 (> height window-height))
757 (setq image (or (create-image data 'imagemagick t
758 :height window-height
759 :ascent 100)
760 image))
761 (setq size (image-size image t)))
762 (when (> (car size) window-width)
763 (setq image (or
764 (create-image data 'imagemagick t
765 :width window-width
766 :ascent 100)
767 image)))
768 image))))
769
770 ;; url-cache-extract autoloads url-cache.
771 (declare-function url-cache-create-filename "url-cache" (url))
772 (autoload 'mm-disable-multibyte "mm-util")
773 (autoload 'browse-url-mail "browse-url")
774
775 (defun shr-get-image-data (url)
776 "Get image data for URL.
777 Return a string with image data."
778 (with-temp-buffer
779 (mm-disable-multibyte)
780 (when (ignore-errors
781 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
782 t)
783 (when (or (search-forward "\n\n" nil t)
784 (search-forward "\r\n\r\n" nil t))
785 (buffer-substring (point) (point-max))))))
786
787 (defun shr-image-displayer (content-function)
788 "Return a function to display an image.
789 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
790 is an argument. The function to be returned takes three arguments URL,
791 START, and END. Note that START and END should be markers."
792 `(lambda (url start end)
793 (when url
794 (if (string-match "\\`cid:" url)
795 ,(when content-function
796 `(let ((image (funcall ,content-function
797 (substring url (match-end 0)))))
798 (when image
799 (goto-char start)
800 (funcall shr-put-image-function
801 image (buffer-substring start end))
802 (delete-region (point) end))))
803 (url-retrieve url 'shr-image-fetched
804 (list (current-buffer) start end)
805 t t)))))
806
807 (defun shr-heading (cont &rest types)
808 (shr-ensure-paragraph)
809 (apply #'shr-fontize-cont cont types)
810 (shr-ensure-paragraph))
811
812 (defun shr-urlify (start url &optional title)
813 (when (and title (string-match "ctx" title)) (debug))
814 (shr-add-font start (point) 'shr-link)
815 (add-text-properties
816 start (point)
817 (list 'shr-url url
818 'help-echo (if title (format "%s (%s)" url title) url)
819 'local-map shr-map)))
820
821 (defun shr-encode-url (url)
822 "Encode URL."
823 (browse-url-url-encode-chars url "[)$ ]"))
824
825 (autoload 'shr-color-visible "shr-color")
826 (autoload 'shr-color->hexadecimal "shr-color")
827
828 (defun shr-color-check (fg bg)
829 "Check that FG is visible on BG.
830 Returns (fg bg) with corrected values.
831 Returns nil if the colors that would be used are the default
832 ones, in case fg and bg are nil."
833 (when (or fg bg)
834 (let ((fixed (cond ((null fg) 'fg)
835 ((null bg) 'bg))))
836 ;; Convert colors to hexadecimal, or set them to default.
837 (let ((fg (or (shr-color->hexadecimal fg)
838 (frame-parameter nil 'foreground-color)))
839 (bg (or (shr-color->hexadecimal bg)
840 (frame-parameter nil 'background-color))))
841 (cond ((eq fixed 'bg)
842 ;; Only return the new fg
843 (list nil (cadr (shr-color-visible bg fg t))))
844 ((eq fixed 'fg)
845 ;; Invert args and results and return only the new bg
846 (list (cadr (shr-color-visible fg bg t)) nil))
847 (t
848 (shr-color-visible bg fg)))))))
849
850 (defun shr-colorize-region (start end fg &optional bg)
851 (when (and (not shr-inhibit-decoration)
852 (or fg bg))
853 (let ((new-colors (shr-color-check fg bg)))
854 (when new-colors
855 (when fg
856 (add-face-text-property start end
857 (list :foreground (cadr new-colors))
858 t))
859 (when bg
860 (add-face-text-property start end
861 (list :background (car new-colors))
862 t)))
863 new-colors)))
864
865 (defun shr-expand-newlines (start end color)
866 (save-restriction
867 ;; Skip past all white space at the start and ends.
868 (goto-char start)
869 (skip-chars-forward " \t\n")
870 (beginning-of-line)
871 (setq start (point))
872 (goto-char end)
873 (skip-chars-backward " \t\n")
874 (forward-line 1)
875 (setq end (point))
876 (narrow-to-region start end)
877 (let ((width (shr-buffer-width))
878 column)
879 (goto-char (point-min))
880 (while (not (eobp))
881 (end-of-line)
882 (when (and (< (setq column (current-column)) width)
883 (< (setq column (shr-previous-newline-padding-width column))
884 width))
885 (let ((overlay (make-overlay (point) (1+ (point)))))
886 (overlay-put overlay 'before-string
887 (concat
888 (mapconcat
889 (lambda (overlay)
890 (let ((string (plist-get
891 (overlay-properties overlay)
892 'before-string)))
893 (if (not string)
894 ""
895 (overlay-put overlay 'before-string "")
896 string)))
897 (overlays-at (point))
898 "")
899 (propertize (make-string (- width column) ? )
900 'face (list :background color))))))
901 (forward-line 1)))))
902
903 (defun shr-previous-newline-padding-width (width)
904 (let ((overlays (overlays-at (point)))
905 (previous-width 0))
906 (if (null overlays)
907 width
908 (dolist (overlay overlays)
909 (setq previous-width
910 (+ previous-width
911 (length (plist-get (overlay-properties overlay)
912 'before-string)))))
913 (+ width previous-width))))
914
915 ;;; Tag-specific rendering rules.
916
917 (defun shr-tag-body (cont)
918 (let* ((start (point))
919 (fgcolor (cdr (or (assq :fgcolor cont)
920 (assq :text cont))))
921 (bgcolor (cdr (assq :bgcolor cont)))
922 (shr-stylesheet (list (cons 'color fgcolor)
923 (cons 'background-color bgcolor))))
924 (shr-generic cont)
925 (shr-colorize-region start (point) fgcolor bgcolor)))
926
927 (defun shr-tag-style (cont)
928 )
929
930 (defun shr-tag-script (cont)
931 )
932
933 (defun shr-tag-comment (cont)
934 )
935
936 (defun shr-dom-to-xml (dom)
937 "Convert DOM into a string containing the xml representation."
938 (let ((arg " ")
939 (text ""))
940 (dolist (sub (cdr dom))
941 (cond
942 ((listp (cdr sub))
943 (setq text (concat text (shr-dom-to-xml sub))))
944 ((eq (car sub) 'text)
945 (setq text (concat text (cdr sub))))
946 (t
947 (setq arg (concat arg (format "%s=\"%s\" "
948 (substring (symbol-name (car sub)) 1)
949 (cdr sub)))))))
950 (format "<%s%s>%s</%s>"
951 (car dom)
952 (substring arg 0 (1- (length arg)))
953 text
954 (car dom))))
955
956 (defun shr-tag-svg (cont)
957 (when (image-type-available-p 'svg)
958 (funcall shr-put-image-function
959 (shr-dom-to-xml (cons 'svg cont))
960 "SVG Image")))
961
962 (defun shr-tag-sup (cont)
963 (let ((start (point)))
964 (shr-generic cont)
965 (put-text-property start (point) 'display '(raise 0.5))))
966
967 (defun shr-tag-sub (cont)
968 (let ((start (point)))
969 (shr-generic cont)
970 (put-text-property start (point) 'display '(raise -0.5))))
971
972 (defun shr-tag-label (cont)
973 (shr-generic cont)
974 (shr-ensure-paragraph))
975
976 (defun shr-tag-p (cont)
977 (shr-ensure-paragraph)
978 (shr-indent)
979 (shr-generic cont)
980 (shr-ensure-paragraph))
981
982 (defun shr-tag-div (cont)
983 (shr-ensure-newline)
984 (shr-indent)
985 (shr-generic cont)
986 (shr-ensure-newline))
987
988 (defun shr-tag-s (cont)
989 (shr-fontize-cont cont 'shr-strike-through))
990
991 (defun shr-tag-del (cont)
992 (shr-fontize-cont cont 'shr-strike-through))
993
994 (defun shr-tag-b (cont)
995 (shr-fontize-cont cont 'bold))
996
997 (defun shr-tag-i (cont)
998 (shr-fontize-cont cont 'italic))
999
1000 (defun shr-tag-em (cont)
1001 (shr-fontize-cont cont 'italic))
1002
1003 (defun shr-tag-strong (cont)
1004 (shr-fontize-cont cont 'bold))
1005
1006 (defun shr-tag-u (cont)
1007 (shr-fontize-cont cont 'underline))
1008
1009 (defun shr-parse-style (style)
1010 (when style
1011 (save-match-data
1012 (when (string-match "\n" style)
1013 (setq style (replace-match " " t t style))))
1014 (let ((plist nil))
1015 (dolist (elem (split-string style ";"))
1016 (when elem
1017 (setq elem (split-string elem ":"))
1018 (when (and (car elem)
1019 (cadr elem))
1020 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1021 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1022 (when (string-match " *!important\\'" value)
1023 (setq value (substring value 0 (match-beginning 0))))
1024 (push (cons (intern name obarray)
1025 value)
1026 plist)))))
1027 plist)))
1028
1029 (defun shr-tag-base (cont)
1030 (let ((base (cdr (assq :href cont))))
1031 (when base
1032 (setq shr-base (shr-parse-base base))))
1033 (shr-generic cont))
1034
1035 (defun shr-tag-a (cont)
1036 (let ((url (cdr (assq :href cont)))
1037 (title (cdr (assq :title cont)))
1038 (start (point))
1039 shr-start)
1040 (shr-generic cont)
1041 (when (and url
1042 (not shr-inhibit-decoration))
1043 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1044
1045 (defun shr-tag-object (cont)
1046 (let ((start (point))
1047 url)
1048 (dolist (elem cont)
1049 (when (eq (car elem) 'embed)
1050 (setq url (or url (cdr (assq :src (cdr elem))))))
1051 (when (and (eq (car elem) 'param)
1052 (equal (cdr (assq :name (cdr elem))) "movie"))
1053 (setq url (or url (cdr (assq :value (cdr elem)))))))
1054 (when url
1055 (shr-insert " [multimedia] ")
1056 (shr-urlify start (shr-expand-url url)))
1057 (shr-generic cont)))
1058
1059 (defun shr-tag-video (cont)
1060 (let ((image (cdr (assq :poster cont)))
1061 (url (cdr (assq :src cont)))
1062 (start (point)))
1063 (shr-tag-img nil image)
1064 (shr-urlify start (shr-expand-url url))))
1065
1066 (defun shr-tag-img (cont &optional url)
1067 (when (or url
1068 (and cont
1069 (cdr (assq :src cont))))
1070 (when (and (> (current-column) 0)
1071 (not (eq shr-state 'image)))
1072 (insert "\n"))
1073 (let ((alt (cdr (assq :alt cont)))
1074 (url (shr-expand-url (or url (cdr (assq :src cont))))))
1075 (let ((start (point-marker)))
1076 (when (zerop (length alt))
1077 (setq alt "*"))
1078 (cond
1079 ((or (member (cdr (assq :height cont)) '("0" "1"))
1080 (member (cdr (assq :width cont)) '("0" "1")))
1081 ;; Ignore zero-sized or single-pixel images.
1082 )
1083 ((and (not shr-inhibit-images)
1084 (string-match "\\`data:" url))
1085 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1086 (if image
1087 (funcall shr-put-image-function image alt)
1088 (insert alt))))
1089 ((and (not shr-inhibit-images)
1090 (string-match "\\`cid:" url))
1091 (let ((url (substring url (match-end 0)))
1092 image)
1093 (if (or (not shr-content-function)
1094 (not (setq image (funcall shr-content-function url))))
1095 (insert alt)
1096 (funcall shr-put-image-function image alt))))
1097 ((or shr-inhibit-images
1098 (and shr-blocked-images
1099 (string-match shr-blocked-images url)))
1100 (setq shr-start (point))
1101 (let ((shr-state 'space))
1102 (if (> (string-width alt) 8)
1103 (shr-insert (truncate-string-to-width alt 8))
1104 (shr-insert alt))))
1105 ((and (not shr-ignore-cache)
1106 (url-is-cached (shr-encode-url url)))
1107 (funcall shr-put-image-function (shr-get-image-data url) alt))
1108 (t
1109 (insert alt " ")
1110 (when (and shr-ignore-cache
1111 (url-is-cached (shr-encode-url url)))
1112 (let ((file (url-cache-create-filename (shr-encode-url url))))
1113 (when (file-exists-p file)
1114 (delete-file file))))
1115 (url-queue-retrieve
1116 (shr-encode-url url) 'shr-image-fetched
1117 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1118 t t)))
1119 (when (zerop shr-table-depth) ;; We are not in a table.
1120 (put-text-property start (point) 'keymap shr-map)
1121 (put-text-property start (point) 'shr-alt alt)
1122 (put-text-property start (point) 'image-url url)
1123 (put-text-property start (point) 'image-displayer
1124 (shr-image-displayer shr-content-function))
1125 (put-text-property start (point) 'help-echo alt))
1126 (setq shr-state 'image)))))
1127
1128 (defun shr-tag-pre (cont)
1129 (let ((shr-folding-mode 'none))
1130 (shr-ensure-newline)
1131 (shr-indent)
1132 (shr-generic cont)
1133 (shr-ensure-newline)))
1134
1135 (defun shr-tag-blockquote (cont)
1136 (shr-ensure-paragraph)
1137 (shr-indent)
1138 (let ((shr-indentation (+ shr-indentation 4)))
1139 (shr-generic cont))
1140 (shr-ensure-paragraph))
1141
1142 (defun shr-tag-dl (cont)
1143 (shr-ensure-paragraph)
1144 (shr-generic cont)
1145 (shr-ensure-paragraph))
1146
1147 (defun shr-tag-dt (cont)
1148 (shr-ensure-newline)
1149 (shr-generic cont)
1150 (shr-ensure-newline))
1151
1152 (defun shr-tag-dd (cont)
1153 (shr-ensure-newline)
1154 (let ((shr-indentation (+ shr-indentation 4)))
1155 (shr-generic cont)))
1156
1157 (defun shr-tag-ul (cont)
1158 (shr-ensure-paragraph)
1159 (let ((shr-list-mode 'ul))
1160 (shr-generic cont))
1161 (shr-ensure-paragraph))
1162
1163 (defun shr-tag-ol (cont)
1164 (shr-ensure-paragraph)
1165 (let ((shr-list-mode 1))
1166 (shr-generic cont))
1167 (shr-ensure-paragraph))
1168
1169 (defun shr-tag-li (cont)
1170 (shr-ensure-newline)
1171 (shr-indent)
1172 (let* ((bullet
1173 (if (numberp shr-list-mode)
1174 (prog1
1175 (format "%d " shr-list-mode)
1176 (setq shr-list-mode (1+ shr-list-mode)))
1177 shr-bullet))
1178 (shr-indentation (+ shr-indentation (length bullet))))
1179 (insert bullet)
1180 (shr-generic cont)))
1181
1182 (defun shr-tag-br (cont)
1183 (when (and (not (bobp))
1184 ;; Only add a newline if we break the current line, or
1185 ;; the previous line isn't a blank line.
1186 (or (not (bolp))
1187 (and (> (- (point) 2) (point-min))
1188 (not (= (char-after (- (point) 2)) ?\n)))))
1189 (insert "\n")
1190 (shr-indent))
1191 (shr-generic cont))
1192
1193 (defun shr-tag-span (cont)
1194 (shr-generic cont))
1195
1196 (defun shr-tag-h1 (cont)
1197 (shr-heading cont 'bold 'underline))
1198
1199 (defun shr-tag-h2 (cont)
1200 (shr-heading cont 'bold))
1201
1202 (defun shr-tag-h3 (cont)
1203 (shr-heading cont 'italic))
1204
1205 (defun shr-tag-h4 (cont)
1206 (shr-heading cont))
1207
1208 (defun shr-tag-h5 (cont)
1209 (shr-heading cont))
1210
1211 (defun shr-tag-h6 (cont)
1212 (shr-heading cont))
1213
1214 (defun shr-tag-hr (cont)
1215 (shr-ensure-newline)
1216 (insert (make-string shr-width shr-hr-line) "\n"))
1217
1218 (defun shr-tag-title (cont)
1219 (shr-heading cont 'bold 'underline))
1220
1221 (defun shr-tag-font (cont)
1222 (let* ((start (point))
1223 (color (cdr (assq :color cont)))
1224 (shr-stylesheet (nconc (list (cons 'color color))
1225 shr-stylesheet)))
1226 (shr-generic cont)
1227 (when color
1228 (shr-colorize-region start (point) color
1229 (cdr (assq 'background-color shr-stylesheet))))))
1230
1231 ;;; Table rendering algorithm.
1232
1233 ;; Table rendering is the only complicated thing here. We do this by
1234 ;; first counting how many TDs there are in each TR, and registering
1235 ;; how wide they think they should be ("width=45%", etc). Then we
1236 ;; render each TD separately (this is done in temporary buffers, so
1237 ;; that we can use all the rendering machinery as if we were in the
1238 ;; main buffer). Now we know how much space each TD really takes, so
1239 ;; we then render everything again with the new widths, and finally
1240 ;; insert all these boxes into the main buffer.
1241 (defun shr-tag-table-1 (cont)
1242 (setq cont (or (cdr (assq 'tbody cont))
1243 cont))
1244 (let* ((shr-inhibit-images t)
1245 (shr-table-depth (1+ shr-table-depth))
1246 (shr-kinsoku-shorten t)
1247 ;; Find all suggested widths.
1248 (columns (shr-column-specs cont))
1249 ;; Compute how many characters wide each TD should be.
1250 (suggested-widths (shr-pro-rate-columns columns))
1251 ;; Do a "test rendering" to see how big each TD is (this can
1252 ;; be smaller (if there's little text) or bigger (if there's
1253 ;; unbreakable text).
1254 (sketch (shr-make-table cont suggested-widths))
1255 ;; Compute the "natural" width by setting each column to 500
1256 ;; characters and see how wide they really render.
1257 (natural (shr-make-table cont (make-vector (length columns) 500)))
1258 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1259 ;; This probably won't work very well.
1260 (when (> (+ (loop for width across sketch-widths
1261 summing (1+ width))
1262 shr-indentation 1)
1263 (frame-width))
1264 (setq truncate-lines t))
1265 ;; Then render the table again with these new "hard" widths.
1266 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)))
1267
1268 (defun shr-tag-table (cont)
1269 (shr-ensure-paragraph)
1270 (let* ((caption (cdr (assq 'caption cont)))
1271 (header (cdr (assq 'thead cont)))
1272 (body (or (cdr (assq 'tbody cont)) cont))
1273 (footer (cdr (assq 'tfoot cont)))
1274 (bgcolor (cdr (assq :bgcolor cont)))
1275 (start (point))
1276 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1277 shr-stylesheet))
1278 (nheader (if header (shr-max-columns header)))
1279 (nbody (if body (shr-max-columns body)))
1280 (nfooter (if footer (shr-max-columns footer))))
1281 (if (and (not caption)
1282 (not header)
1283 (not (cdr (assq 'tbody cont)))
1284 (not (cdr (assq 'tr cont)))
1285 (not footer))
1286 ;; The table is totally invalid and just contains random junk.
1287 ;; Try to output it anyway.
1288 (shr-generic cont)
1289 ;; It's a real table, so render it.
1290 (shr-tag-table-1
1291 (nconc
1292 (if caption `((tr (td ,@caption))))
1293 (if header
1294 (if footer
1295 ;; hader + body + footer
1296 (if (= nheader nbody)
1297 (if (= nbody nfooter)
1298 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
1299 (nconc `((tr (td (table (tbody ,@header ,@body)))))
1300 (if (= nfooter 1)
1301 footer
1302 `((tr (td (table (tbody ,@footer))))))))
1303 (nconc `((tr (td (table (tbody ,@header)))))
1304 (if (= nbody nfooter)
1305 `((tr (td (table (tbody ,@body ,@footer)))))
1306 (nconc `((tr (td (table (tbody ,@body)))))
1307 (if (= nfooter 1)
1308 footer
1309 `((tr (td (table (tbody ,@footer))))))))))
1310 ;; header + body
1311 (if (= nheader nbody)
1312 `((tr (td (table (tbody ,@header ,@body)))))
1313 (if (= nheader 1)
1314 `(,@header (tr (td (table (tbody ,@body)))))
1315 `((tr (td (table (tbody ,@header))))
1316 (tr (td (table (tbody ,@body))))))))
1317 (if footer
1318 ;; body + footer
1319 (if (= nbody nfooter)
1320 `((tr (td (table (tbody ,@body ,@footer)))))
1321 (nconc `((tr (td (table (tbody ,@body)))))
1322 (if (= nfooter 1)
1323 footer
1324 `((tr (td (table (tbody ,@footer))))))))
1325 (if caption
1326 `((tr (td (table (tbody ,@body)))))
1327 body))))))
1328 (when bgcolor
1329 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1330 bgcolor))
1331 ;; Finally, insert all the images after the table. The Emacs buffer
1332 ;; model isn't strong enough to allow us to put the images actually
1333 ;; into the tables.
1334 (when (zerop shr-table-depth)
1335 (dolist (elem (shr-find-elements cont 'img))
1336 (shr-tag-img (cdr elem))))))
1337
1338 (defun shr-find-elements (cont type)
1339 (let (result)
1340 (dolist (elem cont)
1341 (cond ((eq (car elem) type)
1342 (push elem result))
1343 ((consp (cdr elem))
1344 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1345 (nreverse result)))
1346
1347 (defun shr-insert-table (table widths)
1348 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1349 "collapse"))
1350 (shr-table-vertical-line (if collapse "" shr-table-vertical-line)))
1351 (unless collapse
1352 (shr-insert-table-ruler widths))
1353 (dolist (row table)
1354 (let ((start (point))
1355 (height (let ((max 0))
1356 (dolist (column row)
1357 (setq max (max max (cadr column))))
1358 max)))
1359 (dotimes (i height)
1360 (shr-indent)
1361 (insert shr-table-vertical-line "\n"))
1362 (dolist (column row)
1363 (goto-char start)
1364 (let ((lines (nth 2 column)))
1365 (dolist (line lines)
1366 (end-of-line)
1367 (insert line shr-table-vertical-line)
1368 (forward-line 1))
1369 ;; Add blank lines at padding at the bottom of the TD,
1370 ;; possibly.
1371 (dotimes (i (- height (length lines)))
1372 (end-of-line)
1373 (let ((start (point)))
1374 (insert (make-string (string-width (car lines)) ? )
1375 shr-table-vertical-line)
1376 (when (nth 4 column)
1377 (shr-add-font start (1- (point))
1378 (list :background (nth 4 column)))))
1379 (forward-line 1)))))
1380 (unless collapse
1381 (shr-insert-table-ruler widths)))))
1382
1383 (defun shr-insert-table-ruler (widths)
1384 (when (and (bolp)
1385 (> shr-indentation 0))
1386 (shr-indent))
1387 (insert shr-table-corner)
1388 (dotimes (i (length widths))
1389 (insert (make-string (aref widths i) shr-table-horizontal-line)
1390 shr-table-corner))
1391 (insert "\n"))
1392
1393 (defun shr-table-widths (table natural-table suggested-widths)
1394 (let* ((length (length suggested-widths))
1395 (widths (make-vector length 0))
1396 (natural-widths (make-vector length 0)))
1397 (dolist (row table)
1398 (let ((i 0))
1399 (dolist (column row)
1400 (aset widths i (max (aref widths i) column))
1401 (setq i (1+ i)))))
1402 (dolist (row natural-table)
1403 (let ((i 0))
1404 (dolist (column row)
1405 (aset natural-widths i (max (aref natural-widths i) column))
1406 (setq i (1+ i)))))
1407 (let ((extra (- (apply '+ (append suggested-widths nil))
1408 (apply '+ (append widths nil))))
1409 (expanded-columns 0))
1410 ;; We have extra, unused space, so divide this space amongst the
1411 ;; columns.
1412 (when (> extra 0)
1413 ;; If the natural width is wider than the rendered width, we
1414 ;; want to allow the column to expand.
1415 (dotimes (i length)
1416 (when (> (aref natural-widths i) (aref widths i))
1417 (setq expanded-columns (1+ expanded-columns))))
1418 (dotimes (i length)
1419 (when (> (aref natural-widths i) (aref widths i))
1420 (aset widths i (min
1421 (aref natural-widths i)
1422 (+ (/ extra expanded-columns)
1423 (aref widths i))))))))
1424 widths))
1425
1426 (defun shr-make-table (cont widths &optional fill)
1427 (or (cadr (assoc (list cont widths fill) shr-content-cache))
1428 (let ((data (shr-make-table-1 cont widths fill)))
1429 (push (list (list cont widths fill) data)
1430 shr-content-cache)
1431 data)))
1432
1433 (defun shr-make-table-1 (cont widths &optional fill)
1434 (let ((trs nil)
1435 (shr-inhibit-decoration (not fill)))
1436 (dolist (row cont)
1437 (when (eq (car row) 'tr)
1438 (let ((tds nil)
1439 (columns (cdr row))
1440 (i 0)
1441 column)
1442 (while (< i (length widths))
1443 (setq column (pop columns))
1444 (when (or (memq (car column) '(td th))
1445 (null column))
1446 (push (shr-render-td (cdr column) (aref widths i) fill)
1447 tds)
1448 (setq i (1+ i))))
1449 (push (nreverse tds) trs))))
1450 (nreverse trs)))
1451
1452 (defun shr-render-td (cont width fill)
1453 (with-temp-buffer
1454 (let ((bgcolor (cdr (assq :bgcolor cont)))
1455 (fgcolor (cdr (assq :fgcolor cont)))
1456 (style (cdr (assq :style cont)))
1457 (shr-stylesheet shr-stylesheet)
1458 actual-colors)
1459 (when style
1460 (setq style (and (string-match "color" style)
1461 (shr-parse-style style))))
1462 (when bgcolor
1463 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1464 (when fgcolor
1465 (setq style (nconc (list (cons 'color fgcolor)) style)))
1466 (when style
1467 (setq shr-stylesheet (append style shr-stylesheet)))
1468 (let ((shr-width width)
1469 (shr-indentation 0))
1470 (shr-descend (cons 'td cont)))
1471 ;; Delete padding at the bottom of the TDs.
1472 (delete-region
1473 (point)
1474 (progn
1475 (skip-chars-backward " \t\n")
1476 (end-of-line)
1477 (point)))
1478 (goto-char (point-min))
1479 (let ((max 0))
1480 (while (not (eobp))
1481 (end-of-line)
1482 (setq max (max max (current-column)))
1483 (forward-line 1))
1484 (when fill
1485 (goto-char (point-min))
1486 ;; If the buffer is totally empty, then put a single blank
1487 ;; line here.
1488 (if (zerop (buffer-size))
1489 (insert (make-string width ? ))
1490 ;; Otherwise, fill the buffer.
1491 (let ((align (cdr (assq :align cont)))
1492 length)
1493 (while (not (eobp))
1494 (end-of-line)
1495 (setq length (- width (current-column)))
1496 (when (> length 0)
1497 (cond
1498 ((equal align "right")
1499 (beginning-of-line)
1500 (insert (make-string length ? )))
1501 ((equal align "center")
1502 (insert (make-string (/ length 2) ? ))
1503 (beginning-of-line)
1504 (insert (make-string (- length (/ length 2)) ? )))
1505 (t
1506 (insert (make-string length ? )))))
1507 (forward-line 1))))
1508 (when style
1509 (setq actual-colors
1510 (shr-colorize-region
1511 (point-min) (point-max)
1512 (cdr (assq 'color shr-stylesheet))
1513 (cdr (assq 'background-color shr-stylesheet))))))
1514 (if fill
1515 (list max
1516 (count-lines (point-min) (point-max))
1517 (split-string (buffer-string) "\n")
1518 nil
1519 (car actual-colors))
1520 max)))))
1521
1522 (defun shr-buffer-width ()
1523 (goto-char (point-min))
1524 (let ((max 0))
1525 (while (not (eobp))
1526 (end-of-line)
1527 (setq max (max max (current-column)))
1528 (forward-line 1))
1529 max))
1530
1531 (defun shr-pro-rate-columns (columns)
1532 (let ((total-percentage 0)
1533 (widths (make-vector (length columns) 0)))
1534 (dotimes (i (length columns))
1535 (setq total-percentage (+ total-percentage (aref columns i))))
1536 (setq total-percentage (/ 1.0 total-percentage))
1537 (dotimes (i (length columns))
1538 (aset widths i (max (truncate (* (aref columns i)
1539 total-percentage
1540 (- shr-width (1+ (length columns)))))
1541 10)))
1542 widths))
1543
1544 ;; Return a summary of the number and shape of the TDs in the table.
1545 (defun shr-column-specs (cont)
1546 (let ((columns (make-vector (shr-max-columns cont) 1)))
1547 (dolist (row cont)
1548 (when (eq (car row) 'tr)
1549 (let ((i 0))
1550 (dolist (column (cdr row))
1551 (when (memq (car column) '(td th))
1552 (let ((width (cdr (assq :width (cdr column)))))
1553 (when (and width
1554 (string-match "\\([0-9]+\\)%" width)
1555 (not (zerop (setq width (string-to-number
1556 (match-string 1 width))))))
1557 (aset columns i (/ width 100.0))))
1558 (setq i (1+ i)))))))
1559 columns))
1560
1561 (defun shr-count (cont elem)
1562 (let ((i 0))
1563 (dolist (sub cont)
1564 (when (eq (car sub) elem)
1565 (setq i (1+ i))))
1566 i))
1567
1568 (defun shr-max-columns (cont)
1569 (let ((max 0))
1570 (dolist (row cont)
1571 (when (eq (car row) 'tr)
1572 (setq max (max max (+ (shr-count (cdr row) 'td)
1573 (shr-count (cdr row) 'th))))))
1574 max))
1575
1576 ;; Emacs less than 24.3
1577 (unless (fboundp 'add-face-text-property)
1578 (defun add-face-text-property (beg end face &optional appendp object)
1579 "Combine FACE BEG and END."
1580 (let ((b beg))
1581 (while (< b end)
1582 (let ((oldval (get-text-property b 'face)))
1583 (put-text-property
1584 b (setq b (next-single-property-change b 'face nil end))
1585 'face (cond ((null oldval)
1586 face)
1587 ((and (consp oldval)
1588 (not (keywordp (car oldval))))
1589 (if appendp
1590 (nconc oldval (list face))
1591 (cons face oldval)))
1592 (t
1593 (if appendp
1594 (list oldval face)
1595 (list face oldval))))))))))
1596
1597 (provide 'shr)
1598
1599 ;; Local Variables:
1600 ;; coding: utf-8
1601 ;; End:
1602
1603 ;;; shr.el ends here