1 ;;; shr.el --- Simple HTML Renderer -*- lexical-binding: t -*-
3 ;; Copyright (C) 2010-2017 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
)
596 (shr-pixel-column))))
598 (defsubst shr--translate-insertion-chars
()
599 ;; Remove soft hyphens.
600 (goto-char (point-min))
601 (while (search-forward "Â" nil t
)
602 (replace-match "" t t
))
603 ;; Translate non-breaking spaces into real spaces.
604 (goto-char (point-min))
605 (while (search-forward "Â " nil t
)
606 (replace-match " " t t
)))
608 (defun shr-insert (text)
609 (when (and (not (bolp))
610 (get-text-property (1- (point)) 'image-url
))
613 ((eq shr-folding-mode
'none
)
614 (let ((start (point)))
617 (narrow-to-region start
(point))
618 (shr--translate-insertion-chars)
619 (goto-char (point-max)))))
621 (let ((font-start (point)))
622 (when (and (string-match "\\`[ \t\n\r]" text
)
624 (not (eq (char-after (1- (point))) ?
)))
626 (let ((start (point))
630 (narrow-to-region start
(point))
632 (when (looking-at "[ \t\n\r]+")
633 (replace-match "" t t
))
634 (while (re-search-forward "[ \t\n\r]+" nil t
)
635 (replace-match " " t t
))
636 (shr--translate-insertion-chars)
637 (goto-char (point-max)))
638 ;; We may have removed everything we inserted if if was just
640 (unless (= font-start
(point))
641 ;; Mark all lines that should possibly be folded afterwards.
643 (shr-mark-fill start
))
645 (put-text-property font-start
(point)
647 (or shr-current-font
'variable-pitch
)))))))))
649 (defun shr-fill-lines (start end
)
650 (if (<= shr-internal-width
0)
653 (narrow-to-region start end
)
655 (when (get-text-property (point) 'shr-indentation
)
657 (while (setq start
(next-single-property-change start
'shr-indentation
))
661 (goto-char (point-max)))))
663 (defun shr-vertical-motion (column)
664 (if (not shr-use-fonts
)
665 (move-to-column column
)
668 (vertical-motion (cons (/ column
(frame-char-width)) 0))
672 (defun shr-fill-line ()
673 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
674 (continuation (get-text-property
675 (point) 'shr-continuation-indentation
))
677 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
678 (let ((face (get-text-property (point) 'face
))
679 (background-start (point)))
682 (put-text-property background-start
(point) 'face
683 `,(shr-face-background face
))))
685 (setq shr-indentation
(or continuation shr-indentation
))
686 (shr-vertical-motion shr-internal-width
)
687 (when (looking-at " $")
688 (delete-region (point) (line-end-position)))
690 ;; We have to do some folding. First find the first
691 ;; previous point suitable for folding.
692 (if (or (not (shr-find-fill-point (line-beginning-position)))
694 ;; We had unbreakable text (for this width), so just go to
695 ;; the first space and carry on.
698 (skip-chars-forward " ")
699 (search-forward " " (line-end-position) 'move
)))
700 ;; Success; continue.
701 (when (= (preceding-char) ?\s
)
703 (let ((props (text-properties-at (point)))
708 (add-text-properties gap-start
(point) props
)))
710 (shr-vertical-motion shr-internal-width
)
711 (when (looking-at " $")
712 (delete-region (point) (line-end-position))))))
714 (defun shr-find-fill-point (start)
718 (while (not (or (setq failed
(<= (point) start
))
719 (eq (preceding-char) ?
)
720 (eq (following-char) ?
)
721 (shr-char-breakable-p (preceding-char))
722 (shr-char-breakable-p (following-char))
723 (and (shr-char-kinsoku-bol-p (preceding-char))
724 (shr-char-breakable-p (following-char))
725 (not (shr-char-kinsoku-bol-p (following-char))))
726 (shr-char-kinsoku-eol-p (following-char))
730 ;; There's no breakable point, so we give it up.
733 ;; Don't overflow the window edge, even if
734 ;; shr-kinsoku-shorten is nil.
735 (unless (or shr-kinsoku-shorten
(null shr-width
))
736 (while (setq found
(re-search-forward
737 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
738 (line-end-position) 'move
)))
740 (not (match-beginning 1)))
741 (goto-char (match-beginning 0)))))
744 ;; Don't put kinsoku-bol characters at the beginning of a line,
745 ;; or kinsoku-eol characters at the end of a line.
747 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
749 ((or shr-kinsoku-shorten
(null shr-width
))
750 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
751 (or (shr-char-kinsoku-eol-p (preceding-char))
752 (shr-char-kinsoku-bol-p (following-char))))
754 (when (setq failed
(<= (point) start
))
755 ;; There's no breakable point that doesn't violate kinsoku,
756 ;; so we look for the second best position.
762 (shr-char-kinsoku-eol-p (following-char)))))
764 ((shr-char-kinsoku-eol-p (preceding-char))
765 ;; Find backward the point where kinsoku-eol characters begin.
770 (and (> (setq count
(1- count
)) 0)
771 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
772 (or (shr-char-kinsoku-eol-p (preceding-char))
773 (shr-char-kinsoku-bol-p (following-char)))))))
774 (when (setq failed
(<= (point) start
))
775 ;; There's no breakable point that doesn't violate kinsoku,
776 ;; so we go to the second best position.
777 (if (looking-at "\\(\\c<+\\)\\c<")
778 (goto-char (match-end 1))
780 ((shr-char-kinsoku-bol-p (following-char))
781 ;; Find forward the point where kinsoku-bol characters end.
785 (and (>= (setq count
(1- count
)) 0)
786 (shr-char-kinsoku-bol-p (following-char))
787 (shr-char-breakable-p (following-char))))))))
788 (when (eq (following-char) ?
)
792 (defun shr-parse-base (url)
793 ;; Always chop off anchors.
794 (when (string-match "#.*" url
)
795 (setq url
(substring url
0 (match-beginning 0))))
796 ;; NB: <base href="" > URI may itself be relative to the document s URI
797 (setq url
(shr-expand-url url
))
798 (let* ((parsed (url-generic-parse-url url
))
799 (local (url-filename parsed
)))
800 (setf (url-filename parsed
) "")
801 ;; Chop off the bit after the last slash.
802 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
803 (setq local
(match-string 1 local
)))
804 ;; Always make the local bit end with a slash.
805 (when (and (not (zerop (length local
)))
806 (not (eq (aref local
(1- (length local
))) ?
/)))
807 (setq local
(concat local
"/")))
808 (list (url-recreate-url parsed
)
813 (autoload 'url-expand-file-name
"url-expand")
815 ;; FIXME This needs some tests writing.
816 ;; Does it even need to exist, given that url-expand-file-name does?
817 (defun shr-expand-url (url &optional base
)
820 ;; shr-parse-base should never call this with non-nil base!
821 (shr-parse-base base
)
822 ;; Bound by the parser.
824 (when (zerop (length url
))
826 ;; Strip leading whitespace
827 (and url
(string-match "\\`\\s-+" url
)
828 (setq url
(substring url
(match-end 0))))
829 (cond ((zerop (length url
))
832 (string-match "\\`[a-z]*:" url
))
833 ;; Absolute or empty URI
835 ((eq (aref url
0) ?
/)
836 (if (and (> (length url
) 1)
837 (eq (aref url
1) ?
/))
838 ;; //host...; just use the protocol
839 (concat (nth 2 base
) ":" url
)
840 ;; Just use the host name part.
841 (concat (car base
) url
)))
842 ((eq (aref url
0) ?
#)
843 ;; A link to an anchor.
844 (concat (nth 3 base
) url
))
847 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
849 (defun shr-ensure-newline ()
851 (let ((prefix (get-text-property (line-beginning-position)
852 'shr-prefix-length
)))
853 (unless (or (zerop (current-column))
855 (= prefix
(- (point) (line-beginning-position)))))
858 (defun shr-ensure-paragraph ()
860 (let ((prefix (get-text-property (line-beginning-position)
861 'shr-prefix-length
)))
867 ;; We're already at a new paragraph; do nothing.
870 (= prefix
(- (point) (line-beginning-position))))
871 ;; Do nothing; we're at the start of a <li>.
875 ;; If the current line is totally blank, and doesn't even
876 ;; have any face properties set, then delete the blank
878 (and (looking-at " *$")
879 (not (get-text-property (point) 'face
))
880 (not (= (next-single-property-change (point) 'face nil
882 (line-end-position)))))
883 (delete-region (match-beginning 0) (match-end 0)))
884 ;; We have a single blank line.
887 ;; Insert new paragraph.
892 (when (> shr-indentation
0)
894 (if (not shr-use-fonts
)
895 (make-string shr-indentation ?\s
)
898 `(space :width
(,shr-indentation
)))))))
900 (defun shr-fontize-dom (dom &rest types
)
901 (let ((start (point)))
904 (shr-add-font start
(point) type
))))
906 ;; Add face to the region, but avoid putting the font properties on
907 ;; blank text at the start of the line, and the newline at the end, to
909 (defun shr-add-font (start end type
)
912 (while (< (point) end
)
914 (skip-chars-forward " "))
915 (add-face-text-property (point) (min (line-end-position) end
) type t
)
916 (if (< (line-end-position) end
)
920 (defun shr-mouse-browse-url (ev)
921 "Browse the URL under the mouse cursor."
926 (defun shr-browse-url (&optional external mouse-event
)
927 "Browse the URL under point.
928 If EXTERNAL, browse the URL using `shr-external-browser'."
929 (interactive (list current-prefix-arg last-nonmenu-event
))
930 (mouse-set-point mouse-event
)
931 (let ((url (get-text-property (point) 'shr-url
)))
934 (message "No link under point"))
935 ((string-match "^mailto:" url
)
936 (browse-url-mail url
))
939 (funcall shr-external-browser url
)
940 (browse-url url
))))))
942 (defun shr-save-contents (directory)
943 "Save the contents from URL in a file."
944 (interactive "DSave contents of URL to directory: ")
945 (let ((url (get-text-property (point) 'shr-url
)))
947 (message "No link under point")
948 (url-retrieve (shr-encode-url url
)
949 'shr-store-contents
(list url directory
)
952 (defun shr-store-contents (status url directory
)
953 (unless (plist-get status
:error
)
954 (when (or (search-forward "\n\n" nil t
)
955 (search-forward "\r\n\r\n" nil t
))
956 (write-region (point) (point-max)
957 (expand-file-name (file-name-nondirectory url
)
960 (defun shr-image-fetched (status buffer start end
&optional flags
)
961 (let ((image-buffer (current-buffer)))
962 (when (and (buffer-name buffer
)
963 (not (plist-get status
:error
)))
964 (url-store-in-cache image-buffer
)
965 (goto-char (point-min))
966 (when (or (search-forward "\n\n" nil t
)
967 (search-forward "\r\n\r\n" nil t
))
968 (let ((data (shr-parse-image-data)))
969 (with-current-buffer buffer
973 (let ((alt (buffer-substring start end
))
974 (properties (text-properties-at start
))
975 (inhibit-read-only t
))
976 (delete-region start end
)
978 (funcall shr-put-image-function data alt flags
)
980 (let ((type (pop properties
))
981 (value (pop properties
)))
982 (unless (memq type
'(display image-size
))
983 (put-text-property start
(point) type value
)))))))))))
984 (kill-buffer image-buffer
)))
986 (defun shr-image-from-data (data)
987 "Return an image from the data: URI content DATA."
989 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
991 (let ((param (match-string 4 data
))
992 (payload (url-unhex-string (match-string 5 data
))))
993 (when (string-match "^.*\\(;[ \t]*base64\\)$" param
)
994 (setq payload
(ignore-errors
995 (base64-decode-string payload
))))
998 ;; Behind display-graphic-p test.
999 (declare-function image-size
"image.c" (spec &optional pixels frame
))
1000 (declare-function image-animate
"image" (image &optional index limit
))
1002 (defun shr-put-image (spec alt
&optional flags
)
1003 "Insert image SPEC with a string ALT. Return image.
1004 SPEC is either an image data blob, or a list where the first
1005 element is the data blob and the second element is the content-type."
1006 (if (display-graphic-p)
1007 (let* ((size (cdr (assq 'size flags
)))
1008 (data (if (consp spec
)
1011 (content-type (and (consp spec
)
1015 ((eq size
'original
)
1016 (create-image data nil t
:ascent
100
1017 :format content-type
))
1018 ((eq content-type
'image
/svg
+xml
)
1019 (create-image data
'svg t
:ascent
100))
1022 (shr-rescale-image data content-type
1023 (plist-get flags
:width
)
1024 (plist-get flags
:height
))))
1027 (shr-rescale-image data content-type
1028 (plist-get flags
:width
)
1029 (plist-get flags
:height
)))))))
1031 ;; When inserting big-ish pictures, put them at the
1032 ;; beginning of the line.
1033 (when (and (> (current-column) 0)
1034 (> (car (image-size image t
)) 400))
1036 (if (eq size
'original
)
1037 (insert-sliced-image image
(or alt
"*") nil
20 1)
1038 (insert-image image
(or alt
"*")))
1039 (put-text-property start
(point) 'image-size size
)
1040 (when (and shr-image-animate
1041 (cond ((fboundp 'image-multi-frame-p
)
1042 ;; Only animate multi-frame things that specify a
1043 ;; delay; eg animated gifs as opposed to
1044 ;; multi-page tiffs. FIXME?
1045 (cdr (image-multi-frame-p image
)))
1046 ((fboundp 'image-animated-p
)
1047 (image-animated-p image
))))
1048 (image-animate image nil
60)))
1050 (insert (or alt
""))))
1052 (defun shr-rescale-image (data content-type width height
1053 &optional max-width max-height
)
1054 "Rescale DATA, if too big, to fit the current buffer.
1055 WIDTH and HEIGHT are the sizes given in the HTML data, if any.
1057 The size of the displayed image will not exceed
1058 MAX-WIDTH/MAX-HEIGHT. If not given, use the current window
1059 width/height instead."
1060 (if (or (not (fboundp 'imagemagick-types
))
1061 (not (get-buffer-window (current-buffer))))
1062 (create-image data nil t
:ascent
100)
1063 (let* ((edges (window-inside-pixel-edges
1064 (get-buffer-window (current-buffer))))
1065 (max-width (truncate (* shr-max-image-proportion
1067 (- (nth 2 edges
) (nth 0 edges
))))))
1068 (max-height (truncate (* shr-max-image-proportion
1070 (- (nth 3 edges
) (nth 1 edges
))))))
1071 (scaling (image-compute-scaling-factor image-scaling-factor
)))
1072 (when (or (and width
1073 (> width max-width
))
1075 (> height max-height
)))
1078 (if (and width height
1079 (< (* width scaling
) max-width
)
1080 (< (* height scaling
) max-height
))
1086 :format content-type
)
1090 :max-width max-width
1091 :max-height max-height
1092 :format content-type
)))))
1094 ;; url-cache-extract autoloads url-cache.
1095 (declare-function url-cache-create-filename
"url-cache" (url))
1096 (autoload 'mm-disable-multibyte
"mm-util")
1097 (autoload 'browse-url-mail
"browse-url")
1099 (defun shr-get-image-data (url)
1100 "Get image data for URL.
1101 Return a string with image data."
1103 (mm-disable-multibyte)
1104 (when (ignore-errors
1105 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
1107 (when (re-search-forward "\r?\n\r?\n" nil t
)
1108 (shr-parse-image-data)))))
1110 (declare-function libxml-parse-xml-region
"xml.c"
1111 (start end
&optional base-url discard-comments
))
1113 (defun shr-parse-image-data ()
1114 (let ((data (buffer-substring (point) (point-max)))
1118 (narrow-to-region (point-min) (point))
1119 (let ((content-type (mail-fetch-field "content-type")))
1121 ;; Remove any comments in the type string.
1122 (intern (replace-regexp-in-string ";.*" "" content-type
)
1124 ;; SVG images may contain references to further images that we may
1125 ;; want to block. So special-case these by parsing the XML data
1126 ;; and remove anything that looks like a blocked bit.
1127 (when (and shr-blocked-images
1128 (eq content-type
'image
/svg
+xml
))
1130 ;; Note that libxml2 doesn't parse everything perfectly,
1131 ;; so glitches may occur during this transformation.
1133 (libxml-parse-xml-region (point) (point-max)))))
1134 (list data content-type
)))
1136 (defun shr-image-displayer (content-function)
1137 "Return a function to display an image.
1138 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1139 is an argument. The function to be returned takes three arguments URL,
1140 START, and END. Note that START and END should be markers."
1141 `(lambda (url start end
)
1143 (if (string-match "\\`cid:" url
)
1144 ,(when content-function
1145 `(let ((image (funcall ,content-function
1146 (substring url
(match-end 0)))))
1149 (funcall shr-put-image-function
1150 image
(buffer-substring start end
))
1151 (delete-region (point) end
))))
1152 (url-retrieve url
'shr-image-fetched
1153 (list (current-buffer) start end
)
1156 (defun shr-heading (dom &rest types
)
1157 (shr-ensure-paragraph)
1158 (apply #'shr-fontize-dom dom types
)
1159 (shr-ensure-paragraph))
1161 (defun shr-urlify (start url
&optional title
)
1162 (shr-add-font start
(point) 'shr-link
)
1163 (add-text-properties
1166 'help-echo
(let ((iri (or (ignore-errors
1167 (decode-coding-string
1168 (url-unhex-string url
)
1171 (if title
(format "%s (%s)" iri title
) iri
))
1173 'mouse-face
'highlight
))
1174 ;; Don't overwrite any keymaps that are already in the buffer (i.e.,
1178 (let ((next (next-single-property-change start
'keymap nil
(point))))
1179 (if (get-text-property start
'keymap
)
1181 (put-text-property start
(or next
(point)) 'keymap shr-map
)))))
1183 (defun shr-encode-url (url)
1185 (browse-url-url-encode-chars url
"[)$ ]"))
1187 (autoload 'shr-color-visible
"shr-color")
1188 (autoload 'shr-color-
>hexadecimal
"shr-color")
1190 (defun shr-color-check (fg bg
)
1191 "Check that FG is visible on BG.
1192 Returns (fg bg) with corrected values.
1193 Returns nil if the colors that would be used are the default
1194 ones, in case fg and bg are nil."
1196 (let ((fixed (cond ((null fg
) 'fg
)
1198 ;; Convert colors to hexadecimal, or set them to default.
1199 (let ((fg (or (shr-color->hexadecimal fg
)
1200 (frame-parameter nil
'foreground-color
)))
1201 (bg (or (shr-color->hexadecimal bg
)
1202 (frame-parameter nil
'background-color
))))
1203 (cond ((eq fixed
'bg
)
1204 ;; Only return the new fg
1205 (list nil
(cadr (shr-color-visible bg fg t
))))
1207 ;; Invert args and results and return only the new bg
1208 (list (cadr (shr-color-visible fg bg t
)) nil
))
1210 (shr-color-visible bg fg
)))))))
1212 (defun shr-colorize-region (start end fg
&optional bg
)
1213 (when (and shr-use-colors
1215 (>= (display-color-cells) 88))
1216 (let ((new-colors (shr-color-check fg bg
)))
1219 (add-face-text-property start end
1220 (list :foreground
(cadr new-colors
))
1223 (add-face-text-property start end
1224 (list :background
(car new-colors
))
1228 ;;; Tag-specific rendering rules.
1230 (defun shr-tag-html (dom)
1231 (let ((dir (dom-attr dom
'dir
)))
1234 (setq bidi-paragraph-direction
'left-to-right
))
1236 (setq bidi-paragraph-direction
'right-to-left
))
1238 (setq bidi-paragraph-direction nil
))))
1241 (defun shr-tag-body (dom)
1242 (let* ((start (point))
1243 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1244 (bgcolor (dom-attr dom
'bgcolor
))
1245 (shr-stylesheet (list (cons 'color fgcolor
)
1246 (cons 'background-color bgcolor
))))
1248 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1250 (defun shr-tag-style (_dom)
1253 (defun shr-tag-script (_dom)
1256 (defun shr-tag-comment (_dom)
1259 (defun shr-dom-to-xml (dom)
1264 (defun shr-dom-print (dom)
1265 "Convert DOM into a string containing the xml representation."
1266 (insert (format "<%s" (dom-tag dom
)))
1267 (dolist (attr (dom-attributes dom
))
1268 ;; Ignore attributes that start with a colon because they are
1269 ;; private elements.
1270 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1271 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1274 (dolist (elem (dom-children dom
))
1278 ((eq (dom-tag elem
) 'comment
)
1280 ((or (not (eq (dom-tag elem
) 'image
))
1281 ;; Filter out blocked elements inside the SVG image.
1282 (not (setq url
(dom-attr elem
':xlink
:href
)))
1283 (not shr-blocked-images
)
1284 (not (string-match shr-blocked-images url
)))
1286 (shr-dom-print elem
)))))
1287 (insert (format "</%s>" (dom-tag dom
))))
1289 (defun shr-tag-svg (dom)
1290 (when (and (image-type-available-p 'svg
)
1291 (not shr-inhibit-images
)
1292 (dom-attr dom
'width
)
1293 (dom-attr dom
'height
))
1294 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1297 (defun shr-tag-sup (dom)
1298 (let ((start (point)))
1300 (put-text-property start
(point) 'display
'(raise 0.5))))
1302 (defun shr-tag-sub (dom)
1303 (let ((start (point)))
1305 (put-text-property start
(point) 'display
'(raise -
0.5))))
1307 (defun shr-tag-label (dom)
1309 (shr-ensure-paragraph))
1311 (defun shr-tag-p (dom)
1312 (shr-ensure-paragraph)
1314 (shr-ensure-paragraph))
1316 (defun shr-tag-div (dom)
1317 (shr-ensure-newline)
1319 (shr-ensure-newline))
1321 (defun shr-tag-s (dom)
1322 (shr-fontize-dom dom
'shr-strike-through
))
1324 (defun shr-tag-b (dom)
1325 (shr-fontize-dom dom
'bold
))
1327 (defun shr-tag-i (dom)
1328 (shr-fontize-dom dom
'italic
))
1330 (defun shr-tag-em (dom)
1331 (shr-fontize-dom dom
'italic
))
1333 (defun shr-tag-strong (dom)
1334 (shr-fontize-dom dom
'bold
))
1336 (defun shr-tag-u (dom)
1337 (shr-fontize-dom dom
'underline
))
1339 (defun shr-tag-tt (dom)
1340 (let ((shr-current-font 'default
))
1343 (defun shr-tag-ins (cont)
1344 (let* ((start (point))
1346 (shr-stylesheet (nconc (list (cons 'color color
))
1349 (shr-colorize-region start
(point) color
1350 (cdr (assq 'background-color shr-stylesheet
)))))
1352 (defun shr-tag-del (cont)
1353 (let* ((start (point))
1355 (shr-stylesheet (nconc (list (cons 'color color
))
1357 (shr-fontize-dom cont
'shr-strike-through
)
1358 (shr-colorize-region start
(point) color
1359 (cdr (assq 'background-color shr-stylesheet
)))))
1361 (defun shr-parse-style (style)
1364 (when (string-match "\n" style
)
1365 (setq style
(replace-match " " t t style
))))
1367 (dolist (elem (split-string style
";"))
1369 (setq elem
(split-string elem
":"))
1370 (when (and (car elem
)
1372 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1373 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1374 (when (string-match " *!important\\'" value
)
1375 (setq value
(substring value
0 (match-beginning 0))))
1376 (unless (equal value
"inherit")
1377 (push (cons (intern name obarray
)
1382 (defun shr-tag-base (dom)
1383 (when-let* ((base (dom-attr dom
'href
)))
1384 (setq shr-base
(shr-parse-base base
)))
1387 (defun shr-tag-a (dom)
1388 (let ((url (dom-attr dom
'href
))
1389 (title (dom-attr dom
'title
))
1393 (when (and shr-target-id
1394 (equal (dom-attr dom
'name
) shr-target-id
))
1395 ;; We have a zero-length <a name="foo"> element, so just
1396 ;; insert... something.
1397 (when (= start
(point))
1398 (shr-ensure-newline)
1400 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1402 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1404 (defun shr-tag-object (dom)
1405 (unless shr-inhibit-images
1406 (let ((start (point))
1407 url multimedia image
)
1408 (when-let* ((type (dom-attr dom
'type
)))
1409 (when (string-match "\\`image/svg" type
)
1410 (setq url
(dom-attr dom
'data
)
1412 (dolist (child (dom-non-text-children dom
))
1414 ((eq (dom-tag child
) 'embed
)
1415 (setq url
(or url
(dom-attr child
'src
))
1417 ((and (eq (dom-tag child
) 'param
)
1418 (equal (dom-attr child
'name
) "movie"))
1419 (setq url
(or url
(dom-attr child
'value
))
1424 (shr-indirect-call 'img dom url
)
1427 (shr-insert " [multimedia] ")
1428 (shr-urlify start
(shr-expand-url url
)))))
1430 (shr-generic dom
)))))
1432 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1438 "Preferences for media types.
1439 The key element should be a regexp matched against the type of the source or
1440 url if no type is specified. The value should be a float in the range 0.0 to
1441 1.0. Media elements with higher value are preferred."
1444 :type
'(alist :key-type regexp
:value-type float
))
1446 (defun shr--get-media-pref (elem)
1447 "Determine the preference for ELEM.
1448 The preference is a float determined from `shr-prefer-media-type'."
1449 (let ((type (dom-attr elem
'type
))
1452 (setq type
(dom-attr elem
'src
)))
1454 (dolist (pref shr-prefer-media-type-alist
)
1457 (string-match-p (car pref
) type
))
1458 (setq p
(cdr pref
)))))
1461 (defun shr--extract-best-source (dom &optional url pref
)
1462 "Extract the best `:src' property from <source> blocks in DOM."
1463 (setq pref
(or pref -
1.0))
1465 (dolist (elem (dom-non-text-children dom
))
1466 (when (and (eq (dom-tag elem
) 'source
)
1469 (shr--get-media-pref elem
))))
1471 url
(dom-attr elem
'src
))
1472 ;; libxml's html parser isn't HTML5 compliant and non terminated
1473 ;; source tags might end up as children. So recursion it is...
1474 (dolist (child (dom-non-text-children elem
))
1475 (when (eq (dom-tag child
) 'source
)
1476 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1477 (when (< pref
(cdr ret
))
1479 pref
(cdr ret
)))))))))
1482 (defun shr-tag-video (dom)
1483 (let ((image (dom-attr dom
'poster
))
1484 (url (dom-attr dom
'src
))
1487 (setq url
(car (shr--extract-best-source dom
))))
1488 (if (> (length image
) 0)
1489 (shr-indirect-call 'img nil image
)
1490 (shr-insert " [video] "))
1491 (shr-urlify start
(shr-expand-url url
))))
1493 (defun shr-tag-audio (dom)
1494 (let ((url (dom-attr dom
'src
))
1497 (setq url
(car (shr--extract-best-source dom
))))
1498 (shr-insert " [audio] ")
1499 (shr-urlify start
(shr-expand-url url
))))
1501 (defun shr-tag-img (dom &optional url
)
1504 (or (> (length (dom-attr dom
'src
)) 0)
1505 (> (length (dom-attr dom
'srcset
)) 0))))
1506 (when (> (current-column) 0)
1508 (let ((alt (dom-attr dom
'alt
))
1509 (width (shr-string-number (dom-attr dom
'width
)))
1510 (height (shr-string-number (dom-attr dom
'height
)))
1511 (url (shr-expand-url (or url
(shr--preferred-image dom
)))))
1512 (let ((start (point-marker)))
1513 (when (zerop (length alt
))
1516 ((or (member (dom-attr dom
'height
) '("0" "1"))
1517 (member (dom-attr dom
'width
) '("0" "1")))
1518 ;; Ignore zero-sized or single-pixel images.
1520 ((and (not shr-inhibit-images
)
1521 (string-match "\\`data:" url
))
1522 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1524 (funcall shr-put-image-function image alt
1525 (list :width width
:height height
))
1527 ((and (not shr-inhibit-images
)
1528 (string-match "\\`cid:" url
))
1529 (let ((url (substring url
(match-end 0)))
1531 (if (or (not shr-content-function
)
1532 (not (setq image
(funcall shr-content-function url
))))
1534 (funcall shr-put-image-function image alt
1535 (list :width width
:height height
)))))
1536 ((or shr-inhibit-images
1537 (and shr-blocked-images
1538 (string-match shr-blocked-images url
)))
1539 (setq shr-start
(point))
1541 ((and (not shr-ignore-cache
)
1542 (url-is-cached (shr-encode-url url
)))
1543 (funcall shr-put-image-function
(shr-get-image-data url
) alt
1544 (list :width width
:height height
)))
1546 (when (and shr-ignore-cache
1547 (url-is-cached (shr-encode-url url
)))
1548 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1549 (when (file-exists-p file
)
1550 (delete-file file
))))
1551 (when (image-type-available-p 'svg
)
1553 (shr-make-placeholder-image dom
)
1557 (shr-encode-url url
) 'shr-image-fetched
1558 (list (current-buffer) start
(set-marker (make-marker) (point))
1559 (list :width width
:height height
))
1561 (when (zerop shr-table-depth
) ;; We are not in a table.
1562 (put-text-property start
(point) 'keymap shr-image-map
)
1563 (put-text-property start
(point) 'shr-alt alt
)
1564 (put-text-property start
(point) 'image-url url
)
1565 (put-text-property start
(point) 'image-displayer
1566 (shr-image-displayer shr-content-function
))
1567 (put-text-property start
(point) 'help-echo
1569 (or (dom-attr dom
'title
) alt
))))))))
1571 (defun shr--preferred-image (dom)
1572 (let ((srcset (dom-attr dom
'srcset
))
1573 (frame-width (frame-pixel-width))
1574 (width (string-to-number (or (dom-attr dom
'width
) "100")))
1576 (when (> (length srcset
) 0)
1577 ;; srcset consist of a series of URL/size specifications
1578 ;; separated by the ", " string.
1582 (let ((spec (split-string elem
"[\t\n\r ]+")))
1584 ((= (length spec
) 1)
1585 ;; Make sure it's well formed.
1586 (list (car spec
) 0))
1587 ((string-match "\\([0-9]+\\)x\\'" (cadr spec
))
1588 ;; If we have an "x" form, then use the width
1589 ;; spec to compute the real width.
1591 (* width
(string-to-number
1592 (match-string 1 (cadr spec
))))))
1595 (string-to-number (cadr spec
)))))))
1596 (split-string (replace-regexp-in-string
1597 "\\`[\t\n\r ]+\\|[\t\n\r ]+\\'" "" srcset
)
1598 "[\t\n\r ]*,[\t\n\r ]*"))
1600 (> (cadr e1
) (cadr e2
)))))
1601 ;; Choose the smallest picture that's bigger than the current
1603 (setq candidate
(caar srcset
))
1605 (> (cadr (car srcset
)) frame-width
))
1606 (setq candidate
(caar srcset
))
1608 (or candidate
(dom-attr dom
'src
))))
1610 (defun shr-string-number (string)
1613 (setq string
(replace-regexp-in-string "[^0-9]" "" string
))
1614 (if (zerop (length string
))
1616 (string-to-number string
))))
1618 (defun shr-make-placeholder-image (dom)
1620 (get-buffer-window (current-buffer))
1621 (window-inside-pixel-edges
1622 (get-buffer-window (current-buffer)))))
1623 (scaling (image-compute-scaling-factor image-scaling-factor
))
1625 (* (or (shr-string-number (dom-attr dom
'width
)) 100)
1628 (* (or (shr-string-number (dom-attr dom
'height
)) 100)
1632 (truncate (* shr-max-image-proportion
1633 (- (nth 2 edges
) (nth 0 edges
))))))
1634 (max-height (and edges
1635 (truncate (* shr-max-image-proportion
1636 (- (nth 3 edges
) (nth 1 edges
))))))
1638 (when (and max-width
1639 (> width max-width
))
1640 (setq height
(truncate (* (/ (float max-width
) width
) height
))
1642 (when (and max-height
1643 (> height max-height
))
1644 (setq width
(truncate (* (/ (float max-height
) height
) width
))
1646 (setq svg
(svg-create width height
))
1647 (svg-gradient svg
"background" 'linear
'((0 .
"#b0b0b0") (100 .
"#808080")))
1648 (svg-rectangle svg
0 0 width height
:gradient
"background"
1649 :stroke-width
2 :stroke-color
"black")
1650 (let ((image (svg-image svg
)))
1651 (setf (image-property image
:ascent
) 100)
1654 (defun shr-tag-pre (dom)
1655 (let ((shr-folding-mode 'none
)
1656 (shr-current-font 'default
))
1657 (shr-ensure-newline)
1659 (shr-ensure-newline)))
1661 (defun shr-tag-blockquote (dom)
1662 (shr-ensure-paragraph)
1663 (let ((start (point))
1664 (shr-indentation (+ shr-indentation
1665 (* 4 shr-table-separator-pixel-width
))))
1667 (shr-ensure-paragraph)
1668 (shr-mark-fill start
)))
1670 (defun shr-tag-dl (dom)
1671 (shr-ensure-paragraph)
1673 (shr-ensure-paragraph))
1675 (defun shr-tag-dt (dom)
1676 (shr-ensure-newline)
1678 (shr-ensure-newline))
1680 (defun shr-tag-dd (dom)
1681 (shr-ensure-newline)
1682 (let ((shr-indentation (+ shr-indentation
1683 (* 4 shr-table-separator-pixel-width
))))
1686 (defun shr-tag-ul (dom)
1687 (shr-ensure-paragraph)
1688 (let ((shr-list-mode 'ul
))
1690 ;; If we end on an empty <li>, then make sure we really end on a new
1694 (shr-ensure-paragraph))
1696 (defun shr-tag-ol (dom)
1697 (shr-ensure-paragraph)
1698 (let ((shr-list-mode 1))
1700 (shr-ensure-paragraph))
1702 (defun shr-tag-li (dom)
1703 (shr-ensure-newline)
1704 (let ((start (point)))
1706 (if (numberp shr-list-mode
)
1708 (format "%d " shr-list-mode
)
1709 (setq shr-list-mode
(1+ shr-list-mode
)))
1710 (car shr-internal-bullet
)))
1711 (width (if (numberp shr-list-mode
)
1712 (shr-string-pixel-width bullet
)
1713 (cdr shr-internal-bullet
))))
1715 (shr-mark-fill start
)
1716 (let ((shr-indentation (+ shr-indentation width
)))
1717 (put-text-property start
(1+ start
)
1718 'shr-continuation-indentation shr-indentation
)
1719 (put-text-property start
(1+ start
) 'shr-prefix-length
(length bullet
))
1720 (shr-generic dom
))))
1724 (defun shr-mark-fill (start)
1725 ;; We may not have inserted any text to fill.
1726 (unless (= start
(point))
1727 (put-text-property start
(1+ start
)
1728 'shr-indentation shr-indentation
)))
1730 (defun shr-tag-br (dom)
1731 (when (and (not (bobp))
1732 ;; Only add a newline if we break the current line, or
1733 ;; the previous line isn't a blank line.
1735 (and (> (- (point) 2) (point-min))
1736 (not (= (char-after (- (point) 2)) ?
\n)))))
1740 (defun shr-tag-span (dom)
1743 (defun shr-tag-h1 (dom)
1744 (shr-heading dom
(if shr-use-fonts
1745 '(variable-pitch (:height
1.3 :weight bold
))
1748 (defun shr-tag-h2 (dom)
1749 (shr-heading dom
'bold
))
1751 (defun shr-tag-h3 (dom)
1752 (shr-heading dom
'italic
))
1754 (defun shr-tag-h4 (dom)
1757 (defun shr-tag-h5 (dom)
1760 (defun shr-tag-h6 (dom)
1763 (defun shr-tag-hr (_dom)
1764 (shr-ensure-newline)
1765 (insert (make-string (if (not shr-use-fonts
)
1767 (1+ (/ shr-internal-width
1768 shr-table-separator-pixel-width
)))
1772 (defun shr-tag-title (dom)
1773 (shr-heading dom
'bold
'underline
))
1775 (defun shr-tag-font (dom)
1776 (let* ((start (point))
1777 (color (dom-attr dom
'color
))
1778 (shr-stylesheet (nconc (list (cons 'color color
))
1782 (shr-colorize-region start
(point) color
1783 (cdr (assq 'background-color shr-stylesheet
))))))
1785 (defun shr-tag-bdo (dom)
1786 (let* ((direction (dom-attr dom
'dir
))
1788 ((equal direction
"ltr")
1789 ?\N
{LEFT-TO-RIGHT OVERRIDE
})
1790 ((equal direction
"rtl")
1791 ?\N
{RIGHT-TO-LEFT OVERRIDE
}))))
1793 (insert ?\N
{FIRST STRONG ISOLATE
} char
))
1796 (insert ?\N
{POP DIRECTIONAL FORMATTING
} ?\N
{POP DIRECTIONAL ISOLATE
}))))
1798 (defun shr-tag-bdi (dom)
1799 (insert ?\N
{FIRST STRONG ISOLATE
})
1801 (insert ?\N
{POP DIRECTIONAL ISOLATE
}))
1803 ;;; Table rendering algorithm.
1805 ;; Table rendering is the only complicated thing here. We do this by
1806 ;; first counting how many TDs there are in each TR, and registering
1807 ;; how wide they think they should be ("width=45%", etc). Then we
1808 ;; render each TD separately (this is done in temporary buffers, so
1809 ;; that we can use all the rendering machinery as if we were in the
1810 ;; main buffer). Now we know how much space each TD really takes, so
1811 ;; we then render everything again with the new widths, and finally
1812 ;; insert all these boxes into the main buffer.
1813 (defun shr-tag-table-1 (dom)
1814 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1815 (let* ((shr-inhibit-images t
)
1816 (shr-table-depth (1+ shr-table-depth
))
1817 (shr-kinsoku-shorten t
)
1818 ;; Find all suggested widths.
1819 (columns (shr-column-specs dom
))
1820 ;; Compute how many pixels wide each TD should be.
1821 (suggested-widths (shr-pro-rate-columns columns
))
1822 ;; Do a "test rendering" to see how big each TD is (this can
1823 ;; be smaller (if there's little text) or bigger (if there's
1824 ;; unbreakable text).
1825 (elems (or (dom-attr dom
'shr-suggested-widths
)
1826 (shr-make-table dom suggested-widths nil
1827 'shr-suggested-widths
)))
1828 (sketch (cl-loop for line in elems
1829 collect
(mapcar #'car line
)))
1830 (natural (cl-loop for line in elems
1831 collect
(mapcar #'cdr line
)))
1832 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1833 ;; This probably won't work very well.
1834 (when (> (+ (cl-loop for width across sketch-widths
1836 shr-indentation shr-table-separator-pixel-width
)
1838 (setq truncate-lines t
))
1839 ;; Then render the table again with these new "hard" widths.
1840 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1842 (defun shr-table-body (dom)
1843 (let ((tbodies (seq-filter (lambda (child)
1844 (eq (dom-tag child
) 'tbody
))
1845 (dom-non-text-children dom
))))
1849 ((= (length tbodies
) 1)
1852 ;; Table with multiple tbodies. Convert into a single tbody.
1853 `(tbody nil
,@(cl-reduce 'append
1854 (mapcar 'dom-non-text-children tbodies
)))))))
1856 (defun shr-tag-table (dom)
1857 (shr-ensure-paragraph)
1858 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1859 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1860 (body (dom-non-text-children (shr-table-body dom
)))
1861 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1862 (bgcolor (dom-attr dom
'bgcolor
))
1864 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1866 (nheader (if header
(shr-max-columns header
)))
1867 (nbody (if body
(shr-max-columns body
) 0))
1868 (nfooter (if footer
(shr-max-columns footer
))))
1869 (if (and (not caption
)
1871 (not (dom-child-by-tag dom
'tbody
))
1872 (not (dom-child-by-tag dom
'tr
))
1874 ;; The table is totally invalid and just contains random junk.
1875 ;; Try to output it anyway.
1877 ;; It's a real table, so render it.
1878 (if (dom-attr dom
'shr-fixed-table
)
1879 (shr-tag-table-1 dom
)
1880 ;; Only fix up the table once.
1884 (if caption
`((tr nil
(td nil
,@caption
))))
1888 ;; header + body + footer
1889 (if (= nheader nbody
)
1890 (if (= nbody nfooter
)
1891 `((tr nil
(td nil
(table nil
1893 ,@body
,@footer
)))))
1894 (nconc `((tr nil
(td nil
(table nil
1899 `((tr nil
(td nil
(table
1901 nil
,@footer
))))))))
1902 (nconc `((tr nil
(td nil
(table nil
(tbody
1904 (if (= nbody nfooter
)
1905 `((tr nil
(td nil
(table
1906 nil
(tbody nil
,@body
1908 (nconc `((tr nil
(td nil
(table
1913 `((tr nil
(td nil
(table
1919 (if (= nheader nbody
)
1920 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1923 `(,@header
(tr nil
(td nil
(table
1924 nil
(tbody nil
,@body
)))))
1925 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1926 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1929 (if (= nbody nfooter
)
1930 `((tr nil
(td nil
(table
1931 nil
(tbody nil
,@body
,@footer
)))))
1932 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1935 `((tr nil
(td nil
(table
1936 nil
(tbody nil
,@footer
)))))))))
1938 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1940 (dom-set-attribute table
'shr-fixed-table t
)
1941 (setcdr dom
(cdr table
))
1942 (shr-tag-table-1 dom
))))
1944 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1946 ;; Finally, insert all the images after the table. The Emacs buffer
1947 ;; model isn't strong enough to allow us to put the images actually
1948 ;; into the tables. It inserts also non-td/th objects.
1949 (when (zerop shr-table-depth
)
1951 (shr-expand-alignments start
(point)))
1952 (let ((strings (shr-collect-extra-strings-in-table dom
)))
1955 (narrow-to-region (point) (point))
1956 (insert (mapconcat #'identity strings
"\n"))
1957 (shr-fill-lines (point-min) (point-max))))))))
1959 (defun shr-collect-extra-strings-in-table (dom &optional flags
)
1960 "Return extra strings in DOM of which the root is a table clause.
1961 Render <img>s and <object>s, and strings and child <table>s of which
1962 the parent <td> or <th> is lacking. FLAGS is a cons of two boolean
1963 flags that control whether to collect or render objects."
1964 ;; This function runs recursively and collects strings if the cdr of
1965 ;; FLAGS is nil and the car is not nil, and it renders also child
1966 ;; <table>s if the cdr is nil. Note: FLAGS may be nil, not a cons.
1967 ;; FLAGS becomes (t . nil) if a <tr> clause is found in the children
1968 ;; of DOM, and becomes (t . t) if a <td> or a <th> clause is found
1969 ;; and the car is t then. When a <table> clause is found, FLAGS
1970 ;; becomes nil if the cdr is t then. But if FLAGS is (t . nil) then,
1971 ;; it renders the <table>.
1972 (cl-loop for child in
(dom-children dom
) with recurse with tag
1973 do
(setq recurse nil
)
1975 unless
(or (not (car flags
)) (cdr flags
))
1976 when
(string-match "\\(?:[^\t\n\r ]+[\t\n\r ]+\\)*[^\t\n\r ]+"
1978 collect
(match-string 0 child
)
1980 else if
(consp child
)
1981 do
(setq tag
(dom-tag child
)) and
1982 unless
(memq tag
'(comment style
))
1984 do
(shr-indirect-call 'img child
)
1985 else if
(eq tag
'object
)
1986 do
(shr-indirect-call 'object child
)
1988 do
(setq recurse t
) and
1990 do
(setq flags
'(t . nil
))
1991 else if
(memq tag
'(td th
))
1993 do
(setq flags
'(t . t
))
1995 else if
(eq tag
'table
)
1999 do
(setq recurse nil
)
2000 (shr-indirect-call 'table child
)
2001 end end end end end end end end end end
2003 append
(shr-collect-extra-strings-in-table child flags
)))
2005 (defun shr-insert-table (table widths
)
2006 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
2008 (shr-table-separator-length (if collapse
0 1))
2009 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
2011 (setq shr-table-id
(1+ shr-table-id
))
2013 (shr-insert-table-ruler widths
))
2015 (let ((start (point))
2018 (height (let ((max 0))
2019 (dolist (column row
)
2020 (setq max
(max max
(nth 2 column
))))
2022 (dotimes (_ (max height
1))
2024 (insert shr-table-vertical-line
"\n"))
2025 (dolist (column row
)
2026 (when (> (nth 2 column
) -
1)
2028 ;; Sum up all the widths from the column. (There may be
2029 ;; more than one if this is a "colspan" column.)
2030 (dotimes (_ (nth 4 column
))
2031 ;; The colspan directive may be wrong and there may not be
2032 ;; that number of columns.
2033 (when (<= column-number
(1- (length widths
)))
2034 (setq align
(+ align
2035 (aref widths column-number
)
2036 (* 2 shr-table-separator-pixel-width
))))
2037 (setq column-number
(1+ column-number
)))
2038 (let ((lines (nth 3 column
))
2039 (pixel-align (if (not shr-use-fonts
)
2040 (* align
(frame-char-width))
2042 (dolist (line lines
)
2044 (let ((start (point))
2045 (background (and (> (length line
) 0)
2046 (shr-face-background
2048 (1- (length line
)) 'face line
))))
2051 'display
`(space :align-to
(,pixel-align
))
2052 'shr-table-indent shr-table-id
)))
2054 (setq space
(propertize space
'face background
)))
2055 (insert line space shr-table-vertical-line
)
2056 (shr-colorize-region
2057 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2059 ;; Add blank lines at padding at the bottom of the TD,
2061 (dotimes (_ (- height
(length lines
)))
2063 (let ((start (point)))
2064 (insert (propertize " "
2065 'display
`(space :align-to
(,pixel-align
))
2066 'shr-table-indent shr-table-id
)
2067 shr-table-vertical-line
)
2068 (shr-colorize-region
2069 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
2070 (forward-line 1))))))
2072 (shr-insert-table-ruler widths
)))
2073 (unless (= start
(point))
2074 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
2076 (defun shr-face-background (face)
2078 (or (and (plist-get face
:background
)
2079 (list :background
(plist-get face
:background
)))
2080 (let ((background nil
))
2082 (when (and (consp elem
)
2083 (eq (car elem
) :background
)
2085 (setq background
(cadr elem
))))
2087 (list :background background
))))))
2089 (defun shr-expand-alignments (start end
)
2090 (while (< (setq start
(next-single-property-change
2091 start
'shr-table-id nil end
))
2094 (let* ((shr-use-fonts t
)
2095 (id (get-text-property (point) 'shr-table-id
))
2096 (base (shr-pixel-column))
2100 (while (setq elem
(text-property-any
2101 (point) end
'shr-table-indent id
))
2103 (let ((align (get-text-property (point) 'display
)))
2104 (put-text-property (point) (1+ (point)) 'display
2105 `(space :align-to
(,(+ (car (nth 2 align
))
2107 (forward-char 1)))))
2108 (setq start
(1+ start
))))
2110 (defun shr-insert-table-ruler (widths)
2111 (when shr-table-horizontal-line
2113 (> shr-indentation
0))
2115 (insert shr-table-corner
)
2116 (let ((total-width 0))
2117 (dotimes (i (length widths
))
2118 (setq total-width
(+ total-width
(aref widths i
)
2119 (* shr-table-separator-pixel-width
2)))
2120 (insert (make-string (1+ (/ (aref widths i
)
2121 shr-table-separator-pixel-width
))
2122 shr-table-horizontal-line
)
2124 'display
`(space :align-to
(,total-width
))
2125 'shr-table-indent shr-table-id
)
2129 (defun shr-table-widths (table natural-table suggested-widths
)
2130 (let* ((length (length suggested-widths
))
2131 (widths (make-vector length
0))
2132 (natural-widths (make-vector length
0)))
2135 (dolist (column row
)
2136 (aset widths i
(max (aref widths i
) column
))
2138 (dolist (row natural-table
)
2140 (dolist (column row
)
2141 (aset natural-widths i
(max (aref natural-widths i
) column
))
2143 (let ((extra (- (apply '+ (append suggested-widths nil
))
2144 (apply '+ (append widths nil
))
2145 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
2146 (expanded-columns 0))
2147 ;; We have extra, unused space, so divide this space amongst the
2150 ;; If the natural width is wider than the rendered width, we
2151 ;; want to allow the column to expand.
2153 (when (> (aref natural-widths i
) (aref widths i
))
2154 (setq expanded-columns
(1+ expanded-columns
))))
2156 (when (> (aref natural-widths i
) (aref widths i
))
2158 (aref natural-widths i
)
2159 (+ (/ extra expanded-columns
)
2160 (aref widths i
))))))))
2163 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
2164 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
2165 (let ((data (shr-make-table-1 dom widths fill
)))
2166 (push (list (list dom widths fill
) data
)
2168 (when storage-attribute
2169 (dom-set-attribute dom storage-attribute data
))
2172 (defun shr-make-table-1 (dom widths
&optional fill
)
2174 (rowspans (make-vector (length widths
) 0))
2175 (colspan-remaining 0)
2176 colspan-width colspan-count
2178 (dolist (row (dom-non-text-children dom
))
2179 (when (eq (dom-tag row
) 'tr
)
2181 (columns (dom-non-text-children row
))
2185 (while (< i
(length widths
))
2186 ;; If we previously had a rowspan definition, then that
2187 ;; means that we now have a "missing" td/th element here.
2188 ;; So just insert a dummy, empty one to (sort of) emulate
2191 (if (zerop (aref rowspans i
))
2193 (aset rowspans i
(1- (aref rowspans i
)))
2195 (when (and (not (stringp column
))
2196 (or (memq (dom-tag column
) '(td th
))
2198 (when-let* ((span (dom-attr column
'rowspan
)))
2199 (aset rowspans i
(+ (aref rowspans i
)
2200 (1- (string-to-number span
)))))
2201 ;; Sanity check for invalid column-spans.
2202 (when (>= width-column
(length widths
))
2203 (setq width-column
0))
2206 (aref widths width-column
)
2207 (* 10 shr-table-separator-pixel-width
)))
2208 (when (setq colspan
(dom-attr column
'colspan
))
2209 (setq colspan
(min (string-to-number colspan
)
2210 ;; The colspan may be wrong, so
2211 ;; truncate it to the length of the
2212 ;; remaining columns.
2213 (- (length widths
) i
)))
2214 (dotimes (j (1- colspan
))
2216 (if (> (+ i
1 j
) (1- (length widths
)))
2217 ;; If we have a colspan spec that's longer
2218 ;; than the table is wide, just use the last
2219 ;; width as the width.
2220 (aref widths
(1- (length widths
)))
2221 ;; Sum up the widths of the columns we're
2224 shr-table-separator-length
2225 (aref widths
(+ i
1 j
))))))
2226 (setq width-column
(+ width-column
(1- colspan
))
2227 colspan-count colspan
2228 colspan-remaining colspan
))
2230 (let ((data (shr-render-td column width fill
)))
2232 (> colspan-remaining
0))
2234 (setq colspan-width
(car data
))
2235 (let ((this-width (/ colspan-width colspan-count
)))
2236 (push (cons this-width
(cadr data
)) tds
)
2237 (setq colspan-remaining
(1- colspan-remaining
))))
2239 (push (cons (car data
) (cadr data
)) tds
)
2243 (dotimes (_ (1- colspan
))
2247 (list 0 0 -
1 nil
1 nil nil
)
2251 width-column
(1+ width-column
))))
2252 (push (nreverse tds
) trs
))))
2255 (defun shr-pixel-buffer-width ()
2256 (if (not shr-use-fonts
)
2258 (goto-char (point-min))
2262 (setq max
(max max
(current-column)))
2265 (if (get-buffer-window)
2266 (car (window-text-pixel-size nil
(point-min) (point-max)))
2267 (save-window-excursion
2268 ;; Avoid errors if the selected window is a dedicated one,
2269 ;; and they just want to insert a document into it.
2270 (set-window-dedicated-p nil nil
)
2271 (set-window-buffer nil
(current-buffer))
2272 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
2274 (defun shr-render-td (dom width fill
)
2275 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
2276 (or (dom-attr dom cache
)
2279 (dolist (attr (dom-attributes dom
))
2280 (let ((name (symbol-name (car attr
))))
2281 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
2282 (let ((cache-width (string-to-number
2283 (match-string 1 name
))))
2284 (when (and (>= cache-width width
)
2285 (<= (car (cdr attr
)) width
))
2286 (setq result
(cdr attr
)))))))
2288 (let ((result (shr-render-td-1 dom width fill
)))
2289 (dom-set-attribute dom cache result
)
2292 (defun shr-render-td-1 (dom width fill
)
2294 (let ((bgcolor (dom-attr dom
'bgcolor
))
2295 (fgcolor (dom-attr dom
'fgcolor
))
2296 (style (dom-attr dom
'style
))
2297 (shr-stylesheet shr-stylesheet
)
2301 (setq style
(and (string-match "color" style
)
2302 (shr-parse-style style
))))
2304 (setq style
(nconc (list (cons 'background-color bgcolor
))
2307 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
2309 (setq shr-stylesheet
(append style shr-stylesheet
)))
2310 (let ((shr-internal-width width
)
2311 (shr-indentation 0))
2313 (save-window-excursion
2314 ;; Avoid errors if the selected window is a dedicated one,
2315 ;; and they just want to insert a document into it.
2316 (set-window-dedicated-p nil nil
)
2317 (set-window-buffer nil
(current-buffer))
2320 (or (dom-attr dom
'shr-td-cache-natural
)
2321 (let ((natural (max (shr-pixel-buffer-width)
2322 (shr-dom-max-natural-width dom
0))))
2323 (dom-set-attribute dom
'shr-td-cache-natural natural
)
2325 (if (and natural-width
2326 (<= natural-width width
))
2327 (setq max-width natural-width
)
2328 (let ((shr-internal-width width
))
2329 (shr-fill-lines (point-min) (point-max))
2330 (setq max-width
(shr-pixel-buffer-width)))))
2331 (goto-char (point-max))
2332 ;; Delete padding at the bottom of the TDs.
2336 (skip-chars-backward " \t\n")
2339 (goto-char (point-min))
2342 (count-lines (point-min) (point-max))
2343 (split-string (buffer-string) "\n")
2344 (if (dom-attr dom
'colspan
)
2345 (string-to-number (dom-attr dom
'colspan
))
2347 (cdr (assq 'color shr-stylesheet
))
2348 (cdr (assq 'background-color shr-stylesheet
))))))
2350 (defun shr-dom-max-natural-width (dom max
)
2351 (if (eq (dom-tag dom
) 'table
)
2354 for line in
(dom-attr dom
'shr-suggested-widths
)
2356 shr-table-separator-length
2357 (cl-loop for elem in line
2360 (* 2 shr-table-separator-length
)))))
2362 (dolist (child (dom-children dom
))
2363 (unless (stringp child
)
2364 (setq max
(max (shr-dom-max-natural-width child max
)))))
2367 (defun shr-buffer-width ()
2368 (goto-char (point-min))
2372 (setq max
(max max
(current-column)))
2376 (defun shr-pro-rate-columns (columns)
2377 (let ((total-percentage 0)
2378 (widths (make-vector (length columns
) 0)))
2379 (dotimes (i (length columns
))
2380 (setq total-percentage
(+ total-percentage
(aref columns i
))))
2381 (setq total-percentage
(/ 1.0 total-percentage
))
2382 (dotimes (i (length columns
))
2383 (aset widths i
(max (truncate (* (aref columns i
)
2385 (- shr-internal-width
2386 (* (1+ (length columns
))
2387 shr-table-separator-pixel-width
))))
2391 ;; Return a summary of the number and shape of the TDs in the table.
2392 (defun shr-column-specs (dom)
2393 (let ((columns (make-vector (shr-max-columns dom
) 1)))
2394 (dolist (row (dom-non-text-children dom
))
2395 (when (eq (dom-tag row
) 'tr
)
2397 (dolist (column (dom-non-text-children row
))
2398 (when (memq (dom-tag column
) '(td th
))
2399 (let ((width (dom-attr column
'width
)))
2401 (string-match "\\([0-9]+\\)%" width
)
2402 (not (zerop (setq width
(string-to-number
2403 (match-string 1 width
))))))
2404 (aset columns i
(/ width
100.0))))
2405 (setq i
(1+ i
)))))))
2408 (defun shr-count (dom elem
)
2410 (dolist (sub (dom-children dom
))
2411 (when (and (not (stringp sub
))
2412 (eq (dom-tag sub
) elem
))
2416 (defun shr-max-columns (dom)
2418 (dolist (row (dom-children dom
))
2419 (when (and (not (stringp row
))
2420 (eq (dom-tag row
) 'tr
))
2421 (setq max
(max max
(+ (shr-count row
'td
)
2422 (shr-count row
'th
))))))
2427 ;;; shr.el ends here