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.
33 (eval-when-compile (require 'cl-lib
))
34 (eval-when-compile (require 'url
)) ;For url-filename's setf handler.
36 (eval-when-compile (require 'subr-x
))
43 "Simple HTML Renderer"
47 (defcustom shr-max-image-proportion
0.9
48 "How big pictures displayed are in relation to the window they're in.
49 A value of 0.7 means that they are allowed to take up 70% of the
50 width and height of the window. If they are larger than this,
51 and Emacs supports it, then the images will be rescaled down to
57 (defcustom shr-blocked-images nil
58 "Images that have URLs matching this regexp will be blocked."
61 :type
'(choice (const nil
) regexp
))
63 (defcustom shr-use-fonts t
64 "If non-nil, use proportional fonts for text."
69 (defcustom shr-use-colors t
70 "If non-nil, respect color specifications in the HTML."
75 (defcustom shr-table-horizontal-line nil
76 "Character used to draw horizontal table lines.
77 If nil, don't draw horizontal table lines."
79 :type
'(choice (const nil
) character
))
81 (defcustom shr-table-vertical-line ?\s
82 "Character used to draw vertical table lines."
86 (defcustom shr-table-corner ?\s
87 "Character used to draw table corners."
91 (defcustom shr-hr-line ?-
92 "Character used to draw hr lines."
96 (defcustom shr-width nil
97 "Frame width to use for rendering.
98 May either be an integer specifying a fixed width in characters,
99 or nil, meaning that the full width of the window should be used.
100 If `shr-use-fonts' is set, the mean character width is used to
101 compute the pixel width, which is used instead."
103 :type
'(choice (integer :tag
"Fixed width in characters")
104 (const :tag
"Use the width of the window" nil
))
107 (defcustom shr-bullet
"* "
108 "Bullet used for unordered lists.
109 Alternative suggestions are:
116 (defcustom shr-external-browser
'browse-url-default-browser
117 "Function used to launch an external browser."
122 (defcustom shr-image-animate t
123 "Non nil means that images that can be animated will be."
128 (defvar shr-content-function nil
129 "If bound, this should be a function that will return the content.
130 This is used for cid: URLs, and the function is called with the
131 cid: URL as the argument.")
133 (defvar shr-put-image-function
'shr-put-image
134 "Function called to put image and alt string.")
136 (defface shr-strike-through
'((t (:strike-through t
)))
137 "Font for <s> elements."
141 '((t (:inherit link
)))
142 "Font for link elements."
145 (defvar shr-inhibit-images nil
146 "If non-nil, inhibit loading images.")
148 (defvar shr-external-rendering-functions nil
149 "Alist of tag/function pairs used to alter how shr renders certain tags.
150 For instance, eww uses this to alter rendering of title, forms
152 \((title . eww-tag-title)
153 (form . eww-tag-form)
156 ;;; Internal variables.
158 (defvar shr-folding-mode nil
)
159 (defvar shr-start nil
)
160 (defvar shr-indentation
0)
161 (defvar shr-internal-width nil
)
162 (defvar shr-list-mode nil
)
163 (defvar shr-content-cache nil
)
164 (defvar shr-kinsoku-shorten nil
)
165 (defvar shr-table-depth
0)
166 (defvar shr-stylesheet nil
)
167 (defvar shr-base nil
)
169 (defvar shr-warning nil
)
170 (defvar shr-ignore-cache nil
)
171 (defvar shr-target-id nil
)
172 (defvar shr-table-separator-length
1)
173 (defvar shr-table-separator-pixel-width
0)
174 (defvar shr-table-id nil
)
175 (defvar shr-current-font nil
)
176 (defvar shr-internal-bullet nil
)
179 (let ((map (make-sparse-keymap)))
180 (define-key map
"a" 'shr-show-alt-text
)
181 (define-key map
"i" 'shr-browse-image
)
182 (define-key map
"z" 'shr-zoom-image
)
183 (define-key map
[?
\t] 'shr-next-link
)
184 (define-key map
[?\M-
\t] 'shr-previous-link
)
185 (define-key map
[follow-link
] 'mouse-face
)
186 (define-key map
[mouse-2
] 'shr-browse-url
)
187 (define-key map
"I" 'shr-insert-image
)
188 (define-key map
"w" 'shr-maybe-probe-and-copy-url
)
189 (define-key map
"u" 'shr-maybe-probe-and-copy-url
)
190 (define-key map
"v" 'shr-browse-url
)
191 (define-key map
"O" 'shr-save-contents
)
192 (define-key map
"\r" 'shr-browse-url
)
195 (defvar shr-image-map
196 (let ((map (copy-keymap shr-map
)))
197 (when (boundp 'image-map
)
198 (set-keymap-parent map image-map
))
201 ;; Public functions and commands.
202 (declare-function libxml-parse-html-region
"xml.c"
203 (start end
&optional base-url discard-comments
))
205 (defun shr-render-buffer (buffer)
206 "Display the HTML rendering of the current buffer."
207 (interactive (list (current-buffer)))
208 (or (fboundp 'libxml-parse-html-region
)
209 (error "This function requires Emacs to be compiled with libxml2"))
210 (pop-to-buffer "*html*")
213 (with-current-buffer buffer
214 (libxml-parse-html-region (point-min) (point-max))))
215 (goto-char (point-min)))
218 (defun shr-render-region (begin end
&optional buffer
)
219 "Display the HTML rendering of the region between BEGIN and END."
221 (unless (fboundp 'libxml-parse-html-region
)
222 (error "This function requires Emacs to be compiled with libxml2"))
223 (with-current-buffer (or buffer
(current-buffer))
224 (let ((dom (libxml-parse-html-region begin end
)))
225 (delete-region begin end
)
227 (shr-insert-document dom
))))
229 (defun shr--have-one-fringe-p ()
230 "Return non-nil if we know at least one of the fringes has non-zero width."
231 (and (fboundp 'fringe-columns
)
232 (or (not (zerop (fringe-columns 'right
)))
233 (not (zerop (fringe-columns 'left
))))))
236 (defun shr-insert-document (dom)
237 "Render the parsed document DOM into the current buffer.
238 DOM should be a parse tree as generated by
239 `libxml-parse-html-region' or similar."
240 (setq shr-content-cache nil
)
241 (let ((start (point))
247 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
248 (shr-internal-bullet (cons shr-bullet
249 (shr-string-pixel-width shr-bullet
)))
250 (shr-internal-width (or (and shr-width
251 (if (not shr-use-fonts
)
253 (* shr-width
(frame-char-width))))
254 ;; We need to adjust the available
255 ;; width for when the user disables
256 ;; the fringes, which will cause the
257 ;; display engine usurp one column for
258 ;; the continuation glyph.
259 (if (not shr-use-fonts
)
260 (- (window-body-width) 1
261 (if (and (null shr-width
)
262 (not (shr--have-one-fringe-p)))
265 (- (window-body-width nil t
)
266 (* 2 (frame-char-width))
267 (if (and (null shr-width
)
268 (not (shr--have-one-fringe-p)))
269 (* (frame-char-width) 2)
271 bidi-display-reordering
)
272 ;; If the window was hscrolled for some reason, shr-fill-lines
273 ;; below will misbehave, because it silently assumes that it
274 ;; starts with a non-hscrolled window (vertical-motion will move
275 ;; to a wrong place otherwise).
276 (set-window-hscroll nil
0)
278 (shr-fill-lines start
(point))
279 (shr--remove-blank-lines-at-the-end start
(point))
281 (message "%s" shr-warning
))))
283 (defun shr--remove-blank-lines-at-the-end (start end
)
286 (narrow-to-region start end
)
288 (when (and (re-search-backward "[^ \n]" nil t
)
291 (delete-region (point) (point-max))))))
293 (defun shr-url-at-point (image-url)
294 "Return the URL under point as a string.
295 If IMAGE-URL is non-nil, or there is no link under point, but
296 there is an image under point then copy the URL of the image
297 under point instead."
299 (get-text-property (point) 'image-url
)
300 (or (get-text-property (point) 'shr-url
)
301 (get-text-property (point) 'image-url
))))
303 (defun shr-copy-url (url)
304 "Copy the URL under point to the kill ring.
305 If IMAGE-URL (the prefix) is non-nil, or there is no link under
306 point, but there is an image under point then copy the URL of the
307 image under point instead."
308 (interactive (list (shr-url-at-point current-prefix-arg
)))
310 (message "No URL under point")
311 (setq url
(url-encode-url url
))
313 (message "Copied %s" url
)))
315 (defun shr-probe-url (url cont
)
316 "Pass URL's redirect destination to CONT, if it has one.
317 CONT should be a function of one argument, the redirect
318 destination URL. If URL is not redirected, then CONT is never
324 (`(:redirect
,destination .
,_
)
325 ;; Remove common tracking junk from the URL.
326 (funcall cont
(replace-regexp-in-string
327 ".utm_.*" "" destination
)))))
330 (defun shr-probe-and-copy-url (url)
331 "Copy the URL under point to the kill ring.
332 Like `shr-copy-url', but additionally fetch URL and use its
333 redirection destination if it has one."
334 (interactive (list (shr-url-at-point current-prefix-arg
)))
335 (if url
(shr-probe-url url
#'shr-copy-url
)
338 (defun shr-maybe-probe-and-copy-url (url)
339 "Copy the URL under point to the kill ring.
340 If the URL is already at the front of the kill ring act like
341 `shr-probe-and-copy-url', otherwise like `shr-copy-url'."
342 (interactive (list (shr-url-at-point current-prefix-arg
)))
343 (if (equal url
(car kill-ring
))
344 (shr-probe-and-copy-url url
)
347 (defun shr-next-link ()
348 "Skip to the next link."
350 (let ((current (get-text-property (point) 'shr-url
))
353 (while (and (not (eobp))
354 (equal (get-text-property (point) 'shr-url
) current
))
358 (get-text-property (point) 'shr-url
))
359 ;; The next link is adjacent.
360 (message "%s" (get-text-property (point) 'help-echo
)))
362 (not (setq skip
(text-property-not-all (point) (point-max)
365 (message "No next link"))
368 (message "%s" (get-text-property (point) 'help-echo
))))))
370 (defun shr-previous-link ()
371 "Skip to the previous link."
373 (let ((start (point))
375 ;; Skip past the current link.
376 (while (and (not (bobp))
377 (get-text-property (point) 'help-echo
))
379 ;; Find the previous link.
380 (while (and (not (bobp))
381 (not (setq found
(get-text-property (point) 'help-echo
))))
385 (message "No previous link")
387 ;; Put point at the start of the link.
388 (while (and (not (bobp))
389 (get-text-property (point) 'help-echo
))
392 (message "%s" (get-text-property (point) 'help-echo
)))))
394 (defun shr-show-alt-text ()
395 "Show the ALT text of the image under point."
397 (let ((text (get-text-property (point) 'shr-alt
)))
399 (message "No image under point")
400 (message "%s" (shr-fill-text text
)))))
402 (defun shr-browse-image (&optional copy-url
)
403 "Browse the image under point.
404 If COPY-URL (the prefix if called interactively) is non-nil, copy
405 the URL of the image to the kill buffer instead."
407 (let ((url (get-text-property (point) 'image-url
)))
410 (message "No image under point"))
414 (copy-region-as-kill (point-min) (point-max))
415 (message "Copied %s" url
)))
417 (message "Browsing %s..." url
)
420 (defun shr-insert-image ()
421 "Insert the image under point into the buffer."
423 (let ((url (get-text-property (point) 'image-url
)))
425 (message "No image under point")
426 (message "Inserting %s..." url
)
427 (url-retrieve url
'shr-image-fetched
428 (list (current-buffer) (1- (point)) (point-marker))
431 (defun shr-zoom-image ()
432 "Toggle the image size.
433 The size will be rotated between the default size, the original
434 size, and full-buffer size."
436 (let ((url (get-text-property (point) 'image-url
))
437 (size (get-text-property (point) 'image-size
))
438 (buffer-read-only nil
))
440 (message "No image under point")
441 ;; Delete the old picture.
442 (while (get-text-property (point) 'image-url
)
445 (let ((start (point)))
446 (while (get-text-property (point) 'image-url
)
449 (put-text-property start
(point) 'display nil
)
450 (when (> (- (point) start
) 2)
451 (delete-region start
(1- (point)))))
452 (message "Inserting %s..." url
)
453 (url-retrieve url
'shr-image-fetched
454 (list (current-buffer) (1- (point)) (point-marker)
456 (cond ((or (eq size
'default
)
465 ;;; Utility functions.
467 (defsubst shr-generic
(dom)
468 (dolist (sub (dom-children dom
))
473 (defun shr-indirect-call (tag-name dom
&rest args
)
474 (let ((function (intern (concat "shr-tag-" (symbol-name tag-name
)) obarray
))
475 ;; Allow other packages to override (or provide) rendering
477 (external (cdr (assq tag-name shr-external-rendering-functions
))))
479 (apply external dom args
))
481 (apply function dom args
))
483 (apply 'shr-generic dom args
)))))
485 (defun shr-descend (dom)
487 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
))
488 ;; Allow other packages to override (or provide) rendering
490 (external (cdr (assq (dom-tag dom
) shr-external-rendering-functions
)))
491 (style (dom-attr dom
'style
))
492 (shr-stylesheet shr-stylesheet
)
493 (shr-depth (1+ shr-depth
))
495 ;; shr uses many frames per nested node.
496 (if (> shr-depth
(/ max-specpdl-size
15))
497 (setq shr-warning
"Too deeply nested to render properly; consider increasing `max-specpdl-size'")
499 (if (string-match "color\\|display\\|border-collapse" style
)
500 (setq shr-stylesheet
(nconc (shr-parse-style style
)
503 ;; If we have a display:none, then just ignore this part of the DOM.
504 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
505 ;; We don't use shr-indirect-call here, since shr-descend is
506 ;; the central bit of shr.el, and should be as fast as
507 ;; possible. Having one more level of indirection with its
508 ;; negative effect on performance is deemed unjustified in
511 (funcall external dom
))
513 (funcall function dom
))
516 (when (and shr-target-id
517 (equal (dom-attr dom
'id
) shr-target-id
))
518 ;; If the element was empty, we don't have anything to put the
519 ;; anchor on. So just insert a dummy character.
520 (when (= start
(point))
522 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
523 ;; If style is set, then this node has set the color.
527 (cdr (assq 'color shr-stylesheet
))
528 (cdr (assq 'background-color shr-stylesheet
))))))))
530 (defun shr-fill-text (text)
531 (if (zerop (length text
))
534 (let ((shr-indentation 0)
536 (shr-internal-width (- (window-body-width nil t
)
537 (* 2 (frame-char-width))
538 ;; Adjust the window width for when
539 ;; the user disables the fringes,
540 ;; which causes the display engine
541 ;; to usurp one column for the
542 ;; continuation glyph.
543 (if (and (null shr-width
)
544 (not (shr--have-one-fringe-p)))
545 (* (frame-char-width) 2)
548 (shr-fill-lines (point-min) (point-max))
551 (define-inline shr-char-breakable-p
(char)
552 "Return non-nil if a line can be broken before and after CHAR."
553 (inline-quote (aref fill-find-break-point-function-table
,char
)))
554 (define-inline shr-char-nospace-p
(char)
555 "Return non-nil if no space is required before and after CHAR."
556 (inline-quote (aref fill-nospace-between-words-table
,char
)))
558 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
559 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
560 ;; parentheses, and so on, that should not be placed in the beginning
561 ;; of a line or the end of a line.
562 (define-inline shr-char-kinsoku-bol-p
(char)
563 "Return non-nil if a line ought not to begin with CHAR."
564 (inline-letevals (char)
565 (inline-quote (and (not (eq ,char ?
'))
566 (aref (char-category-set ,char
) ?
>)))))
567 (define-inline shr-char-kinsoku-eol-p
(char)
568 "Return non-nil if a line ought not to end with CHAR."
569 (inline-quote (aref (char-category-set ,char
) ?
<)))
570 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
571 (load "kinsoku" nil t
))
573 (defun shr-pixel-column ()
574 (if (not shr-use-fonts
)
576 (if (not (get-buffer-window (current-buffer)))
577 (save-window-excursion
578 ;; Avoid errors if the selected window is a dedicated one,
579 ;; and they just want to insert a document into it.
580 (set-window-dedicated-p nil nil
)
581 (set-window-buffer nil
(current-buffer))
582 (car (window-text-pixel-size nil
(line-beginning-position) (point))))
583 (car (window-text-pixel-size nil
(line-beginning-position) (point))))))
585 (defun shr-pixel-region ()
586 (- (shr-pixel-column)
589 (shr-pixel-column))))
591 (defun shr-string-pixel-width (string)
592 (if (not shr-use-fonts
)
594 ;; Save and restore point across with-temp-buffer, since
595 ;; shr-pixel-column uses save-window-excursion, which can reset
604 (defsubst shr--translate-insertion-chars
()
605 ;; Remove soft hyphens.
606 (goto-char (point-min))
607 (while (search-forward "Â" nil t
)
608 (replace-match "" t t
))
609 ;; Translate non-breaking spaces into real spaces.
610 (goto-char (point-min))
611 (while (search-forward "Â " nil t
)
612 (replace-match " " t t
)))
614 (defun shr-insert (text)
615 (when (and (not (bolp))
616 (get-text-property (1- (point)) 'image-url
))
619 ((eq shr-folding-mode
'none
)
620 (let ((start (point)))
623 (narrow-to-region start
(point))
624 (shr--translate-insertion-chars)
625 (goto-char (point-max)))))
627 (let ((font-start (point)))
628 (when (and (string-match "\\`[ \t\n\r]" text
)
630 (not (eq (char-after (1- (point))) ?
)))
632 (let ((start (point))
636 (narrow-to-region start
(point))
638 (when (looking-at "[ \t\n\r]+")
639 (replace-match "" t t
))
640 (while (re-search-forward "[ \t\n\r]+" nil t
)
641 (replace-match " " t t
))
642 (shr--translate-insertion-chars)
643 (goto-char (point-max)))
644 ;; We may have removed everything we inserted if it was just
646 (unless (= font-start
(point))
647 ;; Mark all lines that should possibly be folded afterwards.
649 (shr-mark-fill start
))
651 (put-text-property font-start
(point)
653 (or shr-current-font
'variable-pitch
)))))))))
655 (defun shr-fill-lines (start end
)
656 (if (<= shr-internal-width
0)
659 (narrow-to-region start end
)
661 (when (get-text-property (point) 'shr-indentation
)
663 (while (setq start
(next-single-property-change start
'shr-indentation
))
667 (goto-char (point-max)))))
669 (defun shr-vertical-motion (column)
670 (if (not shr-use-fonts
)
671 (move-to-column column
)
674 (vertical-motion (cons (/ column
(frame-char-width)) 0))
678 (defun shr-fill-line ()
679 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
680 (continuation (get-text-property
681 (point) 'shr-continuation-indentation
))
683 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
684 (let ((face (get-text-property (point) 'face
))
685 (background-start (point)))
688 (put-text-property background-start
(point) 'face
689 `,(shr-face-background face
))))
691 (setq shr-indentation
(or continuation shr-indentation
))
692 (shr-vertical-motion shr-internal-width
)
693 (when (looking-at " $")
694 (delete-region (point) (line-end-position)))
696 ;; We have to do some folding. First find the first
697 ;; previous point suitable for folding.
698 (if (or (not (shr-find-fill-point (line-beginning-position)))
700 ;; We had unbreakable text (for this width), so just go to
701 ;; the first space and carry on.
704 (skip-chars-forward " ")
705 (search-forward " " (line-end-position) 'move
)))
706 ;; Success; continue.
707 (when (= (preceding-char) ?\s
)
709 (let ((props `(face ,(get-text-property (point) 'face
)
710 ;; Don't break the image-displayer property
711 ;; as it will cause `gnus-article-show-images'
712 ;; to show the two or more same images.
714 ,(get-text-property (point) 'image-displayer
)))
718 (add-text-properties gap-start
(point) props
))
720 (shr-vertical-motion shr-internal-width
)
721 (when (looking-at " $")
722 (delete-region (point) (line-end-position))))))
724 (defun shr-find-fill-point (start)
728 (while (not (or (setq failed
(<= (point) start
))
729 (eq (preceding-char) ?
)
730 (eq (following-char) ?
)
731 (shr-char-breakable-p (preceding-char))
732 (shr-char-breakable-p (following-char))
733 (and (shr-char-kinsoku-bol-p (preceding-char))
734 (shr-char-breakable-p (following-char))
735 (not (shr-char-kinsoku-bol-p (following-char))))
736 (shr-char-kinsoku-eol-p (following-char))
740 ;; There's no breakable point, so we give it up.
743 ;; Don't overflow the window edge, even if
744 ;; shr-kinsoku-shorten is nil.
745 (unless (or shr-kinsoku-shorten
(null shr-width
))
746 (while (setq found
(re-search-forward
747 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
748 (line-end-position) 'move
)))
750 (not (match-beginning 1)))
751 (goto-char (match-beginning 0)))))
754 ;; Don't put kinsoku-bol characters at the beginning of a line,
755 ;; or kinsoku-eol characters at the end of a line.
757 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
759 ((or shr-kinsoku-shorten
(null shr-width
))
760 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
761 (or (shr-char-kinsoku-eol-p (preceding-char))
762 (shr-char-kinsoku-bol-p (following-char))))
764 (when (setq failed
(<= (point) start
))
765 ;; There's no breakable point that doesn't violate kinsoku,
766 ;; so we look for the second best position.
772 (shr-char-kinsoku-eol-p (following-char)))))
774 ((shr-char-kinsoku-eol-p (preceding-char))
775 ;; Find backward the point where kinsoku-eol characters begin.
780 (and (> (setq count
(1- count
)) 0)
781 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
782 (or (shr-char-kinsoku-eol-p (preceding-char))
783 (shr-char-kinsoku-bol-p (following-char)))))))
784 (when (setq failed
(<= (point) start
))
785 ;; There's no breakable point that doesn't violate kinsoku,
786 ;; so we go to the second best position.
787 (if (looking-at "\\(\\c<+\\)\\c<")
788 (goto-char (match-end 1))
790 ((shr-char-kinsoku-bol-p (following-char))
791 ;; Find forward the point where kinsoku-bol characters end.
795 (and (>= (setq count
(1- count
)) 0)
796 (shr-char-kinsoku-bol-p (following-char))
797 (shr-char-breakable-p (following-char))))))))
798 (when (eq (following-char) ?
)
802 (defun shr-parse-base (url)
803 ;; Always chop off anchors.
804 (when (string-match "#.*" url
)
805 (setq url
(substring url
0 (match-beginning 0))))
806 ;; NB: <base href="" > URI may itself be relative to the document s URI
807 (setq url
(shr-expand-url url
))
808 (let* ((parsed (url-generic-parse-url url
))
809 (local (url-filename parsed
)))
810 (setf (url-filename parsed
) "")
811 ;; Chop off the bit after the last slash.
812 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
813 (setq local
(match-string 1 local
)))
814 ;; Always make the local bit end with a slash.
815 (when (and (not (zerop (length local
)))
816 (not (eq (aref local
(1- (length local
))) ?
/)))
817 (setq local
(concat local
"/")))
818 (list (url-recreate-url parsed
)
823 (autoload 'url-expand-file-name
"url-expand")
825 ;; FIXME This needs some tests writing.
826 ;; Does it even need to exist, given that url-expand-file-name does?
827 (defun shr-expand-url (url &optional base
)
830 ;; shr-parse-base should never call this with non-nil base!
831 (shr-parse-base base
)
832 ;; Bound by the parser.
834 (when (zerop (length url
))
836 ;; Strip leading whitespace
837 (and url
(string-match "\\`\\s-+" url
)
838 (setq url
(substring url
(match-end 0))))
839 (cond ((zerop (length url
))
842 (string-match "\\`[a-z]*:" url
))
843 ;; Absolute or empty URI
845 ((eq (aref url
0) ?
/)
846 (if (and (> (length url
) 1)
847 (eq (aref url
1) ?
/))
848 ;; //host...; just use the protocol
849 (concat (nth 2 base
) ":" url
)
850 ;; Just use the host name part.
851 (concat (car base
) url
)))
852 ((eq (aref url
0) ?
#)
853 ;; A link to an anchor.
854 (concat (nth 3 base
) url
))
857 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
859 (defun shr-ensure-newline ()
861 (let ((prefix (get-text-property (line-beginning-position)
862 'shr-prefix-length
)))
863 (unless (or (zerop (current-column))
865 (= prefix
(- (point) (line-beginning-position)))))
868 (defun shr-ensure-paragraph ()
870 (let ((prefix (get-text-property (line-beginning-position)
871 'shr-prefix-length
)))
877 ;; We're already at a new paragraph; do nothing.
880 (= prefix
(- (point) (line-beginning-position))))
881 ;; Do nothing; we're at the start of a <li>.
885 ;; If the current line is totally blank, and doesn't even
886 ;; have any face properties set, then delete the blank
888 (and (looking-at " *$")
889 (not (get-text-property (point) 'face
))
890 (not (= (next-single-property-change (point) 'face nil
892 (line-end-position)))))
893 (delete-region (match-beginning 0) (match-end 0)))
894 ;; We have a single blank line.
897 ;; Insert new paragraph.
902 (when (> shr-indentation
0)
904 (if (not shr-use-fonts
)
905 (make-string shr-indentation ?\s
)
908 `(space :width
(,shr-indentation
)))))))
910 (defun shr-fontize-dom (dom &rest types
)
911 (let ((start (point)))
914 (shr-add-font start
(point) type
))))
916 ;; Add face to the region, but avoid putting the font properties on
917 ;; blank text at the start of the line, and the newline at the end, to
919 (defun shr-add-font (start end type
)
922 (while (< (point) end
)
924 (skip-chars-forward " "))
925 (add-face-text-property (point) (min (line-end-position) end
) type t
)
926 (if (< (line-end-position) end
)
930 (defun shr-mouse-browse-url (ev)
931 "Browse the URL under the mouse cursor."
936 (defun shr-browse-url (&optional external mouse-event
)
937 "Browse the URL under point.
938 If EXTERNAL, browse the URL using `shr-external-browser'."
939 (interactive (list current-prefix-arg last-nonmenu-event
))
940 (mouse-set-point mouse-event
)
941 (let ((url (get-text-property (point) 'shr-url
)))
944 (message "No link under point"))
945 ((string-match "^mailto:" url
)
946 (browse-url-mail url
))
949 (funcall shr-external-browser url
)
950 (browse-url url
))))))
952 (defun shr-save-contents (directory)
953 "Save the contents from URL in a file."
954 (interactive "DSave contents of URL to directory: ")
955 (let ((url (get-text-property (point) 'shr-url
)))
957 (message "No link under point")
958 (url-retrieve (shr-encode-url url
)
959 'shr-store-contents
(list url directory
)
962 (defun shr-store-contents (status url directory
)
963 (unless (plist-get status
:error
)
964 (when (or (search-forward "\n\n" nil t
)
965 (search-forward "\r\n\r\n" nil t
))
966 (write-region (point) (point-max)
967 (expand-file-name (file-name-nondirectory url
)
970 (defun shr-image-fetched (status buffer start end
&optional flags
)
971 (let ((image-buffer (current-buffer)))
972 (when (and (buffer-name buffer
)
973 (not (plist-get status
:error
)))
974 (url-store-in-cache image-buffer
)
975 (goto-char (point-min))
976 (when (or (search-forward "\n\n" nil t
)
977 (search-forward "\r\n\r\n" nil t
))
978 (let ((data (shr-parse-image-data)))
979 (with-current-buffer buffer
983 (let ((alt (buffer-substring start end
))
984 (properties (text-properties-at start
))
985 (inhibit-read-only t
))
986 (delete-region start end
)
988 (funcall shr-put-image-function data alt flags
)
990 (let ((type (pop properties
))
991 (value (pop properties
)))
992 (unless (memq type
'(display image-size
))
993 (put-text-property start
(point) type value
)))))))))))
994 (kill-buffer image-buffer
)))
996 (defun shr-image-from-data (data)
997 "Return an image from the data: URI content DATA."
999 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
1001 (let ((param (match-string 4 data
))
1002 (payload (url-unhex-string (match-string 5 data
))))
1004 (string-match "^.*\\(;[ \t]*base64\\)$" param
))
1005 (setq payload
(ignore-errors
1006 (base64-decode-string payload
))))
1009 ;; Behind display-graphic-p test.
1010 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1011 (declare-function image-animate
"image" (image &optional index limit
))
1013 (defun shr-put-image (spec alt
&optional flags
)
1014 "Insert image SPEC with a string ALT. Return image.
1015 SPEC is either an image data blob, or a list where the first
1016 element is the data blob and the second element is the content-type."
1017 (if (display-graphic-p)
1018 (let* ((size (cdr (assq 'size flags
)))
1019 (data (if (consp spec
)
1022 (content-type (and (consp spec
)
1026 ((eq size
'original
)
1027 (create-image data nil t
:ascent
100
1028 :format content-type
))
1029 ((eq content-type
'image
/svg
+xml
)
1030 (create-image data
'svg t
:ascent
100))
1033 (shr-rescale-image data content-type
1034 (plist-get flags
:width
)
1035 (plist-get flags
:height
))))
1038 (shr-rescale-image data content-type
1039 (plist-get flags
:width
)
1040 (plist-get flags
:height
)))))))
1042 ;; When inserting big-ish pictures, put them at the
1043 ;; beginning of the line.
1044 (when (and (> (current-column) 0)
1045 (> (car (image-size image t
)) 400))
1047 (if (eq size
'original
)
1048 (insert-sliced-image image
(or alt
"*") nil
20 1)
1049 (insert-image image
(or alt
"*")))
1050 (put-text-property start
(point) 'image-size size
)
1051 (when (and shr-image-animate
1052 (cond ((fboundp 'image-multi-frame-p
)
1053 ;; Only animate multi-frame things that specify a
1054 ;; delay; eg animated gifs as opposed to
1055 ;; multi-page tiffs. FIXME?
1056 (cdr (image-multi-frame-p image
)))
1057 ((fboundp 'image-animated-p
)
1058 (image-animated-p image
))))
1059 (image-animate image nil
60)))
1061 (insert (or alt
""))))
1063 (defun shr-rescale-image (data content-type width height
1064 &optional max-width max-height
)
1065 "Rescale DATA, if too big, to fit the current buffer.
1066 WIDTH and HEIGHT are the sizes given in the HTML data, if any.
1068 The size of the displayed image will not exceed
1069 MAX-WIDTH/MAX-HEIGHT. If not given, use the current window
1070 width/height instead."
1071 (if (or (not (fboundp 'imagemagick-types
))
1072 (not (get-buffer-window (current-buffer))))
1073 (create-image data nil t
:ascent
100)
1074 (let* ((edges (window-inside-pixel-edges
1075 (get-buffer-window (current-buffer))))
1076 (max-width (truncate (* shr-max-image-proportion
1078 (- (nth 2 edges
) (nth 0 edges
))))))
1079 (max-height (truncate (* shr-max-image-proportion
1081 (- (nth 3 edges
) (nth 1 edges
))))))
1082 (scaling (image-compute-scaling-factor image-scaling-factor
)))
1083 (when (or (and width
1084 (> width max-width
))
1086 (> height max-height
)))
1089 (if (and width height
1090 (< (* width scaling
) max-width
)
1091 (< (* height scaling
) max-height
))
1097 :format content-type
)
1101 :max-width max-width
1102 :max-height max-height
1103 :format content-type
)))))
1105 ;; url-cache-extract autoloads url-cache.
1106 (declare-function url-cache-create-filename
"url-cache" (url))
1107 (autoload 'mm-disable-multibyte
"mm-util")
1108 (autoload 'browse-url-mail
"browse-url")
1110 (defun shr-get-image-data (url)
1111 "Get image data for URL.
1112 Return a string with image data."
1114 (mm-disable-multibyte)
1115 (when (ignore-errors
1116 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
1118 (when (re-search-forward "\r?\n\r?\n" nil t
)
1119 (shr-parse-image-data)))))
1121 (declare-function libxml-parse-xml-region
"xml.c"
1122 (start end
&optional base-url discard-comments
))
1124 (defun shr-parse-image-data ()
1125 (let ((data (buffer-substring (point) (point-max)))
1129 (narrow-to-region (point-min) (point))
1130 (let ((content-type (mail-fetch-field "content-type")))
1132 ;; Remove any comments in the type string.
1133 (intern (replace-regexp-in-string ";.*" "" content-type
)
1135 ;; SVG images may contain references to further images that we may
1136 ;; want to block. So special-case these by parsing the XML data
1137 ;; and remove anything that looks like a blocked bit.
1138 (when (and shr-blocked-images
1139 (eq content-type
'image
/svg
+xml
))
1141 ;; Note that libxml2 doesn't parse everything perfectly,
1142 ;; so glitches may occur during this transformation.
1144 (libxml-parse-xml-region (point) (point-max)))))
1145 (list data content-type
)))
1147 (defun shr-image-displayer (content-function)
1148 "Return a function to display an image.
1149 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1150 is an argument. The function to be returned takes three arguments URL,
1151 START, and END. Note that START and END should be markers."
1152 `(lambda (url start end
)
1154 (if (string-match "\\`cid:" url
)
1155 ,(when content-function
1156 `(let ((image (funcall ,content-function
1157 (substring url
(match-end 0)))))
1160 (funcall shr-put-image-function
1161 image
(buffer-substring start end
))
1162 (delete-region (point) end
))))
1163 (url-retrieve url
'shr-image-fetched
1164 (list (current-buffer) start end
)
1167 (defun shr-heading (dom &rest types
)
1168 (shr-ensure-paragraph)
1169 (apply #'shr-fontize-dom dom types
)
1170 (shr-ensure-paragraph))
1172 (defun shr-urlify (start url
&optional title
)
1173 (shr-add-font start
(point) 'shr-link
)
1174 (add-text-properties
1177 'help-echo
(let ((iri (or (ignore-errors
1178 (decode-coding-string
1179 (url-unhex-string url
)
1182 (if title
(format "%s (%s)" iri title
) iri
))
1184 'mouse-face
'highlight
))
1185 ;; Don't overwrite any keymaps that are already in the buffer (i.e.,
1189 (let ((next (next-single-property-change start
'keymap nil
(point))))
1190 (if (get-text-property start
'keymap
)
1192 (put-text-property start
(or next
(point)) 'keymap shr-map
)))))
1194 (defun shr-encode-url (url)
1196 (browse-url-url-encode-chars url
"[)$ ]"))
1198 (autoload 'shr-color-visible
"shr-color")
1199 (autoload 'shr-color-
>hexadecimal
"shr-color")
1201 (defun shr-color-check (fg bg
)
1202 "Check that FG is visible on BG.
1203 Returns (fg bg) with corrected values.
1204 Returns nil if the colors that would be used are the default
1205 ones, in case fg and bg are nil."
1207 (let ((fixed (cond ((null fg
) 'fg
)
1209 ;; Convert colors to hexadecimal, or set them to default.
1210 (let ((fg (or (shr-color->hexadecimal fg
)
1211 (frame-parameter nil
'foreground-color
)))
1212 (bg (or (shr-color->hexadecimal bg
)
1213 (frame-parameter nil
'background-color
))))
1214 (cond ((eq fixed
'bg
)
1215 ;; Only return the new fg
1216 (list nil
(cadr (shr-color-visible bg fg t
))))
1218 ;; Invert args and results and return only the new bg
1219 (list (cadr (shr-color-visible fg bg t
)) nil
))
1221 (shr-color-visible bg fg
)))))))
1223 (defun shr-colorize-region (start end fg
&optional bg
)
1224 (when (and shr-use-colors
1226 (>= (display-color-cells) 88))
1227 (let ((new-colors (shr-color-check fg bg
)))
1230 (add-face-text-property start end
1231 (list :foreground
(cadr new-colors
))
1234 (add-face-text-property start end
1235 (list :background
(car new-colors
))
1239 ;;; Tag-specific rendering rules.
1241 (defun shr-tag-html (dom)
1242 (let ((dir (dom-attr dom
'dir
)))
1245 (setq bidi-paragraph-direction
'left-to-right
))
1247 (setq bidi-paragraph-direction
'right-to-left
))
1249 (setq bidi-paragraph-direction nil
))))
1252 (defun shr-tag-body (dom)
1253 (let* ((start (point))
1254 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1255 (bgcolor (dom-attr dom
'bgcolor
))
1256 (shr-stylesheet (list (cons 'color fgcolor
)
1257 (cons 'background-color bgcolor
))))
1259 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1261 (defun shr-tag-style (_dom)
1264 (defun shr-tag-script (_dom)
1267 (defun shr-tag-comment (_dom)
1270 (defun shr-dom-to-xml (dom)
1275 (defun shr-dom-print (dom)
1276 "Convert DOM into a string containing the xml representation."
1277 (insert (format "<%s" (dom-tag dom
)))
1278 (dolist (attr (dom-attributes dom
))
1279 ;; Ignore attributes that start with a colon because they are
1280 ;; private elements.
1281 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1282 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1285 (dolist (elem (dom-children dom
))
1289 ((eq (dom-tag elem
) 'comment
)
1291 ((or (not (eq (dom-tag elem
) 'image
))
1292 ;; Filter out blocked elements inside the SVG image.
1293 (not (setq url
(dom-attr elem
':xlink
:href
)))
1294 (not shr-blocked-images
)
1295 (not (string-match shr-blocked-images url
)))
1297 (shr-dom-print elem
)))))
1298 (insert (format "</%s>" (dom-tag dom
))))
1300 (defun shr-tag-svg (dom)
1301 (when (and (image-type-available-p 'svg
)
1302 (not shr-inhibit-images
)
1303 (dom-attr dom
'width
)
1304 (dom-attr dom
'height
))
1305 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1308 (defun shr-tag-sup (dom)
1309 (let ((start (point)))
1311 (put-text-property start
(point) 'display
'(raise 0.5))))
1313 (defun shr-tag-sub (dom)
1314 (let ((start (point)))
1316 (put-text-property start
(point) 'display
'(raise -
0.5))))
1318 (defun shr-tag-label (dom)
1320 (shr-ensure-paragraph))
1322 (defun shr-tag-p (dom)
1323 (shr-ensure-paragraph)
1325 (shr-ensure-paragraph))
1327 (defun shr-tag-div (dom)
1328 (shr-ensure-newline)
1330 (shr-ensure-newline))
1332 (defun shr-tag-s (dom)
1333 (shr-fontize-dom dom
'shr-strike-through
))
1335 (defun shr-tag-b (dom)
1336 (shr-fontize-dom dom
'bold
))
1338 (defun shr-tag-i (dom)
1339 (shr-fontize-dom dom
'italic
))
1341 (defun shr-tag-em (dom)
1342 (shr-fontize-dom dom
'italic
))
1344 (defun shr-tag-strong (dom)
1345 (shr-fontize-dom dom
'bold
))
1347 (defun shr-tag-u (dom)
1348 (shr-fontize-dom dom
'underline
))
1350 (defun shr-tag-tt (dom)
1351 (let ((shr-current-font 'default
))
1354 (defun shr-tag-ins (cont)
1355 (let* ((start (point))
1357 (shr-stylesheet (nconc (list (cons 'color color
))
1360 (shr-colorize-region start
(point) color
1361 (cdr (assq 'background-color shr-stylesheet
)))))
1363 (defun shr-tag-del (cont)
1364 (let* ((start (point))
1366 (shr-stylesheet (nconc (list (cons 'color color
))
1368 (shr-fontize-dom cont
'shr-strike-through
)
1369 (shr-colorize-region start
(point) color
1370 (cdr (assq 'background-color shr-stylesheet
)))))
1372 (defun shr-parse-style (style)
1375 (when (string-match "\n" style
)
1376 (setq style
(replace-match " " t t style
))))
1378 (dolist (elem (split-string style
";"))
1380 (setq elem
(split-string elem
":"))
1381 (when (and (car elem
)
1383 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1384 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1385 (when (string-match " *!important\\'" value
)
1386 (setq value
(substring value
0 (match-beginning 0))))
1387 (unless (equal value
"inherit")
1388 (push (cons (intern name obarray
)
1393 (defun shr-tag-base (dom)
1394 (when-let* ((base (dom-attr dom
'href
)))
1395 (setq shr-base
(shr-parse-base base
)))
1398 (defun shr-tag-a (dom)
1399 (let ((url (dom-attr dom
'href
))
1400 (title (dom-attr dom
'title
))
1404 (when (and shr-target-id
1405 (equal (dom-attr dom
'name
) shr-target-id
))
1406 ;; We have a zero-length <a name="foo"> element, so just
1407 ;; insert... something.
1408 (when (= start
(point))
1409 (shr-ensure-newline)
1411 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1413 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1415 (defun shr-tag-object (dom)
1416 (unless shr-inhibit-images
1417 (let ((start (point))
1418 url multimedia image
)
1419 (when-let* ((type (dom-attr dom
'type
)))
1420 (when (string-match "\\`image/svg" type
)
1421 (setq url
(dom-attr dom
'data
)
1423 (dolist (child (dom-non-text-children dom
))
1425 ((eq (dom-tag child
) 'embed
)
1426 (setq url
(or url
(dom-attr child
'src
))
1428 ((and (eq (dom-tag child
) 'param
)
1429 (equal (dom-attr child
'name
) "movie"))
1430 (setq url
(or url
(dom-attr child
'value
))
1435 (shr-indirect-call 'img dom url
)
1438 (shr-insert " [multimedia] ")
1439 (shr-urlify start
(shr-expand-url url
)))))
1441 (shr-generic dom
)))))
1443 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1449 "Preferences for media types.
1450 The key element should be a regexp matched against the type of the source or
1451 url if no type is specified. The value should be a float in the range 0.0 to
1452 1.0. Media elements with higher value are preferred."
1455 :type
'(alist :key-type regexp
:value-type float
))
1457 (defun shr--get-media-pref (elem)
1458 "Determine the preference for ELEM.
1459 The preference is a float determined from `shr-prefer-media-type'."
1460 (let ((type (dom-attr elem
'type
))
1463 (setq type
(dom-attr elem
'src
)))
1465 (dolist (pref shr-prefer-media-type-alist
)
1468 (string-match-p (car pref
) type
))
1469 (setq p
(cdr pref
)))))
1472 (defun shr--extract-best-source (dom &optional url pref
)
1473 "Extract the best `:src' property from <source> blocks in DOM."
1474 (setq pref
(or pref -
1.0))
1476 (dolist (elem (dom-non-text-children dom
))
1477 (when (and (eq (dom-tag elem
) 'source
)
1480 (shr--get-media-pref elem
))))
1482 url
(dom-attr elem
'src
))
1483 ;; libxml's html parser isn't HTML5 compliant and non terminated
1484 ;; source tags might end up as children. So recursion it is...
1485 (dolist (child (dom-non-text-children elem
))
1486 (when (eq (dom-tag child
) 'source
)
1487 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1488 (when (< pref
(cdr ret
))
1490 pref
(cdr ret
)))))))))
1493 (defun shr-tag-video (dom)
1494 (let ((image (dom-attr dom
'poster
))
1495 (url (dom-attr dom
'src
))
1498 (setq url
(car (shr--extract-best-source dom
))))
1499 (if (> (length image
) 0)
1500 (shr-indirect-call 'img nil image
)
1501 (shr-insert " [video] "))
1502 (shr-urlify start
(shr-expand-url url
))))
1504 (defun shr-tag-audio (dom)
1505 (let ((url (dom-attr dom
'src
))
1508 (setq url
(car (shr--extract-best-source dom
))))
1509 (shr-insert " [audio] ")
1510 (shr-urlify start
(shr-expand-url url
))))
1512 (defun shr-tag-img (dom &optional url
)
1515 (or (> (length (dom-attr dom
'src
)) 0)
1516 (> (length (dom-attr dom
'srcset
)) 0))))
1517 (when (> (current-column) 0)
1519 (let ((alt (dom-attr dom
'alt
))
1520 (width (shr-string-number (dom-attr dom
'width
)))
1521 (height (shr-string-number (dom-attr dom
'height
)))
1522 (url (shr-expand-url (or url
(shr--preferred-image dom
)))))
1523 (let ((start (point-marker)))
1524 (when (zerop (length alt
))
1527 ((or (member (dom-attr dom
'height
) '("0" "1"))
1528 (member (dom-attr dom
'width
) '("0" "1")))
1529 ;; Ignore zero-sized or single-pixel images.
1531 ((and (not shr-inhibit-images
)
1532 (string-match "\\`data:" url
))
1533 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1535 (funcall shr-put-image-function image alt
1536 (list :width width
:height height
))
1538 ((and (not shr-inhibit-images
)
1539 (string-match "\\`cid:" url
))
1540 (let ((url (substring url
(match-end 0)))
1542 (if (or (not shr-content-function
)
1543 (not (setq image
(funcall shr-content-function url
))))
1545 (funcall shr-put-image-function image alt
1546 (list :width width
:height height
)))))
1547 ((or shr-inhibit-images
1548 (and shr-blocked-images
1549 (string-match shr-blocked-images url
)))
1550 (setq shr-start
(point))
1552 ((and (not shr-ignore-cache
)
1553 (url-is-cached (shr-encode-url url
)))
1554 (funcall shr-put-image-function
(shr-get-image-data url
) alt
1555 (list :width width
:height height
)))
1557 (when (and shr-ignore-cache
1558 (url-is-cached (shr-encode-url url
)))
1559 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1560 (when (file-exists-p file
)
1561 (delete-file file
))))
1562 (when (image-type-available-p 'svg
)
1564 (shr-make-placeholder-image dom
)
1568 (shr-encode-url url
) 'shr-image-fetched
1569 (list (current-buffer) start
(set-marker (make-marker) (point))
1570 (list :width width
:height height
))
1572 (when (zerop shr-table-depth
) ;; We are not in a table.
1573 (put-text-property start
(point) 'keymap shr-image-map
)
1574 (put-text-property start
(point) 'shr-alt alt
)
1575 (put-text-property start
(point) 'image-url url
)
1576 (put-text-property start
(point) 'image-displayer
1577 (shr-image-displayer shr-content-function
))
1578 (put-text-property start
(point) 'help-echo
1580 (or (dom-attr dom
'title
) alt
))))))))
1582 (defun shr--preferred-image (dom)
1583 (let ((srcset (dom-attr dom
'srcset
))
1584 (frame-width (frame-pixel-width))
1585 (width (string-to-number (or (dom-attr dom
'width
) "100")))
1587 (when (> (length srcset
) 0)
1588 ;; srcset consist of a series of URL/size specifications
1589 ;; separated by the ", " string.
1593 (let ((spec (split-string elem
"[\t\n\r ]+")))
1595 ((= (length spec
) 1)
1596 ;; Make sure it's well formed.
1597 (list (car spec
) 0))
1598 ((string-match "\\([0-9]+\\)x\\'" (cadr spec
))
1599 ;; If we have an "x" form, then use the width
1600 ;; spec to compute the real width.
1602 (* width
(string-to-number
1603 (match-string 1 (cadr spec
))))))
1606 (string-to-number (cadr spec
)))))))
1607 (split-string (replace-regexp-in-string
1608 "\\`[\t\n\r ]+\\|[\t\n\r ]+\\'" "" srcset
)
1609 "[\t\n\r ]*,[\t\n\r ]*"))
1611 (> (cadr e1
) (cadr e2
)))))
1612 ;; Choose the smallest picture that's bigger than the current
1614 (setq candidate
(caar srcset
))
1616 (> (cadr (car srcset
)) frame-width
))
1617 (setq candidate
(caar srcset
))
1619 (or candidate
(dom-attr dom
'src
))))
1621 (defun shr-string-number (string)
1624 (setq string
(replace-regexp-in-string "[^0-9]" "" string
))
1625 (if (zerop (length string
))
1627 (string-to-number string
))))
1629 (defun shr-make-placeholder-image (dom)
1631 (get-buffer-window (current-buffer))
1632 (window-inside-pixel-edges
1633 (get-buffer-window (current-buffer)))))
1634 (scaling (image-compute-scaling-factor image-scaling-factor
))
1636 (* (or (shr-string-number (dom-attr dom
'width
)) 100)
1639 (* (or (shr-string-number (dom-attr dom
'height
)) 100)
1643 (truncate (* shr-max-image-proportion
1644 (- (nth 2 edges
) (nth 0 edges
))))))
1645 (max-height (and edges
1646 (truncate (* shr-max-image-proportion
1647 (- (nth 3 edges
) (nth 1 edges
))))))
1649 (when (and max-width
1650 (> width max-width
))
1651 (setq height
(truncate (* (/ (float max-width
) width
) height
))
1653 (when (and max-height
1654 (> height max-height
))
1655 (setq width
(truncate (* (/ (float max-height
) height
) width
))
1657 (setq svg
(svg-create width height
))
1658 (svg-gradient svg
"background" 'linear
'((0 .
"#b0b0b0") (100 .
"#808080")))
1659 (svg-rectangle svg
0 0 width height
:gradient
"background"
1660 :stroke-width
2 :stroke-color
"black")
1661 (let ((image (svg-image svg
)))
1662 (setf (image-property image
:ascent
) 100)
1665 (defun shr-tag-pre (dom)
1666 (let ((shr-folding-mode 'none
)
1667 (shr-current-font 'default
))
1668 (shr-ensure-newline)
1670 (shr-ensure-newline)))
1672 (defun shr-tag-blockquote (dom)
1673 (shr-ensure-paragraph)
1674 (let ((start (point))
1675 (shr-indentation (+ shr-indentation
1676 (* 4 shr-table-separator-pixel-width
))))
1678 (shr-ensure-paragraph)
1679 (shr-mark-fill start
)))
1681 (defun shr-tag-dl (dom)
1682 (shr-ensure-paragraph)
1684 (shr-ensure-paragraph))
1686 (defun shr-tag-dt (dom)
1687 (shr-ensure-newline)
1689 (shr-ensure-newline))
1691 (defun shr-tag-dd (dom)
1692 (shr-ensure-newline)
1693 (let ((shr-indentation (+ shr-indentation
1694 (* 4 shr-table-separator-pixel-width
))))
1697 (defun shr-tag-ul (dom)
1698 (shr-ensure-paragraph)
1699 (let ((shr-list-mode 'ul
))
1701 ;; If we end on an empty <li>, then make sure we really end on a new
1705 (shr-ensure-paragraph))
1707 (defun shr-tag-ol (dom)
1708 (shr-ensure-paragraph)
1709 (let ((shr-list-mode 1))
1711 (shr-ensure-paragraph))
1713 (defun shr-tag-li (dom)
1714 (shr-ensure-newline)
1715 (let ((start (point)))
1717 (if (numberp shr-list-mode
)
1719 (format "%d " shr-list-mode
)
1720 (setq shr-list-mode
(1+ shr-list-mode
)))
1721 (car shr-internal-bullet
)))
1722 (width (if (numberp shr-list-mode
)
1723 (shr-string-pixel-width bullet
)
1724 (cdr shr-internal-bullet
))))
1726 (shr-mark-fill start
)
1727 (let ((shr-indentation (+ shr-indentation width
)))
1728 (put-text-property start
(1+ start
)
1729 'shr-continuation-indentation shr-indentation
)
1730 (put-text-property start
(1+ start
) 'shr-prefix-length
(length bullet
))
1731 (shr-generic dom
))))
1735 (defun shr-mark-fill (start)
1736 ;; We may not have inserted any text to fill.
1737 (unless (= start
(point))
1738 (put-text-property start
(1+ start
)
1739 'shr-indentation shr-indentation
)))
1741 (defun shr-tag-br (dom)
1742 (when (and (not (bobp))
1743 ;; Only add a newline if we break the current line, or
1744 ;; the previous line isn't a blank line.
1746 (and (> (- (point) 2) (point-min))
1747 (not (= (char-after (- (point) 2)) ?
\n)))))
1751 (defun shr-tag-span (dom)
1754 (defun shr-tag-h1 (dom)
1755 (shr-heading dom
(if shr-use-fonts
1756 '(variable-pitch (:height
1.3 :weight bold
))
1759 (defun shr-tag-h2 (dom)
1760 (shr-heading dom
'bold
))
1762 (defun shr-tag-h3 (dom)
1763 (shr-heading dom
'italic
))
1765 (defun shr-tag-h4 (dom)
1768 (defun shr-tag-h5 (dom)
1771 (defun shr-tag-h6 (dom)
1774 (defun shr-tag-hr (_dom)
1775 (shr-ensure-newline)
1776 (insert (make-string (if (not shr-use-fonts
)
1778 (1+ (/ shr-internal-width
1779 shr-table-separator-pixel-width
)))
1783 (defun shr-tag-title (dom)
1784 (shr-heading dom
'bold
'underline
))
1786 (defun shr-tag-font (dom)
1787 (let* ((start (point))
1788 (color (dom-attr dom
'color
))
1789 (shr-stylesheet (nconc (list (cons 'color color
))
1793 (shr-colorize-region start
(point) color
1794 (cdr (assq 'background-color shr-stylesheet
))))))
1796 (defun shr-tag-bdo (dom)
1797 (let* ((direction (dom-attr dom
'dir
))
1799 ((equal direction
"ltr")
1800 ?\N
{LEFT-TO-RIGHT OVERRIDE
})
1801 ((equal direction
"rtl")
1802 ?\N
{RIGHT-TO-LEFT OVERRIDE
}))))
1804 (insert ?\N
{FIRST STRONG ISOLATE
} char
))
1807 (insert ?\N
{POP DIRECTIONAL FORMATTING
} ?\N
{POP DIRECTIONAL ISOLATE
}))))
1809 (defun shr-tag-bdi (dom)
1810 (insert ?\N
{FIRST STRONG ISOLATE
})
1812 (insert ?\N
{POP DIRECTIONAL ISOLATE
}))
1814 ;;; Table rendering algorithm.
1816 ;; Table rendering is the only complicated thing here. We do this by
1817 ;; first counting how many TDs there are in each TR, and registering
1818 ;; how wide they think they should be ("width=45%", etc). Then we
1819 ;; render each TD separately (this is done in temporary buffers, so
1820 ;; that we can use all the rendering machinery as if we were in the
1821 ;; main buffer). Now we know how much space each TD really takes, so
1822 ;; we then render everything again with the new widths, and finally
1823 ;; insert all these boxes into the main buffer.
1824 (defun shr-tag-table-1 (dom)
1825 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1826 (let* ((shr-inhibit-images t
)
1827 (shr-table-depth (1+ shr-table-depth
))
1828 (shr-kinsoku-shorten t
)
1829 ;; Find all suggested widths.
1830 (columns (shr-column-specs dom
))
1831 ;; Compute how many pixels wide each TD should be.
1832 (suggested-widths (shr-pro-rate-columns columns
))
1833 ;; Do a "test rendering" to see how big each TD is (this can
1834 ;; be smaller (if there's little text) or bigger (if there's
1835 ;; unbreakable text).
1836 (elems (or (dom-attr dom
'shr-suggested-widths
)
1837 (shr-make-table dom suggested-widths nil
1838 'shr-suggested-widths
)))
1839 (sketch (cl-loop for line in elems
1840 collect
(mapcar #'car line
)))
1841 (natural (cl-loop for line in elems
1842 collect
(mapcar #'cdr line
)))
1843 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1844 ;; This probably won't work very well.
1845 (when (> (+ (cl-loop for width across sketch-widths
1847 shr-indentation shr-table-separator-pixel-width
)
1849 (setq truncate-lines t
))
1850 ;; Then render the table again with these new "hard" widths.
1851 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1853 (defun shr-table-body (dom)
1854 (let ((tbodies (seq-filter (lambda (child)
1855 (eq (dom-tag child
) 'tbody
))
1856 (dom-non-text-children dom
))))
1860 ((= (length tbodies
) 1)
1863 ;; Table with multiple tbodies. Convert into a single tbody.
1864 `(tbody nil
,@(cl-reduce 'append
1865 (mapcar 'dom-non-text-children tbodies
)))))))
1867 (defun shr-tag-table (dom)
1868 (shr-ensure-paragraph)
1869 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1870 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1871 (body (dom-non-text-children (shr-table-body dom
)))
1872 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1873 (bgcolor (dom-attr dom
'bgcolor
))
1875 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1877 (nheader (if header
(shr-max-columns header
)))
1878 (nbody (if body
(shr-max-columns body
) 0))
1879 (nfooter (if footer
(shr-max-columns footer
))))
1880 (if (and (not caption
)
1882 (not (dom-child-by-tag dom
'tbody
))
1883 (not (dom-child-by-tag dom
'tr
))
1885 ;; The table is totally invalid and just contains random junk.
1886 ;; Try to output it anyway.
1888 ;; It's a real table, so render it.
1889 (if (dom-attr dom
'shr-fixed-table
)
1890 (shr-tag-table-1 dom
)
1891 ;; Only fix up the table once.
1895 (if caption
`((tr nil
(td nil
,@caption
))))
1899 ;; header + body + footer
1900 (if (= nheader nbody
)
1901 (if (= nbody nfooter
)
1902 `((tr nil
(td nil
(table nil
1904 ,@body
,@footer
)))))
1905 (nconc `((tr nil
(td nil
(table nil
1910 `((tr nil
(td nil
(table
1912 nil
,@footer
))))))))
1913 (nconc `((tr nil
(td nil
(table nil
(tbody
1915 (if (= nbody nfooter
)
1916 `((tr nil
(td nil
(table
1917 nil
(tbody nil
,@body
1919 (nconc `((tr nil
(td nil
(table
1924 `((tr nil
(td nil
(table
1930 (if (= nheader nbody
)
1931 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1934 `(,@header
(tr nil
(td nil
(table
1935 nil
(tbody nil
,@body
)))))
1936 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1937 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1940 (if (= nbody nfooter
)
1941 `((tr nil
(td nil
(table
1942 nil
(tbody nil
,@body
,@footer
)))))
1943 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1946 `((tr nil
(td nil
(table
1947 nil
(tbody nil
,@footer
)))))))))
1949 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1951 (dom-set-attribute table
'shr-fixed-table t
)
1952 (setcdr dom
(cdr table
))
1953 (shr-tag-table-1 dom
))))
1955 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1957 ;; Finally, insert all the images after the table. The Emacs buffer
1958 ;; model isn't strong enough to allow us to put the images actually
1959 ;; into the tables. It inserts also non-td/th objects.
1960 (when (zerop shr-table-depth
)
1962 (shr-expand-alignments start
(point)))
1963 (let ((strings (shr-collect-extra-strings-in-table dom
)))
1966 (narrow-to-region (point) (point))
1967 (insert (mapconcat #'identity strings
"\n"))
1968 (shr-fill-lines (point-min) (point-max))))))))
1970 (defun shr-collect-extra-strings-in-table (dom &optional flags
)
1971 "Return extra strings in DOM of which the root is a table clause.
1972 Render <img>s and <object>s, and strings and child <table>s of which
1973 the parent <td> or <th> is lacking. FLAGS is a cons of two boolean
1974 flags that control whether to collect or render objects."
1975 ;; This function runs recursively and collects strings if the cdr of
1976 ;; FLAGS is nil and the car is not nil, and it renders also child
1977 ;; <table>s if the cdr is nil. Note: FLAGS may be nil, not a cons.
1978 ;; FLAGS becomes (t . nil) if a <tr> clause is found in the children
1979 ;; of DOM, and becomes (t . t) if a <td> or a <th> clause is found
1980 ;; and the car is t then. When a <table> clause is found, FLAGS
1981 ;; becomes nil if the cdr is t then. But if FLAGS is (t . nil) then,
1982 ;; it renders the <table>.
1983 (cl-loop for child in
(dom-children dom
) with recurse with tag
1984 do
(setq recurse nil
)
1986 unless
(or (not (car flags
)) (cdr flags
))
1987 when
(string-match "\\(?:[^\t\n\r ]+[\t\n\r ]+\\)*[^\t\n\r ]+"
1989 collect
(match-string 0 child
)
1991 else if
(consp child
)
1992 do
(setq tag
(dom-tag child
)) and
1993 unless
(memq tag
'(comment style
))
1995 do
(shr-indirect-call 'img child
)
1996 else if
(eq tag
'object
)
1997 do
(shr-indirect-call 'object child
)
1999 do
(setq recurse t
) and
2001 do
(setq flags
'(t . nil
))
2002 else if
(memq tag
'(td th
))
2004 do
(setq flags
'(t . t
))
2006 else if
(eq tag
'table
)
2010 do
(setq recurse nil
)
2011 (shr-indirect-call 'table child
)
2012 end end end end end end end end end end
2014 append
(shr-collect-extra-strings-in-table child flags
)))
2016 (defun shr-insert-table (table widths
)
2017 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
2019 (shr-table-separator-length (if collapse
0 1))
2020 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
2022 (setq shr-table-id
(1+ shr-table-id
))
2024 (shr-insert-table-ruler widths
))
2026 (let ((start (point))
2029 (height (let ((max 0))
2030 (dolist (column row
)
2031 (setq max
(max max
(nth 2 column
))))
2033 (dotimes (_ (max height
1))
2035 (insert shr-table-vertical-line
"\n"))
2036 (dolist (column row
)
2037 (when (> (nth 2 column
) -
1)
2039 ;; Sum up all the widths from the column. (There may be
2040 ;; more than one if this is a "colspan" column.)
2041 (dotimes (_ (nth 4 column
))
2042 ;; The colspan directive may be wrong and there may not be
2043 ;; that number of columns.
2044 (when (<= column-number
(1- (length widths
)))
2045 (setq align
(+ align
2046 (aref widths column-number
)
2047 (* 2 shr-table-separator-pixel-width
))))
2048 (setq column-number
(1+ column-number
)))
2049 (let ((lines (nth 3 column
))
2050 (pixel-align (if (not shr-use-fonts
)
2051 (* align
(frame-char-width))
2053 (dolist (line lines
)
2055 (let ((start (point))
2056 (background (and (> (length line
) 0)
2057 (shr-face-background
2059 (1- (length line
)) 'face line
))))
2062 'display
`(space :align-to
(,pixel-align
))
2063 'shr-table-indent shr-table-id
)))
2065 (setq space
(propertize space
'face background
)))
2066 (insert line space shr-table-vertical-line
)
2067 (shr-colorize-region
2068 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2070 ;; Add blank lines at padding at the bottom of the TD,
2072 (dotimes (_ (- height
(length lines
)))
2074 (let ((start (point)))
2075 (insert (propertize " "
2076 'display
`(space :align-to
(,pixel-align
))
2077 'shr-table-indent shr-table-id
)
2078 shr-table-vertical-line
)
2079 (shr-colorize-region
2080 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2081 (forward-line 1))))))
2083 (shr-insert-table-ruler widths
)))
2084 (unless (= start
(point))
2085 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
2087 (defun shr-face-background (face)
2089 (or (and (plist-get face
:background
)
2090 (list :background
(plist-get face
:background
)))
2091 (let ((background nil
))
2093 (when (and (consp elem
)
2094 (eq (car elem
) :background
)
2096 (setq background
(cadr elem
))))
2098 (list :background background
))))))
2100 (defun shr-expand-alignments (start end
)
2101 (while (< (setq start
(next-single-property-change
2102 start
'shr-table-id nil end
))
2105 (let* ((shr-use-fonts t
)
2106 (id (get-text-property (point) 'shr-table-id
))
2107 (base (shr-pixel-column))
2111 (while (setq elem
(text-property-any
2112 (point) end
'shr-table-indent id
))
2114 (let ((align (get-text-property (point) 'display
)))
2115 (put-text-property (point) (1+ (point)) 'display
2116 `(space :align-to
(,(+ (car (nth 2 align
))
2118 (forward-char 1)))))
2119 (setq start
(1+ start
))))
2121 (defun shr-insert-table-ruler (widths)
2122 (when shr-table-horizontal-line
2124 (> shr-indentation
0))
2126 (insert shr-table-corner
)
2127 (let ((total-width 0))
2128 (dotimes (i (length widths
))
2129 (setq total-width
(+ total-width
(aref widths i
)
2130 (* shr-table-separator-pixel-width
2)))
2131 (insert (make-string (1+ (/ (aref widths i
)
2132 shr-table-separator-pixel-width
))
2133 shr-table-horizontal-line
)
2135 'display
`(space :align-to
(,total-width
))
2136 'shr-table-indent shr-table-id
)
2140 (defun shr-table-widths (table natural-table suggested-widths
)
2141 (let* ((length (length suggested-widths
))
2142 (widths (make-vector length
0))
2143 (natural-widths (make-vector length
0)))
2146 (dolist (column row
)
2147 (aset widths i
(max (aref widths i
) column
))
2149 (dolist (row natural-table
)
2151 (dolist (column row
)
2152 (aset natural-widths i
(max (aref natural-widths i
) column
))
2154 (let ((extra (- (apply '+ (append suggested-widths nil
))
2155 (apply '+ (append widths nil
))
2156 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
2157 (expanded-columns 0))
2158 ;; We have extra, unused space, so divide this space amongst the
2161 ;; If the natural width is wider than the rendered width, we
2162 ;; want to allow the column to expand.
2164 (when (> (aref natural-widths i
) (aref widths i
))
2165 (setq expanded-columns
(1+ expanded-columns
))))
2167 (when (> (aref natural-widths i
) (aref widths i
))
2169 (aref natural-widths i
)
2170 (+ (/ extra expanded-columns
)
2171 (aref widths i
))))))))
2174 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
2175 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
2176 (let ((data (shr-make-table-1 dom widths fill
)))
2177 (push (list (list dom widths fill
) data
)
2179 (when storage-attribute
2180 (dom-set-attribute dom storage-attribute data
))
2183 (defun shr-make-table-1 (dom widths
&optional fill
)
2185 (rowspans (make-vector (length widths
) 0))
2186 (colspan-remaining 0)
2187 colspan-width colspan-count
2189 (dolist (row (dom-non-text-children dom
))
2190 (when (eq (dom-tag row
) 'tr
)
2192 (columns (dom-non-text-children row
))
2196 (while (< i
(length widths
))
2197 ;; If we previously had a rowspan definition, then that
2198 ;; means that we now have a "missing" td/th element here.
2199 ;; So just insert a dummy, empty one to (sort of) emulate
2202 (if (zerop (aref rowspans i
))
2204 (aset rowspans i
(1- (aref rowspans i
)))
2206 (when (and (not (stringp column
))
2207 (or (memq (dom-tag column
) '(td th
))
2209 (when-let* ((span (dom-attr column
'rowspan
)))
2210 (aset rowspans i
(+ (aref rowspans i
)
2211 (1- (string-to-number span
)))))
2212 ;; Sanity check for invalid column-spans.
2213 (when (>= width-column
(length widths
))
2214 (setq width-column
0))
2217 (aref widths width-column
)
2218 (* 10 shr-table-separator-pixel-width
)))
2219 (when (setq colspan
(dom-attr column
'colspan
))
2220 (setq colspan
(min (string-to-number colspan
)
2221 ;; The colspan may be wrong, so
2222 ;; truncate it to the length of the
2223 ;; remaining columns.
2224 (- (length widths
) i
)))
2225 (dotimes (j (1- colspan
))
2227 (if (> (+ i
1 j
) (1- (length widths
)))
2228 ;; If we have a colspan spec that's longer
2229 ;; than the table is wide, just use the last
2230 ;; width as the width.
2231 (aref widths
(1- (length widths
)))
2232 ;; Sum up the widths of the columns we're
2235 shr-table-separator-length
2236 (aref widths
(+ i
1 j
))))))
2237 (setq width-column
(+ width-column
(1- colspan
))
2238 colspan-count colspan
2239 colspan-remaining colspan
))
2241 (let ((data (shr-render-td column width fill
)))
2243 (> colspan-remaining
0))
2245 (setq colspan-width
(car data
))
2246 (let ((this-width (/ colspan-width colspan-count
)))
2247 (push (cons this-width
(cadr data
)) tds
)
2248 (setq colspan-remaining
(1- colspan-remaining
))))
2250 (push (cons (car data
) (cadr data
)) tds
)
2254 (dotimes (_ (1- colspan
))
2258 (list 0 0 -
1 nil
1 nil nil
)
2262 width-column
(1+ width-column
))))
2263 (push (nreverse tds
) trs
))))
2266 (defun shr-pixel-buffer-width ()
2267 (if (not shr-use-fonts
)
2269 (goto-char (point-min))
2273 (setq max
(max max
(current-column)))
2276 (if (get-buffer-window)
2277 (car (window-text-pixel-size nil
(point-min) (point-max)))
2278 (save-window-excursion
2279 ;; Avoid errors if the selected window is a dedicated one,
2280 ;; and they just want to insert a document into it.
2281 (set-window-dedicated-p nil nil
)
2282 (set-window-buffer nil
(current-buffer))
2283 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
2285 (defun shr-render-td (dom width fill
)
2286 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
2287 (or (dom-attr dom cache
)
2290 (dolist (attr (dom-attributes dom
))
2291 (let ((name (symbol-name (car attr
))))
2292 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
2293 (let ((cache-width (string-to-number
2294 (match-string 1 name
))))
2295 (when (and (>= cache-width width
)
2296 (<= (car (cdr attr
)) width
))
2297 (setq result
(cdr attr
)))))))
2300 (result (shr-render-td-1 dom width fill
)))
2301 (dom-set-attribute dom cache result
)
2305 (defun shr-render-td-1 (dom width fill
)
2307 (let ((bgcolor (dom-attr dom
'bgcolor
))
2308 (fgcolor (dom-attr dom
'fgcolor
))
2309 (style (dom-attr dom
'style
))
2310 (shr-stylesheet shr-stylesheet
)
2314 (setq style
(and (string-match "color" style
)
2315 (shr-parse-style style
))))
2317 (setq style
(nconc (list (cons 'background-color bgcolor
))
2320 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
2322 (setq shr-stylesheet
(append style shr-stylesheet
)))
2323 (let ((shr-internal-width width
)
2324 (shr-indentation 0))
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))
2333 (or (dom-attr dom
'shr-td-cache-natural
)
2334 (let ((natural (max (shr-pixel-buffer-width)
2335 (shr-dom-max-natural-width dom
0))))
2336 (dom-set-attribute dom
'shr-td-cache-natural natural
)
2338 (if (and natural-width
2339 (<= natural-width width
))
2340 (setq max-width natural-width
)
2341 (let ((shr-internal-width width
))
2342 (shr-fill-lines (point-min) (point-max))
2343 (setq max-width
(shr-pixel-buffer-width)))))
2344 (goto-char (point-max))
2345 ;; Delete padding at the bottom of the TDs.
2349 (skip-chars-backward " \t\n")
2352 (goto-char (point-min))
2355 (count-lines (point-min) (point-max))
2356 (split-string (buffer-string) "\n")
2357 (if (dom-attr dom
'colspan
)
2358 (string-to-number (dom-attr dom
'colspan
))
2360 (cdr (assq 'color shr-stylesheet
))
2361 (cdr (assq 'background-color shr-stylesheet
))))))
2363 (defun shr-dom-max-natural-width (dom max
)
2364 (if (eq (dom-tag dom
) 'table
)
2367 for line in
(dom-attr dom
'shr-suggested-widths
)
2369 shr-table-separator-length
2370 (cl-loop for elem in line
2373 (* 2 shr-table-separator-length
)))))
2375 (dolist (child (dom-children dom
))
2376 (unless (stringp child
)
2377 (setq max
(max (shr-dom-max-natural-width child max
)))))
2380 (defun shr-buffer-width ()
2381 (goto-char (point-min))
2385 (setq max
(max max
(current-column)))
2389 (defun shr-pro-rate-columns (columns)
2390 (let ((total-percentage 0)
2391 (widths (make-vector (length columns
) 0)))
2392 (dotimes (i (length columns
))
2393 (setq total-percentage
(+ total-percentage
(aref columns i
))))
2394 (setq total-percentage
(/ 1.0 total-percentage
))
2395 (dotimes (i (length columns
))
2396 (aset widths i
(max (truncate (* (aref columns i
)
2398 (- shr-internal-width
2399 (* (1+ (length columns
))
2400 shr-table-separator-pixel-width
))))
2404 ;; Return a summary of the number and shape of the TDs in the table.
2405 (defun shr-column-specs (dom)
2406 (let ((columns (make-vector (shr-max-columns dom
) 1)))
2407 (dolist (row (dom-non-text-children dom
))
2408 (when (eq (dom-tag row
) 'tr
)
2410 (dolist (column (dom-non-text-children row
))
2411 (when (memq (dom-tag column
) '(td th
))
2412 (let ((width (dom-attr column
'width
)))
2414 (string-match "\\([0-9]+\\)%" width
)
2415 (not (zerop (setq width
(string-to-number
2416 (match-string 1 width
))))))
2417 (aset columns i
(/ width
100.0))))
2418 (setq i
(1+ i
)))))))
2421 (defun shr-count (dom elem
)
2423 (dolist (sub (dom-children dom
))
2424 (when (and (not (stringp sub
))
2425 (eq (dom-tag sub
) elem
))
2429 (defun shr-max-columns (dom)
2431 (dolist (row (dom-children dom
))
2432 (when (and (not (stringp row
))
2433 (eq (dom-tag row
) 'tr
))
2434 (setq max
(max max
(+ (shr-count row
'td
)
2435 (shr-count row
'th
))))))
2440 ;;; shr.el ends here