1 ;;; shr.el --- Simple HTML Renderer -*- lexical-binding: t -*-
3 ;; Copyright (C) 2010-2018 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 <https://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.
34 (eval-when-compile (require 'url
)) ;For url-filename's setf handler.
36 (eval-when-compile (require 'subr-x
))
44 "Simple HTML Renderer"
48 (defcustom shr-max-image-proportion
0.9
49 "How big pictures displayed are in relation to the window they're in.
50 A value of 0.7 means that they are allowed to take up 70% of the
51 width and height of the window. If they are larger than this,
52 and Emacs supports it, then the images will be rescaled down to
58 (defcustom shr-blocked-images nil
59 "Images that have URLs matching this regexp will be blocked."
62 :type
'(choice (const nil
) regexp
))
64 (defcustom shr-use-fonts t
65 "If non-nil, use proportional fonts for text."
70 (defcustom shr-use-colors t
71 "If non-nil, respect color specifications in the HTML."
76 (defcustom shr-table-horizontal-line nil
77 "Character used to draw horizontal table lines.
78 If nil, don't draw horizontal table lines."
80 :type
'(choice (const nil
) character
))
82 (defcustom shr-table-vertical-line ?\s
83 "Character used to draw vertical table lines."
87 (defcustom shr-table-corner ?\s
88 "Character used to draw table corners."
92 (defcustom shr-hr-line ?-
93 "Character used to draw hr lines."
97 (defcustom shr-width nil
98 "Frame width to use for rendering.
99 May either be an integer specifying a fixed width in characters,
100 or nil, meaning that the full width of the window should be used.
101 If `shr-use-fonts' is set, the mean character width is used to
102 compute the pixel width, which is used instead."
104 :type
'(choice (integer :tag
"Fixed width in characters")
105 (const :tag
"Use the width of the window" nil
))
108 (defcustom shr-bullet
"* "
109 "Bullet used for unordered lists.
110 Alternative suggestions are:
117 (defcustom shr-external-browser
'browse-url-default-browser
118 "Function used to launch an external browser."
123 (defcustom shr-image-animate t
124 "Non nil means that images that can be animated will be."
129 (defvar shr-content-function nil
130 "If bound, this should be a function that will return the content.
131 This is used for cid: URLs, and the function is called with the
132 cid: URL as the argument.")
134 (defvar shr-put-image-function
'shr-put-image
135 "Function called to put image and alt string.")
137 (defface shr-strike-through
'((t (:strike-through t
)))
138 "Font for <s> elements."
142 '((t (:inherit link
)))
143 "Font for link elements."
146 (defface shr-selected-link
147 '((t (:inherit shr-link
:background
"red")))
148 "Font for link elements."
151 (defvar shr-inhibit-images nil
152 "If non-nil, inhibit loading images.")
154 (defvar shr-external-rendering-functions nil
155 "Alist of tag/function pairs used to alter how shr renders certain tags.
156 For instance, eww uses this to alter rendering of title, forms
158 \((title . eww-tag-title)
159 (form . eww-tag-form)
162 ;;; Internal variables.
164 (defvar shr-folding-mode nil
)
165 (defvar shr-start nil
)
166 (defvar shr-indentation
0)
167 (defvar shr-internal-width nil
)
168 (defvar shr-list-mode nil
)
169 (defvar shr-content-cache nil
)
170 (defvar shr-kinsoku-shorten nil
)
171 (defvar shr-table-depth
0)
172 (defvar shr-stylesheet nil
)
173 (defvar shr-base nil
)
175 (defvar shr-warning nil
)
176 (defvar shr-ignore-cache nil
)
177 (defvar shr-target-id nil
)
178 (defvar shr-table-separator-length
1)
179 (defvar shr-table-separator-pixel-width
0)
180 (defvar shr-table-id nil
)
181 (defvar shr-current-font nil
)
182 (defvar shr-internal-bullet nil
)
185 (let ((map (make-sparse-keymap)))
186 (define-key map
"a" 'shr-show-alt-text
)
187 (define-key map
"i" 'shr-browse-image
)
188 (define-key map
"z" 'shr-zoom-image
)
189 (define-key map
[?
\t] 'shr-next-link
)
190 (define-key map
[?\M-
\t] 'shr-previous-link
)
191 (define-key map
[follow-link
] 'mouse-face
)
192 (define-key map
[mouse-2
] 'shr-browse-url
)
193 (define-key map
"I" 'shr-insert-image
)
194 (define-key map
"w" 'shr-maybe-probe-and-copy-url
)
195 (define-key map
"u" 'shr-maybe-probe-and-copy-url
)
196 (define-key map
"v" 'shr-browse-url
)
197 (define-key map
"O" 'shr-save-contents
)
198 (define-key map
"\r" 'shr-browse-url
)
201 (defvar shr-image-map
202 (let ((map (copy-keymap shr-map
)))
203 (when (boundp 'image-map
)
204 (set-keymap-parent map image-map
))
207 ;; Public functions and commands.
208 (declare-function libxml-parse-html-region
"xml.c"
209 (start end
&optional base-url discard-comments
))
211 (defun shr-render-buffer (buffer)
212 "Display the HTML rendering of the current buffer."
213 (interactive (list (current-buffer)))
214 (or (fboundp 'libxml-parse-html-region
)
215 (error "This function requires Emacs to be compiled with libxml2"))
216 (pop-to-buffer "*html*")
219 (with-current-buffer buffer
220 (libxml-parse-html-region (point-min) (point-max))))
221 (goto-char (point-min)))
224 (defun shr-render-region (begin end
&optional buffer
)
225 "Display the HTML rendering of the region between BEGIN and END."
227 (unless (fboundp 'libxml-parse-html-region
)
228 (error "This function requires Emacs to be compiled with libxml2"))
229 (with-current-buffer (or buffer
(current-buffer))
230 (let ((dom (libxml-parse-html-region begin end
)))
231 (delete-region begin end
)
233 (shr-insert-document dom
))))
235 (defun shr--have-one-fringe-p ()
236 "Return non-nil if we know at least one of the fringes has non-zero width."
237 (and (fboundp 'fringe-columns
)
238 (or (not (zerop (fringe-columns 'right
)))
239 (not (zerop (fringe-columns 'left
))))))
242 (defun shr-insert-document (dom)
243 "Render the parsed document DOM into the current buffer.
244 DOM should be a parse tree as generated by
245 `libxml-parse-html-region' or similar."
246 (setq shr-content-cache nil
)
247 (let ((start (point))
253 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
254 (shr-internal-bullet (cons shr-bullet
255 (shr-string-pixel-width shr-bullet
)))
256 (shr-internal-width (or (and shr-width
257 (if (not shr-use-fonts
)
259 (* shr-width
(frame-char-width))))
260 ;; We need to adjust the available
261 ;; width for when the user disables
262 ;; the fringes, which will cause the
263 ;; display engine usurp one column for
264 ;; the continuation glyph.
265 (if (not shr-use-fonts
)
266 (- (window-body-width) 1
267 (if (and (null shr-width
)
268 (not (shr--have-one-fringe-p)))
271 (- (window-body-width nil t
)
272 (* 2 (frame-char-width))
273 (if (and (null shr-width
)
274 (not (shr--have-one-fringe-p)))
275 (* (frame-char-width) 2)
277 (max-specpdl-size max-specpdl-size
)
278 bidi-display-reordering
)
279 ;; If the window was hscrolled for some reason, shr-fill-lines
280 ;; below will misbehave, because it silently assumes that it
281 ;; starts with a non-hscrolled window (vertical-motion will move
282 ;; to a wrong place otherwise).
283 (set-window-hscroll nil
0)
285 (shr-fill-lines start
(point))
286 (shr--remove-blank-lines-at-the-end start
(point))
288 (message "%s" shr-warning
))))
290 (defun shr--remove-blank-lines-at-the-end (start end
)
293 (narrow-to-region start end
)
295 (when (and (re-search-backward "[^ \n]" nil t
)
298 (delete-region (point) (point-max))))))
300 (defun shr-url-at-point (image-url)
301 "Return the URL under point as a string.
302 If IMAGE-URL is non-nil, or there is no link under point, but
303 there is an image under point then copy the URL of the image
304 under point instead."
306 (get-text-property (point) 'image-url
)
307 (or (get-text-property (point) 'shr-url
)
308 (get-text-property (point) 'image-url
))))
310 (defun shr-copy-url (url)
311 "Copy the URL under point to the kill ring.
312 If IMAGE-URL (the prefix) is non-nil, or there is no link under
313 point, but there is an image under point then copy the URL of the
314 image under point instead."
315 (interactive (list (shr-url-at-point current-prefix-arg
)))
317 (message "No URL under point")
318 (setq url
(url-encode-url url
))
320 (message "Copied %s" url
)))
322 (defun shr-probe-url (url cont
)
323 "Pass URL's redirect destination to CONT, if it has one.
324 CONT should be a function of one argument, the redirect
325 destination URL. If URL is not redirected, then CONT is never
331 (`(:redirect
,destination .
,_
)
332 ;; Remove common tracking junk from the URL.
333 (funcall cont
(replace-regexp-in-string
334 ".utm_.*" "" destination
)))))
337 (defun shr-probe-and-copy-url (url)
338 "Copy the URL under point to the kill ring.
339 Like `shr-copy-url', but additionally fetch URL and use its
340 redirection destination if it has one."
341 (interactive (list (shr-url-at-point current-prefix-arg
)))
342 (if url
(shr-probe-url url
#'shr-copy-url
)
345 (defun shr-maybe-probe-and-copy-url (url)
346 "Copy the URL under point to the kill ring.
347 If the URL is already at the front of the kill ring act like
348 `shr-probe-and-copy-url', otherwise like `shr-copy-url'."
349 (interactive (list (shr-url-at-point current-prefix-arg
)))
350 (if (equal url
(car kill-ring
))
351 (shr-probe-and-copy-url url
)
354 (defun shr--current-link-region ()
355 (let ((current (get-text-property (point) 'shr-url
))
358 ;; Go to the beginning.
359 (while (and (not (bobp))
360 (equal (get-text-property (point) 'shr-url
) current
))
362 (unless (equal (get-text-property (point) 'shr-url
) current
)
366 (while (and (not (eobp))
367 (equal (get-text-property (point) 'shr-url
) current
))
369 (list start
(point)))))
371 (defun shr--blink-link ()
372 (let* ((region (shr--current-link-region))
373 (overlay (make-overlay (car region
) (cadr region
))))
374 (overlay-put overlay
'face
'shr-selected-link
)
375 (run-at-time 1 nil
(lambda ()
376 (delete-overlay overlay
)))))
378 (defun shr-next-link ()
379 "Skip to the next link."
381 (let ((current (get-text-property (point) 'shr-url
))
384 (while (and (not (eobp))
385 (equal (get-text-property (point) 'shr-url
) current
))
389 (get-text-property (point) 'shr-url
))
390 ;; The next link is adjacent.
391 (message "%s" (get-text-property (point) 'help-echo
)))
393 (not (setq skip
(text-property-not-all (point) (point-max)
396 (message "No next link"))
399 (message "%s" (get-text-property (point) 'help-echo
))))))
401 (defun shr-previous-link ()
402 "Skip to the previous link."
404 (let ((start (point))
406 ;; Skip past the current link.
407 (while (and (not (bobp))
408 (get-text-property (point) 'help-echo
))
410 ;; Find the previous link.
411 (while (and (not (bobp))
412 (not (setq found
(get-text-property (point) 'help-echo
))))
416 (message "No previous link")
418 ;; Put point at the start of the link.
419 (while (and (not (bobp))
420 (get-text-property (point) 'help-echo
))
423 (message "%s" (get-text-property (point) 'help-echo
)))))
425 (defun shr-show-alt-text ()
426 "Show the ALT text of the image under point."
428 (let ((text (get-text-property (point) 'shr-alt
)))
430 (message "No image under point")
431 (message "%s" (shr-fill-text text
)))))
433 (defun shr-browse-image (&optional copy-url
)
434 "Browse the image under point.
435 If COPY-URL (the prefix if called interactively) is non-nil, copy
436 the URL of the image to the kill buffer instead."
438 (let ((url (get-text-property (point) 'image-url
)))
441 (message "No image under point"))
445 (copy-region-as-kill (point-min) (point-max))
446 (message "Copied %s" url
)))
448 (message "Browsing %s..." url
)
451 (defun shr-insert-image ()
452 "Insert the image under point into the buffer."
454 (let ((url (get-text-property (point) 'image-url
)))
456 (message "No image under point")
457 (message "Inserting %s..." url
)
458 (url-retrieve url
'shr-image-fetched
459 (list (current-buffer) (1- (point)) (point-marker))
462 (defun shr-zoom-image ()
463 "Toggle the image size.
464 The size will be rotated between the default size, the original
465 size, and full-buffer size."
467 (let ((url (get-text-property (point) 'image-url
))
468 (size (get-text-property (point) 'image-size
))
469 (buffer-read-only nil
))
471 (message "No image under point")
472 ;; Delete the old picture.
473 (while (get-text-property (point) 'image-url
)
476 (let ((start (point)))
477 (while (get-text-property (point) 'image-url
)
480 (put-text-property start
(point) 'display nil
)
481 (when (> (- (point) start
) 2)
482 (delete-region start
(1- (point)))))
483 (message "Inserting %s..." url
)
484 (url-retrieve url
'shr-image-fetched
485 (list (current-buffer) (1- (point)) (point-marker)
487 (cond ((or (eq size
'default
)
496 ;;; Utility functions.
498 (defsubst shr-generic
(dom)
499 (dolist (sub (dom-children dom
))
504 (defun shr-indirect-call (tag-name dom
&rest args
)
505 (let ((function (intern (concat "shr-tag-" (symbol-name tag-name
)) obarray
))
506 ;; Allow other packages to override (or provide) rendering
508 (external (cdr (assq tag-name shr-external-rendering-functions
))))
510 (apply external dom args
))
512 (apply function dom args
))
514 (apply 'shr-generic dom args
)))))
516 (defun shr-descend (dom)
518 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
))
519 ;; Allow other packages to override (or provide) rendering
521 (external (cdr (assq (dom-tag dom
) shr-external-rendering-functions
)))
522 (style (dom-attr dom
'style
))
523 (shr-stylesheet shr-stylesheet
)
524 (shr-depth (1+ shr-depth
))
526 ;; shr uses many frames per nested node.
527 (if (and (> shr-depth
(/ max-specpdl-size
15))
528 (not (and (y-or-n-p "Too deeply nested to render properly; increase `max-specpdl-size'?")
529 (setq max-specpdl-size
(* max-specpdl-size
2)))))
531 "Not rendering the complete page because of too-deep nesting")
533 (if (string-match "color\\|display\\|border-collapse" style
)
534 (setq shr-stylesheet
(nconc (shr-parse-style style
)
537 ;; If we have a display:none, then just ignore this part of the DOM.
538 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
539 ;; We don't use shr-indirect-call here, since shr-descend is
540 ;; the central bit of shr.el, and should be as fast as
541 ;; possible. Having one more level of indirection with its
542 ;; negative effect on performance is deemed unjustified in
545 (funcall external dom
))
547 (funcall function dom
))
550 (when (and shr-target-id
551 (equal (dom-attr dom
'id
) shr-target-id
))
552 ;; If the element was empty, we don't have anything to put the
553 ;; anchor on. So just insert a dummy character.
554 (when (= start
(point))
556 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
557 ;; If style is set, then this node has set the color.
561 (cdr (assq 'color shr-stylesheet
))
562 (cdr (assq 'background-color shr-stylesheet
))))))))
564 (defun shr-fill-text (text)
565 (if (zerop (length text
))
568 (let ((shr-indentation 0)
570 (shr-internal-width (- (window-body-width nil t
)
571 (* 2 (frame-char-width))
572 ;; Adjust the window width for when
573 ;; the user disables the fringes,
574 ;; which causes the display engine
575 ;; to usurp one column for the
576 ;; continuation glyph.
577 (if (and (null shr-width
)
578 (not (shr--have-one-fringe-p)))
579 (* (frame-char-width) 2)
582 (shr-fill-lines (point-min) (point-max))
585 (define-inline shr-char-breakable-p
(char)
586 "Return non-nil if a line can be broken before and after CHAR."
587 (inline-quote (aref fill-find-break-point-function-table
,char
)))
588 (define-inline shr-char-nospace-p
(char)
589 "Return non-nil if no space is required before and after CHAR."
590 (inline-quote (aref fill-nospace-between-words-table
,char
)))
592 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
593 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
594 ;; parentheses, and so on, that should not be placed in the beginning
595 ;; of a line or the end of a line.
596 (define-inline shr-char-kinsoku-bol-p
(char)
597 "Return non-nil if a line ought not to begin with CHAR."
598 (inline-letevals (char)
599 (inline-quote (and (not (eq ,char ?
'))
600 (aref (char-category-set ,char
) ?
>)))))
601 (define-inline shr-char-kinsoku-eol-p
(char)
602 "Return non-nil if a line ought not to end with CHAR."
603 (inline-quote (aref (char-category-set ,char
) ?
<)))
604 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
605 (load "kinsoku" nil t
))
607 (defun shr-pixel-column ()
608 (if (not shr-use-fonts
)
610 (if (not (get-buffer-window (current-buffer)))
611 (save-window-excursion
612 ;; Avoid errors if the selected window is a dedicated one,
613 ;; and they just want to insert a document into it.
614 (set-window-dedicated-p nil nil
)
615 (set-window-buffer nil
(current-buffer))
616 (car (window-text-pixel-size nil
(line-beginning-position) (point))))
617 (car (window-text-pixel-size nil
(line-beginning-position) (point))))))
619 (defun shr-pixel-region ()
620 (- (shr-pixel-column)
623 (shr-pixel-column))))
625 (defun shr-string-pixel-width (string)
626 (if (not shr-use-fonts
)
628 ;; Save and restore point across with-temp-buffer, since
629 ;; shr-pixel-column uses save-window-excursion, which can reset
638 (defsubst shr--translate-insertion-chars
()
639 ;; Remove soft hyphens.
640 (goto-char (point-min))
641 (while (search-forward "Â" nil t
)
642 (replace-match "" t t
))
643 ;; Translate non-breaking spaces into real spaces.
644 (goto-char (point-min))
645 (while (search-forward "Â " nil t
)
646 (replace-match " " t t
)))
648 (defun shr-insert (text)
649 (when (and (not (bolp))
650 (get-text-property (1- (point)) 'image-url
))
653 ((eq shr-folding-mode
'none
)
654 (let ((start (point)))
657 (narrow-to-region start
(point))
658 (shr--translate-insertion-chars)
659 (goto-char (point-max)))))
661 (let ((font-start (point)))
662 (when (and (string-match "\\`[ \t\n\r]" text
)
664 (not (eq (char-after (1- (point))) ?
)))
666 (let ((start (point))
670 (narrow-to-region start
(point))
672 (when (looking-at "[ \t\n\r]+")
673 (replace-match "" t t
))
674 (while (re-search-forward "[ \t\n\r]+" nil t
)
675 (replace-match " " t t
))
676 (shr--translate-insertion-chars)
677 (goto-char (point-max)))
678 ;; We may have removed everything we inserted if it was just
680 (unless (= font-start
(point))
681 ;; Mark all lines that should possibly be folded afterwards.
683 (shr-mark-fill start
))
685 (put-text-property font-start
(point)
687 (or shr-current-font
'variable-pitch
)))))))))
689 (defun shr-fill-lines (start end
)
690 (if (<= shr-internal-width
0)
693 (narrow-to-region start end
)
695 (when (get-text-property (point) 'shr-indentation
)
697 (while (setq start
(next-single-property-change start
'shr-indentation
))
701 (goto-char (point-max)))))
703 (defun shr-vertical-motion (column)
704 (if (not shr-use-fonts
)
705 (move-to-column column
)
708 (vertical-motion (cons (/ column
(frame-char-width)) 0))
712 (defun shr-fill-line ()
713 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
714 (continuation (get-text-property
715 (point) 'shr-continuation-indentation
))
717 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
718 (let ((face (get-text-property (point) 'face
))
719 (background-start (point)))
722 (put-text-property background-start
(point) 'face
723 `,(shr-face-background face
))))
725 (setq shr-indentation
(or continuation shr-indentation
))
726 ;; If we have an indentation that's wider than the width we're
727 ;; trying to fill to, then just give up and don't do any filling.
728 (when (< shr-indentation shr-internal-width
)
729 (shr-vertical-motion shr-internal-width
)
730 (when (looking-at " $")
731 (delete-region (point) (line-end-position)))
733 ;; We have to do some folding. First find the first
734 ;; previous point suitable for folding.
735 (if (or (not (shr-find-fill-point (line-beginning-position)))
737 ;; We had unbreakable text (for this width), so just go to
738 ;; the first space and carry on.
741 (skip-chars-forward " ")
742 (search-forward " " (line-end-position) 'move
)))
743 ;; Success; continue.
744 (when (= (preceding-char) ?\s
)
746 (let ((gap-start (point)))
749 (when (and (> (1- gap-start
) (point-min))
750 ;; The link on both sides of the newline are the
752 (equal (get-text-property (point) 'shr-url
)
753 (get-text-property (1- gap-start
) 'shr-url
)))
754 ;; ... so we join the two bits into one link logically, but
755 ;; not visually. This makes navigation between links work
756 ;; well, but avoids underscores before the link on the next
757 ;; line when indented.
758 (let ((props (copy-sequence (text-properties-at (point)))))
759 ;; We don't want to use the faces on the indentation, because
761 (setq props
(plist-put props
'face nil
))
762 (add-text-properties gap-start
(point) props
))))
764 (shr-vertical-motion shr-internal-width
)
765 (when (looking-at " $")
766 (delete-region (point) (line-end-position)))))))
768 (defun shr-find-fill-point (start)
772 (while (not (or (setq failed
(<= (point) start
))
773 (eq (preceding-char) ?
)
774 (eq (following-char) ?
)
775 (shr-char-breakable-p (preceding-char))
776 (shr-char-breakable-p (following-char))
777 (and (shr-char-kinsoku-bol-p (preceding-char))
778 (shr-char-breakable-p (following-char))
779 (not (shr-char-kinsoku-bol-p (following-char))))
780 (shr-char-kinsoku-eol-p (following-char))
784 ;; There's no breakable point, so we give it up.
787 ;; Don't overflow the window edge, even if
788 ;; shr-kinsoku-shorten is nil.
789 (unless (or shr-kinsoku-shorten
(null shr-width
))
790 (while (setq found
(re-search-forward
791 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
792 (line-end-position) 'move
)))
794 (not (match-beginning 1)))
795 (goto-char (match-beginning 0)))))
798 ;; Don't put kinsoku-bol characters at the beginning of a line,
799 ;; or kinsoku-eol characters at the end of a line.
801 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
803 ((or shr-kinsoku-shorten
(null shr-width
))
804 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
805 (or (shr-char-kinsoku-eol-p (preceding-char))
806 (shr-char-kinsoku-bol-p (following-char))))
808 (when (setq failed
(<= (point) start
))
809 ;; There's no breakable point that doesn't violate kinsoku,
810 ;; so we look for the second best position.
816 (shr-char-kinsoku-eol-p (following-char)))))
818 ((shr-char-kinsoku-eol-p (preceding-char))
819 ;; Find backward the point where kinsoku-eol characters begin.
824 (and (> (setq count
(1- count
)) 0)
825 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
826 (or (shr-char-kinsoku-eol-p (preceding-char))
827 (shr-char-kinsoku-bol-p (following-char)))))))
828 (when (setq failed
(<= (point) start
))
829 ;; There's no breakable point that doesn't violate kinsoku,
830 ;; so we go to the second best position.
831 (if (looking-at "\\(\\c<+\\)\\c<")
832 (goto-char (match-end 1))
834 ((shr-char-kinsoku-bol-p (following-char))
835 ;; Find forward the point where kinsoku-bol characters end.
839 (and (>= (setq count
(1- count
)) 0)
840 (shr-char-kinsoku-bol-p (following-char))
841 (shr-char-breakable-p (following-char))))))))
842 (when (eq (following-char) ?
)
846 (defun shr-parse-base (url)
847 ;; Always chop off anchors.
848 (when (string-match "#.*" url
)
849 (setq url
(substring url
0 (match-beginning 0))))
850 ;; NB: <base href="" > URI may itself be relative to the document s URI
851 (setq url
(shr-expand-url url
))
852 (let* ((parsed (url-generic-parse-url url
))
853 (local (url-filename parsed
)))
854 (setf (url-filename parsed
) "")
855 ;; Chop off the bit after the last slash.
856 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
857 (setq local
(match-string 1 local
)))
858 ;; Always make the local bit end with a slash.
859 (when (and (not (zerop (length local
)))
860 (not (eq (aref local
(1- (length local
))) ?
/)))
861 (setq local
(concat local
"/")))
862 (list (url-recreate-url parsed
)
867 (autoload 'url-expand-file-name
"url-expand")
869 ;; FIXME This needs some tests writing.
870 ;; Does it even need to exist, given that url-expand-file-name does?
871 (defun shr-expand-url (url &optional base
)
874 ;; shr-parse-base should never call this with non-nil base!
875 (shr-parse-base base
)
876 ;; Bound by the parser.
878 (when (zerop (length url
))
880 ;; Strip leading whitespace
881 (and url
(string-match "\\`\\s-+" url
)
882 (setq url
(substring url
(match-end 0))))
883 (cond ((zerop (length url
))
886 (string-match "\\`[a-z]*:" url
))
887 ;; Absolute or empty URI
889 ((eq (aref url
0) ?
/)
890 (if (and (> (length url
) 1)
891 (eq (aref url
1) ?
/))
892 ;; //host...; just use the protocol
893 (concat (nth 2 base
) ":" url
)
894 ;; Just use the host name part.
895 (concat (car base
) url
)))
896 ((eq (aref url
0) ?
#)
897 ;; A link to an anchor.
898 (concat (nth 3 base
) url
))
901 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
903 (defun shr-ensure-newline ()
905 (let ((prefix (get-text-property (line-beginning-position)
906 'shr-prefix-length
)))
907 (unless (or (zerop (current-column))
909 (= prefix
(- (point) (line-beginning-position)))))
912 (defun shr-ensure-paragraph ()
914 (let ((prefix (get-text-property (line-beginning-position)
915 'shr-prefix-length
)))
921 ;; We're already at a new paragraph; do nothing.
924 (= prefix
(- (point) (line-beginning-position))))
925 ;; Do nothing; we're at the start of a <li>.
929 ;; If the current line is totally blank, and doesn't even
930 ;; have any face properties set, then delete the blank
932 (and (looking-at " *$")
933 (not (get-text-property (point) 'face
))
934 (not (= (next-single-property-change (point) 'face nil
936 (line-end-position)))))
937 (delete-region (match-beginning 0) (match-end 0)))
938 ;; We have a single blank line.
941 ;; Insert new paragraph.
946 (when (> shr-indentation
0)
948 (if (not shr-use-fonts
)
949 (make-string shr-indentation ?\s
)
952 `(space :width
(,shr-indentation
)))))))
954 (defun shr-fontize-dom (dom &rest types
)
955 (let ((start (point)))
958 (shr-add-font start
(point) type
))))
960 ;; Add face to the region, but avoid putting the font properties on
961 ;; blank text at the start of the line, and the newline at the end, to
963 (defun shr-add-font (start end type
)
966 (while (< (point) end
)
968 (skip-chars-forward " "))
969 (add-face-text-property (point) (min (line-end-position) end
) type t
)
970 (if (< (line-end-position) end
)
974 (defun shr-mouse-browse-url (ev)
975 "Browse the URL under the mouse cursor."
980 (defun shr-browse-url (&optional external mouse-event
)
981 "Browse the URL at point using `browse-url'.
982 If EXTERNAL is non-nil (interactively, the prefix argument), browse
983 the URL using `shr-external-browser'.
984 If this function is invoked by a mouse click, it will browse the URL
985 at the position of the click. Optional argument MOUSE-EVENT describes
986 the mouse click event."
987 (interactive (list current-prefix-arg last-nonmenu-event
))
988 (mouse-set-point mouse-event
)
989 (let ((url (get-text-property (point) 'shr-url
)))
992 (message "No link under point"))
993 ((string-match "^mailto:" url
)
994 (browse-url-mail url
))
998 (funcall shr-external-browser url
)
1000 (browse-url url
))))))
1002 (defun shr-save-contents (directory)
1003 "Save the contents from URL in a file."
1004 (interactive "DSave contents of URL to directory: ")
1005 (let ((url (get-text-property (point) 'shr-url
)))
1007 (message "No link under point")
1008 (url-retrieve (shr-encode-url url
)
1009 'shr-store-contents
(list url directory
)
1012 (defun shr-store-contents (status url directory
)
1013 (unless (plist-get status
:error
)
1014 (when (or (search-forward "\n\n" nil t
)
1015 (search-forward "\r\n\r\n" nil t
))
1016 (write-region (point) (point-max)
1017 (expand-file-name (file-name-nondirectory url
)
1020 (defun shr-image-fetched (status buffer start end
&optional flags
)
1021 (let ((image-buffer (current-buffer)))
1022 (when (and (buffer-name buffer
)
1023 (not (plist-get status
:error
)))
1024 (url-store-in-cache image-buffer
)
1025 (goto-char (point-min))
1026 (when (or (search-forward "\n\n" nil t
)
1027 (search-forward "\r\n\r\n" nil t
))
1028 (let ((data (shr-parse-image-data)))
1029 (with-current-buffer buffer
1033 (let ((alt (buffer-substring start end
))
1034 (properties (text-properties-at start
))
1035 (inhibit-read-only t
))
1036 (delete-region start end
)
1038 (funcall shr-put-image-function data alt flags
)
1040 (let ((type (pop properties
))
1041 (value (pop properties
)))
1042 (unless (memq type
'(display image-size
))
1043 (put-text-property start
(point) type value
)))))))))))
1044 (kill-buffer image-buffer
)))
1046 (defun shr-image-from-data (data)
1047 "Return an image from the data: URI content DATA."
1049 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
1051 (let ((param (match-string 4 data
))
1052 (payload (url-unhex-string (match-string 5 data
))))
1054 (string-match "^.*\\(;[ \t]*base64\\)$" param
))
1055 (setq payload
(ignore-errors
1056 (base64-decode-string payload
))))
1059 ;; Behind display-graphic-p test.
1060 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1061 (declare-function image-animate
"image" (image &optional index limit
))
1063 (defun shr-put-image (spec alt
&optional flags
)
1064 "Insert image SPEC with a string ALT. Return image.
1065 SPEC is either an image data blob, or a list where the first
1066 element is the data blob and the second element is the content-type."
1067 (if (display-graphic-p)
1068 (let* ((size (cdr (assq 'size flags
)))
1069 (data (if (consp spec
)
1072 (content-type (and (consp spec
)
1076 ((eq size
'original
)
1077 (create-image data nil t
:ascent
100
1078 :format content-type
))
1079 ((eq content-type
'image
/svg
+xml
)
1080 (create-image data
'svg t
:ascent
100))
1083 (shr-rescale-image data content-type
1084 (plist-get flags
:width
)
1085 (plist-get flags
:height
))))
1088 (shr-rescale-image data content-type
1089 (plist-get flags
:width
)
1090 (plist-get flags
:height
)))))))
1092 ;; When inserting big-ish pictures, put them at the
1093 ;; beginning of the line.
1094 (when (and (> (current-column) 0)
1095 (> (car (image-size image t
)) 400))
1097 (if (eq size
'original
)
1098 (insert-sliced-image image
(or alt
"*") nil
20 1)
1099 (insert-image image
(or alt
"*")))
1100 (put-text-property start
(point) 'image-size size
)
1101 (when (and shr-image-animate
1102 (cond ((fboundp 'image-multi-frame-p
)
1103 ;; Only animate multi-frame things that specify a
1104 ;; delay; eg animated gifs as opposed to
1105 ;; multi-page tiffs. FIXME?
1106 (cdr (image-multi-frame-p image
)))
1107 ((fboundp 'image-animated-p
)
1108 (image-animated-p image
))))
1109 (image-animate image nil
60)))
1111 (insert (or alt
""))))
1113 (defun shr-rescale-image (data content-type width height
1114 &optional max-width max-height
)
1115 "Rescale DATA, if too big, to fit the current buffer.
1116 WIDTH and HEIGHT are the sizes given in the HTML data, if any.
1118 The size of the displayed image will not exceed
1119 MAX-WIDTH/MAX-HEIGHT. If not given, use the current window
1120 width/height instead."
1121 (if (or (not (fboundp 'imagemagick-types
))
1122 (not (get-buffer-window (current-buffer))))
1123 (create-image data nil t
:ascent
100)
1124 (let* ((edges (window-inside-pixel-edges
1125 (get-buffer-window (current-buffer))))
1126 (max-width (truncate (* shr-max-image-proportion
1128 (- (nth 2 edges
) (nth 0 edges
))))))
1129 (max-height (truncate (* shr-max-image-proportion
1131 (- (nth 3 edges
) (nth 1 edges
))))))
1132 (scaling (image-compute-scaling-factor image-scaling-factor
)))
1133 (when (or (and width
1134 (> width max-width
))
1136 (> height max-height
)))
1139 (if (and width height
1140 (< (* width scaling
) max-width
)
1141 (< (* height scaling
) max-height
))
1147 :format content-type
)
1151 :max-width max-width
1152 :max-height max-height
1153 :format content-type
)))))
1155 ;; url-cache-extract autoloads url-cache.
1156 (declare-function url-cache-create-filename
"url-cache" (url))
1157 (autoload 'mm-disable-multibyte
"mm-util")
1158 (autoload 'browse-url-mail
"browse-url")
1160 (defun shr-get-image-data (url)
1161 "Get image data for URL.
1162 Return a string with image data."
1164 (mm-disable-multibyte)
1165 (when (ignore-errors
1166 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
1168 (when (re-search-forward "\r?\n\r?\n" nil t
)
1169 (shr-parse-image-data)))))
1171 (declare-function libxml-parse-xml-region
"xml.c"
1172 (start end
&optional base-url discard-comments
))
1174 (defun shr-parse-image-data ()
1175 (let ((data (buffer-substring (point) (point-max)))
1179 (narrow-to-region (point-min) (point))
1180 (let ((content-type (mail-fetch-field "content-type")))
1182 ;; Remove any comments in the type string.
1183 (intern (replace-regexp-in-string ";.*" "" content-type
)
1185 ;; SVG images may contain references to further images that we may
1186 ;; want to block. So special-case these by parsing the XML data
1187 ;; and remove anything that looks like a blocked bit.
1188 (when (and shr-blocked-images
1189 (eq content-type
'image
/svg
+xml
))
1191 ;; Note that libxml2 doesn't parse everything perfectly,
1192 ;; so glitches may occur during this transformation.
1194 (libxml-parse-xml-region (point) (point-max)))))
1195 (list data content-type
)))
1197 (defun shr-image-displayer (content-function)
1198 "Return a function to display an image.
1199 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1200 is an argument. The function to be returned takes three arguments URL,
1201 START, and END. Note that START and END should be markers."
1202 `(lambda (url start end
)
1204 (if (string-match "\\`cid:" url
)
1205 ,(when content-function
1206 `(let ((image (funcall ,content-function
1207 (substring url
(match-end 0)))))
1210 (funcall shr-put-image-function
1211 image
(buffer-substring start end
))
1212 (delete-region (point) end
))))
1213 (url-retrieve url
'shr-image-fetched
1214 (list (current-buffer) start end
)
1217 (defun shr-heading (dom &rest types
)
1218 (shr-ensure-paragraph)
1219 (apply #'shr-fontize-dom dom types
)
1220 (shr-ensure-paragraph))
1222 (defun shr-urlify (start url
&optional title
)
1223 (shr-add-font start
(point) 'shr-link
)
1224 (add-text-properties
1227 'help-echo
(let ((parsed (url-generic-parse-url
1229 (decode-coding-string
1230 (url-unhex-string url
)
1234 ;; If we have an IDNA domain, then show the
1235 ;; decoded version in the mouseover to let the
1236 ;; user know that there's something possibly
1238 (when (url-host parsed
)
1239 (setf (url-host parsed
)
1240 (puny-encode-domain (url-host parsed
))))
1241 (setq iri
(url-recreate-url parsed
))
1243 (format "%s (%s)" iri title
)
1246 'mouse-face
'highlight
))
1247 ;; Don't overwrite any keymaps that are already in the buffer (i.e.,
1251 (let ((next (next-single-property-change start
'keymap nil
(point))))
1252 (if (get-text-property start
'keymap
)
1254 (put-text-property start
(or next
(point)) 'keymap shr-map
)))))
1256 (defun shr-encode-url (url)
1258 (browse-url-url-encode-chars url
"[)$ ]"))
1260 (autoload 'shr-color-visible
"shr-color")
1261 (autoload 'shr-color-
>hexadecimal
"shr-color")
1263 (defun shr-color-check (fg bg
)
1264 "Check that FG is visible on BG.
1265 Returns (fg bg) with corrected values.
1266 Returns nil if the colors that would be used are the default
1267 ones, in case fg and bg are nil."
1269 (let ((fixed (cond ((null fg
) 'fg
)
1271 ;; Convert colors to hexadecimal, or set them to default.
1272 (let ((fg (or (shr-color->hexadecimal fg
)
1273 (frame-parameter nil
'foreground-color
)))
1274 (bg (or (shr-color->hexadecimal bg
)
1275 (frame-parameter nil
'background-color
))))
1276 (cond ((eq fixed
'bg
)
1277 ;; Only return the new fg
1278 (list nil
(cadr (shr-color-visible bg fg t
))))
1280 ;; Invert args and results and return only the new bg
1281 (list (cadr (shr-color-visible fg bg t
)) nil
))
1283 (shr-color-visible bg fg
)))))))
1285 (defun shr-colorize-region (start end fg
&optional bg
)
1286 (when (and shr-use-colors
1288 (>= (display-color-cells) 88))
1289 (let ((new-colors (shr-color-check fg bg
)))
1292 (add-face-text-property start end
1293 (list :foreground
(cadr new-colors
))
1296 (add-face-text-property start end
1297 (list :background
(car new-colors
))
1301 ;;; Tag-specific rendering rules.
1303 (defun shr-tag-html (dom)
1304 (let ((dir (dom-attr dom
'dir
)))
1307 (setq bidi-paragraph-direction
'left-to-right
))
1309 (setq bidi-paragraph-direction
'right-to-left
))
1311 (setq bidi-paragraph-direction nil
))))
1314 (defun shr-tag-body (dom)
1315 (let* ((start (point))
1316 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1317 (bgcolor (dom-attr dom
'bgcolor
))
1318 (shr-stylesheet (list (cons 'color fgcolor
)
1319 (cons 'background-color bgcolor
))))
1321 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1323 (defun shr-tag-style (_dom)
1326 (defun shr-tag-script (_dom)
1329 (defun shr-tag-comment (_dom)
1332 (defun shr-dom-to-xml (dom)
1337 (defun shr-dom-print (dom)
1338 "Convert DOM into a string containing the xml representation."
1339 (insert (format "<%s" (dom-tag dom
)))
1340 (dolist (attr (dom-attributes dom
))
1341 ;; Ignore attributes that start with a colon because they are
1342 ;; private elements.
1343 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1344 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1347 (dolist (elem (dom-children dom
))
1351 ((eq (dom-tag elem
) 'comment
)
1353 ((or (not (eq (dom-tag elem
) 'image
))
1354 ;; Filter out blocked elements inside the SVG image.
1355 (not (setq url
(dom-attr elem
':xlink
:href
)))
1356 (not shr-blocked-images
)
1357 (not (string-match shr-blocked-images url
)))
1359 (shr-dom-print elem
)))))
1360 (insert (format "</%s>" (dom-tag dom
))))
1362 (defun shr-tag-svg (dom)
1363 (when (and (image-type-available-p 'svg
)
1364 (not shr-inhibit-images
)
1365 (dom-attr dom
'width
)
1366 (dom-attr dom
'height
))
1367 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1370 (defun shr-tag-sup (dom)
1371 (let ((start (point)))
1373 (put-text-property start
(point) 'display
'(raise 0.5))))
1375 (defun shr-tag-sub (dom)
1376 (let ((start (point)))
1378 (put-text-property start
(point) 'display
'(raise -
0.5))))
1380 (defun shr-tag-p (dom)
1381 (shr-ensure-paragraph)
1383 (shr-ensure-paragraph))
1385 (defun shr-tag-div (dom)
1386 (let ((display (cdr (assq 'display shr-stylesheet
))))
1387 (if (or (equal display
"inline")
1388 (equal display
"inline-block"))
1390 (shr-ensure-newline)
1392 (shr-ensure-newline))))
1394 (defun shr-tag-s (dom)
1395 (shr-fontize-dom dom
'shr-strike-through
))
1397 (defun shr-tag-b (dom)
1398 (shr-fontize-dom dom
'bold
))
1400 (defun shr-tag-i (dom)
1401 (shr-fontize-dom dom
'italic
))
1403 (defun shr-tag-em (dom)
1404 (shr-fontize-dom dom
'italic
))
1406 (defun shr-tag-strong (dom)
1407 (shr-fontize-dom dom
'bold
))
1409 (defun shr-tag-u (dom)
1410 (shr-fontize-dom dom
'underline
))
1412 (defun shr-tag-tt (dom)
1413 (let ((shr-current-font 'default
))
1416 (defun shr-tag-ins (cont)
1417 (let* ((start (point))
1419 (shr-stylesheet (nconc (list (cons 'color color
))
1422 (shr-colorize-region start
(point) color
1423 (cdr (assq 'background-color shr-stylesheet
)))))
1425 (defun shr-tag-del (cont)
1426 (let* ((start (point))
1428 (shr-stylesheet (nconc (list (cons 'color color
))
1430 (shr-fontize-dom cont
'shr-strike-through
)
1431 (shr-colorize-region start
(point) color
1432 (cdr (assq 'background-color shr-stylesheet
)))))
1434 (defun shr-parse-style (style)
1437 (when (string-match "\n" style
)
1438 (setq style
(replace-match " " t t style
))))
1440 (dolist (elem (split-string style
";"))
1442 (setq elem
(split-string elem
":"))
1443 (when (and (car elem
)
1445 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1446 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1447 (when (string-match " *!important\\'" value
)
1448 (setq value
(substring value
0 (match-beginning 0))))
1449 (unless (equal value
"inherit")
1450 (push (cons (intern name obarray
)
1455 (defun shr-tag-base (dom)
1456 (when-let* ((base (dom-attr dom
'href
)))
1457 (setq shr-base
(shr-parse-base base
)))
1460 (defun shr-tag-a (dom)
1461 (let ((url (dom-attr dom
'href
))
1462 (title (dom-attr dom
'title
))
1466 (when (and shr-target-id
1467 (equal (dom-attr dom
'name
) shr-target-id
))
1468 ;; We have a zero-length <a name="foo"> element, so just
1469 ;; insert... something.
1470 (when (= start
(point))
1471 (shr-ensure-newline)
1473 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1475 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1477 (defun shr-tag-object (dom)
1478 (unless shr-inhibit-images
1479 (let ((start (point))
1480 url multimedia image
)
1481 (when-let* ((type (dom-attr dom
'type
)))
1482 (when (string-match "\\`image/svg" type
)
1483 (setq url
(dom-attr dom
'data
)
1485 (dolist (child (dom-non-text-children dom
))
1487 ((eq (dom-tag child
) 'embed
)
1488 (setq url
(or url
(dom-attr child
'src
))
1490 ((and (eq (dom-tag child
) 'param
)
1491 (equal (dom-attr child
'name
) "movie"))
1492 (setq url
(or url
(dom-attr child
'value
))
1497 (shr-indirect-call 'img dom url
)
1500 (shr-insert " [multimedia] ")
1501 (shr-urlify start
(shr-expand-url url
)))))
1503 (shr-generic dom
)))))
1505 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1511 "Preferences for media types.
1512 The key element should be a regexp matched against the type of the source or
1513 url if no type is specified. The value should be a float in the range 0.0 to
1514 1.0. Media elements with higher value are preferred."
1517 :type
'(alist :key-type regexp
:value-type float
))
1519 (defun shr--get-media-pref (elem)
1520 "Determine the preference for ELEM.
1521 The preference is a float determined from `shr-prefer-media-type'."
1522 (let ((type (dom-attr elem
'type
))
1525 (setq type
(dom-attr elem
'src
)))
1527 (dolist (pref shr-prefer-media-type-alist
)
1530 (string-match-p (car pref
) type
))
1531 (setq p
(cdr pref
)))))
1534 (defun shr--extract-best-source (dom &optional url pref
)
1535 "Extract the best `:src' property from <source> blocks in DOM."
1536 (setq pref
(or pref -
1.0))
1538 (dolist (elem (dom-non-text-children dom
))
1539 (when (and (eq (dom-tag elem
) 'source
)
1542 (shr--get-media-pref elem
))))
1544 url
(dom-attr elem
'src
))
1545 ;; libxml's html parser isn't HTML5 compliant and non terminated
1546 ;; source tags might end up as children. So recursion it is...
1547 (dolist (child (dom-non-text-children elem
))
1548 (when (eq (dom-tag child
) 'source
)
1549 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1550 (when (< pref
(cdr ret
))
1552 pref
(cdr ret
)))))))))
1555 (defun shr-tag-video (dom)
1556 (let ((image (dom-attr dom
'poster
))
1557 (url (dom-attr dom
'src
))
1560 (setq url
(car (shr--extract-best-source dom
))))
1561 (if (> (length image
) 0)
1562 (shr-indirect-call 'img nil image
)
1563 (shr-insert " [video] "))
1564 (shr-urlify start
(shr-expand-url url
))))
1566 (defun shr-tag-audio (dom)
1567 (let ((url (dom-attr dom
'src
))
1570 (setq url
(car (shr--extract-best-source dom
))))
1571 (shr-insert " [audio] ")
1572 (shr-urlify start
(shr-expand-url url
))))
1574 (defun shr-tag-img (dom &optional url
)
1577 (or (> (length (dom-attr dom
'src
)) 0)
1578 (> (length (dom-attr dom
'srcset
)) 0))))
1579 (when (> (current-column) 0)
1581 (let ((alt (dom-attr dom
'alt
))
1582 (width (shr-string-number (dom-attr dom
'width
)))
1583 (height (shr-string-number (dom-attr dom
'height
)))
1584 (url (shr-expand-url (or url
(shr--preferred-image dom
)))))
1585 (let ((start (point-marker)))
1586 (when (zerop (length alt
))
1589 ((or (member (dom-attr dom
'height
) '("0" "1"))
1590 (member (dom-attr dom
'width
) '("0" "1")))
1591 ;; Ignore zero-sized or single-pixel images.
1593 ((and (not shr-inhibit-images
)
1594 (string-match "\\`data:" url
))
1595 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1597 (funcall shr-put-image-function image alt
1598 (list :width width
:height height
))
1600 ((and (not shr-inhibit-images
)
1601 (string-match "\\`cid:" url
))
1602 (let ((url (substring url
(match-end 0)))
1604 (if (or (not shr-content-function
)
1605 (not (setq image
(funcall shr-content-function url
))))
1607 (funcall shr-put-image-function image alt
1608 (list :width width
:height height
)))))
1609 ((or shr-inhibit-images
1610 (and shr-blocked-images
1611 (string-match shr-blocked-images url
)))
1612 (setq shr-start
(point))
1614 ((and (not shr-ignore-cache
)
1615 (url-is-cached (shr-encode-url url
)))
1616 (funcall shr-put-image-function
(shr-get-image-data url
) alt
1617 (list :width width
:height height
)))
1619 (when (and shr-ignore-cache
1620 (url-is-cached (shr-encode-url url
)))
1621 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1622 (when (file-exists-p file
)
1623 (delete-file file
))))
1624 (when (image-type-available-p 'svg
)
1626 (shr-make-placeholder-image dom
)
1630 (shr-encode-url url
) 'shr-image-fetched
1631 (list (current-buffer) start
(set-marker (make-marker) (point))
1632 (list :width width
:height height
))
1634 (when (zerop shr-table-depth
) ;; We are not in a table.
1635 (put-text-property start
(point) 'keymap shr-image-map
)
1636 (put-text-property start
(point) 'shr-alt alt
)
1637 (put-text-property start
(point) 'image-url url
)
1638 (put-text-property start
(point) 'image-displayer
1639 (shr-image-displayer shr-content-function
))
1640 (put-text-property start
(point) 'help-echo
1642 (or (dom-attr dom
'title
) alt
))))))))
1644 (defun shr--preferred-image (dom)
1645 (let ((srcset (dom-attr dom
'srcset
))
1646 (frame-width (frame-pixel-width))
1647 (width (string-to-number (or (dom-attr dom
'width
) "100")))
1649 (when (> (length srcset
) 0)
1650 ;; srcset consist of a series of URL/size specifications
1651 ;; separated by the ", " string.
1655 (let ((spec (split-string elem
"[\t\n\r ]+")))
1657 ((= (length spec
) 1)
1658 ;; Make sure it's well formed.
1659 (list (car spec
) 0))
1660 ((string-match "\\([0-9]+\\)x\\'" (cadr spec
))
1661 ;; If we have an "x" form, then use the width
1662 ;; spec to compute the real width.
1664 (* width
(string-to-number
1665 (match-string 1 (cadr spec
))))))
1668 (string-to-number (cadr spec
)))))))
1669 (split-string (replace-regexp-in-string
1670 "\\`[\t\n\r ]+\\|[\t\n\r ]+\\'" "" srcset
)
1671 "[\t\n\r ]*,[\t\n\r ]*"))
1673 (> (cadr e1
) (cadr e2
)))))
1674 ;; Choose the smallest picture that's bigger than the current
1676 (setq candidate
(caar srcset
))
1678 (> (cadr (car srcset
)) frame-width
))
1679 (setq candidate
(caar srcset
))
1681 (or candidate
(dom-attr dom
'src
))))
1683 (defun shr-string-number (string)
1686 (setq string
(replace-regexp-in-string "[^0-9]" "" string
))
1687 (if (zerop (length string
))
1689 (string-to-number string
))))
1691 (defun shr-make-placeholder-image (dom)
1693 (get-buffer-window (current-buffer))
1694 (window-inside-pixel-edges
1695 (get-buffer-window (current-buffer)))))
1696 (scaling (image-compute-scaling-factor image-scaling-factor
))
1698 (* (or (shr-string-number (dom-attr dom
'width
)) 100)
1701 (* (or (shr-string-number (dom-attr dom
'height
)) 100)
1705 (truncate (* shr-max-image-proportion
1706 (- (nth 2 edges
) (nth 0 edges
))))))
1707 (max-height (and edges
1708 (truncate (* shr-max-image-proportion
1709 (- (nth 3 edges
) (nth 1 edges
))))))
1711 (when (and max-width
1712 (> width max-width
))
1713 (setq height
(truncate (* (/ (float max-width
) width
) height
))
1715 (when (and max-height
1716 (> height max-height
))
1717 (setq width
(truncate (* (/ (float max-height
) height
) width
))
1719 (setq svg
(svg-create width height
))
1720 (svg-gradient svg
"background" 'linear
'((0 .
"#b0b0b0") (100 .
"#808080")))
1721 (svg-rectangle svg
0 0 width height
:gradient
"background"
1722 :stroke-width
2 :stroke-color
"black")
1723 (let ((image (svg-image svg
)))
1724 (setf (image-property image
:ascent
) 100)
1727 (defun shr-tag-pre (dom)
1728 (let ((shr-folding-mode 'none
)
1729 (shr-current-font 'default
))
1730 (shr-ensure-newline)
1732 (shr-ensure-newline)))
1734 (defun shr-tag-blockquote (dom)
1735 (shr-ensure-paragraph)
1736 (let ((start (point))
1737 (shr-indentation (+ shr-indentation
1738 (* 4 shr-table-separator-pixel-width
))))
1740 (shr-ensure-paragraph)
1741 (shr-mark-fill start
)))
1743 (defun shr-tag-dl (dom)
1744 (shr-ensure-paragraph)
1746 (shr-ensure-paragraph))
1748 (defun shr-tag-dt (dom)
1749 (shr-ensure-newline)
1751 (shr-ensure-newline))
1753 (defun shr-tag-dd (dom)
1754 (shr-ensure-newline)
1755 (let ((shr-indentation (+ shr-indentation
1756 (* 4 shr-table-separator-pixel-width
))))
1759 (defun shr-tag-ul (dom)
1760 (shr-ensure-paragraph)
1761 (let ((shr-list-mode 'ul
))
1763 ;; If we end on an empty <li>, then make sure we really end on a new
1767 (shr-ensure-paragraph))
1769 (defun shr-tag-ol (dom)
1770 (shr-ensure-paragraph)
1771 (let ((shr-list-mode 1))
1773 (shr-ensure-paragraph))
1775 (defun shr-tag-li (dom)
1776 (shr-ensure-newline)
1777 (let ((start (point)))
1779 (if (numberp shr-list-mode
)
1781 (format "%d " shr-list-mode
)
1782 (setq shr-list-mode
(1+ shr-list-mode
)))
1783 (car shr-internal-bullet
)))
1784 (width (if (numberp shr-list-mode
)
1785 (shr-string-pixel-width bullet
)
1786 (cdr shr-internal-bullet
))))
1788 (shr-mark-fill start
)
1789 (let ((shr-indentation (+ shr-indentation width
)))
1790 (put-text-property start
(1+ start
)
1791 'shr-continuation-indentation shr-indentation
)
1792 (put-text-property start
(1+ start
) 'shr-prefix-length
(length bullet
))
1793 (shr-generic dom
))))
1797 (defun shr-mark-fill (start)
1798 ;; We may not have inserted any text to fill.
1799 (unless (= start
(point))
1800 (put-text-property start
(1+ start
)
1801 'shr-indentation shr-indentation
)))
1803 (defun shr-tag-br (dom)
1804 (when (and (not (bobp))
1805 ;; Only add a newline if we break the current line, or
1806 ;; the previous line isn't a blank line.
1808 (and (> (- (point) 2) (point-min))
1809 (not (= (char-after (- (point) 2)) ?
\n)))))
1813 (defun shr-tag-span (dom)
1816 (defun shr-tag-h1 (dom)
1817 (shr-heading dom
(if shr-use-fonts
1818 '(variable-pitch (:height
1.3 :weight bold
))
1821 (defun shr-tag-h2 (dom)
1822 (shr-heading dom
'bold
))
1824 (defun shr-tag-h3 (dom)
1825 (shr-heading dom
'italic
))
1827 (defun shr-tag-h4 (dom)
1830 (defun shr-tag-h5 (dom)
1833 (defun shr-tag-h6 (dom)
1836 (defun shr-tag-hr (_dom)
1837 (shr-ensure-newline)
1838 (insert (make-string (if (not shr-use-fonts
)
1840 (1+ (/ shr-internal-width
1841 shr-table-separator-pixel-width
)))
1845 (defun shr-tag-title (dom)
1846 (shr-heading dom
'bold
'underline
))
1848 (defun shr-tag-font (dom)
1849 (let* ((start (point))
1850 (color (dom-attr dom
'color
))
1851 (shr-stylesheet (nconc (list (cons 'color color
))
1855 (shr-colorize-region start
(point) color
1856 (cdr (assq 'background-color shr-stylesheet
))))))
1858 (defun shr-tag-bdo (dom)
1859 (let* ((direction (dom-attr dom
'dir
))
1861 ((equal direction
"ltr")
1862 ?\N
{LEFT-TO-RIGHT OVERRIDE
})
1863 ((equal direction
"rtl")
1864 ?\N
{RIGHT-TO-LEFT OVERRIDE
}))))
1866 (insert ?\N
{FIRST STRONG ISOLATE
} char
))
1869 (insert ?\N
{POP DIRECTIONAL FORMATTING
} ?\N
{POP DIRECTIONAL ISOLATE
}))))
1871 (defun shr-tag-bdi (dom)
1872 (insert ?\N
{FIRST STRONG ISOLATE
})
1874 (insert ?\N
{POP DIRECTIONAL ISOLATE
}))
1876 ;;; Table rendering algorithm.
1878 ;; Table rendering is the only complicated thing here. We do this by
1879 ;; first counting how many TDs there are in each TR, and registering
1880 ;; how wide they think they should be ("width=45%", etc). Then we
1881 ;; render each TD separately (this is done in temporary buffers, so
1882 ;; that we can use all the rendering machinery as if we were in the
1883 ;; main buffer). Now we know how much space each TD really takes, so
1884 ;; we then render everything again with the new widths, and finally
1885 ;; insert all these boxes into the main buffer.
1886 (defun shr-tag-table-1 (dom)
1887 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1888 (let* ((shr-inhibit-images t
)
1889 (shr-table-depth (1+ shr-table-depth
))
1890 (shr-kinsoku-shorten t
)
1891 ;; Find all suggested widths.
1892 (columns (shr-column-specs dom
))
1893 ;; Compute how many pixels wide each TD should be.
1894 (suggested-widths (shr-pro-rate-columns columns
))
1895 ;; Do a "test rendering" to see how big each TD is (this can
1896 ;; be smaller (if there's little text) or bigger (if there's
1897 ;; unbreakable text).
1898 (elems (or (dom-attr dom
'shr-suggested-widths
)
1899 (shr-make-table dom suggested-widths nil
1900 'shr-suggested-widths
)))
1901 (sketch (cl-loop for line in elems
1902 collect
(mapcar #'car line
)))
1903 (natural (cl-loop for line in elems
1904 collect
(mapcar #'cdr line
)))
1905 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1906 ;; This probably won't work very well.
1907 (when (> (+ (cl-loop for width across sketch-widths
1909 shr-indentation shr-table-separator-pixel-width
)
1911 (setq truncate-lines t
))
1912 ;; Then render the table again with these new "hard" widths.
1913 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1915 (defun shr-table-body (dom)
1916 (let ((tbodies (seq-filter (lambda (child)
1917 (eq (dom-tag child
) 'tbody
))
1918 (dom-non-text-children dom
))))
1922 ((= (length tbodies
) 1)
1925 ;; Table with multiple tbodies. Convert into a single tbody.
1926 `(tbody nil
,@(cl-reduce 'append
1927 (mapcar 'dom-non-text-children tbodies
)))))))
1929 (defun shr-tag-table (dom)
1930 (shr-ensure-paragraph)
1931 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1932 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1933 (body (dom-non-text-children (shr-table-body dom
)))
1934 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1935 (bgcolor (dom-attr dom
'bgcolor
))
1937 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1939 (nheader (if header
(shr-max-columns header
)))
1940 (nbody (if body
(shr-max-columns body
) 0))
1941 (nfooter (if footer
(shr-max-columns footer
))))
1942 (if (and (not caption
)
1944 (not (dom-child-by-tag dom
'tbody
))
1945 (not (dom-child-by-tag dom
'tr
))
1947 ;; The table is totally invalid and just contains random junk.
1948 ;; Try to output it anyway.
1950 ;; It's a real table, so render it.
1951 (if (dom-attr dom
'shr-fixed-table
)
1952 (shr-tag-table-1 dom
)
1953 ;; Only fix up the table once.
1957 (if caption
`((tr nil
(td nil
,@caption
))))
1961 ;; header + body + footer
1962 (if (= nheader nbody
)
1963 (if (= nbody nfooter
)
1964 `((tr nil
(td nil
(table nil
1966 ,@body
,@footer
)))))
1967 (nconc `((tr nil
(td nil
(table nil
1972 `((tr nil
(td nil
(table
1974 nil
,@footer
))))))))
1975 (nconc `((tr nil
(td nil
(table nil
(tbody
1977 (if (= nbody nfooter
)
1978 `((tr nil
(td nil
(table
1979 nil
(tbody nil
,@body
1981 (nconc `((tr nil
(td nil
(table
1986 `((tr nil
(td nil
(table
1992 (if (= nheader nbody
)
1993 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1996 `(,@header
(tr nil
(td nil
(table
1997 nil
(tbody nil
,@body
)))))
1998 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1999 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
2002 (if (= nbody nfooter
)
2003 `((tr nil
(td nil
(table
2004 nil
(tbody nil
,@body
,@footer
)))))
2005 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
2008 `((tr nil
(td nil
(table
2009 nil
(tbody nil
,@footer
)))))))))
2011 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
2013 (dom-set-attribute table
'shr-fixed-table t
)
2014 (setcdr dom
(cdr table
))
2015 (shr-tag-table-1 dom
))))
2017 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
2019 ;; Finally, insert all the images after the table. The Emacs buffer
2020 ;; model isn't strong enough to allow us to put the images actually
2021 ;; into the tables. It inserts also non-td/th objects.
2022 (when (zerop shr-table-depth
)
2024 (shr-expand-alignments start
(point)))
2025 (let ((strings (shr-collect-extra-strings-in-table dom
)))
2028 (narrow-to-region (point) (point))
2029 (insert (mapconcat #'identity strings
"\n"))
2030 (shr-fill-lines (point-min) (point-max))))))))
2032 (defun shr-collect-extra-strings-in-table (dom &optional flags
)
2033 "Return extra strings in DOM of which the root is a table clause.
2034 Render <img>s and <object>s, and strings and child <table>s of which
2035 the parent <td> or <th> is lacking. FLAGS is a cons of two boolean
2036 flags that control whether to collect or render objects."
2037 ;; This function runs recursively and collects strings if the cdr of
2038 ;; FLAGS is nil and the car is not nil, and it renders also child
2039 ;; <table>s if the cdr is nil. Note: FLAGS may be nil, not a cons.
2040 ;; FLAGS becomes (t . nil) if a <tr> clause is found in the children
2041 ;; of DOM, and becomes (t . t) if a <td> or a <th> clause is found
2042 ;; and the car is t then. When a <table> clause is found, FLAGS
2043 ;; becomes nil if the cdr is t then. But if FLAGS is (t . nil) then,
2044 ;; it renders the <table>.
2045 (cl-loop for child in
(dom-children dom
) with recurse with tag
2046 do
(setq recurse nil
)
2048 unless
(or (not (car flags
)) (cdr flags
))
2049 when
(string-match "\\(?:[^\t\n\r ]+[\t\n\r ]+\\)*[^\t\n\r ]+"
2051 collect
(match-string 0 child
)
2053 else if
(consp child
)
2054 do
(setq tag
(dom-tag child
)) and
2055 unless
(memq tag
'(comment style
))
2057 do
(shr-indirect-call 'img child
)
2058 else if
(eq tag
'object
)
2059 do
(shr-indirect-call 'object child
)
2061 do
(setq recurse t
) and
2063 do
(setq flags
'(t . nil
))
2064 else if
(memq tag
'(td th
))
2066 do
(setq flags
'(t . t
))
2068 else if
(eq tag
'table
)
2072 do
(setq recurse nil
)
2073 (shr-indirect-call 'table child
)
2074 end end end end end end end end end end
2076 append
(shr-collect-extra-strings-in-table child flags
)))
2078 (defun shr-insert-table (table widths
)
2079 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
2081 (shr-table-separator-length (if collapse
0 1))
2082 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
2084 (setq shr-table-id
(1+ shr-table-id
))
2086 (shr-insert-table-ruler widths
))
2088 (let ((start (point))
2091 (height (let ((max 0))
2092 (dolist (column row
)
2093 (setq max
(max max
(nth 2 column
))))
2095 (dotimes (_ (max height
1))
2097 (insert shr-table-vertical-line
"\n"))
2098 (dolist (column row
)
2099 (when (> (nth 2 column
) -
1)
2101 ;; Sum up all the widths from the column. (There may be
2102 ;; more than one if this is a "colspan" column.)
2103 (dotimes (_ (nth 4 column
))
2104 ;; The colspan directive may be wrong and there may not be
2105 ;; that number of columns.
2106 (when (<= column-number
(1- (length widths
)))
2107 (setq align
(+ align
2108 (aref widths column-number
)
2109 (* 2 shr-table-separator-pixel-width
))))
2110 (setq column-number
(1+ column-number
)))
2111 (let ((lines (nth 3 column
))
2112 (pixel-align (if (not shr-use-fonts
)
2113 (* align
(frame-char-width))
2115 (dolist (line lines
)
2117 (let ((start (point))
2118 (background (and (> (length line
) 0)
2119 (shr-face-background
2121 (1- (length line
)) 'face line
))))
2124 'display
`(space :align-to
(,pixel-align
))
2125 'shr-table-indent shr-table-id
)))
2127 (setq space
(propertize space
'face background
)))
2128 (insert line space shr-table-vertical-line
)
2129 (shr-colorize-region
2130 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2132 ;; Add blank lines at padding at the bottom of the TD,
2134 (dotimes (_ (- height
(length lines
)))
2136 (let ((start (point)))
2137 (insert (propertize " "
2138 'display
`(space :align-to
(,pixel-align
))
2139 'shr-table-indent shr-table-id
)
2140 shr-table-vertical-line
)
2141 (shr-colorize-region
2142 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2143 (forward-line 1))))))
2145 (shr-insert-table-ruler widths
)))
2146 (unless (= start
(point))
2147 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
2149 (defun shr-face-background (face)
2151 (or (and (plist-get face
:background
)
2152 (list :background
(plist-get face
:background
)))
2153 (let ((background nil
))
2155 (when (and (consp elem
)
2156 (eq (car elem
) :background
)
2158 (setq background
(cadr elem
))))
2160 (list :background background
))))))
2162 (defun shr-expand-alignments (start end
)
2163 (while (< (setq start
(next-single-property-change
2164 start
'shr-table-id nil end
))
2167 (let* ((shr-use-fonts t
)
2168 (id (get-text-property (point) 'shr-table-id
))
2169 (base (shr-pixel-column))
2173 (while (setq elem
(text-property-any
2174 (point) end
'shr-table-indent id
))
2176 (let ((align (get-text-property (point) 'display
)))
2177 (put-text-property (point) (1+ (point)) 'display
2178 `(space :align-to
(,(+ (car (nth 2 align
))
2180 (forward-char 1)))))
2181 (setq start
(1+ start
))))
2183 (defun shr-insert-table-ruler (widths)
2184 (when shr-table-horizontal-line
2186 (> shr-indentation
0))
2188 (insert shr-table-corner
)
2189 (let ((total-width 0))
2190 (dotimes (i (length widths
))
2191 (setq total-width
(+ total-width
(aref widths i
)
2192 (* shr-table-separator-pixel-width
2)))
2193 (insert (make-string (1+ (/ (aref widths i
)
2194 shr-table-separator-pixel-width
))
2195 shr-table-horizontal-line
)
2197 'display
`(space :align-to
(,total-width
))
2198 'shr-table-indent shr-table-id
)
2202 (defun shr-table-widths (table natural-table suggested-widths
)
2203 (let* ((length (length suggested-widths
))
2204 (widths (make-vector length
0))
2205 (natural-widths (make-vector length
0)))
2208 (dolist (column row
)
2209 (aset widths i
(max (aref widths i
) column
))
2211 (dolist (row natural-table
)
2213 (dolist (column row
)
2214 (aset natural-widths i
(max (aref natural-widths i
) column
))
2216 (let ((extra (- (apply '+ (append suggested-widths nil
))
2217 (apply '+ (append widths nil
))
2218 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
2219 (expanded-columns 0))
2220 ;; We have extra, unused space, so divide this space amongst the
2223 ;; If the natural width is wider than the rendered width, we
2224 ;; want to allow the column to expand.
2226 (when (> (aref natural-widths i
) (aref widths i
))
2227 (setq expanded-columns
(1+ expanded-columns
))))
2229 (when (> (aref natural-widths i
) (aref widths i
))
2231 (aref natural-widths i
)
2232 (+ (/ extra expanded-columns
)
2233 (aref widths i
))))))))
2236 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
2237 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
2238 (let ((data (shr-make-table-1 dom widths fill
)))
2239 (push (list (list dom widths fill
) data
)
2241 (when storage-attribute
2242 (dom-set-attribute dom storage-attribute data
))
2245 (defun shr-make-table-1 (dom widths
&optional fill
)
2247 (rowspans (make-vector (length widths
) 0))
2248 (colspan-remaining 0)
2249 colspan-width colspan-count
2251 (dolist (row (dom-non-text-children dom
))
2252 (when (eq (dom-tag row
) 'tr
)
2254 (columns (dom-non-text-children row
))
2258 (while (< i
(length widths
))
2259 ;; If we previously had a rowspan definition, then that
2260 ;; means that we now have a "missing" td/th element here.
2261 ;; So just insert a dummy, empty one to (sort of) emulate
2264 (if (zerop (aref rowspans i
))
2266 (aset rowspans i
(1- (aref rowspans i
)))
2268 (when (and (not (stringp column
))
2269 (or (memq (dom-tag column
) '(td th
))
2271 (when-let* ((span (dom-attr column
'rowspan
)))
2272 (aset rowspans i
(+ (aref rowspans i
)
2273 (1- (string-to-number span
)))))
2274 ;; Sanity check for invalid column-spans.
2275 (when (>= width-column
(length widths
))
2276 (setq width-column
0))
2279 (aref widths width-column
)
2280 (* 10 shr-table-separator-pixel-width
)))
2281 (when (setq colspan
(dom-attr column
'colspan
))
2282 (setq colspan
(min (string-to-number colspan
)
2283 ;; The colspan may be wrong, so
2284 ;; truncate it to the length of the
2285 ;; remaining columns.
2286 (- (length widths
) i
)))
2287 (dotimes (j (1- colspan
))
2289 (if (> (+ i
1 j
) (1- (length widths
)))
2290 ;; If we have a colspan spec that's longer
2291 ;; than the table is wide, just use the last
2292 ;; width as the width.
2293 (aref widths
(1- (length widths
)))
2294 ;; Sum up the widths of the columns we're
2297 shr-table-separator-length
2298 (aref widths
(+ i
1 j
))))))
2299 (setq width-column
(+ width-column
(1- colspan
))
2300 colspan-count colspan
2301 colspan-remaining colspan
))
2303 (let ((data (shr-render-td column width fill
)))
2305 (> colspan-remaining
0))
2307 (setq colspan-width
(car data
))
2308 (let ((this-width (/ colspan-width colspan-count
)))
2309 (push (cons this-width
(cadr data
)) tds
)
2310 (setq colspan-remaining
(1- colspan-remaining
))))
2312 (push (cons (car data
) (cadr data
)) tds
)
2316 (dotimes (_ (1- colspan
))
2320 (list 0 0 -
1 nil
1 nil nil
)
2324 width-column
(1+ width-column
))))
2325 (push (nreverse tds
) trs
))))
2328 (defun shr-pixel-buffer-width ()
2329 (if (not shr-use-fonts
)
2331 (goto-char (point-min))
2335 (setq max
(max max
(current-column)))
2338 (if (get-buffer-window)
2339 (car (window-text-pixel-size nil
(point-min) (point-max)))
2340 (save-window-excursion
2341 ;; Avoid errors if the selected window is a dedicated one,
2342 ;; and they just want to insert a document into it.
2343 (set-window-dedicated-p nil nil
)
2344 (set-window-buffer nil
(current-buffer))
2345 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
2347 (defun shr-render-td (dom width fill
)
2348 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
2349 (or (dom-attr dom cache
)
2352 (dolist (attr (dom-attributes dom
))
2353 (let ((name (symbol-name (car attr
))))
2354 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
2355 (let ((cache-width (string-to-number
2356 (match-string 1 name
))))
2357 (when (and (>= cache-width width
)
2358 (<= (car (cdr attr
)) width
))
2359 (setq result
(cdr attr
)))))))
2362 (result (shr-render-td-1 dom width fill
)))
2363 (dom-set-attribute dom cache result
)
2367 (defun shr-render-td-1 (dom width fill
)
2369 (let ((bgcolor (dom-attr dom
'bgcolor
))
2370 (fgcolor (dom-attr dom
'fgcolor
))
2371 (style (dom-attr dom
'style
))
2372 (shr-stylesheet shr-stylesheet
)
2376 (setq style
(and (string-match "color" style
)
2377 (shr-parse-style style
))))
2379 (setq style
(nconc (list (cons 'background-color bgcolor
))
2382 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
2384 (setq shr-stylesheet
(append style shr-stylesheet
)))
2385 (let ((shr-internal-width width
)
2386 (shr-indentation 0))
2388 (save-window-excursion
2389 ;; Avoid errors if the selected window is a dedicated one,
2390 ;; and they just want to insert a document into it.
2391 (set-window-dedicated-p nil nil
)
2392 (set-window-buffer nil
(current-buffer))
2395 (or (dom-attr dom
'shr-td-cache-natural
)
2396 (let ((natural (max (shr-pixel-buffer-width)
2397 (shr-dom-max-natural-width dom
0))))
2398 (dom-set-attribute dom
'shr-td-cache-natural natural
)
2400 (if (and natural-width
2401 (<= natural-width width
))
2402 (setq max-width natural-width
)
2403 (let ((shr-internal-width width
))
2404 (shr-fill-lines (point-min) (point-max))
2405 (setq max-width
(shr-pixel-buffer-width)))))
2406 (goto-char (point-max))
2407 ;; Delete padding at the bottom of the TDs.
2411 (skip-chars-backward " \t\n")
2414 (goto-char (point-min))
2417 (count-lines (point-min) (point-max))
2418 (split-string (buffer-string) "\n")
2419 (if (dom-attr dom
'colspan
)
2420 (string-to-number (dom-attr dom
'colspan
))
2422 (cdr (assq 'color shr-stylesheet
))
2423 (cdr (assq 'background-color shr-stylesheet
))))))
2425 (defun shr-dom-max-natural-width (dom max
)
2426 (if (eq (dom-tag dom
) 'table
)
2429 for line in
(dom-attr dom
'shr-suggested-widths
)
2431 shr-table-separator-length
2432 (cl-loop for elem in line
2435 (* 2 shr-table-separator-length
)))))
2437 (dolist (child (dom-children dom
))
2438 (unless (stringp child
)
2439 (setq max
(max (shr-dom-max-natural-width child max
)))))
2442 (defun shr-buffer-width ()
2443 (goto-char (point-min))
2447 (setq max
(max max
(current-column)))
2451 (defun shr-pro-rate-columns (columns)
2452 (let ((total-percentage 0)
2453 (widths (make-vector (length columns
) 0)))
2454 (dotimes (i (length columns
))
2455 (setq total-percentage
(+ total-percentage
(aref columns i
))))
2456 (setq total-percentage
(/ 1.0 total-percentage
))
2457 (dotimes (i (length columns
))
2458 (aset widths i
(max (truncate (* (aref columns i
)
2460 (- shr-internal-width
2461 (* (1+ (length columns
))
2462 shr-table-separator-pixel-width
))))
2466 ;; Return a summary of the number and shape of the TDs in the table.
2467 (defun shr-column-specs (dom)
2468 (let ((columns (make-vector (shr-max-columns dom
) 1)))
2469 (dolist (row (dom-non-text-children dom
))
2470 (when (eq (dom-tag row
) 'tr
)
2472 (dolist (column (dom-non-text-children row
))
2473 (when (memq (dom-tag column
) '(td th
))
2474 (let ((width (dom-attr column
'width
)))
2476 (string-match "\\([0-9]+\\)%" width
)
2477 (not (zerop (setq width
(string-to-number
2478 (match-string 1 width
))))))
2479 (aset columns i
(/ width
100.0))))
2480 (setq i
(1+ i
)))))))
2483 (defun shr-count (dom elem
)
2485 (dolist (sub (dom-children dom
))
2486 (when (and (not (stringp sub
))
2487 (eq (dom-tag sub
) elem
))
2491 (defun shr-max-columns (dom)
2493 (dolist (row (dom-children dom
))
2494 (when (and (not (stringp row
))
2495 (eq (dom-tag row
) 'tr
))
2496 (setq max
(max max
(+ (shr-count row
'td
)
2497 (shr-count row
'th
))))))
2502 ;;; shr.el ends here