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-use-fonts nil
61 "If non-nil, use proportional fonts for text."
66 (defcustom shr-table-horizontal-line nil
67 "Character used to draw horizontal table lines.
68 If nil, don't draw horizontal table lines."
70 :type
'(choice (const nil
) character
))
72 (defcustom shr-table-vertical-line ?\s
73 "Character used to draw vertical table lines."
77 (defcustom shr-table-corner ?\s
78 "Character used to draw table corners."
82 (defcustom shr-hr-line ?-
83 "Character used to draw hr lines."
87 (defcustom shr-width nil
88 "Frame width to use for rendering.
89 May either be an integer specifying a fixed width in characters,
90 or nil, meaning that the full width of the window should be
93 :type
'(choice (integer :tag
"Fixed width in characters")
94 (const :tag
"Use the width of the window" nil
))
97 (defcustom shr-bullet
"* "
98 "Bullet used for unordered lists.
99 Alternative suggestions are:
106 (defcustom shr-external-browser
'browse-url-default-browser
107 "Function used to launch an external browser."
112 (defcustom shr-image-animate t
113 "Non nil means that images that can be animated will be."
118 (defvar shr-content-function nil
119 "If bound, this should be a function that will return the content.
120 This is used for cid: URLs, and the function is called with the
121 cid: URL as the argument.")
123 (defvar shr-put-image-function
'shr-put-image
124 "Function called to put image and alt string.")
126 (defface shr-strike-through
'((t (:strike-through t
)))
127 "Font for <s> elements."
131 '((t (:inherit link
)))
132 "Font for link elements."
135 (defvar shr-inhibit-images nil
136 "If non-nil, inhibit loading images.")
138 ;;; Internal variables.
140 (defvar shr-folding-mode nil
)
141 (defvar shr-start nil
)
142 (defvar shr-indentation
0)
143 (defvar shr-internal-width nil
)
144 (defvar shr-list-mode nil
)
145 (defvar shr-content-cache nil
)
146 (defvar shr-kinsoku-shorten nil
)
147 (defvar shr-table-depth
0)
148 (defvar shr-stylesheet nil
)
149 (defvar shr-base nil
)
151 (defvar shr-warning nil
)
152 (defvar shr-ignore-cache nil
)
153 (defvar shr-external-rendering-functions nil
)
154 (defvar shr-target-id nil
)
155 (defvar shr-inhibit-decoration nil
)
156 (defvar shr-table-separator-length
1)
157 (defvar shr-table-separator-pixel-width
0)
158 (defvar shr-table-id nil
)
159 (defvar shr-current-font nil
)
162 (let ((map (make-sparse-keymap)))
163 (define-key map
"a" 'shr-show-alt-text
)
164 (define-key map
"i" 'shr-browse-image
)
165 (define-key map
"z" 'shr-zoom-image
)
166 (define-key map
[?
\t] 'shr-next-link
)
167 (define-key map
[?\M-
\t] 'shr-previous-link
)
168 (define-key map
[follow-link
] 'mouse-face
)
169 (define-key map
[mouse-2
] 'shr-browse-url
)
170 (define-key map
"I" 'shr-insert-image
)
171 (define-key map
"w" 'shr-copy-url
)
172 (define-key map
"u" 'shr-copy-url
)
173 (define-key map
"v" 'shr-browse-url
)
174 (define-key map
"o" 'shr-save-contents
)
175 (define-key map
"\r" 'shr-browse-url
)
178 ;; Public functions and commands.
179 (declare-function libxml-parse-html-region
"xml.c"
180 (start end
&optional base-url
))
182 (defun shr-render-buffer (buffer)
183 "Display the HTML rendering of the current buffer."
184 (interactive (list (current-buffer)))
185 (or (fboundp 'libxml-parse-html-region
)
186 (error "This function requires Emacs to be compiled with libxml2"))
187 (pop-to-buffer "*html*")
190 (with-current-buffer buffer
191 (libxml-parse-html-region (point-min) (point-max))))
192 (goto-char (point-min)))
195 (defun shr-render-region (begin end
&optional buffer
)
196 "Display the HTML rendering of the region between BEGIN and END."
198 (unless (fboundp 'libxml-parse-html-region
)
199 (error "This function requires Emacs to be compiled with libxml2"))
200 (with-current-buffer (or buffer
(current-buffer))
201 (let ((dom (libxml-parse-html-region begin end
)))
202 (delete-region begin end
)
204 (shr-insert-document dom
))))
207 (defun shr-insert-document (dom)
208 "Render the parsed document DOM into the current buffer.
209 DOM should be a parse tree as generated by
210 `libxml-parse-html-region' or similar."
211 (setq shr-content-cache nil
)
212 (let ((start (point))
218 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
219 (shr-internal-width (or (and shr-width
220 (if (not shr-use-fonts
)
222 (* shr-width
(frame-char-width))))
223 (if (not shr-use-fonts
)
225 (- (window-pixel-width)
226 (* (frame-fringe-width) 2))))))
228 (shr-fill-lines start
(point))
229 (shr-remove-trailing-whitespace start
(point))
231 (message "%s" shr-warning
))))
233 (defun shr-remove-trailing-whitespace (start end
)
234 (let ((width (window-width)))
236 (narrow-to-region start end
)
240 (when (> (shr-previous-newline-padding-width (current-column)) width
)
241 (dolist (overlay (overlays-at (point)))
242 (when (overlay-get overlay
'before-string
)
243 (overlay-put overlay
'before-string nil
))))
246 (defun shr-copy-url (&optional image-url
)
247 "Copy the URL under point to the kill ring.
248 If IMAGE-URL (the prefix) is non-nil, or there is no link under
249 point, but there is an image under point then copy the URL of the
250 image under point instead.
251 If called twice, then try to fetch the URL and see whether it
252 redirects somewhere else."
254 (let ((url (or (get-text-property (point) 'shr-url
)
255 (get-text-property (point) 'image-url
))))
258 (message "No URL under point"))
259 ;; Resolve redirected URLs.
260 ((equal url
(car kill-ring
))
265 (eq (car a
) :redirect
))
268 (goto-char (point-min))
269 ;; Remove common tracking junk from the URL.
270 (when (re-search-forward ".utm_.*" nil t
)
271 (replace-match "" t t
))
272 (message "Copied %s" (buffer-string))
273 (copy-region-as-kill (point-min) (point-max)))))
275 ;; Copy the URL to the kill ring.
278 (insert (url-encode-url url
))
279 (copy-region-as-kill (point-min) (point-max))
280 (message "Copied %s" (buffer-string)))))))
282 (defun shr-next-link ()
283 "Skip to the next link."
285 (let ((skip (text-property-any (point) (point-max) 'help-echo nil
)))
287 (not (setq skip
(text-property-not-all skip
(point-max)
289 (message "No next link")
291 (message "%s" (get-text-property (point) 'help-echo
)))))
293 (defun shr-previous-link ()
294 "Skip to the previous link."
296 (let ((start (point))
298 ;; Skip past the current link.
299 (while (and (not (bobp))
300 (get-text-property (point) 'help-echo
))
302 ;; Find the previous link.
303 (while (and (not (bobp))
304 (not (setq found
(get-text-property (point) 'help-echo
))))
308 (message "No previous link")
310 ;; Put point at the start of the link.
311 (while (and (not (bobp))
312 (get-text-property (point) 'help-echo
))
315 (message "%s" (get-text-property (point) 'help-echo
)))))
317 (defun shr-show-alt-text ()
318 "Show the ALT text of the image under point."
320 (let ((text (get-text-property (point) 'shr-alt
)))
322 (message "No image under point")
323 (message "%s" (shr-fill-text text
)))))
325 (defun shr-browse-image (&optional copy-url
)
326 "Browse the image under point.
327 If COPY-URL (the prefix if called interactively) is non-nil, copy
328 the URL of the image to the kill buffer instead."
330 (let ((url (get-text-property (point) 'image-url
)))
333 (message "No image under point"))
337 (copy-region-as-kill (point-min) (point-max))
338 (message "Copied %s" url
)))
340 (message "Browsing %s..." url
)
343 (defun shr-insert-image ()
344 "Insert the image under point into the buffer."
346 (let ((url (get-text-property (point) 'image-url
)))
348 (message "No image under point")
349 (message "Inserting %s..." url
)
350 (url-retrieve url
'shr-image-fetched
351 (list (current-buffer) (1- (point)) (point-marker))
354 (defun shr-zoom-image ()
355 "Toggle the image size.
356 The size will be rotated between the default size, the original
357 size, and full-buffer size."
359 (let ((url (get-text-property (point) 'image-url
))
360 (size (get-text-property (point) 'image-size
))
361 (buffer-read-only nil
))
363 (message "No image under point")
364 ;; Delete the old picture.
365 (while (get-text-property (point) 'image-url
)
368 (let ((start (point)))
369 (while (get-text-property (point) 'image-url
)
372 (put-text-property start
(point) 'display nil
)
373 (when (> (- (point) start
) 2)
374 (delete-region start
(1- (point)))))
375 (message "Inserting %s..." url
)
376 (url-retrieve url
'shr-image-fetched
377 (list (current-buffer) (1- (point)) (point-marker)
379 (cond ((or (eq size
'default
)
388 ;;; Utility functions.
390 (defsubst shr-generic
(dom)
391 (dolist (sub (dom-children dom
))
396 (defun shr-descend (dom)
399 ;; Allow other packages to override (or provide) rendering
401 (cdr (assq (dom-tag dom
) shr-external-rendering-functions
))
402 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
)))
403 (style (dom-attr dom
'style
))
404 (shr-stylesheet shr-stylesheet
)
405 (shr-depth (1+ shr-depth
))
407 ;; shr uses about 12 frames per nested node.
408 (if (> shr-depth
(/ max-specpdl-size
12))
409 (setq shr-warning
"Too deeply nested to render properly; consider increasing `max-specpdl-size'")
411 (if (string-match "color\\|display\\|border-collapse" style
)
412 (setq shr-stylesheet
(nconc (shr-parse-style style
)
415 ;; If we have a display:none, then just ignore this part of the DOM.
416 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
417 (if (fboundp function
)
418 (funcall function dom
)
420 (when (and shr-target-id
421 (equal (dom-attr dom
'id
) shr-target-id
))
422 ;; If the element was empty, we don't have anything to put the
423 ;; anchor on. So just insert a dummy character.
424 (when (= start
(point))
426 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
427 ;; If style is set, then this node has set the color.
431 (cdr (assq 'color shr-stylesheet
))
432 (cdr (assq 'background-color shr-stylesheet
))))))))
434 (defun shr-fill-text (text)
435 (if (zerop (length text
))
438 (let ((shr-indentation 0)
440 (shr-internal-width (- (window-pixel-width)
441 (* (frame-fringe-width) 2))))
445 (define-inline shr-char-breakable-p
(char)
446 "Return non-nil if a line can be broken before and after CHAR."
447 (inline-quote (aref fill-find-break-point-function-table
,char
)))
448 (define-inline shr-char-nospace-p
(char)
449 "Return non-nil if no space is required before and after CHAR."
450 (inline-quote (aref fill-nospace-between-words-table
,char
)))
452 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
453 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
454 ;; parentheses, and so on, that should not be placed in the beginning
455 ;; of a line or the end of a line.
456 (define-inline shr-char-kinsoku-bol-p
(char)
457 "Return non-nil if a line ought not to begin with CHAR."
458 (inline-letevals (char)
459 (inline-quote (and (not (eq ,char ?
'))
460 (aref (char-category-set ,char
) ?
>)))))
461 (define-inline shr-char-kinsoku-eol-p
(char)
462 "Return non-nil if a line ought not to end with CHAR."
463 (inline-quote (aref (char-category-set ,char
) ?
<)))
464 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
465 (load "kinsoku" nil t
))
467 (defun shr-pixel-column ()
468 (if (not shr-use-fonts
)
470 (if (not (get-buffer-window (current-buffer)))
471 (save-window-excursion
472 (set-window-buffer nil
(current-buffer))
473 (car (window-text-pixel-size nil
(line-beginning-position) (point))))
474 (car (window-text-pixel-size nil
(line-beginning-position) (point))))))
476 (defun shr-pixel-region ()
477 (- (shr-pixel-column)
480 (shr-pixel-column))))
482 (defun shr-string-pixel-width (string)
483 (if (not shr-use-fonts
)
487 (shr-pixel-column))))
489 (defun shr-insert (text)
490 (when (and (not (bolp))
491 (get-text-property (1- (point)) 'image-url
))
494 ((eq shr-folding-mode
'none
)
497 (when (and (string-match "\\`[ \t\n\r ]" text
)
499 (not (eq (char-after (1- (point))) ?
)))
501 (let ((start (point))
505 (narrow-to-region start
(point))
507 (when (looking-at "[ \t\n\r ]+")
508 (replace-match "" t t
))
509 (while (re-search-forward "[ \t\n\r ]+" nil t
)
510 (replace-match " " t t
))
511 (goto-char (point-max)))
512 ;; We may have removed everything we inserted if if was just
514 (unless (= start
(point))
515 ;; Mark all lines that should possibly be folded afterwards.
517 (shr-mark-fill start
))
519 (add-face-text-property start
(point)
520 (or shr-current-font
'variable-pitch
)
523 (defun shr-fill-lines (start end
)
524 (if (<= shr-internal-width
0)
527 (narrow-to-region start end
)
529 (when (get-text-property (point) 'shr-indentation
)
531 (while (setq start
(next-single-property-change start
'shr-indentation
))
535 (goto-char (point-max)))))
537 (defun shr-vertical-motion (column)
538 (if (not shr-use-fonts
)
539 (move-to-column column
)
542 (vertical-motion (cons (/ column
(frame-char-width)) 0))
546 (defun shr-fill-line ()
547 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
548 (continuation (get-text-property
549 (point) 'shr-continuation-indentation
))
551 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
554 (setq shr-indentation
(or continuation shr-indentation
))
555 (shr-vertical-motion shr-internal-width
)
556 (when (looking-at " $")
557 (delete-region (point) (line-end-position)))
559 ;; We have to do some folding. First find the first
560 ;; previous point suitable for folding.
561 (if (or (not (shr-find-fill-point (line-beginning-position)))
563 ;; We had unbreakable text (for this width), so just go to
564 ;; the first space and carry on.
567 (skip-chars-forward " ")
568 (search-forward " " (line-end-position) 'move
)))
569 ;; Success; continue.
570 (when (= (preceding-char) ?\s
)
575 (shr-vertical-motion shr-internal-width
)
576 (when (looking-at " $")
577 (delete-region (point) (line-end-position))))))
579 (defun shr-find-fill-point (start)
583 (while (not (or (setq failed
(<= (point) start
))
584 (eq (preceding-char) ?
)
585 (eq (following-char) ?
)
586 (shr-char-breakable-p (preceding-char))
587 (shr-char-breakable-p (following-char))
588 (and (shr-char-kinsoku-bol-p (preceding-char))
589 (shr-char-breakable-p (following-char))
590 (not (shr-char-kinsoku-bol-p (following-char))))
591 (shr-char-kinsoku-eol-p (following-char))
595 ;; There's no breakable point, so we give it up.
598 (unless shr-kinsoku-shorten
599 (while (setq found
(re-search-forward
600 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
601 (line-end-position) 'move
)))
603 (not (match-beginning 1)))
604 (goto-char (match-beginning 0)))))
607 ;; Don't put kinsoku-bol characters at the beginning of a line,
608 ;; or kinsoku-eol characters at the end of a line.
611 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
612 (shr-char-kinsoku-eol-p (preceding-char)))
614 (when (setq failed
(<= (point) start
))
615 ;; There's no breakable point that doesn't violate kinsoku,
616 ;; so we look for the second best position.
622 (shr-char-kinsoku-eol-p (following-char)))))
624 ((shr-char-kinsoku-eol-p (preceding-char))
625 ;; Find backward the point where kinsoku-eol characters begin.
630 (and (> (setq count
(1- count
)) 0)
631 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
632 (or (shr-char-kinsoku-eol-p (preceding-char))
633 (shr-char-kinsoku-bol-p (following-char)))))))
634 (when (setq failed
(<= (point) start
))
635 ;; There's no breakable point that doesn't violate kinsoku,
636 ;; so we go to the second best position.
637 (if (looking-at "\\(\\c<+\\)\\c<")
638 (goto-char (match-end 1))
640 ((shr-char-kinsoku-bol-p (following-char))
641 ;; Find forward the point where kinsoku-bol characters end.
645 (and (>= (setq count
(1- count
)) 0)
646 (shr-char-kinsoku-bol-p (following-char))
647 (shr-char-breakable-p (following-char))))))))
648 (when (eq (following-char) ?
)
652 (defun shr-parse-base (url)
653 ;; Always chop off anchors.
654 (when (string-match "#.*" url
)
655 (setq url
(substring url
0 (match-beginning 0))))
656 ;; NB: <base href="" > URI may itself be relative to the document s URI
657 (setq url
(shr-expand-url url
))
658 (let* ((parsed (url-generic-parse-url url
))
659 (local (url-filename parsed
)))
660 (setf (url-filename parsed
) "")
661 ;; Chop off the bit after the last slash.
662 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
663 (setq local
(match-string 1 local
)))
664 ;; Always make the local bit end with a slash.
665 (when (and (not (zerop (length local
)))
666 (not (eq (aref local
(1- (length local
))) ?
/)))
667 (setq local
(concat local
"/")))
668 (list (url-recreate-url parsed
)
673 (autoload 'url-expand-file-name
"url-expand")
675 ;; FIXME This needs some tests writing.
676 ;; Does it even need to exist, given that url-expand-file-name does?
677 (defun shr-expand-url (url &optional base
)
680 ;; shr-parse-base should never call this with non-nil base!
681 (shr-parse-base base
)
682 ;; Bound by the parser.
684 (when (zerop (length url
))
688 (string-match "\\`[a-z]*:" url
))
689 ;; Absolute or empty URI
690 (or url
(nth 3 base
)))
691 ((eq (aref url
0) ?
/)
692 (if (and (> (length url
) 1)
693 (eq (aref url
1) ?
/))
694 ;; //host...; just use the protocol
695 (concat (nth 2 base
) ":" url
)
696 ;; Just use the host name part.
697 (concat (car base
) url
)))
698 ((eq (aref url
0) ?
#)
699 ;; A link to an anchor.
700 (concat (nth 3 base
) url
))
703 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
705 (defun shr-ensure-newline ()
706 (unless (zerop (current-column))
709 (defun shr-ensure-paragraph ()
711 (if (<= (current-column) shr-indentation
)
712 (unless (save-excursion
718 ;; If the current line is totally blank, and doesn't even
719 ;; have any face properties set, then delete the blank
721 (and (looking-at " *$")
722 (not (get-text-property (point) 'face
))
723 (not (= (next-single-property-change (point) 'face nil
725 (line-end-position)))))
726 (delete-region (match-beginning 0) (match-end 0))
730 (when (> shr-indentation
0)
732 (if (not shr-use-fonts
)
733 (make-string shr-indentation ?\s
)
736 `(space :width
(,shr-indentation
)))))))
738 (defun shr-fontize-dom (dom &rest types
)
739 (let ((start (point)))
742 (shr-add-font start
(point) type
))))
744 ;; Add face to the region, but avoid putting the font properties on
745 ;; blank text at the start of the line, and the newline at the end, to
747 (defun shr-add-font (start end type
)
748 (unless shr-inhibit-decoration
751 (while (< (point) end
)
753 (skip-chars-forward " "))
754 (add-face-text-property (point) (min (line-end-position) end
) type t
)
755 (if (< (line-end-position) end
)
759 (defun shr-mouse-browse-url (ev)
760 "Browse the URL under the mouse cursor."
765 (defun shr-browse-url (&optional external mouse-event
)
766 "Browse the URL under point.
767 If EXTERNAL, browse the URL using `shr-external-browser'."
768 (interactive (list current-prefix-arg last-nonmenu-event
))
769 (mouse-set-point mouse-event
)
770 (let ((url (get-text-property (point) 'shr-url
)))
773 (message "No link under point"))
774 ((string-match "^mailto:" url
)
775 (browse-url-mail url
))
778 (funcall shr-external-browser url
)
779 (browse-url url
))))))
781 (defun shr-save-contents (directory)
782 "Save the contents from URL in a file."
783 (interactive "DSave contents of URL to directory: ")
784 (let ((url (get-text-property (point) 'shr-url
)))
786 (message "No link under point")
787 (url-retrieve (shr-encode-url url
)
788 'shr-store-contents
(list url directory
)
791 (defun shr-store-contents (status url directory
)
792 (unless (plist-get status
:error
)
793 (when (or (search-forward "\n\n" nil t
)
794 (search-forward "\r\n\r\n" nil t
))
795 (write-region (point) (point-max)
796 (expand-file-name (file-name-nondirectory url
)
799 (defun shr-image-fetched (status buffer start end
&optional flags
)
800 (let ((image-buffer (current-buffer)))
801 (when (and (buffer-name buffer
)
802 (not (plist-get status
:error
)))
803 (url-store-in-cache image-buffer
)
804 (when (or (search-forward "\n\n" nil t
)
805 (search-forward "\r\n\r\n" nil t
))
806 (let ((data (shr-parse-image-data)))
807 (with-current-buffer buffer
809 (let ((alt (buffer-substring start end
))
810 (properties (text-properties-at start
))
811 (inhibit-read-only t
))
812 (delete-region start end
)
814 (funcall shr-put-image-function data alt flags
)
816 (let ((type (pop properties
))
817 (value (pop properties
)))
818 (unless (memq type
'(display image-size
))
819 (put-text-property start
(point) type value
))))))))))
820 (kill-buffer image-buffer
)))
822 (defun shr-image-from-data (data)
823 "Return an image from the data: URI content DATA."
825 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
827 (let ((param (match-string 4 data
))
828 (payload (url-unhex-string (match-string 5 data
))))
829 (when (string-match "^.*\\(;[ \t]*base64\\)$" param
)
830 (setq payload
(base64-decode-string payload
)))
833 ;; Behind display-graphic-p test.
834 (declare-function image-size
"image.c" (spec &optional pixels frame
))
835 (declare-function image-animate
"image" (image &optional index limit
))
837 (defun shr-put-image (spec alt
&optional flags
)
838 "Insert image SPEC with a string ALT. Return image.
839 SPEC is either an image data blob, or a list where the first
840 element is the data blob and the second element is the content-type."
841 (if (display-graphic-p)
842 (let* ((size (cdr (assq 'size flags
)))
843 (data (if (consp spec
)
846 (content-type (and (consp spec
)
851 (create-image data nil t
:ascent
100
852 :format content-type
))
853 ((eq content-type
'image
/svg
+xml
)
854 (create-image data
'svg t
:ascent
100))
857 (shr-rescale-image data content-type
)))
860 (shr-rescale-image data content-type
))))))
862 ;; When inserting big-ish pictures, put them at the
863 ;; beginning of the line.
864 (when (and (> (current-column) 0)
865 (> (car (image-size image t
)) 400))
867 (if (eq size
'original
)
868 (insert-sliced-image image
(or alt
"*") nil
20 1)
869 (insert-image image
(or alt
"*")))
870 (put-text-property start
(point) 'image-size size
)
871 (when (and shr-image-animate
872 (cond ((fboundp 'image-multi-frame-p
)
873 ;; Only animate multi-frame things that specify a
874 ;; delay; eg animated gifs as opposed to
875 ;; multi-page tiffs. FIXME?
876 (cdr (image-multi-frame-p image
)))
877 ((fboundp 'image-animated-p
)
878 (image-animated-p image
))))
879 (image-animate image nil
60)))
883 (defun shr-rescale-image (data &optional content-type
)
884 "Rescale DATA, if too big, to fit the current buffer."
885 (if (not (and (fboundp 'imagemagick-types
)
886 (get-buffer-window (current-buffer))))
887 (create-image data nil t
:ascent
100)
888 (let ((edges (window-inside-pixel-edges
889 (get-buffer-window (current-buffer)))))
893 :max-width
(truncate (* shr-max-image-proportion
894 (- (nth 2 edges
) (nth 0 edges
))))
895 :max-height
(truncate (* shr-max-image-proportion
896 (- (nth 3 edges
) (nth 1 edges
))))
897 :format content-type
))))
899 ;; url-cache-extract autoloads url-cache.
900 (declare-function url-cache-create-filename
"url-cache" (url))
901 (autoload 'mm-disable-multibyte
"mm-util")
902 (autoload 'browse-url-mail
"browse-url")
904 (defun shr-get-image-data (url)
905 "Get image data for URL.
906 Return a string with image data."
908 (mm-disable-multibyte)
910 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
912 (when (or (search-forward "\n\n" nil t
)
913 (search-forward "\r\n\r\n" nil t
))
914 (shr-parse-image-data)))))
916 (defun shr-parse-image-data ()
917 (let ((data (buffer-substring (point) (point-max)))
921 (narrow-to-region (point-min) (point))
922 (let ((content-type (mail-fetch-field "content-type")))
924 ;; Remove any comments in the type string.
925 (intern (replace-regexp-in-string ";.*" "" content-type
)
927 ;; SVG images may contain references to further images that we may
928 ;; want to block. So special-case these by parsing the XML data
929 ;; and remove the blocked bits.
930 (when (eq content-type
'image
/svg
+xml
)
933 (libxml-parse-xml-region (point) (point-max)))))
934 (list data content-type
)))
936 (defun shr-image-displayer (content-function)
937 "Return a function to display an image.
938 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
939 is an argument. The function to be returned takes three arguments URL,
940 START, and END. Note that START and END should be markers."
941 `(lambda (url start end
)
943 (if (string-match "\\`cid:" url
)
944 ,(when content-function
945 `(let ((image (funcall ,content-function
946 (substring url
(match-end 0)))))
949 (funcall shr-put-image-function
950 image
(buffer-substring start end
))
951 (delete-region (point) end
))))
952 (url-retrieve url
'shr-image-fetched
953 (list (current-buffer) start end
)
956 (defun shr-heading (dom &rest types
)
957 (shr-ensure-paragraph)
958 (apply #'shr-fontize-dom dom types
)
959 (shr-ensure-paragraph))
961 (defun shr-urlify (start url
&optional title
)
962 (shr-add-font start
(point) 'shr-link
)
966 'help-echo
(let ((iri (or (ignore-errors
967 (decode-coding-string
968 (url-unhex-string url
)
971 (if title
(format "%s (%s)" iri title
) iri
))
973 'mouse-face
'highlight
976 (defun shr-encode-url (url)
978 (browse-url-url-encode-chars url
"[)$ ]"))
980 (autoload 'shr-color-visible
"shr-color")
981 (autoload 'shr-color-
>hexadecimal
"shr-color")
983 (defun shr-color-check (fg bg
)
984 "Check that FG is visible on BG.
985 Returns (fg bg) with corrected values.
986 Returns nil if the colors that would be used are the default
987 ones, in case fg and bg are nil."
989 (let ((fixed (cond ((null fg
) 'fg
)
991 ;; Convert colors to hexadecimal, or set them to default.
992 (let ((fg (or (shr-color->hexadecimal fg
)
993 (frame-parameter nil
'foreground-color
)))
994 (bg (or (shr-color->hexadecimal bg
)
995 (frame-parameter nil
'background-color
))))
996 (cond ((eq fixed
'bg
)
997 ;; Only return the new fg
998 (list nil
(cadr (shr-color-visible bg fg t
))))
1000 ;; Invert args and results and return only the new bg
1001 (list (cadr (shr-color-visible fg bg t
)) nil
))
1003 (shr-color-visible bg fg
)))))))
1005 (defun shr-colorize-region (start end fg
&optional bg
)
1006 (when (and (not shr-inhibit-decoration
)
1008 (let ((new-colors (shr-color-check fg bg
)))
1011 (add-face-text-property start end
1012 (list :foreground
(cadr new-colors
))
1015 (add-face-text-property start end
1016 (list :background
(car new-colors
))
1020 (defun shr-previous-newline-padding-width (width)
1021 (let ((overlays (overlays-at (point)))
1025 (dolist (overlay overlays
)
1026 (setq previous-width
1028 (length (plist-get (overlay-properties overlay
)
1030 (+ width previous-width
))))
1032 ;;; Tag-specific rendering rules.
1034 (defun shr-tag-body (dom)
1035 (let* ((start (point))
1036 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1037 (bgcolor (dom-attr dom
'bgcolor
))
1038 (shr-stylesheet (list (cons 'color fgcolor
)
1039 (cons 'background-color bgcolor
))))
1041 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1043 (defun shr-tag-style (_dom)
1046 (defun shr-tag-script (_dom)
1049 (defun shr-tag-comment (_dom)
1052 (defun shr-dom-to-xml (dom)
1057 (defun shr-dom-print (dom)
1058 "Convert DOM into a string containing the xml representation."
1059 (insert (format "<%s" (dom-tag dom
)))
1060 (dolist (attr (dom-attributes dom
))
1061 ;; Ignore attributes that start with a colon because they are
1062 ;; private elements.
1063 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1064 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1067 (dolist (elem (dom-children dom
))
1071 ((eq (dom-tag elem
) 'comment
)
1073 ((or (not (eq (dom-tag elem
) 'image
))
1074 ;; Filter out blocked elements inside the SVG image.
1075 (not (setq url
(dom-attr elem
':xlink
:href
)))
1076 (not shr-blocked-images
)
1077 (not (string-match shr-blocked-images url
)))
1079 (shr-dom-print elem
)))))
1080 (insert (format "</%s>" (dom-tag dom
))))
1082 (defun shr-tag-svg (dom)
1083 (when (and (image-type-available-p 'svg
)
1084 (not shr-inhibit-images
))
1085 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1088 (defun shr-tag-sup (dom)
1089 (let ((start (point)))
1091 (put-text-property start
(point) 'display
'(raise 0.5))))
1093 (defun shr-tag-sub (dom)
1094 (let ((start (point)))
1096 (put-text-property start
(point) 'display
'(raise -
0.5))))
1098 (defun shr-tag-label (dom)
1100 (shr-ensure-paragraph))
1102 (defun shr-tag-p (dom)
1103 (shr-ensure-paragraph)
1105 (shr-ensure-paragraph))
1107 (defun shr-tag-div (dom)
1108 (shr-ensure-newline)
1110 (shr-ensure-newline))
1112 (defun shr-tag-s (dom)
1113 (shr-fontize-dom dom
'shr-strike-through
))
1115 (defun shr-tag-del (dom)
1116 (shr-fontize-dom dom
'shr-strike-through
))
1118 (defun shr-tag-b (dom)
1119 (shr-fontize-dom dom
'bold
))
1121 (defun shr-tag-i (dom)
1122 (shr-fontize-dom dom
'italic
))
1124 (defun shr-tag-em (dom)
1125 (shr-fontize-dom dom
'italic
))
1127 (defun shr-tag-strong (dom)
1128 (shr-fontize-dom dom
'bold
))
1130 (defun shr-tag-u (dom)
1131 (shr-fontize-dom dom
'underline
))
1133 (defun shr-tag-tt (dom)
1134 (let ((shr-current-font 'default
))
1137 (defun shr-parse-style (style)
1140 (when (string-match "\n" style
)
1141 (setq style
(replace-match " " t t style
))))
1143 (dolist (elem (split-string style
";"))
1145 (setq elem
(split-string elem
":"))
1146 (when (and (car elem
)
1148 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1149 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1150 (when (string-match " *!important\\'" value
)
1151 (setq value
(substring value
0 (match-beginning 0))))
1152 (unless (equal value
"inherit")
1153 (push (cons (intern name obarray
)
1158 (defun shr-tag-base (dom)
1159 (when-let (base (dom-attr dom
'href
))
1160 (setq shr-base
(shr-parse-base base
)))
1163 (defun shr-tag-a (dom)
1164 (let ((url (dom-attr dom
'href
))
1165 (title (dom-attr dom
'title
))
1169 (when (and shr-target-id
1170 (equal (dom-attr dom
'name
) shr-target-id
))
1171 ;; We have a zero-length <a name="foo"> element, so just
1172 ;; insert... something.
1173 (when (= start
(point))
1174 (shr-ensure-newline)
1176 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1178 (not shr-inhibit-decoration
))
1179 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1181 (defun shr-tag-object (dom)
1182 (unless shr-inhibit-images
1183 (let ((start (point))
1184 url multimedia image
)
1185 (when-let (type (dom-attr dom
'type
))
1186 (when (string-match "\\`image/svg" type
)
1187 (setq url
(dom-attr dom
'data
)
1189 (dolist (child (dom-non-text-children dom
))
1191 ((eq (dom-tag child
) 'embed
)
1192 (setq url
(or url
(dom-attr child
'src
))
1194 ((and (eq (dom-tag child
) 'param
)
1195 (equal (dom-attr child
'name
) "movie"))
1196 (setq url
(or url
(dom-attr child
'value
))
1201 (shr-tag-img dom url
)
1204 (shr-insert " [multimedia] ")
1205 (shr-urlify start
(shr-expand-url url
)))))
1207 (shr-generic dom
)))))
1209 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1215 "Preferences for media types.
1216 The key element should be a regexp matched against the type of the source or
1217 url if no type is specified. The value should be a float in the range 0.0 to
1218 1.0. Media elements with higher value are preferred."
1221 :type
'(alist :key-type regexp
:value-type float
))
1223 (defun shr--get-media-pref (elem)
1224 "Determine the preference for ELEM.
1225 The preference is a float determined from `shr-prefer-media-type'."
1226 (let ((type (dom-attr elem
'type
))
1229 (setq type
(dom-attr elem
'src
)))
1231 (dolist (pref shr-prefer-media-type-alist
)
1234 (string-match-p (car pref
) type
))
1235 (setq p
(cdr pref
)))))
1238 (defun shr--extract-best-source (dom &optional url pref
)
1239 "Extract the best `:src' property from <source> blocks in DOM."
1240 (setq pref
(or pref -
1.0))
1242 (dolist (elem (dom-non-text-children dom
))
1243 (when (and (eq (dom-tag elem
) 'source
)
1246 (shr--get-media-pref elem
))))
1248 url
(dom-attr elem
'src
))
1249 ;; libxml's html parser isn't HTML5 compliant and non terminated
1250 ;; source tags might end up as children. So recursion it is...
1251 (dolist (child (dom-non-text-children elem
))
1252 (when (eq (dom-tag child
) 'source
)
1253 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1254 (when (< pref
(cdr ret
))
1256 pref
(cdr ret
)))))))))
1259 (defun shr-tag-video (dom)
1260 (let ((image (dom-attr dom
'poster
))
1261 (url (dom-attr dom
'src
))
1264 (setq url
(car (shr--extract-best-source dom
))))
1266 (shr-tag-img nil image
)
1267 (shr-insert " [video] "))
1268 (shr-urlify start
(shr-expand-url url
))))
1270 (defun shr-tag-audio (dom)
1271 (let ((url (dom-attr dom
'src
))
1274 (setq url
(car (shr--extract-best-source dom
))))
1275 (shr-insert " [audio] ")
1276 (shr-urlify start
(shr-expand-url url
))))
1278 (defun shr-tag-img (dom &optional url
)
1281 (> (length (dom-attr dom
'src
)) 0)))
1282 (when (> (current-column) 0)
1284 (let ((alt (dom-attr dom
'alt
))
1285 (url (shr-expand-url (or url
(dom-attr dom
'src
)))))
1286 (let ((start (point-marker)))
1287 (when (zerop (length alt
))
1290 ((or (member (dom-attr dom
'height
) '("0" "1"))
1291 (member (dom-attr dom
'width
) '("0" "1")))
1292 ;; Ignore zero-sized or single-pixel images.
1294 ((and (not shr-inhibit-images
)
1295 (string-match "\\`data:" url
))
1296 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1298 (funcall shr-put-image-function image alt
)
1300 ((and (not shr-inhibit-images
)
1301 (string-match "\\`cid:" url
))
1302 (let ((url (substring url
(match-end 0)))
1304 (if (or (not shr-content-function
)
1305 (not (setq image
(funcall shr-content-function url
))))
1307 (funcall shr-put-image-function image alt
))))
1308 ((or shr-inhibit-images
1309 (and shr-blocked-images
1310 (string-match shr-blocked-images url
)))
1311 (setq shr-start
(point))
1312 (if (> (string-width alt
) 8)
1313 (shr-insert (truncate-string-to-width alt
8))
1315 ((and (not shr-ignore-cache
)
1316 (url-is-cached (shr-encode-url url
)))
1317 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
1320 (when (and shr-ignore-cache
1321 (url-is-cached (shr-encode-url url
)))
1322 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1323 (when (file-exists-p file
)
1324 (delete-file file
))))
1326 (shr-encode-url url
) 'shr-image-fetched
1327 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
1329 (when (zerop shr-table-depth
) ;; We are not in a table.
1330 (put-text-property start
(point) 'keymap shr-map
)
1331 (put-text-property start
(point) 'shr-alt alt
)
1332 (put-text-property start
(point) 'image-url url
)
1333 (put-text-property start
(point) 'image-displayer
1334 (shr-image-displayer shr-content-function
))
1335 (put-text-property start
(point) 'help-echo
1337 (or (dom-attr dom
'title
) alt
))))))))
1339 (defun shr-tag-pre (dom)
1340 (let ((shr-folding-mode 'none
)
1341 (shr-current-font 'default
))
1342 (shr-ensure-newline)
1344 (shr-ensure-newline)))
1346 (defun shr-tag-blockquote (dom)
1347 (shr-ensure-paragraph)
1348 (let ((start (point))
1349 (shr-indentation (+ shr-indentation
1350 (* 4 shr-table-separator-pixel-width
))))
1352 (shr-ensure-paragraph)
1353 (shr-mark-fill start
)))
1355 (defun shr-tag-dl (dom)
1356 (shr-ensure-paragraph)
1358 (shr-ensure-paragraph))
1360 (defun shr-tag-dt (dom)
1361 (shr-ensure-newline)
1363 (shr-ensure-newline))
1365 (defun shr-tag-dd (dom)
1366 (shr-ensure-newline)
1367 (let ((shr-indentation (+ shr-indentation
1368 (* 4 shr-table-separator-pixel-width
))))
1371 (defun shr-tag-ul (dom)
1372 (shr-ensure-paragraph)
1373 (let ((shr-list-mode 'ul
))
1375 (shr-ensure-paragraph))
1377 (defun shr-tag-ol (dom)
1378 (shr-ensure-paragraph)
1379 (let ((shr-list-mode 1))
1381 (shr-ensure-paragraph))
1383 (defun shr-tag-li (dom)
1384 (shr-ensure-newline)
1385 (let ((start (point)))
1387 (if (numberp shr-list-mode
)
1389 (format "%d " shr-list-mode
)
1390 (setq shr-list-mode
(1+ shr-list-mode
)))
1393 (shr-mark-fill start
)
1394 (let ((shr-indentation (+ shr-indentation
1395 (shr-string-pixel-width bullet
))))
1396 (put-text-property start
(1+ start
)
1397 'shr-continuation-indentation shr-indentation
)
1398 (shr-generic dom
)))))
1400 (defun shr-mark-fill (start)
1401 ;; We may not have inserted any text to fill.
1402 (unless (= start
(point))
1403 (put-text-property start
(1+ start
)
1404 'shr-indentation shr-indentation
)))
1406 (defun shr-tag-br (dom)
1407 (when (and (not (bobp))
1408 ;; Only add a newline if we break the current line, or
1409 ;; the previous line isn't a blank line.
1411 (and (> (- (point) 2) (point-min))
1412 (not (= (char-after (- (point) 2)) ?
\n)))))
1416 (defun shr-tag-span (dom)
1419 (defun shr-tag-h1 (dom)
1420 (shr-heading dom
(if shr-use-fonts
1421 '(variable-pitch (:height
1.3 :weight bold
))
1424 (defun shr-tag-h2 (dom)
1425 (shr-heading dom
'bold
))
1427 (defun shr-tag-h3 (dom)
1428 (shr-heading dom
'italic
))
1430 (defun shr-tag-h4 (dom)
1433 (defun shr-tag-h5 (dom)
1436 (defun shr-tag-h6 (dom)
1439 (defun shr-tag-hr (_dom)
1440 (shr-ensure-newline)
1441 (insert (make-string (if (not shr-use-fonts
)
1443 (1+ (/ shr-internal-width
1444 shr-table-separator-pixel-width
)))
1448 (defun shr-tag-title (dom)
1449 (shr-heading dom
'bold
'underline
))
1451 (defun shr-tag-font (dom)
1452 (let* ((start (point))
1453 (color (dom-attr dom
'color
))
1454 (shr-stylesheet (nconc (list (cons 'color color
))
1458 (shr-colorize-region start
(point) color
1459 (cdr (assq 'background-color shr-stylesheet
))))))
1461 ;;; Table rendering algorithm.
1463 ;; Table rendering is the only complicated thing here. We do this by
1464 ;; first counting how many TDs there are in each TR, and registering
1465 ;; how wide they think they should be ("width=45%", etc). Then we
1466 ;; render each TD separately (this is done in temporary buffers, so
1467 ;; that we can use all the rendering machinery as if we were in the
1468 ;; main buffer). Now we know how much space each TD really takes, so
1469 ;; we then render everything again with the new widths, and finally
1470 ;; insert all these boxes into the main buffer.
1471 (defun shr-tag-table-1 (dom)
1472 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1473 (let* ((shr-inhibit-images t
)
1474 (shr-table-depth (1+ shr-table-depth
))
1475 (shr-kinsoku-shorten t
)
1476 ;; Find all suggested widths.
1477 (columns (shr-column-specs dom
))
1478 ;; Compute how many pixels wide each TD should be.
1479 (suggested-widths (shr-pro-rate-columns columns
))
1480 ;; Do a "test rendering" to see how big each TD is (this can
1481 ;; be smaller (if there's little text) or bigger (if there's
1482 ;; unbreakable text).
1483 (elems (or (dom-attr dom
'shr-suggested-widths
)
1484 (shr-make-table dom suggested-widths nil
1485 'shr-suggested-widths
)))
1486 (sketch (loop for line in elems
1487 collect
(mapcar #'car line
)))
1488 (natural (loop for line in elems
1489 collect
(mapcar #'cdr line
)))
1490 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1491 ;; This probably won't work very well.
1492 (when (> (+ (loop for width across sketch-widths
1494 shr-indentation shr-table-separator-pixel-width
)
1496 (setq truncate-lines t
))
1497 ;; Then render the table again with these new "hard" widths.
1498 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1500 (defun shr-tag-table (dom)
1501 (shr-ensure-paragraph)
1502 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1503 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1504 (body (dom-non-text-children (or (dom-child-by-tag dom
'tbody
)
1506 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1507 (bgcolor (dom-attr dom
'bgcolor
))
1509 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1511 (nheader (if header
(shr-max-columns header
)))
1512 (nbody (if body
(shr-max-columns body
)))
1513 (nfooter (if footer
(shr-max-columns footer
))))
1514 (if (and (not caption
)
1516 (not (dom-child-by-tag dom
'tbody
))
1517 (not (dom-child-by-tag dom
'tr
))
1519 ;; The table is totally invalid and just contains random junk.
1520 ;; Try to output it anyway.
1522 ;; It's a real table, so render it.
1523 (if (dom-attr dom
'shr-fixed-table
)
1524 (shr-tag-table-1 dom
)
1525 ;; Only fix up the table once.
1529 (if caption
`((tr nil
(td nil
,@caption
))))
1533 ;; header + body + footer
1534 (if (= nheader nbody
)
1535 (if (= nbody nfooter
)
1536 `((tr nil
(td nil
(table nil
1538 ,@body
,@footer
)))))
1539 (nconc `((tr nil
(td nil
(table nil
1544 `((tr nil
(td nil
(table
1546 nil
,@footer
))))))))
1547 (nconc `((tr nil
(td nil
(table nil
(tbody
1549 (if (= nbody nfooter
)
1550 `((tr nil
(td nil
(table
1551 nil
(tbody nil
,@body
1553 (nconc `((tr nil
(td nil
(table
1558 `((tr nil
(td nil
(table
1564 (if (= nheader nbody
)
1565 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1568 `(,@header
(tr nil
(td nil
(table
1569 nil
(tbody nil
,@body
)))))
1570 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1571 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1574 (if (= nbody nfooter
)
1575 `((tr nil
(td nil
(table
1576 nil
(tbody nil
,@body
,@footer
)))))
1577 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1580 `((tr nil
(td nil
(table
1581 nil
(tbody nil
,@footer
)))))))))
1583 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1585 (dom-set-attribute table
'shr-fixed-table t
)
1586 (setcdr dom
(cdr table
))
1587 (shr-tag-table-1 dom
))))
1589 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1591 ;; Finally, insert all the images after the table. The Emacs buffer
1592 ;; model isn't strong enough to allow us to put the images actually
1594 (when (zerop shr-table-depth
)
1596 (shr-expand-alignments start
(point)))
1597 (dolist (elem (dom-by-tag dom
'object
))
1598 (shr-tag-object elem
))
1599 (dolist (elem (dom-by-tag dom
'img
))
1600 (shr-tag-img elem
)))))
1602 (defun shr-insert-table (table widths
)
1603 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
1605 (shr-table-separator-length (if collapse
0 1))
1606 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
1608 (setq shr-table-id
(1+ shr-table-id
))
1610 (shr-insert-table-ruler widths
))
1612 (let ((start (point))
1615 (height (let ((max 0))
1616 (dolist (column row
)
1617 (setq max
(max max
(nth 2 column
))))
1619 (dotimes (i (max height
1))
1621 (insert shr-table-vertical-line
"\n"))
1622 (dolist (column row
)
1623 (when (> (nth 2 column
) -
1)
1625 ;; Sum up all the widths from the column. (There may be
1626 ;; more than one if this is a "colspan" column.)
1627 (dotimes (i (nth 4 column
))
1628 ;; The colspan directive may be wrong and there may not be
1629 ;; that number of columns.
1630 (when (<= column-number
(1- (length widths
)))
1631 (setq align
(+ align
1632 (aref widths column-number
)
1633 (* 2 shr-table-separator-pixel-width
))))
1634 (setq column-number
(1+ column-number
)))
1635 (let ((lines (nth 3 column
))
1636 (pixel-align (if (not shr-use-fonts
)
1637 (* align
(frame-char-width))
1639 (dolist (line lines
)
1641 (let ((start (point)))
1645 'display
`(space :align-to
(,pixel-align
))
1646 'face
(and (> (length line
) 0)
1648 (1- (length line
)) 'face line
))
1649 'shr-table-indent shr-table-id
)
1650 shr-table-vertical-line
)
1651 (shr-colorize-region
1652 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1654 ;; Add blank lines at padding at the bottom of the TD,
1656 (dotimes (i (- height
(length lines
)))
1658 (let ((start (point)))
1659 (insert (propertize " "
1660 'display
`(space :align-to
(,pixel-align
))
1661 'shr-table-indent shr-table-id
)
1662 shr-table-vertical-line
)
1663 (shr-colorize-region
1664 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1665 (forward-line 1))))))
1667 (shr-insert-table-ruler widths
)))
1668 (unless (= start
(point))
1669 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
1671 (defun shr-expand-alignments (start end
)
1672 (while (< (setq start
(next-single-property-change
1673 start
'shr-table-id nil end
))
1676 (let* ((shr-use-fonts t
)
1677 (id (get-text-property (point) 'shr-table-id
))
1678 (base (shr-pixel-column))
1682 (while (setq elem
(text-property-any
1683 (point) end
'shr-table-indent id
))
1685 (let ((align (get-text-property (point) 'display
)))
1686 (put-text-property (point) (1+ (point)) 'display
1687 `(space :align-to
(,(+ (car (nth 2 align
))
1689 (forward-char 1)))))
1690 (setq start
(1+ start
))))
1692 (defun shr-insert-table-ruler (widths)
1693 (when shr-table-horizontal-line
1695 (> shr-indentation
0))
1697 (insert shr-table-corner
)
1698 (let ((total-width 0))
1699 (dotimes (i (length widths
))
1700 (setq total-width
(+ total-width
(aref widths i
)
1701 (* shr-table-separator-pixel-width
2)))
1702 (insert (make-string (1+ (/ (aref widths i
)
1703 shr-table-separator-pixel-width
))
1704 shr-table-horizontal-line
)
1706 'display
`(space :align-to
(,total-width
))
1707 'shr-table-indent shr-table-id
)
1711 (defun shr-table-widths (table natural-table suggested-widths
)
1712 (let* ((length (length suggested-widths
))
1713 (widths (make-vector length
0))
1714 (natural-widths (make-vector length
0)))
1717 (dolist (column row
)
1718 (aset widths i
(max (aref widths i
) column
))
1720 (dolist (row natural-table
)
1722 (dolist (column row
)
1723 (aset natural-widths i
(max (aref natural-widths i
) column
))
1725 (let ((extra (- (apply '+ (append suggested-widths nil
))
1726 (apply '+ (append widths nil
))
1727 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
1728 (expanded-columns 0))
1729 ;; We have extra, unused space, so divide this space amongst the
1732 ;; If the natural width is wider than the rendered width, we
1733 ;; want to allow the column to expand.
1735 (when (> (aref natural-widths i
) (aref widths i
))
1736 (setq expanded-columns
(1+ expanded-columns
))))
1738 (when (> (aref natural-widths i
) (aref widths i
))
1740 (aref natural-widths i
)
1741 (+ (/ extra expanded-columns
)
1742 (aref widths i
))))))))
1745 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
1746 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
1747 (let ((data (shr-make-table-1 dom widths fill
)))
1748 (push (list (list dom widths fill
) data
)
1750 (when storage-attribute
1751 (dom-set-attribute dom storage-attribute data
))
1754 (defun shr-make-table-1 (dom widths
&optional fill
)
1756 (shr-inhibit-decoration (not fill
))
1757 (rowspans (make-vector (length widths
) 0))
1758 (colspan-remaining 0)
1759 colspan-width colspan-count
1761 (dolist (row (dom-non-text-children dom
))
1762 (when (eq (dom-tag row
) 'tr
)
1764 (columns (dom-non-text-children row
))
1768 (while (< i
(length widths
))
1769 ;; If we previously had a rowspan definition, then that
1770 ;; means that we now have a "missing" td/th element here.
1771 ;; So just insert a dummy, empty one to (sort of) emulate
1774 (if (zerop (aref rowspans i
))
1776 (aset rowspans i
(1- (aref rowspans i
)))
1778 (when (and (not (stringp column
))
1779 (or (memq (dom-tag column
) '(td th
))
1781 (when-let (span (dom-attr column
'rowspan
))
1782 (aset rowspans i
(+ (aref rowspans i
)
1783 (1- (string-to-number span
)))))
1784 ;; Sanity check for invalid column-spans.
1785 (when (>= width-column
(length widths
))
1786 (setq width-column
0))
1789 (aref widths width-column
)
1790 (* 10 shr-table-separator-pixel-width
)))
1791 (when (setq colspan
(dom-attr column
'colspan
))
1792 (setq colspan
(min (string-to-number colspan
)
1793 ;; The colspan may be wrong, so
1794 ;; truncate it to the length of the
1795 ;; remaining columns.
1796 (- (length widths
) i
)))
1797 (dotimes (j (1- colspan
))
1799 (if (> (+ i
1 j
) (1- (length widths
)))
1800 ;; If we have a colspan spec that's longer
1801 ;; than the table is wide, just use the last
1802 ;; width as the width.
1803 (aref widths
(1- (length widths
)))
1804 ;; Sum up the widths of the columns we're
1807 shr-table-separator-length
1808 (aref widths
(+ i
1 j
))))))
1809 (setq width-column
(+ width-column
(1- colspan
))
1810 colspan-count colspan
1811 colspan-remaining colspan
))
1813 (let ((data (shr-render-td column width fill
)))
1815 (> colspan-remaining
0))
1817 (setq colspan-width
(car data
))
1818 (let ((this-width (/ colspan-width colspan-count
)))
1819 (push (cons this-width
(cadr data
)) tds
)
1820 (setq colspan-remaining
(1- colspan-remaining
))))
1822 (push (cons (car data
) (cadr data
)) tds
)
1826 (dotimes (c (1- colspan
))
1830 (list 0 0 -
1 nil
1 nil nil
)
1834 width-column
(1+ width-column
))))
1835 (push (nreverse tds
) trs
))))
1838 (defun shr-pixel-buffer-width ()
1839 (if (not shr-use-fonts
)
1841 (goto-char (point-min))
1845 (setq max
(max max
(current-column)))
1848 (if (get-buffer-window)
1849 (car (window-text-pixel-size nil
(point-min) (point-max)))
1850 (save-window-excursion
1851 (set-window-buffer nil
(current-buffer))
1852 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
1854 (defun shr-render-td (dom width fill
)
1855 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
1856 (or (dom-attr dom cache
)
1859 (dolist (attr (dom-attributes dom
))
1860 (let ((name (symbol-name (car attr
))))
1861 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
1862 (let ((cache-width (string-to-number
1863 (match-string 1 name
))))
1864 (when (and (>= cache-width width
)
1865 (<= (car (cdr attr
)) width
))
1866 (setq result
(cdr attr
)))))))
1868 (let ((result (shr-render-td-1 dom width fill
)))
1869 (dom-set-attribute dom cache result
)
1872 (defun shr-render-td-1 (dom width fill
)
1874 (let ((bgcolor (dom-attr dom
'bgcolor
))
1875 (fgcolor (dom-attr dom
'fgcolor
))
1876 (style (dom-attr dom
'style
))
1877 (shr-stylesheet shr-stylesheet
)
1881 (setq style
(and (string-match "color" style
)
1882 (shr-parse-style style
))))
1884 (setq style
(nconc (list (cons 'background-color bgcolor
))
1887 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1889 (setq shr-stylesheet
(append style shr-stylesheet
)))
1890 (let ((shr-internal-width width
)
1891 (shr-indentation 0))
1893 (save-window-excursion
1894 (set-window-buffer nil
(current-buffer))
1897 (or (dom-attr dom
'shr-td-cache-natural
)
1898 (let ((natural (max (shr-pixel-buffer-width)
1899 (shr-dom-max-natural-width dom
0))))
1900 (dom-set-attribute dom
'shr-td-cache-natural natural
)
1902 (if (and natural-width
1903 (<= natural-width width
))
1904 (setq max-width natural-width
)
1905 (let ((shr-internal-width width
))
1906 (shr-fill-lines (point-min) (point-max))
1907 (setq max-width
(shr-pixel-buffer-width)))))
1908 (goto-char (point-max))
1909 ;; Delete padding at the bottom of the TDs.
1913 (skip-chars-backward " \t\n")
1916 (goto-char (point-min))
1919 (count-lines (point-min) (point-max))
1920 (split-string (buffer-string) "\n")
1921 (if (dom-attr dom
'colspan
)
1922 (string-to-number (dom-attr dom
'colspan
))
1924 (cdr (assq 'color shr-stylesheet
))
1925 (cdr (assq 'background-color shr-stylesheet
))))))
1927 (defun shr-dom-max-natural-width (dom max
)
1928 (if (eq (dom-tag dom
) 'table
)
1930 (loop for line in
(dom-attr dom
'shr-suggested-widths
)
1932 shr-table-separator-length
1933 (loop for elem in line
1936 (* 2 shr-table-separator-length
)))))
1938 (dolist (child (dom-children dom
))
1939 (unless (stringp child
)
1940 (setq max
(max (shr-dom-max-natural-width child max
)))))
1943 (defun shr-buffer-width ()
1944 (goto-char (point-min))
1948 (setq max
(max max
(current-column)))
1952 (defun shr-pro-rate-columns (columns)
1953 (let ((total-percentage 0)
1954 (widths (make-vector (length columns
) 0)))
1955 (dotimes (i (length columns
))
1956 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1957 (setq total-percentage
(/ 1.0 total-percentage
))
1958 (dotimes (i (length columns
))
1959 (aset widths i
(max (truncate (* (aref columns i
)
1961 (- shr-internal-width
1962 (* (1+ (length columns
))
1963 shr-table-separator-pixel-width
))))
1967 ;; Return a summary of the number and shape of the TDs in the table.
1968 (defun shr-column-specs (dom)
1969 (let ((columns (make-vector (shr-max-columns dom
) 1)))
1970 (dolist (row (dom-non-text-children dom
))
1971 (when (eq (dom-tag row
) 'tr
)
1973 (dolist (column (dom-non-text-children row
))
1974 (when (memq (dom-tag column
) '(td th
))
1975 (let ((width (dom-attr column
'width
)))
1977 (string-match "\\([0-9]+\\)%" width
)
1978 (not (zerop (setq width
(string-to-number
1979 (match-string 1 width
))))))
1980 (aset columns i
(/ width
100.0))))
1981 (setq i
(1+ i
)))))))
1984 (defun shr-count (dom elem
)
1986 (dolist (sub (dom-children dom
))
1987 (when (and (not (stringp sub
))
1988 (eq (dom-tag sub
) elem
))
1992 (defun shr-max-columns (dom)
1994 (dolist (row (dom-children dom
))
1995 (when (and (not (stringp row
))
1996 (eq (dom-tag row
) 'tr
))
1997 (setq max
(max max
(+ (shr-count row
'td
)
1998 (shr-count row
'th
))))))
2007 ;;; shr.el ends here