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 bidi-display-reordering
)
278 ;; If the window was hscrolled for some reason, shr-fill-lines
279 ;; below will misbehave, because it silently assumes that it
280 ;; starts with a non-hscrolled window (vertical-motion will move
281 ;; to a wrong place otherwise).
282 (set-window-hscroll nil
0)
284 (shr-fill-lines start
(point))
285 (shr--remove-blank-lines-at-the-end start
(point))
287 (message "%s" shr-warning
))))
289 (defun shr--remove-blank-lines-at-the-end (start end
)
292 (narrow-to-region start end
)
294 (when (and (re-search-backward "[^ \n]" nil t
)
297 (delete-region (point) (point-max))))))
299 (defun shr-url-at-point (image-url)
300 "Return the URL under point as a string.
301 If IMAGE-URL is non-nil, or there is no link under point, but
302 there is an image under point then copy the URL of the image
303 under point instead."
305 (get-text-property (point) 'image-url
)
306 (or (get-text-property (point) 'shr-url
)
307 (get-text-property (point) 'image-url
))))
309 (defun shr-copy-url (url)
310 "Copy the URL under point to the kill ring.
311 If IMAGE-URL (the prefix) is non-nil, or there is no link under
312 point, but there is an image under point then copy the URL of the
313 image under point instead."
314 (interactive (list (shr-url-at-point current-prefix-arg
)))
316 (message "No URL under point")
317 (setq url
(url-encode-url url
))
319 (message "Copied %s" url
)))
321 (defun shr-probe-url (url cont
)
322 "Pass URL's redirect destination to CONT, if it has one.
323 CONT should be a function of one argument, the redirect
324 destination URL. If URL is not redirected, then CONT is never
330 (`(:redirect
,destination .
,_
)
331 ;; Remove common tracking junk from the URL.
332 (funcall cont
(replace-regexp-in-string
333 ".utm_.*" "" destination
)))))
336 (defun shr-probe-and-copy-url (url)
337 "Copy the URL under point to the kill ring.
338 Like `shr-copy-url', but additionally fetch URL and use its
339 redirection destination if it has one."
340 (interactive (list (shr-url-at-point current-prefix-arg
)))
341 (if url
(shr-probe-url url
#'shr-copy-url
)
344 (defun shr-maybe-probe-and-copy-url (url)
345 "Copy the URL under point to the kill ring.
346 If the URL is already at the front of the kill ring act like
347 `shr-probe-and-copy-url', otherwise like `shr-copy-url'."
348 (interactive (list (shr-url-at-point current-prefix-arg
)))
349 (if (equal url
(car kill-ring
))
350 (shr-probe-and-copy-url url
)
353 (defun shr--current-link-region ()
354 (let ((current (get-text-property (point) 'shr-url
))
357 ;; Go to the beginning.
358 (while (and (not (bobp))
359 (equal (get-text-property (point) 'shr-url
) current
))
361 (unless (equal (get-text-property (point) 'shr-url
) current
)
365 (while (and (not (eobp))
366 (equal (get-text-property (point) 'shr-url
) current
))
368 (list start
(point)))))
370 (defun shr--blink-link ()
371 (let* ((region (shr--current-link-region))
372 (overlay (make-overlay (car region
) (cadr region
))))
373 (overlay-put overlay
'face
'shr-selected-link
)
374 (run-at-time 1 nil
(lambda ()
375 (delete-overlay overlay
)))))
377 (defun shr-next-link ()
378 "Skip to the next link."
380 (let ((current (get-text-property (point) 'shr-url
))
383 (while (and (not (eobp))
384 (equal (get-text-property (point) 'shr-url
) current
))
388 (get-text-property (point) 'shr-url
))
389 ;; The next link is adjacent.
390 (message "%s" (get-text-property (point) 'help-echo
)))
392 (not (setq skip
(text-property-not-all (point) (point-max)
395 (message "No next link"))
398 (message "%s" (get-text-property (point) 'help-echo
))))))
400 (defun shr-previous-link ()
401 "Skip to the previous link."
403 (let ((start (point))
405 ;; Skip past the current link.
406 (while (and (not (bobp))
407 (get-text-property (point) 'help-echo
))
409 ;; Find the previous link.
410 (while (and (not (bobp))
411 (not (setq found
(get-text-property (point) 'help-echo
))))
415 (message "No previous link")
417 ;; Put point at the start of the link.
418 (while (and (not (bobp))
419 (get-text-property (point) 'help-echo
))
422 (message "%s" (get-text-property (point) 'help-echo
)))))
424 (defun shr-show-alt-text ()
425 "Show the ALT text of the image under point."
427 (let ((text (get-text-property (point) 'shr-alt
)))
429 (message "No image under point")
430 (message "%s" (shr-fill-text text
)))))
432 (defun shr-browse-image (&optional copy-url
)
433 "Browse the image under point.
434 If COPY-URL (the prefix if called interactively) is non-nil, copy
435 the URL of the image to the kill buffer instead."
437 (let ((url (get-text-property (point) 'image-url
)))
440 (message "No image under point"))
444 (copy-region-as-kill (point-min) (point-max))
445 (message "Copied %s" url
)))
447 (message "Browsing %s..." url
)
450 (defun shr-insert-image ()
451 "Insert the image under point into the buffer."
453 (let ((url (get-text-property (point) 'image-url
)))
455 (message "No image under point")
456 (message "Inserting %s..." url
)
457 (url-retrieve url
'shr-image-fetched
458 (list (current-buffer) (1- (point)) (point-marker))
461 (defun shr-zoom-image ()
462 "Toggle the image size.
463 The size will be rotated between the default size, the original
464 size, and full-buffer size."
466 (let ((url (get-text-property (point) 'image-url
))
467 (size (get-text-property (point) 'image-size
))
468 (buffer-read-only nil
))
470 (message "No image under point")
471 ;; Delete the old picture.
472 (while (get-text-property (point) 'image-url
)
475 (let ((start (point)))
476 (while (get-text-property (point) 'image-url
)
479 (put-text-property start
(point) 'display nil
)
480 (when (> (- (point) start
) 2)
481 (delete-region start
(1- (point)))))
482 (message "Inserting %s..." url
)
483 (url-retrieve url
'shr-image-fetched
484 (list (current-buffer) (1- (point)) (point-marker)
486 (cond ((or (eq size
'default
)
495 ;;; Utility functions.
497 (defsubst shr-generic
(dom)
498 (dolist (sub (dom-children dom
))
503 (defun shr-indirect-call (tag-name dom
&rest args
)
504 (let ((function (intern (concat "shr-tag-" (symbol-name tag-name
)) obarray
))
505 ;; Allow other packages to override (or provide) rendering
507 (external (cdr (assq tag-name shr-external-rendering-functions
))))
509 (apply external dom args
))
511 (apply function dom args
))
513 (apply 'shr-generic dom args
)))))
515 (defun shr-descend (dom)
517 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
))
518 ;; Allow other packages to override (or provide) rendering
520 (external (cdr (assq (dom-tag dom
) shr-external-rendering-functions
)))
521 (style (dom-attr dom
'style
))
522 (shr-stylesheet shr-stylesheet
)
523 (shr-depth (1+ shr-depth
))
525 ;; shr uses many frames per nested node.
526 (if (> shr-depth
(/ max-specpdl-size
15))
527 (setq shr-warning
"Too deeply nested to render properly; consider increasing `max-specpdl-size'")
529 (if (string-match "color\\|display\\|border-collapse" style
)
530 (setq shr-stylesheet
(nconc (shr-parse-style style
)
533 ;; If we have a display:none, then just ignore this part of the DOM.
534 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
535 ;; We don't use shr-indirect-call here, since shr-descend is
536 ;; the central bit of shr.el, and should be as fast as
537 ;; possible. Having one more level of indirection with its
538 ;; negative effect on performance is deemed unjustified in
541 (funcall external dom
))
543 (funcall function dom
))
546 (when (and shr-target-id
547 (equal (dom-attr dom
'id
) shr-target-id
))
548 ;; If the element was empty, we don't have anything to put the
549 ;; anchor on. So just insert a dummy character.
550 (when (= start
(point))
552 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
553 ;; If style is set, then this node has set the color.
557 (cdr (assq 'color shr-stylesheet
))
558 (cdr (assq 'background-color shr-stylesheet
))))))))
560 (defun shr-fill-text (text)
561 (if (zerop (length text
))
564 (let ((shr-indentation 0)
566 (shr-internal-width (- (window-body-width nil t
)
567 (* 2 (frame-char-width))
568 ;; Adjust the window width for when
569 ;; the user disables the fringes,
570 ;; which causes the display engine
571 ;; to usurp one column for the
572 ;; continuation glyph.
573 (if (and (null shr-width
)
574 (not (shr--have-one-fringe-p)))
575 (* (frame-char-width) 2)
578 (shr-fill-lines (point-min) (point-max))
581 (define-inline shr-char-breakable-p
(char)
582 "Return non-nil if a line can be broken before and after CHAR."
583 (inline-quote (aref fill-find-break-point-function-table
,char
)))
584 (define-inline shr-char-nospace-p
(char)
585 "Return non-nil if no space is required before and after CHAR."
586 (inline-quote (aref fill-nospace-between-words-table
,char
)))
588 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
589 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
590 ;; parentheses, and so on, that should not be placed in the beginning
591 ;; of a line or the end of a line.
592 (define-inline shr-char-kinsoku-bol-p
(char)
593 "Return non-nil if a line ought not to begin with CHAR."
594 (inline-letevals (char)
595 (inline-quote (and (not (eq ,char ?
'))
596 (aref (char-category-set ,char
) ?
>)))))
597 (define-inline shr-char-kinsoku-eol-p
(char)
598 "Return non-nil if a line ought not to end with CHAR."
599 (inline-quote (aref (char-category-set ,char
) ?
<)))
600 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
601 (load "kinsoku" nil t
))
603 (defun shr-pixel-column ()
604 (if (not shr-use-fonts
)
606 (if (not (get-buffer-window (current-buffer)))
607 (save-window-excursion
608 ;; Avoid errors if the selected window is a dedicated one,
609 ;; and they just want to insert a document into it.
610 (set-window-dedicated-p nil nil
)
611 (set-window-buffer nil
(current-buffer))
612 (car (window-text-pixel-size nil
(line-beginning-position) (point))))
613 (car (window-text-pixel-size nil
(line-beginning-position) (point))))))
615 (defun shr-pixel-region ()
616 (- (shr-pixel-column)
619 (shr-pixel-column))))
621 (defun shr-string-pixel-width (string)
622 (if (not shr-use-fonts
)
624 ;; Save and restore point across with-temp-buffer, since
625 ;; shr-pixel-column uses save-window-excursion, which can reset
634 (defsubst shr--translate-insertion-chars
()
635 ;; Remove soft hyphens.
636 (goto-char (point-min))
637 (while (search-forward "Â" nil t
)
638 (replace-match "" t t
))
639 ;; Translate non-breaking spaces into real spaces.
640 (goto-char (point-min))
641 (while (search-forward "Â " nil t
)
642 (replace-match " " t t
)))
644 (defun shr-insert (text)
645 (when (and (not (bolp))
646 (get-text-property (1- (point)) 'image-url
))
649 ((eq shr-folding-mode
'none
)
650 (let ((start (point)))
653 (narrow-to-region start
(point))
654 (shr--translate-insertion-chars)
655 (goto-char (point-max)))))
657 (let ((font-start (point)))
658 (when (and (string-match "\\`[ \t\n\r]" text
)
660 (not (eq (char-after (1- (point))) ?
)))
662 (let ((start (point))
666 (narrow-to-region start
(point))
668 (when (looking-at "[ \t\n\r]+")
669 (replace-match "" t t
))
670 (while (re-search-forward "[ \t\n\r]+" nil t
)
671 (replace-match " " t t
))
672 (shr--translate-insertion-chars)
673 (goto-char (point-max)))
674 ;; We may have removed everything we inserted if it was just
676 (unless (= font-start
(point))
677 ;; Mark all lines that should possibly be folded afterwards.
679 (shr-mark-fill start
))
681 (put-text-property font-start
(point)
683 (or shr-current-font
'variable-pitch
)))))))))
685 (defun shr-fill-lines (start end
)
686 (if (<= shr-internal-width
0)
689 (narrow-to-region start end
)
691 (when (get-text-property (point) 'shr-indentation
)
693 (while (setq start
(next-single-property-change start
'shr-indentation
))
697 (goto-char (point-max)))))
699 (defun shr-vertical-motion (column)
700 (if (not shr-use-fonts
)
701 (move-to-column column
)
704 (vertical-motion (cons (/ column
(frame-char-width)) 0))
708 (defun shr-fill-line ()
709 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
710 (continuation (get-text-property
711 (point) 'shr-continuation-indentation
))
713 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
714 (let ((face (get-text-property (point) 'face
))
715 (background-start (point)))
718 (put-text-property background-start
(point) 'face
719 `,(shr-face-background face
))))
721 (setq shr-indentation
(or continuation shr-indentation
))
722 (shr-vertical-motion shr-internal-width
)
723 (when (looking-at " $")
724 (delete-region (point) (line-end-position)))
726 ;; We have to do some folding. First find the first
727 ;; previous point suitable for folding.
728 (if (or (not (shr-find-fill-point (line-beginning-position)))
730 ;; We had unbreakable text (for this width), so just go to
731 ;; the first space and carry on.
734 (skip-chars-forward " ")
735 (search-forward " " (line-end-position) 'move
)))
736 ;; Success; continue.
737 (let ((props (copy-sequence (text-properties-at (point))))
739 (when (= (preceding-char) ?\s
)
741 ;; We don't want to use the faces on the indentation, because
742 ;; that's ugly, but we want all the other properties to be
743 ;; continuous so that links do not split up into many links
744 ;; (which makes navigation awkward).
745 (setq props
(plist-put props
'face nil
))
748 (add-text-properties gap-start
(point) props
))
750 (shr-vertical-motion shr-internal-width
)
751 (when (looking-at " $")
752 (delete-region (point) (line-end-position))))))
754 (defun shr-find-fill-point (start)
758 (while (not (or (setq failed
(<= (point) start
))
759 (eq (preceding-char) ?
)
760 (eq (following-char) ?
)
761 (shr-char-breakable-p (preceding-char))
762 (shr-char-breakable-p (following-char))
763 (and (shr-char-kinsoku-bol-p (preceding-char))
764 (shr-char-breakable-p (following-char))
765 (not (shr-char-kinsoku-bol-p (following-char))))
766 (shr-char-kinsoku-eol-p (following-char))
770 ;; There's no breakable point, so we give it up.
773 ;; Don't overflow the window edge, even if
774 ;; shr-kinsoku-shorten is nil.
775 (unless (or shr-kinsoku-shorten
(null shr-width
))
776 (while (setq found
(re-search-forward
777 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
778 (line-end-position) 'move
)))
780 (not (match-beginning 1)))
781 (goto-char (match-beginning 0)))))
784 ;; Don't put kinsoku-bol characters at the beginning of a line,
785 ;; or kinsoku-eol characters at the end of a line.
787 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
789 ((or shr-kinsoku-shorten
(null shr-width
))
790 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
791 (or (shr-char-kinsoku-eol-p (preceding-char))
792 (shr-char-kinsoku-bol-p (following-char))))
794 (when (setq failed
(<= (point) start
))
795 ;; There's no breakable point that doesn't violate kinsoku,
796 ;; so we look for the second best position.
802 (shr-char-kinsoku-eol-p (following-char)))))
804 ((shr-char-kinsoku-eol-p (preceding-char))
805 ;; Find backward the point where kinsoku-eol characters begin.
810 (and (> (setq count
(1- count
)) 0)
811 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
812 (or (shr-char-kinsoku-eol-p (preceding-char))
813 (shr-char-kinsoku-bol-p (following-char)))))))
814 (when (setq failed
(<= (point) start
))
815 ;; There's no breakable point that doesn't violate kinsoku,
816 ;; so we go to the second best position.
817 (if (looking-at "\\(\\c<+\\)\\c<")
818 (goto-char (match-end 1))
820 ((shr-char-kinsoku-bol-p (following-char))
821 ;; Find forward the point where kinsoku-bol characters end.
825 (and (>= (setq count
(1- count
)) 0)
826 (shr-char-kinsoku-bol-p (following-char))
827 (shr-char-breakable-p (following-char))))))))
828 (when (eq (following-char) ?
)
832 (defun shr-parse-base (url)
833 ;; Always chop off anchors.
834 (when (string-match "#.*" url
)
835 (setq url
(substring url
0 (match-beginning 0))))
836 ;; NB: <base href="" > URI may itself be relative to the document s URI
837 (setq url
(shr-expand-url url
))
838 (let* ((parsed (url-generic-parse-url url
))
839 (local (url-filename parsed
)))
840 (setf (url-filename parsed
) "")
841 ;; Chop off the bit after the last slash.
842 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
843 (setq local
(match-string 1 local
)))
844 ;; Always make the local bit end with a slash.
845 (when (and (not (zerop (length local
)))
846 (not (eq (aref local
(1- (length local
))) ?
/)))
847 (setq local
(concat local
"/")))
848 (list (url-recreate-url parsed
)
853 (autoload 'url-expand-file-name
"url-expand")
855 ;; FIXME This needs some tests writing.
856 ;; Does it even need to exist, given that url-expand-file-name does?
857 (defun shr-expand-url (url &optional base
)
860 ;; shr-parse-base should never call this with non-nil base!
861 (shr-parse-base base
)
862 ;; Bound by the parser.
864 (when (zerop (length url
))
866 ;; Strip leading whitespace
867 (and url
(string-match "\\`\\s-+" url
)
868 (setq url
(substring url
(match-end 0))))
869 (cond ((zerop (length url
))
872 (string-match "\\`[a-z]*:" url
))
873 ;; Absolute or empty URI
875 ((eq (aref url
0) ?
/)
876 (if (and (> (length url
) 1)
877 (eq (aref url
1) ?
/))
878 ;; //host...; just use the protocol
879 (concat (nth 2 base
) ":" url
)
880 ;; Just use the host name part.
881 (concat (car base
) url
)))
882 ((eq (aref url
0) ?
#)
883 ;; A link to an anchor.
884 (concat (nth 3 base
) url
))
887 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
889 (defun shr-ensure-newline ()
891 (let ((prefix (get-text-property (line-beginning-position)
892 'shr-prefix-length
)))
893 (unless (or (zerop (current-column))
895 (= prefix
(- (point) (line-beginning-position)))))
898 (defun shr-ensure-paragraph ()
900 (let ((prefix (get-text-property (line-beginning-position)
901 'shr-prefix-length
)))
907 ;; We're already at a new paragraph; do nothing.
910 (= prefix
(- (point) (line-beginning-position))))
911 ;; Do nothing; we're at the start of a <li>.
915 ;; If the current line is totally blank, and doesn't even
916 ;; have any face properties set, then delete the blank
918 (and (looking-at " *$")
919 (not (get-text-property (point) 'face
))
920 (not (= (next-single-property-change (point) 'face nil
922 (line-end-position)))))
923 (delete-region (match-beginning 0) (match-end 0)))
924 ;; We have a single blank line.
927 ;; Insert new paragraph.
932 (when (> shr-indentation
0)
934 (if (not shr-use-fonts
)
935 (make-string shr-indentation ?\s
)
938 `(space :width
(,shr-indentation
)))))))
940 (defun shr-fontize-dom (dom &rest types
)
941 (let ((start (point)))
944 (shr-add-font start
(point) type
))))
946 ;; Add face to the region, but avoid putting the font properties on
947 ;; blank text at the start of the line, and the newline at the end, to
949 (defun shr-add-font (start end type
)
952 (while (< (point) end
)
954 (skip-chars-forward " "))
955 (add-face-text-property (point) (min (line-end-position) end
) type t
)
956 (if (< (line-end-position) end
)
960 (defun shr-mouse-browse-url (ev)
961 "Browse the URL under the mouse cursor."
966 (defun shr-browse-url (&optional external mouse-event
)
967 "Browse the URL at point using `browse-url'.
968 If EXTERNAL is non-nil (interactively, the prefix argument), browse
969 the URL using `shr-external-browser'.
970 If this function is invoked by a mouse click, it will browse the URL
971 at the position of the click. Optional argument MOUSE-EVENT describes
972 the mouse click event."
973 (interactive (list current-prefix-arg last-nonmenu-event
))
974 (mouse-set-point mouse-event
)
975 (let ((url (get-text-property (point) 'shr-url
)))
978 (message "No link under point"))
979 ((string-match "^mailto:" url
)
980 (browse-url-mail url
))
984 (funcall shr-external-browser url
)
986 (browse-url url
))))))
988 (defun shr-save-contents (directory)
989 "Save the contents from URL in a file."
990 (interactive "DSave contents of URL to directory: ")
991 (let ((url (get-text-property (point) 'shr-url
)))
993 (message "No link under point")
994 (url-retrieve (shr-encode-url url
)
995 'shr-store-contents
(list url directory
)
998 (defun shr-store-contents (status url directory
)
999 (unless (plist-get status
:error
)
1000 (when (or (search-forward "\n\n" nil t
)
1001 (search-forward "\r\n\r\n" nil t
))
1002 (write-region (point) (point-max)
1003 (expand-file-name (file-name-nondirectory url
)
1006 (defun shr-image-fetched (status buffer start end
&optional flags
)
1007 (let ((image-buffer (current-buffer)))
1008 (when (and (buffer-name buffer
)
1009 (not (plist-get status
:error
)))
1010 (url-store-in-cache image-buffer
)
1011 (goto-char (point-min))
1012 (when (or (search-forward "\n\n" nil t
)
1013 (search-forward "\r\n\r\n" nil t
))
1014 (let ((data (shr-parse-image-data)))
1015 (with-current-buffer buffer
1019 (let ((alt (buffer-substring start end
))
1020 (properties (text-properties-at start
))
1021 (inhibit-read-only t
))
1022 (delete-region start end
)
1024 (funcall shr-put-image-function data alt flags
)
1026 (let ((type (pop properties
))
1027 (value (pop properties
)))
1028 (unless (memq type
'(display image-size
))
1029 (put-text-property start
(point) type value
)))))))))))
1030 (kill-buffer image-buffer
)))
1032 (defun shr-image-from-data (data)
1033 "Return an image from the data: URI content DATA."
1035 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
1037 (let ((param (match-string 4 data
))
1038 (payload (url-unhex-string (match-string 5 data
))))
1040 (string-match "^.*\\(;[ \t]*base64\\)$" param
))
1041 (setq payload
(ignore-errors
1042 (base64-decode-string payload
))))
1045 ;; Behind display-graphic-p test.
1046 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1047 (declare-function image-animate
"image" (image &optional index limit
))
1049 (defun shr-put-image (spec alt
&optional flags
)
1050 "Insert image SPEC with a string ALT. Return image.
1051 SPEC is either an image data blob, or a list where the first
1052 element is the data blob and the second element is the content-type."
1053 (if (display-graphic-p)
1054 (let* ((size (cdr (assq 'size flags
)))
1055 (data (if (consp spec
)
1058 (content-type (and (consp spec
)
1062 ((eq size
'original
)
1063 (create-image data nil t
:ascent
100
1064 :format content-type
))
1065 ((eq content-type
'image
/svg
+xml
)
1066 (create-image data
'svg t
:ascent
100))
1069 (shr-rescale-image data content-type
1070 (plist-get flags
:width
)
1071 (plist-get flags
:height
))))
1074 (shr-rescale-image data content-type
1075 (plist-get flags
:width
)
1076 (plist-get flags
:height
)))))))
1078 ;; When inserting big-ish pictures, put them at the
1079 ;; beginning of the line.
1080 (when (and (> (current-column) 0)
1081 (> (car (image-size image t
)) 400))
1083 (if (eq size
'original
)
1084 (insert-sliced-image image
(or alt
"*") nil
20 1)
1085 (insert-image image
(or alt
"*")))
1086 (put-text-property start
(point) 'image-size size
)
1087 (when (and shr-image-animate
1088 (cond ((fboundp 'image-multi-frame-p
)
1089 ;; Only animate multi-frame things that specify a
1090 ;; delay; eg animated gifs as opposed to
1091 ;; multi-page tiffs. FIXME?
1092 (cdr (image-multi-frame-p image
)))
1093 ((fboundp 'image-animated-p
)
1094 (image-animated-p image
))))
1095 (image-animate image nil
60)))
1097 (insert (or alt
""))))
1099 (defun shr-rescale-image (data content-type width height
1100 &optional max-width max-height
)
1101 "Rescale DATA, if too big, to fit the current buffer.
1102 WIDTH and HEIGHT are the sizes given in the HTML data, if any.
1104 The size of the displayed image will not exceed
1105 MAX-WIDTH/MAX-HEIGHT. If not given, use the current window
1106 width/height instead."
1107 (if (or (not (fboundp 'imagemagick-types
))
1108 (not (get-buffer-window (current-buffer))))
1109 (create-image data nil t
:ascent
100)
1110 (let* ((edges (window-inside-pixel-edges
1111 (get-buffer-window (current-buffer))))
1112 (max-width (truncate (* shr-max-image-proportion
1114 (- (nth 2 edges
) (nth 0 edges
))))))
1115 (max-height (truncate (* shr-max-image-proportion
1117 (- (nth 3 edges
) (nth 1 edges
))))))
1118 (scaling (image-compute-scaling-factor image-scaling-factor
)))
1119 (when (or (and width
1120 (> width max-width
))
1122 (> height max-height
)))
1125 (if (and width height
1126 (< (* width scaling
) max-width
)
1127 (< (* height scaling
) max-height
))
1133 :format content-type
)
1137 :max-width max-width
1138 :max-height max-height
1139 :format content-type
)))))
1141 ;; url-cache-extract autoloads url-cache.
1142 (declare-function url-cache-create-filename
"url-cache" (url))
1143 (autoload 'mm-disable-multibyte
"mm-util")
1144 (autoload 'browse-url-mail
"browse-url")
1146 (defun shr-get-image-data (url)
1147 "Get image data for URL.
1148 Return a string with image data."
1150 (mm-disable-multibyte)
1151 (when (ignore-errors
1152 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
1154 (when (re-search-forward "\r?\n\r?\n" nil t
)
1155 (shr-parse-image-data)))))
1157 (declare-function libxml-parse-xml-region
"xml.c"
1158 (start end
&optional base-url discard-comments
))
1160 (defun shr-parse-image-data ()
1161 (let ((data (buffer-substring (point) (point-max)))
1165 (narrow-to-region (point-min) (point))
1166 (let ((content-type (mail-fetch-field "content-type")))
1168 ;; Remove any comments in the type string.
1169 (intern (replace-regexp-in-string ";.*" "" content-type
)
1171 ;; SVG images may contain references to further images that we may
1172 ;; want to block. So special-case these by parsing the XML data
1173 ;; and remove anything that looks like a blocked bit.
1174 (when (and shr-blocked-images
1175 (eq content-type
'image
/svg
+xml
))
1177 ;; Note that libxml2 doesn't parse everything perfectly,
1178 ;; so glitches may occur during this transformation.
1180 (libxml-parse-xml-region (point) (point-max)))))
1181 (list data content-type
)))
1183 (defun shr-image-displayer (content-function)
1184 "Return a function to display an image.
1185 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1186 is an argument. The function to be returned takes three arguments URL,
1187 START, and END. Note that START and END should be markers."
1188 `(lambda (url start end
)
1190 (if (string-match "\\`cid:" url
)
1191 ,(when content-function
1192 `(let ((image (funcall ,content-function
1193 (substring url
(match-end 0)))))
1196 (funcall shr-put-image-function
1197 image
(buffer-substring start end
))
1198 (delete-region (point) end
))))
1199 (url-retrieve url
'shr-image-fetched
1200 (list (current-buffer) start end
)
1203 (defun shr-heading (dom &rest types
)
1204 (shr-ensure-paragraph)
1205 (apply #'shr-fontize-dom dom types
)
1206 (shr-ensure-paragraph))
1208 (defun shr-urlify (start url
&optional title
)
1209 (shr-add-font start
(point) 'shr-link
)
1210 (add-text-properties
1213 'help-echo
(let ((parsed (url-generic-parse-url
1215 (decode-coding-string
1216 (url-unhex-string url
)
1220 ;; If we have an IDNA domain, then show the
1221 ;; decoded version in the mouseover to let the
1222 ;; user know that there's something possibly
1224 (when (url-host parsed
)
1225 (setf (url-host parsed
)
1226 (puny-encode-domain (url-host parsed
))))
1227 (setq iri
(url-recreate-url parsed
))
1229 (format "%s (%s)" iri title
)
1232 'mouse-face
'highlight
))
1233 ;; Don't overwrite any keymaps that are already in the buffer (i.e.,
1237 (let ((next (next-single-property-change start
'keymap nil
(point))))
1238 (if (get-text-property start
'keymap
)
1240 (put-text-property start
(or next
(point)) 'keymap shr-map
)))))
1242 (defun shr-encode-url (url)
1244 (browse-url-url-encode-chars url
"[)$ ]"))
1246 (autoload 'shr-color-visible
"shr-color")
1247 (autoload 'shr-color-
>hexadecimal
"shr-color")
1249 (defun shr-color-check (fg bg
)
1250 "Check that FG is visible on BG.
1251 Returns (fg bg) with corrected values.
1252 Returns nil if the colors that would be used are the default
1253 ones, in case fg and bg are nil."
1255 (let ((fixed (cond ((null fg
) 'fg
)
1257 ;; Convert colors to hexadecimal, or set them to default.
1258 (let ((fg (or (shr-color->hexadecimal fg
)
1259 (frame-parameter nil
'foreground-color
)))
1260 (bg (or (shr-color->hexadecimal bg
)
1261 (frame-parameter nil
'background-color
))))
1262 (cond ((eq fixed
'bg
)
1263 ;; Only return the new fg
1264 (list nil
(cadr (shr-color-visible bg fg t
))))
1266 ;; Invert args and results and return only the new bg
1267 (list (cadr (shr-color-visible fg bg t
)) nil
))
1269 (shr-color-visible bg fg
)))))))
1271 (defun shr-colorize-region (start end fg
&optional bg
)
1272 (when (and shr-use-colors
1274 (>= (display-color-cells) 88))
1275 (let ((new-colors (shr-color-check fg bg
)))
1278 (add-face-text-property start end
1279 (list :foreground
(cadr new-colors
))
1282 (add-face-text-property start end
1283 (list :background
(car new-colors
))
1287 ;;; Tag-specific rendering rules.
1289 (defun shr-tag-html (dom)
1290 (let ((dir (dom-attr dom
'dir
)))
1293 (setq bidi-paragraph-direction
'left-to-right
))
1295 (setq bidi-paragraph-direction
'right-to-left
))
1297 (setq bidi-paragraph-direction nil
))))
1300 (defun shr-tag-body (dom)
1301 (let* ((start (point))
1302 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1303 (bgcolor (dom-attr dom
'bgcolor
))
1304 (shr-stylesheet (list (cons 'color fgcolor
)
1305 (cons 'background-color bgcolor
))))
1307 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1309 (defun shr-tag-style (_dom)
1312 (defun shr-tag-script (_dom)
1315 (defun shr-tag-comment (_dom)
1318 (defun shr-dom-to-xml (dom)
1323 (defun shr-dom-print (dom)
1324 "Convert DOM into a string containing the xml representation."
1325 (insert (format "<%s" (dom-tag dom
)))
1326 (dolist (attr (dom-attributes dom
))
1327 ;; Ignore attributes that start with a colon because they are
1328 ;; private elements.
1329 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1330 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1333 (dolist (elem (dom-children dom
))
1337 ((eq (dom-tag elem
) 'comment
)
1339 ((or (not (eq (dom-tag elem
) 'image
))
1340 ;; Filter out blocked elements inside the SVG image.
1341 (not (setq url
(dom-attr elem
':xlink
:href
)))
1342 (not shr-blocked-images
)
1343 (not (string-match shr-blocked-images url
)))
1345 (shr-dom-print elem
)))))
1346 (insert (format "</%s>" (dom-tag dom
))))
1348 (defun shr-tag-svg (dom)
1349 (when (and (image-type-available-p 'svg
)
1350 (not shr-inhibit-images
)
1351 (dom-attr dom
'width
)
1352 (dom-attr dom
'height
))
1353 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1356 (defun shr-tag-sup (dom)
1357 (let ((start (point)))
1359 (put-text-property start
(point) 'display
'(raise 0.5))))
1361 (defun shr-tag-sub (dom)
1362 (let ((start (point)))
1364 (put-text-property start
(point) 'display
'(raise -
0.5))))
1366 (defun shr-tag-p (dom)
1367 (shr-ensure-paragraph)
1369 (shr-ensure-paragraph))
1371 (defun shr-tag-div (dom)
1372 (let ((display (cdr (assq 'display shr-stylesheet
))))
1373 (if (or (equal display
"inline")
1374 (equal display
"inline-block"))
1376 (shr-ensure-newline)
1378 (shr-ensure-newline))))
1380 (defun shr-tag-s (dom)
1381 (shr-fontize-dom dom
'shr-strike-through
))
1383 (defun shr-tag-b (dom)
1384 (shr-fontize-dom dom
'bold
))
1386 (defun shr-tag-i (dom)
1387 (shr-fontize-dom dom
'italic
))
1389 (defun shr-tag-em (dom)
1390 (shr-fontize-dom dom
'italic
))
1392 (defun shr-tag-strong (dom)
1393 (shr-fontize-dom dom
'bold
))
1395 (defun shr-tag-u (dom)
1396 (shr-fontize-dom dom
'underline
))
1398 (defun shr-tag-tt (dom)
1399 (let ((shr-current-font 'default
))
1402 (defun shr-tag-ins (cont)
1403 (let* ((start (point))
1405 (shr-stylesheet (nconc (list (cons 'color color
))
1408 (shr-colorize-region start
(point) color
1409 (cdr (assq 'background-color shr-stylesheet
)))))
1411 (defun shr-tag-del (cont)
1412 (let* ((start (point))
1414 (shr-stylesheet (nconc (list (cons 'color color
))
1416 (shr-fontize-dom cont
'shr-strike-through
)
1417 (shr-colorize-region start
(point) color
1418 (cdr (assq 'background-color shr-stylesheet
)))))
1420 (defun shr-parse-style (style)
1423 (when (string-match "\n" style
)
1424 (setq style
(replace-match " " t t style
))))
1426 (dolist (elem (split-string style
";"))
1428 (setq elem
(split-string elem
":"))
1429 (when (and (car elem
)
1431 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1432 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1433 (when (string-match " *!important\\'" value
)
1434 (setq value
(substring value
0 (match-beginning 0))))
1435 (unless (equal value
"inherit")
1436 (push (cons (intern name obarray
)
1441 (defun shr-tag-base (dom)
1442 (when-let* ((base (dom-attr dom
'href
)))
1443 (setq shr-base
(shr-parse-base base
)))
1446 (defun shr-tag-a (dom)
1447 (let ((url (dom-attr dom
'href
))
1448 (title (dom-attr dom
'title
))
1452 (when (and shr-target-id
1453 (equal (dom-attr dom
'name
) shr-target-id
))
1454 ;; We have a zero-length <a name="foo"> element, so just
1455 ;; insert... something.
1456 (when (= start
(point))
1457 (shr-ensure-newline)
1459 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1461 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1463 (defun shr-tag-object (dom)
1464 (unless shr-inhibit-images
1465 (let ((start (point))
1466 url multimedia image
)
1467 (when-let* ((type (dom-attr dom
'type
)))
1468 (when (string-match "\\`image/svg" type
)
1469 (setq url
(dom-attr dom
'data
)
1471 (dolist (child (dom-non-text-children dom
))
1473 ((eq (dom-tag child
) 'embed
)
1474 (setq url
(or url
(dom-attr child
'src
))
1476 ((and (eq (dom-tag child
) 'param
)
1477 (equal (dom-attr child
'name
) "movie"))
1478 (setq url
(or url
(dom-attr child
'value
))
1483 (shr-indirect-call 'img dom url
)
1486 (shr-insert " [multimedia] ")
1487 (shr-urlify start
(shr-expand-url url
)))))
1489 (shr-generic dom
)))))
1491 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1497 "Preferences for media types.
1498 The key element should be a regexp matched against the type of the source or
1499 url if no type is specified. The value should be a float in the range 0.0 to
1500 1.0. Media elements with higher value are preferred."
1503 :type
'(alist :key-type regexp
:value-type float
))
1505 (defun shr--get-media-pref (elem)
1506 "Determine the preference for ELEM.
1507 The preference is a float determined from `shr-prefer-media-type'."
1508 (let ((type (dom-attr elem
'type
))
1511 (setq type
(dom-attr elem
'src
)))
1513 (dolist (pref shr-prefer-media-type-alist
)
1516 (string-match-p (car pref
) type
))
1517 (setq p
(cdr pref
)))))
1520 (defun shr--extract-best-source (dom &optional url pref
)
1521 "Extract the best `:src' property from <source> blocks in DOM."
1522 (setq pref
(or pref -
1.0))
1524 (dolist (elem (dom-non-text-children dom
))
1525 (when (and (eq (dom-tag elem
) 'source
)
1528 (shr--get-media-pref elem
))))
1530 url
(dom-attr elem
'src
))
1531 ;; libxml's html parser isn't HTML5 compliant and non terminated
1532 ;; source tags might end up as children. So recursion it is...
1533 (dolist (child (dom-non-text-children elem
))
1534 (when (eq (dom-tag child
) 'source
)
1535 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1536 (when (< pref
(cdr ret
))
1538 pref
(cdr ret
)))))))))
1541 (defun shr-tag-video (dom)
1542 (let ((image (dom-attr dom
'poster
))
1543 (url (dom-attr dom
'src
))
1546 (setq url
(car (shr--extract-best-source dom
))))
1547 (if (> (length image
) 0)
1548 (shr-indirect-call 'img nil image
)
1549 (shr-insert " [video] "))
1550 (shr-urlify start
(shr-expand-url url
))))
1552 (defun shr-tag-audio (dom)
1553 (let ((url (dom-attr dom
'src
))
1556 (setq url
(car (shr--extract-best-source dom
))))
1557 (shr-insert " [audio] ")
1558 (shr-urlify start
(shr-expand-url url
))))
1560 (defun shr-tag-img (dom &optional url
)
1563 (or (> (length (dom-attr dom
'src
)) 0)
1564 (> (length (dom-attr dom
'srcset
)) 0))))
1565 (when (> (current-column) 0)
1567 (let ((alt (dom-attr dom
'alt
))
1568 (width (shr-string-number (dom-attr dom
'width
)))
1569 (height (shr-string-number (dom-attr dom
'height
)))
1570 (url (shr-expand-url (or url
(shr--preferred-image dom
)))))
1571 (let ((start (point-marker)))
1572 (when (zerop (length alt
))
1575 ((or (member (dom-attr dom
'height
) '("0" "1"))
1576 (member (dom-attr dom
'width
) '("0" "1")))
1577 ;; Ignore zero-sized or single-pixel images.
1579 ((and (not shr-inhibit-images
)
1580 (string-match "\\`data:" url
))
1581 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1583 (funcall shr-put-image-function image alt
1584 (list :width width
:height height
))
1586 ((and (not shr-inhibit-images
)
1587 (string-match "\\`cid:" url
))
1588 (let ((url (substring url
(match-end 0)))
1590 (if (or (not shr-content-function
)
1591 (not (setq image
(funcall shr-content-function url
))))
1593 (funcall shr-put-image-function image alt
1594 (list :width width
:height height
)))))
1595 ((or shr-inhibit-images
1596 (and shr-blocked-images
1597 (string-match shr-blocked-images url
)))
1598 (setq shr-start
(point))
1600 ((and (not shr-ignore-cache
)
1601 (url-is-cached (shr-encode-url url
)))
1602 (funcall shr-put-image-function
(shr-get-image-data url
) alt
1603 (list :width width
:height height
)))
1605 (when (and shr-ignore-cache
1606 (url-is-cached (shr-encode-url url
)))
1607 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1608 (when (file-exists-p file
)
1609 (delete-file file
))))
1610 (when (image-type-available-p 'svg
)
1612 (shr-make-placeholder-image dom
)
1616 (shr-encode-url url
) 'shr-image-fetched
1617 (list (current-buffer) start
(set-marker (make-marker) (point))
1618 (list :width width
:height height
))
1620 (when (zerop shr-table-depth
) ;; We are not in a table.
1621 (put-text-property start
(point) 'keymap shr-image-map
)
1622 (put-text-property start
(point) 'shr-alt alt
)
1623 (put-text-property start
(point) 'image-url url
)
1624 (put-text-property start
(point) 'image-displayer
1625 (shr-image-displayer shr-content-function
))
1626 (put-text-property start
(point) 'help-echo
1628 (or (dom-attr dom
'title
) alt
))))))))
1630 (defun shr--preferred-image (dom)
1631 (let ((srcset (dom-attr dom
'srcset
))
1632 (frame-width (frame-pixel-width))
1633 (width (string-to-number (or (dom-attr dom
'width
) "100")))
1635 (when (> (length srcset
) 0)
1636 ;; srcset consist of a series of URL/size specifications
1637 ;; separated by the ", " string.
1641 (let ((spec (split-string elem
"[\t\n\r ]+")))
1643 ((= (length spec
) 1)
1644 ;; Make sure it's well formed.
1645 (list (car spec
) 0))
1646 ((string-match "\\([0-9]+\\)x\\'" (cadr spec
))
1647 ;; If we have an "x" form, then use the width
1648 ;; spec to compute the real width.
1650 (* width
(string-to-number
1651 (match-string 1 (cadr spec
))))))
1654 (string-to-number (cadr spec
)))))))
1655 (split-string (replace-regexp-in-string
1656 "\\`[\t\n\r ]+\\|[\t\n\r ]+\\'" "" srcset
)
1657 "[\t\n\r ]*,[\t\n\r ]*"))
1659 (> (cadr e1
) (cadr e2
)))))
1660 ;; Choose the smallest picture that's bigger than the current
1662 (setq candidate
(caar srcset
))
1664 (> (cadr (car srcset
)) frame-width
))
1665 (setq candidate
(caar srcset
))
1667 (or candidate
(dom-attr dom
'src
))))
1669 (defun shr-string-number (string)
1672 (setq string
(replace-regexp-in-string "[^0-9]" "" string
))
1673 (if (zerop (length string
))
1675 (string-to-number string
))))
1677 (defun shr-make-placeholder-image (dom)
1679 (get-buffer-window (current-buffer))
1680 (window-inside-pixel-edges
1681 (get-buffer-window (current-buffer)))))
1682 (scaling (image-compute-scaling-factor image-scaling-factor
))
1684 (* (or (shr-string-number (dom-attr dom
'width
)) 100)
1687 (* (or (shr-string-number (dom-attr dom
'height
)) 100)
1691 (truncate (* shr-max-image-proportion
1692 (- (nth 2 edges
) (nth 0 edges
))))))
1693 (max-height (and edges
1694 (truncate (* shr-max-image-proportion
1695 (- (nth 3 edges
) (nth 1 edges
))))))
1697 (when (and max-width
1698 (> width max-width
))
1699 (setq height
(truncate (* (/ (float max-width
) width
) height
))
1701 (when (and max-height
1702 (> height max-height
))
1703 (setq width
(truncate (* (/ (float max-height
) height
) width
))
1705 (setq svg
(svg-create width height
))
1706 (svg-gradient svg
"background" 'linear
'((0 .
"#b0b0b0") (100 .
"#808080")))
1707 (svg-rectangle svg
0 0 width height
:gradient
"background"
1708 :stroke-width
2 :stroke-color
"black")
1709 (let ((image (svg-image svg
)))
1710 (setf (image-property image
:ascent
) 100)
1713 (defun shr-tag-pre (dom)
1714 (let ((shr-folding-mode 'none
)
1715 (shr-current-font 'default
))
1716 (shr-ensure-newline)
1718 (shr-ensure-newline)))
1720 (defun shr-tag-blockquote (dom)
1721 (shr-ensure-paragraph)
1722 (let ((start (point))
1723 (shr-indentation (+ shr-indentation
1724 (* 4 shr-table-separator-pixel-width
))))
1726 (shr-ensure-paragraph)
1727 (shr-mark-fill start
)))
1729 (defun shr-tag-dl (dom)
1730 (shr-ensure-paragraph)
1732 (shr-ensure-paragraph))
1734 (defun shr-tag-dt (dom)
1735 (shr-ensure-newline)
1737 (shr-ensure-newline))
1739 (defun shr-tag-dd (dom)
1740 (shr-ensure-newline)
1741 (let ((shr-indentation (+ shr-indentation
1742 (* 4 shr-table-separator-pixel-width
))))
1745 (defun shr-tag-ul (dom)
1746 (shr-ensure-paragraph)
1747 (let ((shr-list-mode 'ul
))
1749 ;; If we end on an empty <li>, then make sure we really end on a new
1753 (shr-ensure-paragraph))
1755 (defun shr-tag-ol (dom)
1756 (shr-ensure-paragraph)
1757 (let ((shr-list-mode 1))
1759 (shr-ensure-paragraph))
1761 (defun shr-tag-li (dom)
1762 (shr-ensure-newline)
1763 (let ((start (point)))
1765 (if (numberp shr-list-mode
)
1767 (format "%d " shr-list-mode
)
1768 (setq shr-list-mode
(1+ shr-list-mode
)))
1769 (car shr-internal-bullet
)))
1770 (width (if (numberp shr-list-mode
)
1771 (shr-string-pixel-width bullet
)
1772 (cdr shr-internal-bullet
))))
1774 (shr-mark-fill start
)
1775 (let ((shr-indentation (+ shr-indentation width
)))
1776 (put-text-property start
(1+ start
)
1777 'shr-continuation-indentation shr-indentation
)
1778 (put-text-property start
(1+ start
) 'shr-prefix-length
(length bullet
))
1779 (shr-generic dom
))))
1783 (defun shr-mark-fill (start)
1784 ;; We may not have inserted any text to fill.
1785 (unless (= start
(point))
1786 (put-text-property start
(1+ start
)
1787 'shr-indentation shr-indentation
)))
1789 (defun shr-tag-br (dom)
1790 (when (and (not (bobp))
1791 ;; Only add a newline if we break the current line, or
1792 ;; the previous line isn't a blank line.
1794 (and (> (- (point) 2) (point-min))
1795 (not (= (char-after (- (point) 2)) ?
\n)))))
1799 (defun shr-tag-span (dom)
1802 (defun shr-tag-h1 (dom)
1803 (shr-heading dom
(if shr-use-fonts
1804 '(variable-pitch (:height
1.3 :weight bold
))
1807 (defun shr-tag-h2 (dom)
1808 (shr-heading dom
'bold
))
1810 (defun shr-tag-h3 (dom)
1811 (shr-heading dom
'italic
))
1813 (defun shr-tag-h4 (dom)
1816 (defun shr-tag-h5 (dom)
1819 (defun shr-tag-h6 (dom)
1822 (defun shr-tag-hr (_dom)
1823 (shr-ensure-newline)
1824 (insert (make-string (if (not shr-use-fonts
)
1826 (1+ (/ shr-internal-width
1827 shr-table-separator-pixel-width
)))
1831 (defun shr-tag-title (dom)
1832 (shr-heading dom
'bold
'underline
))
1834 (defun shr-tag-font (dom)
1835 (let* ((start (point))
1836 (color (dom-attr dom
'color
))
1837 (shr-stylesheet (nconc (list (cons 'color color
))
1841 (shr-colorize-region start
(point) color
1842 (cdr (assq 'background-color shr-stylesheet
))))))
1844 (defun shr-tag-bdo (dom)
1845 (let* ((direction (dom-attr dom
'dir
))
1847 ((equal direction
"ltr")
1848 ?\N
{LEFT-TO-RIGHT OVERRIDE
})
1849 ((equal direction
"rtl")
1850 ?\N
{RIGHT-TO-LEFT OVERRIDE
}))))
1852 (insert ?\N
{FIRST STRONG ISOLATE
} char
))
1855 (insert ?\N
{POP DIRECTIONAL FORMATTING
} ?\N
{POP DIRECTIONAL ISOLATE
}))))
1857 (defun shr-tag-bdi (dom)
1858 (insert ?\N
{FIRST STRONG ISOLATE
})
1860 (insert ?\N
{POP DIRECTIONAL ISOLATE
}))
1862 ;;; Table rendering algorithm.
1864 ;; Table rendering is the only complicated thing here. We do this by
1865 ;; first counting how many TDs there are in each TR, and registering
1866 ;; how wide they think they should be ("width=45%", etc). Then we
1867 ;; render each TD separately (this is done in temporary buffers, so
1868 ;; that we can use all the rendering machinery as if we were in the
1869 ;; main buffer). Now we know how much space each TD really takes, so
1870 ;; we then render everything again with the new widths, and finally
1871 ;; insert all these boxes into the main buffer.
1872 (defun shr-tag-table-1 (dom)
1873 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1874 (let* ((shr-inhibit-images t
)
1875 (shr-table-depth (1+ shr-table-depth
))
1876 (shr-kinsoku-shorten t
)
1877 ;; Find all suggested widths.
1878 (columns (shr-column-specs dom
))
1879 ;; Compute how many pixels wide each TD should be.
1880 (suggested-widths (shr-pro-rate-columns columns
))
1881 ;; Do a "test rendering" to see how big each TD is (this can
1882 ;; be smaller (if there's little text) or bigger (if there's
1883 ;; unbreakable text).
1884 (elems (or (dom-attr dom
'shr-suggested-widths
)
1885 (shr-make-table dom suggested-widths nil
1886 'shr-suggested-widths
)))
1887 (sketch (cl-loop for line in elems
1888 collect
(mapcar #'car line
)))
1889 (natural (cl-loop for line in elems
1890 collect
(mapcar #'cdr line
)))
1891 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1892 ;; This probably won't work very well.
1893 (when (> (+ (cl-loop for width across sketch-widths
1895 shr-indentation shr-table-separator-pixel-width
)
1897 (setq truncate-lines t
))
1898 ;; Then render the table again with these new "hard" widths.
1899 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1901 (defun shr-table-body (dom)
1902 (let ((tbodies (seq-filter (lambda (child)
1903 (eq (dom-tag child
) 'tbody
))
1904 (dom-non-text-children dom
))))
1908 ((= (length tbodies
) 1)
1911 ;; Table with multiple tbodies. Convert into a single tbody.
1912 `(tbody nil
,@(cl-reduce 'append
1913 (mapcar 'dom-non-text-children tbodies
)))))))
1915 (defun shr-tag-table (dom)
1916 (shr-ensure-paragraph)
1917 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1918 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1919 (body (dom-non-text-children (shr-table-body dom
)))
1920 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1921 (bgcolor (dom-attr dom
'bgcolor
))
1923 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1925 (nheader (if header
(shr-max-columns header
)))
1926 (nbody (if body
(shr-max-columns body
) 0))
1927 (nfooter (if footer
(shr-max-columns footer
))))
1928 (if (and (not caption
)
1930 (not (dom-child-by-tag dom
'tbody
))
1931 (not (dom-child-by-tag dom
'tr
))
1933 ;; The table is totally invalid and just contains random junk.
1934 ;; Try to output it anyway.
1936 ;; It's a real table, so render it.
1937 (if (dom-attr dom
'shr-fixed-table
)
1938 (shr-tag-table-1 dom
)
1939 ;; Only fix up the table once.
1943 (if caption
`((tr nil
(td nil
,@caption
))))
1947 ;; header + body + footer
1948 (if (= nheader nbody
)
1949 (if (= nbody nfooter
)
1950 `((tr nil
(td nil
(table nil
1952 ,@body
,@footer
)))))
1953 (nconc `((tr nil
(td nil
(table nil
1958 `((tr nil
(td nil
(table
1960 nil
,@footer
))))))))
1961 (nconc `((tr nil
(td nil
(table nil
(tbody
1963 (if (= nbody nfooter
)
1964 `((tr nil
(td nil
(table
1965 nil
(tbody nil
,@body
1967 (nconc `((tr nil
(td nil
(table
1972 `((tr nil
(td nil
(table
1978 (if (= nheader nbody
)
1979 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1982 `(,@header
(tr nil
(td nil
(table
1983 nil
(tbody nil
,@body
)))))
1984 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1985 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1988 (if (= nbody nfooter
)
1989 `((tr nil
(td nil
(table
1990 nil
(tbody nil
,@body
,@footer
)))))
1991 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1994 `((tr nil
(td nil
(table
1995 nil
(tbody nil
,@footer
)))))))))
1997 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1999 (dom-set-attribute table
'shr-fixed-table t
)
2000 (setcdr dom
(cdr table
))
2001 (shr-tag-table-1 dom
))))
2003 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
2005 ;; Finally, insert all the images after the table. The Emacs buffer
2006 ;; model isn't strong enough to allow us to put the images actually
2007 ;; into the tables. It inserts also non-td/th objects.
2008 (when (zerop shr-table-depth
)
2010 (shr-expand-alignments start
(point)))
2011 (let ((strings (shr-collect-extra-strings-in-table dom
)))
2014 (narrow-to-region (point) (point))
2015 (insert (mapconcat #'identity strings
"\n"))
2016 (shr-fill-lines (point-min) (point-max))))))))
2018 (defun shr-collect-extra-strings-in-table (dom &optional flags
)
2019 "Return extra strings in DOM of which the root is a table clause.
2020 Render <img>s and <object>s, and strings and child <table>s of which
2021 the parent <td> or <th> is lacking. FLAGS is a cons of two boolean
2022 flags that control whether to collect or render objects."
2023 ;; This function runs recursively and collects strings if the cdr of
2024 ;; FLAGS is nil and the car is not nil, and it renders also child
2025 ;; <table>s if the cdr is nil. Note: FLAGS may be nil, not a cons.
2026 ;; FLAGS becomes (t . nil) if a <tr> clause is found in the children
2027 ;; of DOM, and becomes (t . t) if a <td> or a <th> clause is found
2028 ;; and the car is t then. When a <table> clause is found, FLAGS
2029 ;; becomes nil if the cdr is t then. But if FLAGS is (t . nil) then,
2030 ;; it renders the <table>.
2031 (cl-loop for child in
(dom-children dom
) with recurse with tag
2032 do
(setq recurse nil
)
2034 unless
(or (not (car flags
)) (cdr flags
))
2035 when
(string-match "\\(?:[^\t\n\r ]+[\t\n\r ]+\\)*[^\t\n\r ]+"
2037 collect
(match-string 0 child
)
2039 else if
(consp child
)
2040 do
(setq tag
(dom-tag child
)) and
2041 unless
(memq tag
'(comment style
))
2043 do
(shr-indirect-call 'img child
)
2044 else if
(eq tag
'object
)
2045 do
(shr-indirect-call 'object child
)
2047 do
(setq recurse t
) and
2049 do
(setq flags
'(t . nil
))
2050 else if
(memq tag
'(td th
))
2052 do
(setq flags
'(t . t
))
2054 else if
(eq tag
'table
)
2058 do
(setq recurse nil
)
2059 (shr-indirect-call 'table child
)
2060 end end end end end end end end end end
2062 append
(shr-collect-extra-strings-in-table child flags
)))
2064 (defun shr-insert-table (table widths
)
2065 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
2067 (shr-table-separator-length (if collapse
0 1))
2068 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
2070 (setq shr-table-id
(1+ shr-table-id
))
2072 (shr-insert-table-ruler widths
))
2074 (let ((start (point))
2077 (height (let ((max 0))
2078 (dolist (column row
)
2079 (setq max
(max max
(nth 2 column
))))
2081 (dotimes (_ (max height
1))
2083 (insert shr-table-vertical-line
"\n"))
2084 (dolist (column row
)
2085 (when (> (nth 2 column
) -
1)
2087 ;; Sum up all the widths from the column. (There may be
2088 ;; more than one if this is a "colspan" column.)
2089 (dotimes (_ (nth 4 column
))
2090 ;; The colspan directive may be wrong and there may not be
2091 ;; that number of columns.
2092 (when (<= column-number
(1- (length widths
)))
2093 (setq align
(+ align
2094 (aref widths column-number
)
2095 (* 2 shr-table-separator-pixel-width
))))
2096 (setq column-number
(1+ column-number
)))
2097 (let ((lines (nth 3 column
))
2098 (pixel-align (if (not shr-use-fonts
)
2099 (* align
(frame-char-width))
2101 (dolist (line lines
)
2103 (let ((start (point))
2104 (background (and (> (length line
) 0)
2105 (shr-face-background
2107 (1- (length line
)) 'face line
))))
2110 'display
`(space :align-to
(,pixel-align
))
2111 'shr-table-indent shr-table-id
)))
2113 (setq space
(propertize space
'face background
)))
2114 (insert line space shr-table-vertical-line
)
2115 (shr-colorize-region
2116 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2118 ;; Add blank lines at padding at the bottom of the TD,
2120 (dotimes (_ (- height
(length lines
)))
2122 (let ((start (point)))
2123 (insert (propertize " "
2124 'display
`(space :align-to
(,pixel-align
))
2125 'shr-table-indent shr-table-id
)
2126 shr-table-vertical-line
)
2127 (shr-colorize-region
2128 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2129 (forward-line 1))))))
2131 (shr-insert-table-ruler widths
)))
2132 (unless (= start
(point))
2133 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
2135 (defun shr-face-background (face)
2137 (or (and (plist-get face
:background
)
2138 (list :background
(plist-get face
:background
)))
2139 (let ((background nil
))
2141 (when (and (consp elem
)
2142 (eq (car elem
) :background
)
2144 (setq background
(cadr elem
))))
2146 (list :background background
))))))
2148 (defun shr-expand-alignments (start end
)
2149 (while (< (setq start
(next-single-property-change
2150 start
'shr-table-id nil end
))
2153 (let* ((shr-use-fonts t
)
2154 (id (get-text-property (point) 'shr-table-id
))
2155 (base (shr-pixel-column))
2159 (while (setq elem
(text-property-any
2160 (point) end
'shr-table-indent id
))
2162 (let ((align (get-text-property (point) 'display
)))
2163 (put-text-property (point) (1+ (point)) 'display
2164 `(space :align-to
(,(+ (car (nth 2 align
))
2166 (forward-char 1)))))
2167 (setq start
(1+ start
))))
2169 (defun shr-insert-table-ruler (widths)
2170 (when shr-table-horizontal-line
2172 (> shr-indentation
0))
2174 (insert shr-table-corner
)
2175 (let ((total-width 0))
2176 (dotimes (i (length widths
))
2177 (setq total-width
(+ total-width
(aref widths i
)
2178 (* shr-table-separator-pixel-width
2)))
2179 (insert (make-string (1+ (/ (aref widths i
)
2180 shr-table-separator-pixel-width
))
2181 shr-table-horizontal-line
)
2183 'display
`(space :align-to
(,total-width
))
2184 'shr-table-indent shr-table-id
)
2188 (defun shr-table-widths (table natural-table suggested-widths
)
2189 (let* ((length (length suggested-widths
))
2190 (widths (make-vector length
0))
2191 (natural-widths (make-vector length
0)))
2194 (dolist (column row
)
2195 (aset widths i
(max (aref widths i
) column
))
2197 (dolist (row natural-table
)
2199 (dolist (column row
)
2200 (aset natural-widths i
(max (aref natural-widths i
) column
))
2202 (let ((extra (- (apply '+ (append suggested-widths nil
))
2203 (apply '+ (append widths nil
))
2204 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
2205 (expanded-columns 0))
2206 ;; We have extra, unused space, so divide this space amongst the
2209 ;; If the natural width is wider than the rendered width, we
2210 ;; want to allow the column to expand.
2212 (when (> (aref natural-widths i
) (aref widths i
))
2213 (setq expanded-columns
(1+ expanded-columns
))))
2215 (when (> (aref natural-widths i
) (aref widths i
))
2217 (aref natural-widths i
)
2218 (+ (/ extra expanded-columns
)
2219 (aref widths i
))))))))
2222 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
2223 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
2224 (let ((data (shr-make-table-1 dom widths fill
)))
2225 (push (list (list dom widths fill
) data
)
2227 (when storage-attribute
2228 (dom-set-attribute dom storage-attribute data
))
2231 (defun shr-make-table-1 (dom widths
&optional fill
)
2233 (rowspans (make-vector (length widths
) 0))
2234 (colspan-remaining 0)
2235 colspan-width colspan-count
2237 (dolist (row (dom-non-text-children dom
))
2238 (when (eq (dom-tag row
) 'tr
)
2240 (columns (dom-non-text-children row
))
2244 (while (< i
(length widths
))
2245 ;; If we previously had a rowspan definition, then that
2246 ;; means that we now have a "missing" td/th element here.
2247 ;; So just insert a dummy, empty one to (sort of) emulate
2250 (if (zerop (aref rowspans i
))
2252 (aset rowspans i
(1- (aref rowspans i
)))
2254 (when (and (not (stringp column
))
2255 (or (memq (dom-tag column
) '(td th
))
2257 (when-let* ((span (dom-attr column
'rowspan
)))
2258 (aset rowspans i
(+ (aref rowspans i
)
2259 (1- (string-to-number span
)))))
2260 ;; Sanity check for invalid column-spans.
2261 (when (>= width-column
(length widths
))
2262 (setq width-column
0))
2265 (aref widths width-column
)
2266 (* 10 shr-table-separator-pixel-width
)))
2267 (when (setq colspan
(dom-attr column
'colspan
))
2268 (setq colspan
(min (string-to-number colspan
)
2269 ;; The colspan may be wrong, so
2270 ;; truncate it to the length of the
2271 ;; remaining columns.
2272 (- (length widths
) i
)))
2273 (dotimes (j (1- colspan
))
2275 (if (> (+ i
1 j
) (1- (length widths
)))
2276 ;; If we have a colspan spec that's longer
2277 ;; than the table is wide, just use the last
2278 ;; width as the width.
2279 (aref widths
(1- (length widths
)))
2280 ;; Sum up the widths of the columns we're
2283 shr-table-separator-length
2284 (aref widths
(+ i
1 j
))))))
2285 (setq width-column
(+ width-column
(1- colspan
))
2286 colspan-count colspan
2287 colspan-remaining colspan
))
2289 (let ((data (shr-render-td column width fill
)))
2291 (> colspan-remaining
0))
2293 (setq colspan-width
(car data
))
2294 (let ((this-width (/ colspan-width colspan-count
)))
2295 (push (cons this-width
(cadr data
)) tds
)
2296 (setq colspan-remaining
(1- colspan-remaining
))))
2298 (push (cons (car data
) (cadr data
)) tds
)
2302 (dotimes (_ (1- colspan
))
2306 (list 0 0 -
1 nil
1 nil nil
)
2310 width-column
(1+ width-column
))))
2311 (push (nreverse tds
) trs
))))
2314 (defun shr-pixel-buffer-width ()
2315 (if (not shr-use-fonts
)
2317 (goto-char (point-min))
2321 (setq max
(max max
(current-column)))
2324 (if (get-buffer-window)
2325 (car (window-text-pixel-size nil
(point-min) (point-max)))
2326 (save-window-excursion
2327 ;; Avoid errors if the selected window is a dedicated one,
2328 ;; and they just want to insert a document into it.
2329 (set-window-dedicated-p nil nil
)
2330 (set-window-buffer nil
(current-buffer))
2331 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
2333 (defun shr-render-td (dom width fill
)
2334 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
2335 (or (dom-attr dom cache
)
2338 (dolist (attr (dom-attributes dom
))
2339 (let ((name (symbol-name (car attr
))))
2340 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
2341 (let ((cache-width (string-to-number
2342 (match-string 1 name
))))
2343 (when (and (>= cache-width width
)
2344 (<= (car (cdr attr
)) width
))
2345 (setq result
(cdr attr
)))))))
2348 (result (shr-render-td-1 dom width fill
)))
2349 (dom-set-attribute dom cache result
)
2353 (defun shr-render-td-1 (dom width fill
)
2355 (let ((bgcolor (dom-attr dom
'bgcolor
))
2356 (fgcolor (dom-attr dom
'fgcolor
))
2357 (style (dom-attr dom
'style
))
2358 (shr-stylesheet shr-stylesheet
)
2362 (setq style
(and (string-match "color" style
)
2363 (shr-parse-style style
))))
2365 (setq style
(nconc (list (cons 'background-color bgcolor
))
2368 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
2370 (setq shr-stylesheet
(append style shr-stylesheet
)))
2371 (let ((shr-internal-width width
)
2372 (shr-indentation 0))
2374 (save-window-excursion
2375 ;; Avoid errors if the selected window is a dedicated one,
2376 ;; and they just want to insert a document into it.
2377 (set-window-dedicated-p nil nil
)
2378 (set-window-buffer nil
(current-buffer))
2381 (or (dom-attr dom
'shr-td-cache-natural
)
2382 (let ((natural (max (shr-pixel-buffer-width)
2383 (shr-dom-max-natural-width dom
0))))
2384 (dom-set-attribute dom
'shr-td-cache-natural natural
)
2386 (if (and natural-width
2387 (<= natural-width width
))
2388 (setq max-width natural-width
)
2389 (let ((shr-internal-width width
))
2390 (shr-fill-lines (point-min) (point-max))
2391 (setq max-width
(shr-pixel-buffer-width)))))
2392 (goto-char (point-max))
2393 ;; Delete padding at the bottom of the TDs.
2397 (skip-chars-backward " \t\n")
2400 (goto-char (point-min))
2403 (count-lines (point-min) (point-max))
2404 (split-string (buffer-string) "\n")
2405 (if (dom-attr dom
'colspan
)
2406 (string-to-number (dom-attr dom
'colspan
))
2408 (cdr (assq 'color shr-stylesheet
))
2409 (cdr (assq 'background-color shr-stylesheet
))))))
2411 (defun shr-dom-max-natural-width (dom max
)
2412 (if (eq (dom-tag dom
) 'table
)
2415 for line in
(dom-attr dom
'shr-suggested-widths
)
2417 shr-table-separator-length
2418 (cl-loop for elem in line
2421 (* 2 shr-table-separator-length
)))))
2423 (dolist (child (dom-children dom
))
2424 (unless (stringp child
)
2425 (setq max
(max (shr-dom-max-natural-width child max
)))))
2428 (defun shr-buffer-width ()
2429 (goto-char (point-min))
2433 (setq max
(max max
(current-column)))
2437 (defun shr-pro-rate-columns (columns)
2438 (let ((total-percentage 0)
2439 (widths (make-vector (length columns
) 0)))
2440 (dotimes (i (length columns
))
2441 (setq total-percentage
(+ total-percentage
(aref columns i
))))
2442 (setq total-percentage
(/ 1.0 total-percentage
))
2443 (dotimes (i (length columns
))
2444 (aset widths i
(max (truncate (* (aref columns i
)
2446 (- shr-internal-width
2447 (* (1+ (length columns
))
2448 shr-table-separator-pixel-width
))))
2452 ;; Return a summary of the number and shape of the TDs in the table.
2453 (defun shr-column-specs (dom)
2454 (let ((columns (make-vector (shr-max-columns dom
) 1)))
2455 (dolist (row (dom-non-text-children dom
))
2456 (when (eq (dom-tag row
) 'tr
)
2458 (dolist (column (dom-non-text-children row
))
2459 (when (memq (dom-tag column
) '(td th
))
2460 (let ((width (dom-attr column
'width
)))
2462 (string-match "\\([0-9]+\\)%" width
)
2463 (not (zerop (setq width
(string-to-number
2464 (match-string 1 width
))))))
2465 (aset columns i
(/ width
100.0))))
2466 (setq i
(1+ i
)))))))
2469 (defun shr-count (dom elem
)
2471 (dolist (sub (dom-children dom
))
2472 (when (and (not (stringp sub
))
2473 (eq (dom-tag sub
) elem
))
2477 (defun shr-max-columns (dom)
2479 (dolist (row (dom-children dom
))
2480 (when (and (not (stringp row
))
2481 (eq (dom-tag row
) 'tr
))
2482 (setq max
(max max
(+ (shr-count row
'td
)
2483 (shr-count row
'th
))))))
2488 ;;; shr.el ends here