1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2015 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 ;; 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.
33 (eval-when-compile (require 'cl
))
34 (eval-when-compile (require 'url
)) ;For url-filename's setf handler.
40 "Simple HTML Renderer"
44 (defcustom shr-max-image-proportion
0.9
45 "How big pictures displayed are in relation to the window they're in.
46 A value of 0.7 means that they are allowed to take up 70% of the
47 width and height of the window. If they are larger than this,
48 and Emacs supports it, then the images will be rescaled down to
54 (defcustom shr-blocked-images nil
55 "Images that have URLs matching this regexp will be blocked."
58 :type
'(choice (const nil
) regexp
))
60 (defcustom shr-table-horizontal-line nil
61 "Character used to draw horizontal table lines.
62 If nil, don't draw horizontal table lines."
64 :type
'(choice (const nil
) character
))
66 (defcustom shr-table-vertical-line ?\s
67 "Character used to draw vertical table lines."
71 (defcustom shr-table-corner ?\s
72 "Character used to draw table corners."
76 (defcustom shr-hr-line ?-
77 "Character used to draw hr lines."
81 (defcustom shr-width nil
82 "Frame width to use for rendering.
83 May either be an integer specifying a fixed width in characters,
84 or nil, meaning that the full width of the window should be
87 :type
'(choice (integer :tag
"Fixed width in characters")
88 (const :tag
"Use the width of the window" nil
))
91 (defcustom shr-bullet
"* "
92 "Bullet used for unordered lists.
93 Alternative suggestions are:
100 (defcustom shr-external-browser
'browse-url-default-browser
101 "Function used to launch an external browser."
106 (defcustom shr-image-animate t
107 "Non nil means that images that can be animated will be."
112 (defvar shr-content-function nil
113 "If bound, this should be a function that will return the content.
114 This is used for cid: URLs, and the function is called with the
115 cid: URL as the argument.")
117 (defvar shr-put-image-function
'shr-put-image
118 "Function called to put image and alt string.")
120 (defface shr-strike-through
'((t (:strike-through t
)))
121 "Font for <s> elements."
125 '((t (:inherit link
)))
126 "Font for link elements."
129 (defvar shr-inhibit-images nil
130 "If non-nil, inhibit loading images.")
132 ;;; Internal variables.
134 (defvar shr-folding-mode nil
)
135 (defvar shr-state nil
)
136 (defvar shr-start nil
)
137 (defvar shr-indentation
0)
138 (defvar shr-internal-width
(or shr-width
(1- (window-width))))
139 (defvar shr-list-mode nil
)
140 (defvar shr-content-cache nil
)
141 (defvar shr-kinsoku-shorten nil
)
142 (defvar shr-table-depth
0)
143 (defvar shr-stylesheet nil
)
144 (defvar shr-base nil
)
146 (defvar shr-warning nil
)
147 (defvar shr-ignore-cache nil
)
148 (defvar shr-external-rendering-functions nil
)
149 (defvar shr-target-id nil
)
150 (defvar shr-inhibit-decoration nil
)
151 (defvar shr-table-separator-length
1)
154 (let ((map (make-sparse-keymap)))
155 (define-key map
"a" 'shr-show-alt-text
)
156 (define-key map
"i" 'shr-browse-image
)
157 (define-key map
"z" 'shr-zoom-image
)
158 (define-key map
[?
\t] 'shr-next-link
)
159 (define-key map
[?\M-
\t] 'shr-previous-link
)
160 (define-key map
[follow-link
] 'mouse-face
)
161 (define-key map
[mouse-2
] 'shr-browse-url
)
162 (define-key map
"I" 'shr-insert-image
)
163 (define-key map
"w" 'shr-copy-url
)
164 (define-key map
"u" 'shr-copy-url
)
165 (define-key map
"v" 'shr-browse-url
)
166 (define-key map
"o" 'shr-save-contents
)
167 (define-key map
"\r" 'shr-browse-url
)
170 ;; Public functions and commands.
171 (declare-function libxml-parse-html-region
"xml.c"
172 (start end
&optional base-url
))
174 (defun shr-render-buffer (buffer)
175 "Display the HTML rendering of the current buffer."
176 (interactive (list (current-buffer)))
177 (or (fboundp 'libxml-parse-html-region
)
178 (error "This function requires Emacs to be compiled with libxml2"))
179 (pop-to-buffer "*html*")
182 (with-current-buffer buffer
183 (libxml-parse-html-region (point-min) (point-max))))
184 (goto-char (point-min)))
187 (defun shr-render-region (begin end
&optional buffer
)
188 "Display the HTML rendering of the region between BEGIN and END."
190 (unless (fboundp 'libxml-parse-html-region
)
191 (error "This function requires Emacs to be compiled with libxml2"))
192 (with-current-buffer (or buffer
(current-buffer))
193 (let ((dom (libxml-parse-html-region begin end
)))
194 (delete-region begin end
)
196 (shr-insert-document dom
))))
199 (defun shr-insert-document (dom)
200 "Render the parsed document DOM into the current buffer.
201 DOM should be a parse tree as generated by
202 `libxml-parse-html-region' or similar."
203 (setq shr-content-cache nil
)
204 (let ((start (point))
210 (shr-internal-width (or shr-width
(1- (window-width)))))
212 (shr-remove-trailing-whitespace start
(point))
214 (message "%s" shr-warning
))))
216 (defun shr-remove-trailing-whitespace (start end
)
217 (let ((width (window-width)))
219 (narrow-to-region start end
)
223 (when (> (shr-previous-newline-padding-width (current-column)) width
)
224 (dolist (overlay (overlays-at (point)))
225 (when (overlay-get overlay
'before-string
)
226 (overlay-put overlay
'before-string nil
))))
229 (defun shr-copy-url (&optional image-url
)
230 "Copy the URL under point to the kill ring.
231 If IMAGE-URL (the prefix) is non-nil, or there is no link under
232 point, but there is an image under point then copy the URL of the
233 image under point instead.
234 If called twice, then try to fetch the URL and see whether it
235 redirects somewhere else."
237 (let ((url (or (get-text-property (point) 'shr-url
)
238 (get-text-property (point) 'image-url
))))
241 (message "No URL under point"))
242 ;; Resolve redirected URLs.
243 ((equal url
(car kill-ring
))
248 (eq (car a
) :redirect
))
251 (goto-char (point-min))
252 ;; Remove common tracking junk from the URL.
253 (when (re-search-forward ".utm_.*" nil t
)
254 (replace-match "" t t
))
255 (message "Copied %s" (buffer-string))
256 (copy-region-as-kill (point-min) (point-max)))))
258 ;; Copy the URL to the kill ring.
261 (insert (url-encode-url url
))
262 (copy-region-as-kill (point-min) (point-max))
263 (message "Copied %s" (buffer-string)))))))
265 (defun shr-next-link ()
266 "Skip to the next link."
268 (let ((skip (text-property-any (point) (point-max) 'help-echo nil
)))
270 (not (setq skip
(text-property-not-all skip
(point-max)
272 (message "No next link")
274 (message "%s" (get-text-property (point) 'help-echo
)))))
276 (defun shr-previous-link ()
277 "Skip to the previous link."
279 (let ((start (point))
281 ;; Skip past the current link.
282 (while (and (not (bobp))
283 (get-text-property (point) 'help-echo
))
285 ;; Find the previous link.
286 (while (and (not (bobp))
287 (not (setq found
(get-text-property (point) 'help-echo
))))
291 (message "No previous link")
293 ;; Put point at the start of the link.
294 (while (and (not (bobp))
295 (get-text-property (point) 'help-echo
))
298 (message "%s" (get-text-property (point) 'help-echo
)))))
300 (defun shr-show-alt-text ()
301 "Show the ALT text of the image under point."
303 (let ((text (get-text-property (point) 'shr-alt
)))
305 (message "No image under point")
306 (message "%s" (shr-fold-text text
)))))
308 (defun shr-browse-image (&optional copy-url
)
309 "Browse the image under point.
310 If COPY-URL (the prefix if called interactively) is non-nil, copy
311 the URL of the image to the kill buffer instead."
313 (let ((url (get-text-property (point) 'image-url
)))
316 (message "No image under point"))
320 (copy-region-as-kill (point-min) (point-max))
321 (message "Copied %s" url
)))
323 (message "Browsing %s..." url
)
326 (defun shr-insert-image ()
327 "Insert the image under point into the buffer."
329 (let ((url (get-text-property (point) 'image-url
)))
331 (message "No image under point")
332 (message "Inserting %s..." url
)
333 (url-retrieve url
'shr-image-fetched
334 (list (current-buffer) (1- (point)) (point-marker))
337 (defun shr-zoom-image ()
338 "Toggle the image size.
339 The size will be rotated between the default size, the original
340 size, and full-buffer size."
342 (let ((url (get-text-property (point) 'image-url
))
343 (size (get-text-property (point) 'image-size
))
344 (buffer-read-only nil
))
346 (message "No image under point")
347 ;; Delete the old picture.
348 (while (get-text-property (point) 'image-url
)
351 (let ((start (point)))
352 (while (get-text-property (point) 'image-url
)
355 (put-text-property start
(point) 'display nil
)
356 (when (> (- (point) start
) 2)
357 (delete-region start
(1- (point)))))
358 (message "Inserting %s..." url
)
359 (url-retrieve url
'shr-image-fetched
360 (list (current-buffer) (1- (point)) (point-marker)
362 (cond ((or (eq size
'default
)
371 ;;; Utility functions.
373 (defsubst shr-generic
(dom)
374 (dolist (sub (dom-children dom
))
379 (defun shr-descend (dom)
382 ;; Allow other packages to override (or provide) rendering
384 (cdr (assq (dom-tag dom
) shr-external-rendering-functions
))
385 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
)))
386 (style (dom-attr dom
'style
))
387 (shr-stylesheet shr-stylesheet
)
388 (shr-depth (1+ shr-depth
))
390 ;; shr uses about 12 frames per nested node.
391 (if (> shr-depth
(/ max-specpdl-size
12))
392 (setq shr-warning
"Too deeply nested to render properly; consider increasing `max-specpdl-size'")
394 (if (string-match "color\\|display\\|border-collapse" style
)
395 (setq shr-stylesheet
(nconc (shr-parse-style style
)
398 ;; If we have a display:none, then just ignore this part of the DOM.
399 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
400 (if (fboundp function
)
401 (funcall function dom
)
403 (when (and shr-target-id
404 (equal (dom-attr dom
'id
) shr-target-id
))
405 ;; If the element was empty, we don't have anything to put the
406 ;; anchor on. So just insert a dummy character.
407 (when (= start
(point))
409 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
410 ;; If style is set, then this node has set the color.
414 (cdr (assq 'color shr-stylesheet
))
415 (cdr (assq 'background-color shr-stylesheet
))))))))
417 (defun shr-fold-text (text)
418 (if (zerop (length text
))
421 (let ((shr-indentation 0)
424 (shr-internal-width (window-width)))
428 (define-inline shr-char-breakable-p
(char)
429 "Return non-nil if a line can be broken before and after CHAR."
430 (inline-quote (aref fill-find-break-point-function-table
,char
)))
431 (define-inline shr-char-nospace-p
(char)
432 "Return non-nil if no space is required before and after CHAR."
433 (inline-quote (aref fill-nospace-between-words-table
,char
)))
435 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
436 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
437 ;; parentheses, and so on, that should not be placed in the beginning
438 ;; of a line or the end of a line.
439 (define-inline shr-char-kinsoku-bol-p
(char)
440 "Return non-nil if a line ought not to begin with CHAR."
441 (inline-letevals (char)
442 (inline-quote (and (not (eq ,char ?
'))
443 (aref (char-category-set ,char
) ?
>)))))
444 (define-inline shr-char-kinsoku-eol-p
(char)
445 "Return non-nil if a line ought not to end with CHAR."
446 (inline-quote (aref (char-category-set ,char
) ?
<)))
447 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
448 (load "kinsoku" nil t
))
450 (defun shr-insert (text)
451 (when (and (eq shr-state
'image
)
453 (not (string-match "\\`[ \t\n]+\\'" text
)))
455 (setq shr-state nil
))
457 ((eq shr-folding-mode
'none
)
460 (when (and (string-match "\\`[ \t\n ]" text
)
462 (not (eq (char-after (1- (point))) ?
)))
464 (dolist (elem (split-string text
"[ \f\t\n\r\v ]+" t
))
466 (> shr-indentation
0))
468 ;; No space is needed behind a wide character categorized as
469 ;; kinsoku-bol, between characters both categorized as nospace,
470 ;; or at the beginning of a line.
472 (when (and (> (current-column) shr-indentation
)
473 (eq (preceding-char) ?
)
474 (or (= (line-beginning-position) (1- (point)))
475 (and (shr-char-breakable-p
476 (setq prev
(char-after (- (point) 2))))
477 (shr-char-kinsoku-bol-p prev
))
478 (and (shr-char-nospace-p prev
)
479 (shr-char-nospace-p (aref elem
0)))))
481 ;; The shr-start is a special variable that is used to pass
482 ;; upwards the first point in the buffer where the text really
485 (setq shr-start
(point)))
489 (while (and (> (current-column) shr-internal-width
)
490 (> shr-internal-width
0)
492 (setq found
(shr-find-fill-point))
494 (when (eq (preceding-char) ?
)
498 ;; No space is needed at the beginning of a line.
499 (when (eq (following-char) ?
)
501 (when (> shr-indentation
0)
504 (if (<= (current-column) shr-internal-width
)
506 ;; In case we couldn't get a valid break point (because of a
507 ;; word that's longer than `shr-internal-width'), just break anyway.
509 (when (> shr-indentation
0)
511 (unless (string-match "[ \t\r\n ]\\'" text
)
514 (defun shr-find-fill-point ()
515 (when (> (move-to-column shr-internal-width
) shr-internal-width
)
519 (while (not (or (setq failed
(<= (current-column) shr-indentation
))
520 (eq (preceding-char) ?
)
521 (eq (following-char) ?
)
522 (shr-char-breakable-p (preceding-char))
523 (shr-char-breakable-p (following-char))
524 (and (shr-char-kinsoku-bol-p (preceding-char))
525 (shr-char-breakable-p (following-char))
526 (not (shr-char-kinsoku-bol-p (following-char))))
527 (shr-char-kinsoku-eol-p (following-char))
531 ;; There's no breakable point, so we give it up.
534 (unless shr-kinsoku-shorten
535 (while (setq found
(re-search-forward
536 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
537 (line-end-position) 'move
)))
539 (not (match-beginning 1)))
540 (goto-char (match-beginning 0)))))
543 ;; Don't put kinsoku-bol characters at the beginning of a line,
544 ;; or kinsoku-eol characters at the end of a line.
547 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
548 (shr-char-kinsoku-eol-p (preceding-char)))
550 (when (setq failed
(<= (current-column) shr-indentation
))
551 ;; There's no breakable point that doesn't violate kinsoku,
552 ;; so we look for the second best position.
555 (<= (current-column) shr-internal-width
))
558 (shr-char-kinsoku-eol-p (following-char)))))
560 ((shr-char-kinsoku-eol-p (preceding-char))
561 ;; Find backward the point where kinsoku-eol characters begin.
566 (and (> (setq count
(1- count
)) 0)
567 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
568 (or (shr-char-kinsoku-eol-p (preceding-char))
569 (shr-char-kinsoku-bol-p (following-char)))))))
570 (when (setq failed
(<= (current-column) shr-indentation
))
571 ;; There's no breakable point that doesn't violate kinsoku,
572 ;; so we go to the second best position.
573 (if (looking-at "\\(\\c<+\\)\\c<")
574 (goto-char (match-end 1))
576 ((shr-char-kinsoku-bol-p (following-char))
577 ;; Find forward the point where kinsoku-bol characters end.
581 (and (>= (setq count
(1- count
)) 0)
582 (shr-char-kinsoku-bol-p (following-char))
583 (shr-char-breakable-p (following-char))))))))
584 (when (eq (following-char) ?
)
588 (defun shr-parse-base (url)
589 ;; Always chop off anchors.
590 (when (string-match "#.*" url
)
591 (setq url
(substring url
0 (match-beginning 0))))
592 ;; NB: <base href="" > URI may itself be relative to the document s URI
593 (setq url
(shr-expand-url url
))
594 (let* ((parsed (url-generic-parse-url url
))
595 (local (url-filename parsed
)))
596 (setf (url-filename parsed
) "")
597 ;; Chop off the bit after the last slash.
598 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
599 (setq local
(match-string 1 local
)))
600 ;; Always make the local bit end with a slash.
601 (when (and (not (zerop (length local
)))
602 (not (eq (aref local
(1- (length local
))) ?
/)))
603 (setq local
(concat local
"/")))
604 (list (url-recreate-url parsed
)
609 (autoload 'url-expand-file-name
"url-expand")
611 ;; FIXME This needs some tests writing.
612 ;; Does it even need to exist, given that url-expand-file-name does?
613 (defun shr-expand-url (url &optional base
)
616 ;; shr-parse-base should never call this with non-nil base!
617 (shr-parse-base base
)
618 ;; Bound by the parser.
620 (when (zerop (length url
))
624 (string-match "\\`[a-z]*:" url
))
625 ;; Absolute or empty URI
626 (or url
(nth 3 base
)))
627 ((eq (aref url
0) ?
/)
628 (if (and (> (length url
) 1)
629 (eq (aref url
1) ?
/))
630 ;; //host...; just use the protocol
631 (concat (nth 2 base
) ":" url
)
632 ;; Just use the host name part.
633 (concat (car base
) url
)))
634 ((eq (aref url
0) ?
#)
635 ;; A link to an anchor.
636 (concat (nth 3 base
) url
))
639 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
641 (defun shr-ensure-newline ()
642 (unless (zerop (current-column))
645 (defun shr-ensure-paragraph ()
647 (if (<= (current-column) shr-indentation
)
648 (unless (save-excursion
654 ;; If the current line is totally blank, and doesn't even
655 ;; have any face properties set, then delete the blank
657 (and (looking-at " *$")
658 (not (get-text-property (point) 'face
))
659 (not (= (next-single-property-change (point) 'face nil
661 (line-end-position)))))
662 (delete-region (match-beginning 0) (match-end 0))
666 (when (> shr-indentation
0)
667 (insert (make-string shr-indentation ?
))))
669 (defun shr-fontize-dom (dom &rest types
)
673 (shr-add-font (or shr-start
(point)) (point) type
))))
675 ;; Add face to the region, but avoid putting the font properties on
676 ;; blank text at the start of the line, and the newline at the end, to
678 (defun shr-add-font (start end type
)
679 (unless shr-inhibit-decoration
682 (while (< (point) end
)
684 (skip-chars-forward " "))
685 (add-face-text-property (point) (min (line-end-position) end
) type t
)
686 (if (< (line-end-position) end
)
690 (defun shr-mouse-browse-url (ev)
691 "Browse the URL under the mouse cursor."
696 (defun shr-browse-url (&optional external mouse-event
)
697 "Browse the URL under point.
698 If EXTERNAL, browse the URL using `shr-external-browser'."
699 (interactive (list current-prefix-arg last-nonmenu-event
))
700 (mouse-set-point mouse-event
)
701 (let ((url (get-text-property (point) 'shr-url
)))
704 (message "No link under point"))
705 ((string-match "^mailto:" url
)
706 (browse-url-mail url
))
709 (funcall shr-external-browser url
)
710 (browse-url url
))))))
712 (defun shr-save-contents (directory)
713 "Save the contents from URL in a file."
714 (interactive "DSave contents of URL to directory: ")
715 (let ((url (get-text-property (point) 'shr-url
)))
717 (message "No link under point")
718 (url-retrieve (shr-encode-url url
)
719 'shr-store-contents
(list url directory
)
722 (defun shr-store-contents (status url directory
)
723 (unless (plist-get status
:error
)
724 (when (or (search-forward "\n\n" nil t
)
725 (search-forward "\r\n\r\n" nil t
))
726 (write-region (point) (point-max)
727 (expand-file-name (file-name-nondirectory url
)
730 (defun shr-image-fetched (status buffer start end
&optional flags
)
731 (let ((image-buffer (current-buffer)))
732 (when (and (buffer-name buffer
)
733 (not (plist-get status
:error
)))
734 (url-store-in-cache image-buffer
)
735 (when (or (search-forward "\n\n" nil t
)
736 (search-forward "\r\n\r\n" nil t
))
737 (let ((data (shr-parse-image-data)))
738 (with-current-buffer buffer
740 (let ((alt (buffer-substring start end
))
741 (properties (text-properties-at start
))
742 (inhibit-read-only t
))
743 (delete-region start end
)
745 (funcall shr-put-image-function data alt flags
)
747 (let ((type (pop properties
))
748 (value (pop properties
)))
749 (unless (memq type
'(display image-size
))
750 (put-text-property start
(point) type value
))))))))))
751 (kill-buffer image-buffer
)))
753 (defun shr-image-from-data (data)
754 "Return an image from the data: URI content DATA."
756 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
758 (let ((param (match-string 4 data
))
759 (payload (url-unhex-string (match-string 5 data
))))
760 (when (string-match "^.*\\(;[ \t]*base64\\)$" param
)
761 (setq payload
(base64-decode-string payload
)))
764 ;; Behind display-graphic-p test.
765 (declare-function image-size
"image.c" (spec &optional pixels frame
))
766 (declare-function image-animate
"image" (image &optional index limit
))
768 (defun shr-put-image (spec alt
&optional flags
)
769 "Insert image SPEC with a string ALT. Return image.
770 SPEC is either an image data blob, or a list where the first
771 element is the data blob and the second element is the content-type."
772 (if (display-graphic-p)
773 (let* ((size (cdr (assq 'size flags
)))
774 (data (if (consp spec
)
777 (content-type (and (consp spec
)
782 (create-image data nil t
:ascent
100
783 :format content-type
))
784 ((eq content-type
'image
/svg
+xml
)
785 (create-image data
'svg t
:ascent
100))
788 (shr-rescale-image data content-type
)))
791 (shr-rescale-image data content-type
))))))
793 ;; When inserting big-ish pictures, put them at the
794 ;; beginning of the line.
795 (when (and (> (current-column) 0)
796 (> (car (image-size image t
)) 400))
798 (if (eq size
'original
)
799 (insert-sliced-image image
(or alt
"*") nil
20 1)
800 (insert-image image
(or alt
"*")))
801 (put-text-property start
(point) 'image-size size
)
802 (when (and shr-image-animate
803 (cond ((fboundp 'image-multi-frame-p
)
804 ;; Only animate multi-frame things that specify a
805 ;; delay; eg animated gifs as opposed to
806 ;; multi-page tiffs. FIXME?
807 (cdr (image-multi-frame-p image
)))
808 ((fboundp 'image-animated-p
)
809 (image-animated-p image
))))
810 (image-animate image nil
60)))
814 (defun shr-rescale-image (data &optional content-type
)
815 "Rescale DATA, if too big, to fit the current buffer."
816 (if (not (and (fboundp 'imagemagick-types
)
817 (get-buffer-window (current-buffer))))
818 (create-image data nil t
:ascent
100)
819 (let ((edges (window-inside-pixel-edges
820 (get-buffer-window (current-buffer)))))
824 :max-width
(truncate (* shr-max-image-proportion
825 (- (nth 2 edges
) (nth 0 edges
))))
826 :max-height
(truncate (* shr-max-image-proportion
827 (- (nth 3 edges
) (nth 1 edges
))))
828 :format content-type
))))
830 ;; url-cache-extract autoloads url-cache.
831 (declare-function url-cache-create-filename
"url-cache" (url))
832 (autoload 'mm-disable-multibyte
"mm-util")
833 (autoload 'browse-url-mail
"browse-url")
835 (defun shr-get-image-data (url)
836 "Get image data for URL.
837 Return a string with image data."
839 (mm-disable-multibyte)
841 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
843 (when (or (search-forward "\n\n" nil t
)
844 (search-forward "\r\n\r\n" nil t
))
845 (shr-parse-image-data)))))
847 (defun shr-parse-image-data ()
848 (let ((data (buffer-substring (point) (point-max)))
852 (narrow-to-region (point-min) (point))
853 (let ((content-type (mail-fetch-field "content-type")))
855 ;; Remove any comments in the type string.
856 (intern (replace-regexp-in-string ";.*" "" content-type
)
858 ;; SVG images may contain references to further images that we may
859 ;; want to block. So special-case these by parsing the XML data
860 ;; and remove the blocked bits.
861 (when (eq content-type
'image
/svg
+xml
)
864 (libxml-parse-xml-region (point) (point-max)))))
865 (list data content-type
)))
867 (defun shr-image-displayer (content-function)
868 "Return a function to display an image.
869 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
870 is an argument. The function to be returned takes three arguments URL,
871 START, and END. Note that START and END should be markers."
872 `(lambda (url start end
)
874 (if (string-match "\\`cid:" url
)
875 ,(when content-function
876 `(let ((image (funcall ,content-function
877 (substring url
(match-end 0)))))
880 (funcall shr-put-image-function
881 image
(buffer-substring start end
))
882 (delete-region (point) end
))))
883 (url-retrieve url
'shr-image-fetched
884 (list (current-buffer) start end
)
887 (defun shr-heading (dom &rest types
)
888 (shr-ensure-paragraph)
889 (apply #'shr-fontize-dom dom types
)
890 (shr-ensure-paragraph))
892 (defun shr-urlify (start url
&optional title
)
893 (shr-add-font start
(point) 'shr-link
)
897 'help-echo
(let ((iri (or (with-demoted-errors
899 (decode-coding-string
900 (url-unhex-string url
)
903 (if title
(format "%s (%s)" iri title
) iri
))
905 'mouse-face
'highlight
908 (defun shr-encode-url (url)
910 (browse-url-url-encode-chars url
"[)$ ]"))
912 (autoload 'shr-color-visible
"shr-color")
913 (autoload 'shr-color-
>hexadecimal
"shr-color")
915 (defun shr-color-check (fg bg
)
916 "Check that FG is visible on BG.
917 Returns (fg bg) with corrected values.
918 Returns nil if the colors that would be used are the default
919 ones, in case fg and bg are nil."
921 (let ((fixed (cond ((null fg
) 'fg
)
923 ;; Convert colors to hexadecimal, or set them to default.
924 (let ((fg (or (shr-color->hexadecimal fg
)
925 (frame-parameter nil
'foreground-color
)))
926 (bg (or (shr-color->hexadecimal bg
)
927 (frame-parameter nil
'background-color
))))
928 (cond ((eq fixed
'bg
)
929 ;; Only return the new fg
930 (list nil
(cadr (shr-color-visible bg fg t
))))
932 ;; Invert args and results and return only the new bg
933 (list (cadr (shr-color-visible fg bg t
)) nil
))
935 (shr-color-visible bg fg
)))))))
937 (defun shr-colorize-region (start end fg
&optional bg
)
938 (when (and (not shr-inhibit-decoration
)
940 (let ((new-colors (shr-color-check fg bg
)))
943 (add-face-text-property start end
944 (list :foreground
(cadr new-colors
))
947 (add-face-text-property start end
948 (list :background
(car new-colors
))
952 (defun shr-expand-newlines (start end color
)
954 ;; Skip past all white space at the start and ends.
956 (skip-chars-forward " \t\n")
960 (skip-chars-backward " \t\n")
963 (narrow-to-region start end
)
964 (let ((width (shr-buffer-width))
966 (goto-char (point-min))
969 (when (and (< (setq column
(current-column)) width
)
970 (< (setq column
(shr-previous-newline-padding-width column
))
972 (let ((overlay (make-overlay (point) (1+ (point)))))
973 (overlay-put overlay
'before-string
977 (let ((string (plist-get
978 (overlay-properties overlay
)
982 (overlay-put overlay
'before-string
"")
984 (overlays-at (point))
986 (propertize (make-string (- width column
) ?
)
987 'face
(list :background color
))))))
990 (defun shr-previous-newline-padding-width (width)
991 (let ((overlays (overlays-at (point)))
995 (dolist (overlay overlays
)
998 (length (plist-get (overlay-properties overlay
)
1000 (+ width previous-width
))))
1002 ;;; Tag-specific rendering rules.
1004 (defun shr-tag-body (dom)
1005 (let* ((start (point))
1006 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1007 (bgcolor (dom-attr dom
'bgcolor
))
1008 (shr-stylesheet (list (cons 'color fgcolor
)
1009 (cons 'background-color bgcolor
))))
1011 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1013 (defun shr-tag-style (_dom)
1016 (defun shr-tag-script (_dom)
1019 (defun shr-tag-comment (_dom)
1022 (defun shr-dom-to-xml (dom)
1027 (defun shr-dom-print (dom)
1028 "Convert DOM into a string containing the xml representation."
1029 (insert (format "<%s" (dom-tag dom
)))
1030 (dolist (attr (dom-attributes dom
))
1031 ;; Ignore attributes that start with a colon because they are
1032 ;; private elements.
1033 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1034 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1037 (dolist (elem (dom-children dom
))
1041 ((eq (dom-tag elem
) 'comment
)
1043 ((or (not (eq (dom-tag elem
) 'image
))
1044 ;; Filter out blocked elements inside the SVG image.
1045 (not (setq url
(dom-attr elem
':xlink
:href
)))
1046 (not shr-blocked-images
)
1047 (not (string-match shr-blocked-images url
)))
1049 (shr-dom-print elem
)))))
1050 (insert (format "</%s>" (dom-tag dom
))))
1052 (defun shr-tag-svg (dom)
1053 (when (and (image-type-available-p 'svg
)
1054 (not shr-inhibit-images
))
1055 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1058 (defun shr-tag-sup (dom)
1059 (let ((start (point)))
1061 (put-text-property start
(point) 'display
'(raise 0.5))))
1063 (defun shr-tag-sub (dom)
1064 (let ((start (point)))
1066 (put-text-property start
(point) 'display
'(raise -
0.5))))
1068 (defun shr-tag-label (dom)
1070 (shr-ensure-paragraph))
1072 (defun shr-tag-p (dom)
1073 (shr-ensure-paragraph)
1076 (shr-ensure-paragraph))
1078 (defun shr-tag-div (dom)
1079 (shr-ensure-newline)
1082 (shr-ensure-newline))
1084 (defun shr-tag-s (dom)
1085 (shr-fontize-dom dom
'shr-strike-through
))
1087 (defun shr-tag-del (dom)
1088 (shr-fontize-dom dom
'shr-strike-through
))
1090 (defun shr-tag-b (dom)
1091 (shr-fontize-dom dom
'bold
))
1093 (defun shr-tag-i (dom)
1094 (shr-fontize-dom dom
'italic
))
1096 (defun shr-tag-em (dom)
1097 (shr-fontize-dom dom
'italic
))
1099 (defun shr-tag-strong (dom)
1100 (shr-fontize-dom dom
'bold
))
1102 (defun shr-tag-u (dom)
1103 (shr-fontize-dom dom
'underline
))
1105 (defun shr-parse-style (style)
1108 (when (string-match "\n" style
)
1109 (setq style
(replace-match " " t t style
))))
1111 (dolist (elem (split-string style
";"))
1113 (setq elem
(split-string elem
":"))
1114 (when (and (car elem
)
1116 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1117 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1118 (when (string-match " *!important\\'" value
)
1119 (setq value
(substring value
0 (match-beginning 0))))
1120 (push (cons (intern name obarray
)
1125 (defun shr-tag-base (dom)
1126 (when-let (base (dom-attr dom
'href
))
1127 (setq shr-base
(shr-parse-base base
)))
1130 (defun shr-tag-a (dom)
1131 (let ((url (dom-attr dom
'href
))
1132 (title (dom-attr dom
'title
))
1136 (when (and shr-target-id
1137 (equal (dom-attr dom
'name
) shr-target-id
))
1138 ;; We have a zero-length <a name="foo"> element, so just
1139 ;; insert... something.
1140 (when (= start
(point))
1141 (shr-ensure-newline)
1143 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1145 (not shr-inhibit-decoration
))
1146 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1148 (defun shr-tag-object (dom)
1149 (unless shr-inhibit-images
1150 (let ((start (point))
1151 url multimedia image
)
1152 (when-let (type (dom-attr dom
'type
))
1153 (when (string-match "\\`image/svg" type
)
1154 (setq url
(dom-attr dom
'data
)
1156 (dolist (child (dom-non-text-children dom
))
1158 ((eq (dom-tag child
) 'embed
)
1159 (setq url
(or url
(dom-attr child
'src
))
1161 ((and (eq (dom-tag child
) 'param
)
1162 (equal (dom-attr child
'name
) "movie"))
1163 (setq url
(or url
(dom-attr child
'value
))
1168 (shr-tag-img dom url
)
1171 (shr-insert " [multimedia] ")
1172 (shr-urlify start
(shr-expand-url url
)))))
1174 (shr-generic dom
)))))
1176 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1182 "Preferences for media types.
1183 The key element should be a regexp matched against the type of the source or
1184 url if no type is specified. The value should be a float in the range 0.0 to
1185 1.0. Media elements with higher value are preferred."
1188 :type
'(alist :key-type regexp
:value-type float
))
1190 (defun shr--get-media-pref (elem)
1191 "Determine the preference for ELEM.
1192 The preference is a float determined from `shr-prefer-media-type'."
1193 (let ((type (dom-attr elem
'type
))
1196 (setq type
(dom-attr elem
'src
)))
1198 (dolist (pref shr-prefer-media-type-alist
)
1201 (string-match-p (car pref
) type
))
1202 (setq p
(cdr pref
)))))
1205 (defun shr--extract-best-source (dom &optional url pref
)
1206 "Extract the best `:src' property from <source> blocks in DOM."
1207 (setq pref
(or pref -
1.0))
1209 (dolist (elem (dom-non-text-children dom
))
1210 (when (and (eq (dom-tag elem
) 'source
)
1213 (shr--get-media-pref elem
))))
1215 url
(dom-attr elem
'src
))
1216 ;; libxml's html parser isn't HTML5 compliant and non terminated
1217 ;; source tags might end up as children. So recursion it is...
1218 (dolist (child (dom-non-text-children elem
))
1219 (when (eq (dom-tag child
) 'source
)
1220 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1221 (when (< pref
(cdr ret
))
1223 pref
(cdr ret
)))))))))
1226 (defun shr-tag-video (dom)
1227 (let ((image (dom-attr dom
'poster
))
1228 (url (dom-attr dom
'src
))
1231 (setq url
(car (shr--extract-best-source dom
))))
1233 (shr-tag-img nil image
)
1234 (shr-insert " [video] "))
1235 (shr-urlify start
(shr-expand-url url
))))
1237 (defun shr-tag-audio (dom)
1238 (let ((url (dom-attr dom
'src
))
1241 (setq url
(car (shr--extract-best-source dom
))))
1242 (shr-insert " [audio] ")
1243 (shr-urlify start
(shr-expand-url url
))))
1245 (defun shr-tag-img (dom &optional url
)
1248 (> (length (dom-attr dom
'src
)) 0)))
1249 (when (and (> (current-column) 0)
1250 (not (eq shr-state
'image
)))
1252 (let ((alt (dom-attr dom
'alt
))
1253 (url (shr-expand-url (or url
(dom-attr dom
'src
)))))
1254 (let ((start (point-marker)))
1255 (when (zerop (length alt
))
1258 ((or (member (dom-attr dom
'height
) '("0" "1"))
1259 (member (dom-attr dom
'width
) '("0" "1")))
1260 ;; Ignore zero-sized or single-pixel images.
1262 ((and (not shr-inhibit-images
)
1263 (string-match "\\`data:" url
))
1264 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1266 (funcall shr-put-image-function image alt
)
1268 ((and (not shr-inhibit-images
)
1269 (string-match "\\`cid:" url
))
1270 (let ((url (substring url
(match-end 0)))
1272 (if (or (not shr-content-function
)
1273 (not (setq image
(funcall shr-content-function url
))))
1275 (funcall shr-put-image-function image alt
))))
1276 ((or shr-inhibit-images
1277 (and shr-blocked-images
1278 (string-match shr-blocked-images url
)))
1279 (setq shr-start
(point))
1280 (let ((shr-state 'space
))
1281 (if (> (string-width alt
) 8)
1282 (shr-insert (truncate-string-to-width alt
8))
1284 ((and (not shr-ignore-cache
)
1285 (url-is-cached (shr-encode-url url
)))
1286 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
1289 (when (and shr-ignore-cache
1290 (url-is-cached (shr-encode-url url
)))
1291 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1292 (when (file-exists-p file
)
1293 (delete-file file
))))
1295 (shr-encode-url url
) 'shr-image-fetched
1296 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
1298 (when (zerop shr-table-depth
) ;; We are not in a table.
1299 (put-text-property start
(point) 'keymap shr-map
)
1300 (put-text-property start
(point) 'shr-alt alt
)
1301 (put-text-property start
(point) 'image-url url
)
1302 (put-text-property start
(point) 'image-displayer
1303 (shr-image-displayer shr-content-function
))
1304 (put-text-property start
(point) 'help-echo
1305 (shr-fold-text (or (dom-attr dom
'title
) alt
))))
1306 (setq shr-state
'image
)))))
1308 (defun shr-tag-pre (dom)
1309 (let ((shr-folding-mode 'none
))
1310 (shr-ensure-newline)
1313 (shr-ensure-newline)))
1315 (defun shr-tag-blockquote (dom)
1316 (shr-ensure-paragraph)
1318 (let ((shr-indentation (+ shr-indentation
4)))
1320 (shr-ensure-paragraph))
1322 (defun shr-tag-dl (dom)
1323 (shr-ensure-paragraph)
1325 (shr-ensure-paragraph))
1327 (defun shr-tag-dt (dom)
1328 (shr-ensure-newline)
1330 (shr-ensure-newline))
1332 (defun shr-tag-dd (dom)
1333 (shr-ensure-newline)
1334 (let ((shr-indentation (+ shr-indentation
4)))
1337 (defun shr-tag-ul (dom)
1338 (shr-ensure-paragraph)
1339 (let ((shr-list-mode 'ul
))
1341 (shr-ensure-paragraph))
1343 (defun shr-tag-ol (dom)
1344 (shr-ensure-paragraph)
1345 (let ((shr-list-mode 1))
1347 (shr-ensure-paragraph))
1349 (defun shr-tag-li (dom)
1350 (shr-ensure-newline)
1353 (if (numberp shr-list-mode
)
1355 (format "%d " shr-list-mode
)
1356 (setq shr-list-mode
(1+ shr-list-mode
)))
1358 (shr-indentation (+ shr-indentation
(length bullet
))))
1362 (defun shr-tag-br (dom)
1363 (when (and (not (bobp))
1364 ;; Only add a newline if we break the current line, or
1365 ;; the previous line isn't a blank line.
1367 (and (> (- (point) 2) (point-min))
1368 (not (= (char-after (- (point) 2)) ?
\n)))))
1373 (defun shr-tag-span (dom)
1376 (defun shr-tag-h1 (dom)
1377 (shr-heading dom
'bold
'underline
))
1379 (defun shr-tag-h2 (dom)
1380 (shr-heading dom
'bold
))
1382 (defun shr-tag-h3 (dom)
1383 (shr-heading dom
'italic
))
1385 (defun shr-tag-h4 (dom)
1388 (defun shr-tag-h5 (dom)
1391 (defun shr-tag-h6 (dom)
1394 (defun shr-tag-hr (_dom)
1395 (shr-ensure-newline)
1396 (insert (make-string shr-internal-width shr-hr-line
) "\n"))
1398 (defun shr-tag-title (dom)
1399 (shr-heading dom
'bold
'underline
))
1401 (defun shr-tag-font (dom)
1402 (let* ((start (point))
1403 (color (dom-attr dom
'color
))
1404 (shr-stylesheet (nconc (list (cons 'color color
))
1408 (shr-colorize-region start
(point) color
1409 (cdr (assq 'background-color shr-stylesheet
))))))
1411 ;;; Table rendering algorithm.
1413 ;; Table rendering is the only complicated thing here. We do this by
1414 ;; first counting how many TDs there are in each TR, and registering
1415 ;; how wide they think they should be ("width=45%", etc). Then we
1416 ;; render each TD separately (this is done in temporary buffers, so
1417 ;; that we can use all the rendering machinery as if we were in the
1418 ;; main buffer). Now we know how much space each TD really takes, so
1419 ;; we then render everything again with the new widths, and finally
1420 ;; insert all these boxes into the main buffer.
1421 (defun shr-tag-table-1 (dom)
1422 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1423 (let* ((shr-inhibit-images t
)
1424 (shr-table-depth (1+ shr-table-depth
))
1425 (shr-kinsoku-shorten t
)
1426 ;; Find all suggested widths.
1427 (columns (shr-column-specs dom
))
1428 ;; Compute how many characters wide each TD should be.
1429 (suggested-widths (shr-pro-rate-columns columns
))
1430 ;; Do a "test rendering" to see how big each TD is (this can
1431 ;; be smaller (if there's little text) or bigger (if there's
1432 ;; unbreakable text).
1433 (sketch (shr-make-table dom suggested-widths
))
1434 ;; Compute the "natural" width by setting each column to 500
1435 ;; characters and see how wide they really render.
1436 (natural (shr-make-table dom
(make-vector (length columns
) 500)))
1437 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1438 ;; This probably won't work very well.
1439 (when (> (+ (loop for width across sketch-widths
1443 (setq truncate-lines t
))
1444 ;; Then render the table again with these new "hard" widths.
1445 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1447 (defun shr-tag-table (dom)
1448 (shr-ensure-paragraph)
1449 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1450 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1451 (body (dom-non-text-children (or (dom-child-by-tag dom
'tbody
)
1453 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1454 (bgcolor (dom-attr dom
'bgcolor
))
1456 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1458 (nheader (if header
(shr-max-columns header
)))
1459 (nbody (if body
(shr-max-columns body
)))
1460 (nfooter (if footer
(shr-max-columns footer
))))
1461 (if (and (not caption
)
1463 (not (dom-child-by-tag dom
'tbody
))
1464 (not (dom-child-by-tag dom
'tr
))
1466 ;; The table is totally invalid and just contains random junk.
1467 ;; Try to output it anyway.
1469 ;; It's a real table, so render it.
1473 (if caption
`((tr nil
(td nil
,@caption
))))
1476 ;; header + body + footer
1477 (if (= nheader nbody
)
1478 (if (= nbody nfooter
)
1479 `((tr nil
(td nil
(table nil
1481 ,@body
,@footer
)))))
1482 (nconc `((tr nil
(td nil
(table nil
1487 `((tr nil
(td nil
(table
1489 nil
,@footer
))))))))
1490 (nconc `((tr nil
(td nil
(table nil
(tbody
1492 (if (= nbody nfooter
)
1493 `((tr nil
(td nil
(table
1494 nil
(tbody nil
,@body
1496 (nconc `((tr nil
(td nil
(table
1501 `((tr nil
(td nil
(table
1507 (if (= nheader nbody
)
1508 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1511 `(,@header
(tr nil
(td nil
(table
1512 nil
(tbody nil
,@body
)))))
1513 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1514 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1517 (if (= nbody nfooter
)
1518 `((tr nil
(td nil
(table
1519 nil
(tbody nil
,@body
,@footer
)))))
1520 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1523 `((tr nil
(td nil
(table
1524 nil
(tbody nil
,@footer
)))))))))
1526 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1529 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1531 ;; Finally, insert all the images after the table. The Emacs buffer
1532 ;; model isn't strong enough to allow us to put the images actually
1534 (when (zerop shr-table-depth
)
1535 (dolist (elem (dom-by-tag dom
'object
))
1536 (shr-tag-object elem
))
1537 (dolist (elem (dom-by-tag dom
'img
))
1538 (shr-tag-img elem
)))))
1540 (defun shr-insert-table (table widths
)
1541 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
1543 (shr-table-separator-length (if collapse
0 1))
1544 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
)))
1546 (shr-insert-table-ruler widths
))
1548 (let ((start (point))
1549 (height (let ((max 0))
1550 (dolist (column row
)
1551 (setq max
(max max
(cadr column
))))
1555 (insert shr-table-vertical-line
"\n"))
1556 (dolist (column row
)
1558 (let ((lines (nth 2 column
)))
1559 (dolist (line lines
)
1561 (insert line shr-table-vertical-line
)
1563 ;; Add blank lines at padding at the bottom of the TD,
1565 (dotimes (i (- height
(length lines
)))
1567 (let ((start (point)))
1568 (insert (make-string (string-width (car lines
)) ?
)
1569 shr-table-vertical-line
)
1570 (when (nth 4 column
)
1571 (shr-add-font start
(1- (point))
1572 (list :background
(nth 4 column
)))))
1573 (forward-line 1)))))
1575 (shr-insert-table-ruler widths
)))))
1577 (defun shr-insert-table-ruler (widths)
1578 (when shr-table-horizontal-line
1580 (> shr-indentation
0))
1582 (insert shr-table-corner
)
1583 (dotimes (i (length widths
))
1584 (insert (make-string (aref widths i
) shr-table-horizontal-line
)
1588 (defun shr-table-widths (table natural-table suggested-widths
)
1589 (let* ((length (length suggested-widths
))
1590 (widths (make-vector length
0))
1591 (natural-widths (make-vector length
0)))
1594 (dolist (column row
)
1595 (aset widths i
(max (aref widths i
) column
))
1597 (dolist (row natural-table
)
1599 (dolist (column row
)
1600 (aset natural-widths i
(max (aref natural-widths i
) column
))
1602 (let ((extra (- (apply '+ (append suggested-widths nil
))
1603 (apply '+ (append widths nil
))))
1604 (expanded-columns 0))
1605 ;; We have extra, unused space, so divide this space amongst the
1608 ;; If the natural width is wider than the rendered width, we
1609 ;; want to allow the column to expand.
1611 (when (> (aref natural-widths i
) (aref widths i
))
1612 (setq expanded-columns
(1+ expanded-columns
))))
1614 (when (> (aref natural-widths i
) (aref widths i
))
1616 (aref natural-widths i
)
1617 (+ (/ extra expanded-columns
)
1618 (aref widths i
))))))))
1621 (defun shr-make-table (dom widths
&optional fill
)
1622 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
1623 (let ((data (shr-make-table-1 dom widths fill
)))
1624 (push (list (list dom widths fill
) data
)
1628 (defun shr-make-table-1 (dom widths
&optional fill
)
1630 (shr-inhibit-decoration (not fill
))
1631 (rowspans (make-vector (length widths
) 0))
1633 (dolist (row (dom-non-text-children dom
))
1634 (when (eq (dom-tag row
) 'tr
)
1636 (columns (dom-children row
))
1640 (while (< i
(length widths
))
1641 ;; If we previously had a rowspan definition, then that
1642 ;; means that we now have a "missing" td/th element here.
1643 ;; So just insert a dummy, empty one to (sort of) emulate
1646 (if (zerop (aref rowspans i
))
1648 (aset rowspans i
(1- (aref rowspans i
)))
1650 (when (and (not (stringp column
))
1651 (or (memq (dom-tag column
) '(td th
))
1653 (when-let (span (dom-attr column
'rowspan
))
1654 (aset rowspans i
(+ (aref rowspans i
)
1655 (1- (string-to-number span
)))))
1656 ;; Sanity check for invalid column-spans.
1657 (when (>= width-column
(length widths
))
1658 (setq width-column
0))
1661 (aref widths width-column
)
1664 (setq colspan
(dom-attr column colspan
)))
1665 (setq colspan
(min (string-to-number colspan
)
1666 ;; The colspan may be wrong, so
1667 ;; truncate it to the length of the
1668 ;; remaining columns.
1669 (- (length widths
) i
)))
1670 (dotimes (j (1- colspan
))
1671 (if (> (+ i
1 j
) (1- (length widths
)))
1672 (setq width
(aref widths
(1- (length widths
))))
1673 (setq width
(+ width
1674 shr-table-separator-length
1675 (aref widths
(+ i
1 j
))))))
1676 (setq width-column
(+ width-column
(1- colspan
))))
1679 (push (shr-render-td column width fill
)
1682 width-column
(1+ width-column
))))
1683 (push (nreverse tds
) trs
))))
1686 (defun shr-render-td (dom width fill
)
1688 (let ((bgcolor (dom-attr dom
'bgcolor
))
1689 (fgcolor (dom-attr dom
'fgcolor
))
1690 (style (dom-attr dom
'style
))
1691 (shr-stylesheet shr-stylesheet
)
1694 (setq style
(and (string-match "color" style
)
1695 (shr-parse-style style
))))
1697 (setq style
(nconc (list (cons 'background-color bgcolor
)) style
)))
1699 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1701 (setq shr-stylesheet
(append style shr-stylesheet
)))
1702 (let ((shr-internal-width width
)
1703 (shr-indentation 0))
1705 ;; Delete padding at the bottom of the TDs.
1709 (skip-chars-backward " \t\n")
1712 (goto-char (point-min))
1716 (setq max
(max max
(current-column)))
1719 (goto-char (point-min))
1720 ;; If the buffer is totally empty, then put a single blank
1722 (if (zerop (buffer-size))
1723 (insert (make-string width ?
))
1724 ;; Otherwise, fill the buffer.
1725 (let ((align (dom-attr dom
'align
))
1729 (setq length
(- width
(current-column)))
1732 ((equal align
"right")
1734 (insert (make-string length ?
)))
1735 ((equal align
"center")
1736 (insert (make-string (/ length
2) ?
))
1738 (insert (make-string (- length
(/ length
2)) ?
)))
1740 (insert (make-string length ?
)))))
1744 (shr-colorize-region
1745 (point-min) (point-max)
1746 (cdr (assq 'color shr-stylesheet
))
1747 (cdr (assq 'background-color shr-stylesheet
))))))
1750 (count-lines (point-min) (point-max))
1751 (split-string (buffer-string) "\n")
1753 (car actual-colors
))
1756 (defun shr-buffer-width ()
1757 (goto-char (point-min))
1761 (setq max
(max max
(current-column)))
1765 (defun shr-pro-rate-columns (columns)
1766 (let ((total-percentage 0)
1767 (widths (make-vector (length columns
) 0)))
1768 (dotimes (i (length columns
))
1769 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1770 (setq total-percentage
(/ 1.0 total-percentage
))
1771 (dotimes (i (length columns
))
1772 (aset widths i
(max (truncate (* (aref columns i
)
1774 (- shr-internal-width
1775 (1+ (length columns
)))))
1779 ;; Return a summary of the number and shape of the TDs in the table.
1780 (defun shr-column-specs (dom)
1781 (let ((columns (make-vector (shr-max-columns dom
) 1)))
1782 (dolist (row (dom-non-text-children dom
))
1783 (when (eq (dom-tag row
) 'tr
)
1785 (dolist (column (dom-children row
))
1786 (when (and (not (stringp column
))
1787 (memq (dom-tag column
) '(td th
)))
1788 (let ((width (dom-attr column
'width
)))
1790 (string-match "\\([0-9]+\\)%" width
)
1791 (not (zerop (setq width
(string-to-number
1792 (match-string 1 width
))))))
1793 (aset columns i
(/ width
100.0))))
1794 (setq i
(1+ i
)))))))
1797 (defun shr-count (dom elem
)
1799 (dolist (sub (dom-children dom
))
1800 (when (and (not (stringp sub
))
1801 (eq (dom-tag sub
) elem
))
1805 (defun shr-max-columns (dom)
1807 (dolist (row (dom-children dom
))
1808 (when (and (not (stringp row
))
1809 (eq (dom-tag row
) 'tr
))
1810 (setq max
(max max
(+ (shr-count row
'td
)
1811 (shr-count row
'th
))))))
1820 ;;; shr.el ends here