1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package takes a HTML parse tree (as provided by
26 ;; libxml-parse-html-region) and renders it in the current buffer. It
27 ;; does not do CSS, JavaScript or anything advanced: It's geared
28 ;; towards rendering typical short snippets of HTML, like what you'd
29 ;; find in HTML email and the like.
33 (eval-when-compile (require 'cl
))
34 (eval-when-compile (require 'url
)) ;For url-filename's setf handler.
41 "Simple HTML Renderer"
45 (defcustom shr-max-image-proportion
0.9
46 "How big pictures displayed are in relation to the window they're in.
47 A value of 0.7 means that they are allowed to take up 70% of the
48 width and height of the window. If they are larger than this,
49 and Emacs supports it, then the images will be rescaled down to
55 (defcustom shr-blocked-images nil
56 "Images that have URLs matching this regexp will be blocked."
59 :type
'(choice (const nil
) regexp
))
61 (defcustom shr-use-fonts t
62 "If non-nil, use proportional fonts for text."
67 (defcustom shr-table-horizontal-line nil
68 "Character used to draw horizontal table lines.
69 If nil, don't draw horizontal table lines."
71 :type
'(choice (const nil
) character
))
73 (defcustom shr-table-vertical-line ?\s
74 "Character used to draw vertical table lines."
78 (defcustom shr-table-corner ?\s
79 "Character used to draw table corners."
83 (defcustom shr-hr-line ?-
84 "Character used to draw hr lines."
88 (defcustom shr-width nil
89 "Frame width to use for rendering.
90 May either be an integer specifying a fixed width in characters,
91 or nil, meaning that the full width of the window should be
94 :type
'(choice (integer :tag
"Fixed width in characters")
95 (const :tag
"Use the width of the window" nil
))
98 (defcustom shr-bullet
"* "
99 "Bullet used for unordered lists.
100 Alternative suggestions are:
107 (defcustom shr-external-browser
'browse-url-default-browser
108 "Function used to launch an external browser."
113 (defcustom shr-image-animate t
114 "Non nil means that images that can be animated will be."
119 (defvar shr-content-function nil
120 "If bound, this should be a function that will return the content.
121 This is used for cid: URLs, and the function is called with the
122 cid: URL as the argument.")
124 (defvar shr-put-image-function
'shr-put-image
125 "Function called to put image and alt string.")
127 (defface shr-strike-through
'((t (:strike-through t
)))
128 "Font for <s> elements."
132 '((t (:inherit link
)))
133 "Font for link elements."
136 (defvar shr-inhibit-images nil
137 "If non-nil, inhibit loading images.")
139 ;;; Internal variables.
141 (defvar shr-folding-mode nil
)
142 (defvar shr-start nil
)
143 (defvar shr-indentation
0)
144 (defvar shr-internal-width nil
)
145 (defvar shr-list-mode nil
)
146 (defvar shr-content-cache nil
)
147 (defvar shr-kinsoku-shorten nil
)
148 (defvar shr-table-depth
0)
149 (defvar shr-stylesheet nil
)
150 (defvar shr-base nil
)
152 (defvar shr-warning nil
)
153 (defvar shr-ignore-cache nil
)
154 (defvar shr-external-rendering-functions nil
)
155 (defvar shr-target-id nil
)
156 (defvar shr-table-separator-length
1)
157 (defvar shr-table-separator-pixel-width
0)
158 (defvar shr-table-id nil
)
159 (defvar shr-current-font nil
)
160 (defvar shr-internal-bullet nil
)
163 (let ((map (make-sparse-keymap)))
164 (define-key map
"a" 'shr-show-alt-text
)
165 (define-key map
"i" 'shr-browse-image
)
166 (define-key map
"z" 'shr-zoom-image
)
167 (define-key map
[?
\t] 'shr-next-link
)
168 (define-key map
[?\M-
\t] 'shr-previous-link
)
169 (define-key map
[follow-link
] 'mouse-face
)
170 (define-key map
[mouse-2
] 'shr-browse-url
)
171 (define-key map
"I" 'shr-insert-image
)
172 (define-key map
"w" 'shr-copy-url
)
173 (define-key map
"u" 'shr-copy-url
)
174 (define-key map
"v" 'shr-browse-url
)
175 (define-key map
"o" 'shr-save-contents
)
176 (define-key map
"\r" 'shr-browse-url
)
179 ;; Public functions and commands.
180 (declare-function libxml-parse-html-region
"xml.c"
181 (start end
&optional base-url discard-comments
))
183 (defun shr-render-buffer (buffer)
184 "Display the HTML rendering of the current buffer."
185 (interactive (list (current-buffer)))
186 (or (fboundp 'libxml-parse-html-region
)
187 (error "This function requires Emacs to be compiled with libxml2"))
188 (pop-to-buffer "*html*")
191 (with-current-buffer buffer
192 (libxml-parse-html-region (point-min) (point-max))))
193 (goto-char (point-min)))
196 (defun shr-render-region (begin end
&optional buffer
)
197 "Display the HTML rendering of the region between BEGIN and END."
199 (unless (fboundp 'libxml-parse-html-region
)
200 (error "This function requires Emacs to be compiled with libxml2"))
201 (with-current-buffer (or buffer
(current-buffer))
202 (let ((dom (libxml-parse-html-region begin end
)))
203 (delete-region begin end
)
205 (shr-insert-document dom
))))
207 (defun shr--have-one-fringe-p ()
208 "Return non-nil if we know at least one of the fringes has non-zero width."
209 (and (fboundp 'fringe-columns
)
210 (or (not (zerop (fringe-columns 'right
)))
211 (not (zerop (fringe-columns 'left
))))))
214 (defun shr-insert-document (dom)
215 "Render the parsed document DOM into the current buffer.
216 DOM should be a parse tree as generated by
217 `libxml-parse-html-region' or similar."
218 (setq shr-content-cache nil
)
219 (let ((start (point))
225 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
226 (shr-internal-bullet (cons shr-bullet
227 (shr-string-pixel-width shr-bullet
)))
228 (shr-internal-width (or (and shr-width
229 (if (not shr-use-fonts
)
231 (* shr-width
(frame-char-width))))
232 ;; We need to adjust the available
233 ;; width for when the user disables
234 ;; the fringes, which will cause the
235 ;; display engine usurp one column for
236 ;; the continuation glyph.
237 (if (not shr-use-fonts
)
238 (- (window-body-width) 1
239 (if (and (null shr-width
)
240 (not (shr--have-one-fringe-p)))
243 (- (window-body-width nil t
)
244 (* 2 (frame-char-width))
245 (if (and (null shr-width
)
246 (not (shr--have-one-fringe-p)))
247 (* (frame-char-width) 2)
249 bidi-display-reordering
)
250 ;; If the window was hscrolled for some reason, shr-fill-lines
251 ;; below will misbehave, because it silently assumes that it
252 ;; starts with a non-hscrolled window (vertical-motion will move
253 ;; to a wrong place otherwise).
254 (set-window-hscroll nil
0)
256 (shr-fill-lines start
(point))
257 (shr-remove-trailing-whitespace start
(point))
259 (message "%s" shr-warning
))))
261 (defun shr-remove-trailing-whitespace (start end
)
262 (let ((width (window-width)))
264 (narrow-to-region start end
)
268 (when (> (shr-previous-newline-padding-width (current-column)) width
)
269 (dolist (overlay (overlays-at (point)))
270 (when (overlay-get overlay
'before-string
)
271 (overlay-put overlay
'before-string nil
))))
274 (defun shr-copy-url (&optional image-url
)
275 "Copy the URL under point to the kill ring.
276 If IMAGE-URL (the prefix) is non-nil, or there is no link under
277 point, but there is an image under point then copy the URL of the
278 image under point instead.
279 If called twice, then try to fetch the URL and see whether it
280 redirects somewhere else."
282 (let ((url (or (get-text-property (point) 'shr-url
)
283 (get-text-property (point) 'image-url
))))
286 (message "No URL under point"))
287 ;; Resolve redirected URLs.
288 ((equal url
(car kill-ring
))
293 (eq (car a
) :redirect
))
296 (goto-char (point-min))
297 ;; Remove common tracking junk from the URL.
298 (when (re-search-forward ".utm_.*" nil t
)
299 (replace-match "" t t
))
300 (message "Copied %s" (buffer-string))
301 (copy-region-as-kill (point-min) (point-max)))))
303 ;; Copy the URL to the kill ring.
306 (insert (url-encode-url url
))
307 (copy-region-as-kill (point-min) (point-max))
308 (message "Copied %s" (buffer-string)))))))
310 (defun shr-next-link ()
311 "Skip to the next link."
313 (let ((current (get-text-property (point) 'shr-url
))
316 (while (and (not (eobp))
317 (equal (get-text-property (point) 'shr-url
) current
))
321 (get-text-property (point) 'shr-url
))
322 ;; The next link is adjacent.
323 (message "%s" (get-text-property (point) 'help-echo
)))
325 (not (setq skip
(text-property-not-all (point) (point-max)
328 (message "No next link"))
331 (message "%s" (get-text-property (point) 'help-echo
))))))
333 (defun shr-previous-link ()
334 "Skip to the previous link."
336 (let ((start (point))
338 ;; Skip past the current link.
339 (while (and (not (bobp))
340 (get-text-property (point) 'help-echo
))
342 ;; Find the previous link.
343 (while (and (not (bobp))
344 (not (setq found
(get-text-property (point) 'help-echo
))))
348 (message "No previous link")
350 ;; Put point at the start of the link.
351 (while (and (not (bobp))
352 (get-text-property (point) 'help-echo
))
355 (message "%s" (get-text-property (point) 'help-echo
)))))
357 (defun shr-show-alt-text ()
358 "Show the ALT text of the image under point."
360 (let ((text (get-text-property (point) 'shr-alt
)))
362 (message "No image under point")
363 (message "%s" (shr-fill-text text
)))))
365 (defun shr-browse-image (&optional copy-url
)
366 "Browse the image under point.
367 If COPY-URL (the prefix if called interactively) is non-nil, copy
368 the URL of the image to the kill buffer instead."
370 (let ((url (get-text-property (point) 'image-url
)))
373 (message "No image under point"))
377 (copy-region-as-kill (point-min) (point-max))
378 (message "Copied %s" url
)))
380 (message "Browsing %s..." url
)
383 (defun shr-insert-image ()
384 "Insert the image under point into the buffer."
386 (let ((url (get-text-property (point) 'image-url
)))
388 (message "No image under point")
389 (message "Inserting %s..." url
)
390 (url-retrieve url
'shr-image-fetched
391 (list (current-buffer) (1- (point)) (point-marker))
394 (defun shr-zoom-image ()
395 "Toggle the image size.
396 The size will be rotated between the default size, the original
397 size, and full-buffer size."
399 (let ((url (get-text-property (point) 'image-url
))
400 (size (get-text-property (point) 'image-size
))
401 (buffer-read-only nil
))
403 (message "No image under point")
404 ;; Delete the old picture.
405 (while (get-text-property (point) 'image-url
)
408 (let ((start (point)))
409 (while (get-text-property (point) 'image-url
)
412 (put-text-property start
(point) 'display nil
)
413 (when (> (- (point) start
) 2)
414 (delete-region start
(1- (point)))))
415 (message "Inserting %s..." url
)
416 (url-retrieve url
'shr-image-fetched
417 (list (current-buffer) (1- (point)) (point-marker)
419 (cond ((or (eq size
'default
)
428 ;;; Utility functions.
430 (defsubst shr-generic
(dom)
431 (dolist (sub (dom-children dom
))
436 (defun shr-descend (dom)
439 ;; Allow other packages to override (or provide) rendering
441 (cdr (assq (dom-tag dom
) shr-external-rendering-functions
))
442 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
)))
443 (style (dom-attr dom
'style
))
444 (shr-stylesheet shr-stylesheet
)
445 (shr-depth (1+ shr-depth
))
447 ;; shr uses many frames per nested node.
448 (if (> shr-depth
(/ max-specpdl-size
15))
449 (setq shr-warning
"Too deeply nested to render properly; consider increasing `max-specpdl-size'")
451 (if (string-match "color\\|display\\|border-collapse" style
)
452 (setq shr-stylesheet
(nconc (shr-parse-style style
)
455 ;; If we have a display:none, then just ignore this part of the DOM.
456 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
457 (if (fboundp function
)
458 (funcall function dom
)
460 (when (and shr-target-id
461 (equal (dom-attr dom
'id
) shr-target-id
))
462 ;; If the element was empty, we don't have anything to put the
463 ;; anchor on. So just insert a dummy character.
464 (when (= start
(point))
466 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
467 ;; If style is set, then this node has set the color.
471 (cdr (assq 'color shr-stylesheet
))
472 (cdr (assq 'background-color shr-stylesheet
))))))))
474 (defun shr-fill-text (text)
475 (if (zerop (length text
))
478 (let ((shr-indentation 0)
480 (shr-internal-width (- (window-body-width nil t
)
481 (* 2 (frame-char-width))
482 ;; Adjust the window width for when
483 ;; the user disables the fringes,
484 ;; which causes the display engine
485 ;; to usurp one column for the
486 ;; continuation glyph.
487 (if (and (null shr-width
)
488 (not (shr--have-one-fringe-p)))
489 (* (frame-char-width) 2)
494 (define-inline shr-char-breakable-p
(char)
495 "Return non-nil if a line can be broken before and after CHAR."
496 (inline-quote (aref fill-find-break-point-function-table
,char
)))
497 (define-inline shr-char-nospace-p
(char)
498 "Return non-nil if no space is required before and after CHAR."
499 (inline-quote (aref fill-nospace-between-words-table
,char
)))
501 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
502 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
503 ;; parentheses, and so on, that should not be placed in the beginning
504 ;; of a line or the end of a line.
505 (define-inline shr-char-kinsoku-bol-p
(char)
506 "Return non-nil if a line ought not to begin with CHAR."
507 (inline-letevals (char)
508 (inline-quote (and (not (eq ,char ?
'))
509 (aref (char-category-set ,char
) ?
>)))))
510 (define-inline shr-char-kinsoku-eol-p
(char)
511 "Return non-nil if a line ought not to end with CHAR."
512 (inline-quote (aref (char-category-set ,char
) ?
<)))
513 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
514 (load "kinsoku" nil t
))
516 (defun shr-pixel-column ()
517 (if (not shr-use-fonts
)
519 (if (not (get-buffer-window (current-buffer)))
520 (save-window-excursion
521 (set-window-buffer nil
(current-buffer))
522 (car (window-text-pixel-size nil
(line-beginning-position) (point))))
523 (car (window-text-pixel-size nil
(line-beginning-position) (point))))))
525 (defun shr-pixel-region ()
526 (- (shr-pixel-column)
529 (shr-pixel-column))))
531 (defun shr-string-pixel-width (string)
532 (if (not shr-use-fonts
)
536 (shr-pixel-column))))
538 (defun shr-insert (text)
539 (when (and (not (bolp))
540 (get-text-property (1- (point)) 'image-url
))
543 ((eq shr-folding-mode
'none
)
544 (let ((start (point)))
547 (narrow-to-region start
(point))
548 ;; Remove soft hyphens.
549 (goto-char (point-min))
550 (while (search-forward "Â" nil t
)
551 (replace-match "" t t
))
552 (goto-char (point-max)))))
554 (let ((font-start (point)))
555 (when (and (string-match "\\`[ \t\n\r ]" text
)
557 (not (eq (char-after (1- (point))) ?
)))
559 (let ((start (point))
563 (narrow-to-region start
(point))
565 (when (looking-at "[ \t\n\r ]+")
566 (replace-match "" t t
))
567 (while (re-search-forward "[ \t\n\r ]+" nil t
)
568 (replace-match " " t t
))
569 ;; Remove soft hyphens.
570 (goto-char (point-min))
571 (while (search-forward "Â" nil t
)
572 (replace-match "" t t
))
573 (goto-char (point-max)))
574 ;; We may have removed everything we inserted if if was just
576 (unless (= font-start
(point))
577 ;; Mark all lines that should possibly be folded afterwards.
579 (shr-mark-fill start
))
581 (put-text-property font-start
(point)
583 (or shr-current-font
'variable-pitch
)))))))))
585 (defun shr-fill-lines (start end
)
586 (if (<= shr-internal-width
0)
589 (narrow-to-region start end
)
591 (when (get-text-property (point) 'shr-indentation
)
593 (while (setq start
(next-single-property-change start
'shr-indentation
))
597 (goto-char (point-max)))))
599 (defun shr-vertical-motion (column)
600 (if (not shr-use-fonts
)
601 (move-to-column column
)
604 (vertical-motion (cons (/ column
(frame-char-width)) 0))
608 (defun shr-fill-line ()
609 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
610 (continuation (get-text-property
611 (point) 'shr-continuation-indentation
))
613 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
614 (let ((face (get-text-property (point) 'face
))
615 (background-start (point)))
618 (put-text-property background-start
(point) 'face
619 `,(shr-face-background face
))))
621 (setq shr-indentation
(or continuation shr-indentation
))
622 (shr-vertical-motion shr-internal-width
)
623 (when (looking-at " $")
624 (delete-region (point) (line-end-position)))
626 ;; We have to do some folding. First find the first
627 ;; previous point suitable for folding.
628 (if (or (not (shr-find-fill-point (line-beginning-position)))
630 ;; We had unbreakable text (for this width), so just go to
631 ;; the first space and carry on.
634 (skip-chars-forward " ")
635 (search-forward " " (line-end-position) 'move
)))
636 ;; Success; continue.
637 (when (= (preceding-char) ?\s
)
639 (let ((face (get-text-property (point) 'face
))
640 (background-start (point)))
644 (put-text-property background-start
(point) 'face
645 `,(shr-face-background face
))))
647 (shr-vertical-motion shr-internal-width
)
648 (when (looking-at " $")
649 (delete-region (point) (line-end-position))))))
651 (defun shr-find-fill-point (start)
655 (while (not (or (setq failed
(<= (point) start
))
656 (eq (preceding-char) ?
)
657 (eq (following-char) ?
)
658 (shr-char-breakable-p (preceding-char))
659 (shr-char-breakable-p (following-char))
660 (and (shr-char-kinsoku-bol-p (preceding-char))
661 (shr-char-breakable-p (following-char))
662 (not (shr-char-kinsoku-bol-p (following-char))))
663 (shr-char-kinsoku-eol-p (following-char))
667 ;; There's no breakable point, so we give it up.
670 ;; Don't overflow the window edge, even if
671 ;; shr-kinsoku-shorten is nil.
672 (unless (or shr-kinsoku-shorten
(null shr-width
))
673 (while (setq found
(re-search-forward
674 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
675 (line-end-position) 'move
)))
677 (not (match-beginning 1)))
678 (goto-char (match-beginning 0)))))
681 ;; Don't put kinsoku-bol characters at the beginning of a line,
682 ;; or kinsoku-eol characters at the end of a line.
684 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
686 ((or shr-kinsoku-shorten
(null shr-width
))
687 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
688 (or (shr-char-kinsoku-eol-p (preceding-char))
689 (shr-char-kinsoku-bol-p (following-char))))
691 (when (setq failed
(<= (point) start
))
692 ;; There's no breakable point that doesn't violate kinsoku,
693 ;; so we look for the second best position.
699 (shr-char-kinsoku-eol-p (following-char)))))
701 ((shr-char-kinsoku-eol-p (preceding-char))
702 ;; Find backward the point where kinsoku-eol characters begin.
707 (and (> (setq count
(1- count
)) 0)
708 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
709 (or (shr-char-kinsoku-eol-p (preceding-char))
710 (shr-char-kinsoku-bol-p (following-char)))))))
711 (when (setq failed
(<= (point) start
))
712 ;; There's no breakable point that doesn't violate kinsoku,
713 ;; so we go to the second best position.
714 (if (looking-at "\\(\\c<+\\)\\c<")
715 (goto-char (match-end 1))
717 ((shr-char-kinsoku-bol-p (following-char))
718 ;; Find forward the point where kinsoku-bol characters end.
722 (and (>= (setq count
(1- count
)) 0)
723 (shr-char-kinsoku-bol-p (following-char))
724 (shr-char-breakable-p (following-char))))))))
725 (when (eq (following-char) ?
)
729 (defun shr-parse-base (url)
730 ;; Always chop off anchors.
731 (when (string-match "#.*" url
)
732 (setq url
(substring url
0 (match-beginning 0))))
733 ;; NB: <base href="" > URI may itself be relative to the document s URI
734 (setq url
(shr-expand-url url
))
735 (let* ((parsed (url-generic-parse-url url
))
736 (local (url-filename parsed
)))
737 (setf (url-filename parsed
) "")
738 ;; Chop off the bit after the last slash.
739 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
740 (setq local
(match-string 1 local
)))
741 ;; Always make the local bit end with a slash.
742 (when (and (not (zerop (length local
)))
743 (not (eq (aref local
(1- (length local
))) ?
/)))
744 (setq local
(concat local
"/")))
745 (list (url-recreate-url parsed
)
750 (autoload 'url-expand-file-name
"url-expand")
752 ;; FIXME This needs some tests writing.
753 ;; Does it even need to exist, given that url-expand-file-name does?
754 (defun shr-expand-url (url &optional base
)
757 ;; shr-parse-base should never call this with non-nil base!
758 (shr-parse-base base
)
759 ;; Bound by the parser.
761 (when (zerop (length url
))
763 ;; Strip leading whitespace
764 (and url
(string-match "\\`\\s-+" url
)
765 (setq url
(substring url
(match-end 0))))
768 (string-match "\\`[a-z]*:" url
))
769 ;; Absolute or empty URI
770 (or url
(nth 3 base
)))
771 ((eq (aref url
0) ?
/)
772 (if (and (> (length url
) 1)
773 (eq (aref url
1) ?
/))
774 ;; //host...; just use the protocol
775 (concat (nth 2 base
) ":" url
)
776 ;; Just use the host name part.
777 (concat (car base
) url
)))
778 ((eq (aref url
0) ?
#)
779 ;; A link to an anchor.
780 (concat (nth 3 base
) url
))
783 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
785 (defun shr-ensure-newline ()
786 (unless (zerop (current-column))
789 (defun shr-ensure-paragraph ()
791 (let ((prefix (get-text-property (line-beginning-position)
792 'shr-prefix-length
)))
798 ;; We're already at a new paragraph; do nothing.
801 (= prefix
(- (point) (line-beginning-position))))
802 ;; Do nothing; we're at the start of a <li>.
806 ;; If the current line is totally blank, and doesn't even
807 ;; have any face properties set, then delete the blank
809 (and (looking-at " *$")
810 (not (get-text-property (point) 'face
))
811 (not (= (next-single-property-change (point) 'face nil
813 (line-end-position)))))
814 (delete-region (match-beginning 0) (match-end 0)))
819 (when (> shr-indentation
0)
821 (if (not shr-use-fonts
)
822 (make-string shr-indentation ?\s
)
825 `(space :width
(,shr-indentation
)))))))
827 (defun shr-fontize-dom (dom &rest types
)
828 (let ((start (point)))
831 (shr-add-font start
(point) type
))))
833 ;; Add face to the region, but avoid putting the font properties on
834 ;; blank text at the start of the line, and the newline at the end, to
836 (defun shr-add-font (start end type
)
839 (while (< (point) end
)
841 (skip-chars-forward " "))
842 (add-face-text-property (point) (min (line-end-position) end
) type t
)
843 (if (< (line-end-position) end
)
847 (defun shr-mouse-browse-url (ev)
848 "Browse the URL under the mouse cursor."
853 (defun shr-browse-url (&optional external mouse-event
)
854 "Browse the URL under point.
855 If EXTERNAL, browse the URL using `shr-external-browser'."
856 (interactive (list current-prefix-arg last-nonmenu-event
))
857 (mouse-set-point mouse-event
)
858 (let ((url (get-text-property (point) 'shr-url
)))
861 (message "No link under point"))
862 ((string-match "^mailto:" url
)
863 (browse-url-mail url
))
866 (funcall shr-external-browser url
)
867 (browse-url url
))))))
869 (defun shr-save-contents (directory)
870 "Save the contents from URL in a file."
871 (interactive "DSave contents of URL to directory: ")
872 (let ((url (get-text-property (point) 'shr-url
)))
874 (message "No link under point")
875 (url-retrieve (shr-encode-url url
)
876 'shr-store-contents
(list url directory
)
879 (defun shr-store-contents (status url directory
)
880 (unless (plist-get status
:error
)
881 (when (or (search-forward "\n\n" nil t
)
882 (search-forward "\r\n\r\n" nil t
))
883 (write-region (point) (point-max)
884 (expand-file-name (file-name-nondirectory url
)
887 (defun shr-image-fetched (status buffer start end
&optional flags
)
888 (let ((image-buffer (current-buffer)))
889 (when (and (buffer-name buffer
)
890 (not (plist-get status
:error
)))
891 (url-store-in-cache image-buffer
)
892 (when (or (search-forward "\n\n" nil t
)
893 (search-forward "\r\n\r\n" nil t
))
894 (let ((data (shr-parse-image-data)))
895 (with-current-buffer buffer
897 (let ((alt (buffer-substring start end
))
898 (properties (text-properties-at start
))
899 (inhibit-read-only t
))
900 (delete-region start end
)
902 (funcall shr-put-image-function data alt flags
)
904 (let ((type (pop properties
))
905 (value (pop properties
)))
906 (unless (memq type
'(display image-size
))
907 (put-text-property start
(point) type value
))))))))))
908 (kill-buffer image-buffer
)))
910 (defun shr-image-from-data (data)
911 "Return an image from the data: URI content DATA."
913 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
915 (let ((param (match-string 4 data
))
916 (payload (url-unhex-string (match-string 5 data
))))
917 (when (string-match "^.*\\(;[ \t]*base64\\)$" param
)
918 (setq payload
(base64-decode-string payload
)))
921 ;; Behind display-graphic-p test.
922 (declare-function image-size
"image.c" (spec &optional pixels frame
))
923 (declare-function image-animate
"image" (image &optional index limit
))
925 (defun shr-put-image (spec alt
&optional flags
)
926 "Insert image SPEC with a string ALT. Return image.
927 SPEC is either an image data blob, or a list where the first
928 element is the data blob and the second element is the content-type."
929 (if (display-graphic-p)
930 (let* ((size (cdr (assq 'size flags
)))
931 (data (if (consp spec
)
934 (content-type (and (consp spec
)
939 (create-image data nil t
:ascent
100
940 :format content-type
))
941 ((eq content-type
'image
/svg
+xml
)
942 (create-image data
'svg t
:ascent
100))
945 (shr-rescale-image data content-type
)))
948 (shr-rescale-image data content-type
))))))
950 ;; When inserting big-ish pictures, put them at the
951 ;; beginning of the line.
952 (when (and (> (current-column) 0)
953 (> (car (image-size image t
)) 400))
955 (if (eq size
'original
)
956 (insert-sliced-image image
(or alt
"*") nil
20 1)
957 (insert-image image
(or alt
"*")))
958 (put-text-property start
(point) 'image-size size
)
959 (when (and shr-image-animate
960 (cond ((fboundp 'image-multi-frame-p
)
961 ;; Only animate multi-frame things that specify a
962 ;; delay; eg animated gifs as opposed to
963 ;; multi-page tiffs. FIXME?
964 (cdr (image-multi-frame-p image
)))
965 ((fboundp 'image-animated-p
)
966 (image-animated-p image
))))
967 (image-animate image nil
60)))
969 (insert (or alt
""))))
971 (defun shr-rescale-image (data &optional content-type
)
972 "Rescale DATA, if too big, to fit the current buffer."
973 (if (not (and (fboundp 'imagemagick-types
)
974 (get-buffer-window (current-buffer))))
975 (create-image data nil t
:ascent
100)
976 (let ((edges (window-inside-pixel-edges
977 (get-buffer-window (current-buffer)))))
981 :max-width
(truncate (* shr-max-image-proportion
982 (- (nth 2 edges
) (nth 0 edges
))))
983 :max-height
(truncate (* shr-max-image-proportion
984 (- (nth 3 edges
) (nth 1 edges
))))
985 :format content-type
))))
987 ;; url-cache-extract autoloads url-cache.
988 (declare-function url-cache-create-filename
"url-cache" (url))
989 (autoload 'mm-disable-multibyte
"mm-util")
990 (autoload 'browse-url-mail
"browse-url")
992 (defun shr-get-image-data (url)
993 "Get image data for URL.
994 Return a string with image data."
996 (mm-disable-multibyte)
998 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
1000 (when (or (search-forward "\n\n" nil t
)
1001 (search-forward "\r\n\r\n" nil t
))
1002 (shr-parse-image-data)))))
1004 (declare-function libxml-parse-xml-region
"xml.c"
1005 (start end
&optional base-url discard-comments
))
1007 (defun shr-parse-image-data ()
1008 (let ((data (buffer-substring (point) (point-max)))
1012 (narrow-to-region (point-min) (point))
1013 (let ((content-type (mail-fetch-field "content-type")))
1015 ;; Remove any comments in the type string.
1016 (intern (replace-regexp-in-string ";.*" "" content-type
)
1018 ;; SVG images may contain references to further images that we may
1019 ;; want to block. So special-case these by parsing the XML data
1020 ;; and remove the blocked bits.
1021 (when (eq content-type
'image
/svg
+xml
)
1024 (libxml-parse-xml-region (point) (point-max)))))
1025 (list data content-type
)))
1027 (defun shr-image-displayer (content-function)
1028 "Return a function to display an image.
1029 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1030 is an argument. The function to be returned takes three arguments URL,
1031 START, and END. Note that START and END should be markers."
1032 `(lambda (url start end
)
1034 (if (string-match "\\`cid:" url
)
1035 ,(when content-function
1036 `(let ((image (funcall ,content-function
1037 (substring url
(match-end 0)))))
1040 (funcall shr-put-image-function
1041 image
(buffer-substring start end
))
1042 (delete-region (point) end
))))
1043 (url-retrieve url
'shr-image-fetched
1044 (list (current-buffer) start end
)
1047 (defun shr-heading (dom &rest types
)
1048 (shr-ensure-paragraph)
1049 (apply #'shr-fontize-dom dom types
)
1050 (shr-ensure-paragraph))
1052 (defun shr-urlify (start url
&optional title
)
1053 (shr-add-font start
(point) 'shr-link
)
1054 (add-text-properties
1057 'help-echo
(let ((iri (or (ignore-errors
1058 (decode-coding-string
1059 (url-unhex-string url
)
1062 (if title
(format "%s (%s)" iri title
) iri
))
1064 'mouse-face
'highlight
1067 (defun shr-encode-url (url)
1069 (browse-url-url-encode-chars url
"[)$ ]"))
1071 (autoload 'shr-color-visible
"shr-color")
1072 (autoload 'shr-color-
>hexadecimal
"shr-color")
1074 (defun shr-color-check (fg bg
)
1075 "Check that FG is visible on BG.
1076 Returns (fg bg) with corrected values.
1077 Returns nil if the colors that would be used are the default
1078 ones, in case fg and bg are nil."
1080 (let ((fixed (cond ((null fg
) 'fg
)
1082 ;; Convert colors to hexadecimal, or set them to default.
1083 (let ((fg (or (shr-color->hexadecimal fg
)
1084 (frame-parameter nil
'foreground-color
)))
1085 (bg (or (shr-color->hexadecimal bg
)
1086 (frame-parameter nil
'background-color
))))
1087 (cond ((eq fixed
'bg
)
1088 ;; Only return the new fg
1089 (list nil
(cadr (shr-color-visible bg fg t
))))
1091 ;; Invert args and results and return only the new bg
1092 (list (cadr (shr-color-visible fg bg t
)) nil
))
1094 (shr-color-visible bg fg
)))))))
1096 (defun shr-colorize-region (start end fg
&optional bg
)
1097 (when (and (or fg bg
) (>= (display-color-cells) 88))
1098 (let ((new-colors (shr-color-check fg bg
)))
1101 (add-face-text-property start end
1102 (list :foreground
(cadr new-colors
))
1105 (add-face-text-property start end
1106 (list :background
(car new-colors
))
1110 (defun shr-previous-newline-padding-width (width)
1111 (let ((overlays (overlays-at (point)))
1115 (dolist (overlay overlays
)
1116 (setq previous-width
1118 (length (plist-get (overlay-properties overlay
)
1120 (+ width previous-width
))))
1122 ;;; Tag-specific rendering rules.
1124 (defun shr-tag-html (dom)
1125 (let ((dir (dom-attr dom
'dir
)))
1128 (setq bidi-paragraph-direction
'left-to-right
))
1130 (setq bidi-paragraph-direction
'right-to-left
))))
1133 (defun shr-tag-body (dom)
1134 (let* ((start (point))
1135 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1136 (bgcolor (dom-attr dom
'bgcolor
))
1137 (shr-stylesheet (list (cons 'color fgcolor
)
1138 (cons 'background-color bgcolor
))))
1140 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1142 (defun shr-tag-style (_dom)
1145 (defun shr-tag-script (_dom)
1148 (defun shr-tag-comment (_dom)
1151 (defun shr-dom-to-xml (dom)
1156 (defun shr-dom-print (dom)
1157 "Convert DOM into a string containing the xml representation."
1158 (insert (format "<%s" (dom-tag dom
)))
1159 (dolist (attr (dom-attributes dom
))
1160 ;; Ignore attributes that start with a colon because they are
1161 ;; private elements.
1162 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1163 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1166 (dolist (elem (dom-children dom
))
1170 ((eq (dom-tag elem
) 'comment
)
1172 ((or (not (eq (dom-tag elem
) 'image
))
1173 ;; Filter out blocked elements inside the SVG image.
1174 (not (setq url
(dom-attr elem
':xlink
:href
)))
1175 (not shr-blocked-images
)
1176 (not (string-match shr-blocked-images url
)))
1178 (shr-dom-print elem
)))))
1179 (insert (format "</%s>" (dom-tag dom
))))
1181 (defun shr-tag-svg (dom)
1182 (when (and (image-type-available-p 'svg
)
1183 (not shr-inhibit-images
)
1184 (dom-attr dom
'width
)
1185 (dom-attr dom
'height
))
1186 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1189 (defun shr-tag-sup (dom)
1190 (let ((start (point)))
1192 (put-text-property start
(point) 'display
'(raise 0.5))))
1194 (defun shr-tag-sub (dom)
1195 (let ((start (point)))
1197 (put-text-property start
(point) 'display
'(raise -
0.5))))
1199 (defun shr-tag-label (dom)
1201 (shr-ensure-paragraph))
1203 (defun shr-tag-p (dom)
1204 (shr-ensure-paragraph)
1206 (shr-ensure-paragraph))
1208 (defun shr-tag-div (dom)
1209 (shr-ensure-paragraph)
1211 (shr-ensure-newline))
1213 (defun shr-tag-s (dom)
1214 (shr-fontize-dom dom
'shr-strike-through
))
1216 (defun shr-tag-del (dom)
1217 (shr-fontize-dom dom
'shr-strike-through
))
1219 (defun shr-tag-b (dom)
1220 (shr-fontize-dom dom
'bold
))
1222 (defun shr-tag-i (dom)
1223 (shr-fontize-dom dom
'italic
))
1225 (defun shr-tag-em (dom)
1226 (shr-fontize-dom dom
'italic
))
1228 (defun shr-tag-strong (dom)
1229 (shr-fontize-dom dom
'bold
))
1231 (defun shr-tag-u (dom)
1232 (shr-fontize-dom dom
'underline
))
1234 (defun shr-tag-tt (dom)
1235 (let ((shr-current-font 'default
))
1238 (defun shr-parse-style (style)
1241 (when (string-match "\n" style
)
1242 (setq style
(replace-match " " t t style
))))
1244 (dolist (elem (split-string style
";"))
1246 (setq elem
(split-string elem
":"))
1247 (when (and (car elem
)
1249 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1250 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1251 (when (string-match " *!important\\'" value
)
1252 (setq value
(substring value
0 (match-beginning 0))))
1253 (unless (equal value
"inherit")
1254 (push (cons (intern name obarray
)
1259 (defun shr-tag-base (dom)
1260 (when-let (base (dom-attr dom
'href
))
1261 (setq shr-base
(shr-parse-base base
)))
1264 (defun shr-tag-a (dom)
1265 (let ((url (dom-attr dom
'href
))
1266 (title (dom-attr dom
'title
))
1270 (when (and shr-target-id
1271 (equal (dom-attr dom
'name
) shr-target-id
))
1272 ;; We have a zero-length <a name="foo"> element, so just
1273 ;; insert... something.
1274 (when (= start
(point))
1275 (shr-ensure-newline)
1277 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1279 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1281 (defun shr-tag-object (dom)
1282 (unless shr-inhibit-images
1283 (let ((start (point))
1284 url multimedia image
)
1285 (when-let (type (dom-attr dom
'type
))
1286 (when (string-match "\\`image/svg" type
)
1287 (setq url
(dom-attr dom
'data
)
1289 (dolist (child (dom-non-text-children dom
))
1291 ((eq (dom-tag child
) 'embed
)
1292 (setq url
(or url
(dom-attr child
'src
))
1294 ((and (eq (dom-tag child
) 'param
)
1295 (equal (dom-attr child
'name
) "movie"))
1296 (setq url
(or url
(dom-attr child
'value
))
1301 (shr-tag-img dom url
)
1304 (shr-insert " [multimedia] ")
1305 (shr-urlify start
(shr-expand-url url
)))))
1307 (shr-generic dom
)))))
1309 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1315 "Preferences for media types.
1316 The key element should be a regexp matched against the type of the source or
1317 url if no type is specified. The value should be a float in the range 0.0 to
1318 1.0. Media elements with higher value are preferred."
1321 :type
'(alist :key-type regexp
:value-type float
))
1323 (defun shr--get-media-pref (elem)
1324 "Determine the preference for ELEM.
1325 The preference is a float determined from `shr-prefer-media-type'."
1326 (let ((type (dom-attr elem
'type
))
1329 (setq type
(dom-attr elem
'src
)))
1331 (dolist (pref shr-prefer-media-type-alist
)
1334 (string-match-p (car pref
) type
))
1335 (setq p
(cdr pref
)))))
1338 (defun shr--extract-best-source (dom &optional url pref
)
1339 "Extract the best `:src' property from <source> blocks in DOM."
1340 (setq pref
(or pref -
1.0))
1342 (dolist (elem (dom-non-text-children dom
))
1343 (when (and (eq (dom-tag elem
) 'source
)
1346 (shr--get-media-pref elem
))))
1348 url
(dom-attr elem
'src
))
1349 ;; libxml's html parser isn't HTML5 compliant and non terminated
1350 ;; source tags might end up as children. So recursion it is...
1351 (dolist (child (dom-non-text-children elem
))
1352 (when (eq (dom-tag child
) 'source
)
1353 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1354 (when (< pref
(cdr ret
))
1356 pref
(cdr ret
)))))))))
1359 (defun shr-tag-video (dom)
1360 (let ((image (dom-attr dom
'poster
))
1361 (url (dom-attr dom
'src
))
1364 (setq url
(car (shr--extract-best-source dom
))))
1365 (if (> (length image
) 0)
1366 (shr-tag-img nil image
)
1367 (shr-insert " [video] "))
1368 (shr-urlify start
(shr-expand-url url
))))
1370 (defun shr-tag-audio (dom)
1371 (let ((url (dom-attr dom
'src
))
1374 (setq url
(car (shr--extract-best-source dom
))))
1375 (shr-insert " [audio] ")
1376 (shr-urlify start
(shr-expand-url url
))))
1378 (defun shr-tag-img (dom &optional url
)
1381 (> (length (dom-attr dom
'src
)) 0)))
1382 (when (> (current-column) 0)
1384 (let ((alt (dom-attr dom
'alt
))
1385 (url (shr-expand-url (or url
(dom-attr dom
'src
)))))
1386 (let ((start (point-marker)))
1387 (when (zerop (length alt
))
1390 ((or (member (dom-attr dom
'height
) '("0" "1"))
1391 (member (dom-attr dom
'width
) '("0" "1")))
1392 ;; Ignore zero-sized or single-pixel images.
1394 ((and (not shr-inhibit-images
)
1395 (string-match "\\`data:" url
))
1396 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1398 (funcall shr-put-image-function image alt
)
1400 ((and (not shr-inhibit-images
)
1401 (string-match "\\`cid:" url
))
1402 (let ((url (substring url
(match-end 0)))
1404 (if (or (not shr-content-function
)
1405 (not (setq image
(funcall shr-content-function url
))))
1407 (funcall shr-put-image-function image alt
))))
1408 ((or shr-inhibit-images
1409 (and shr-blocked-images
1410 (string-match shr-blocked-images url
)))
1411 (setq shr-start
(point))
1413 ((and (not shr-ignore-cache
)
1414 (url-is-cached (shr-encode-url url
)))
1415 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
1418 (when (and shr-ignore-cache
1419 (url-is-cached (shr-encode-url url
)))
1420 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1421 (when (file-exists-p file
)
1422 (delete-file file
))))
1424 (shr-encode-url url
) 'shr-image-fetched
1425 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
1427 (when (zerop shr-table-depth
) ;; We are not in a table.
1428 (put-text-property start
(point) 'keymap shr-map
)
1429 (put-text-property start
(point) 'shr-alt alt
)
1430 (put-text-property start
(point) 'image-url url
)
1431 (put-text-property start
(point) 'image-displayer
1432 (shr-image-displayer shr-content-function
))
1433 (put-text-property start
(point) 'help-echo
1435 (or (dom-attr dom
'title
) alt
))))))))
1437 (defun shr-tag-pre (dom)
1438 (let ((shr-folding-mode 'none
)
1439 (shr-current-font 'default
))
1440 (shr-ensure-newline)
1442 (shr-ensure-newline)))
1444 (defun shr-tag-blockquote (dom)
1445 (shr-ensure-paragraph)
1446 (let ((start (point))
1447 (shr-indentation (+ shr-indentation
1448 (* 4 shr-table-separator-pixel-width
))))
1450 (shr-ensure-paragraph)
1451 (shr-mark-fill start
)))
1453 (defun shr-tag-dl (dom)
1454 (shr-ensure-paragraph)
1456 (shr-ensure-paragraph))
1458 (defun shr-tag-dt (dom)
1459 (shr-ensure-newline)
1461 (shr-ensure-newline))
1463 (defun shr-tag-dd (dom)
1464 (shr-ensure-newline)
1465 (let ((shr-indentation (+ shr-indentation
1466 (* 4 shr-table-separator-pixel-width
))))
1469 (defun shr-tag-ul (dom)
1470 (shr-ensure-paragraph)
1471 (let ((shr-list-mode 'ul
))
1473 (shr-ensure-paragraph))
1475 (defun shr-tag-ol (dom)
1476 (shr-ensure-paragraph)
1477 (let ((shr-list-mode 1))
1479 (shr-ensure-paragraph))
1481 (defun shr-tag-li (dom)
1482 (shr-ensure-newline)
1483 (let ((start (point)))
1485 (if (numberp shr-list-mode
)
1487 (format "%d " shr-list-mode
)
1488 (setq shr-list-mode
(1+ shr-list-mode
)))
1489 (car shr-internal-bullet
)))
1490 (width (if (numberp shr-list-mode
)
1491 (shr-string-pixel-width bullet
)
1492 (cdr shr-internal-bullet
))))
1494 (shr-mark-fill start
)
1495 (let ((shr-indentation (+ shr-indentation width
)))
1496 (put-text-property start
(1+ start
)
1497 'shr-continuation-indentation shr-indentation
)
1498 (put-text-property start
(1+ start
) 'shr-prefix-length
(length bullet
))
1499 (shr-generic dom
)))))
1501 (defun shr-mark-fill (start)
1502 ;; We may not have inserted any text to fill.
1503 (unless (= start
(point))
1504 (put-text-property start
(1+ start
)
1505 'shr-indentation shr-indentation
)))
1507 (defun shr-tag-br (dom)
1508 (when (and (not (bobp))
1509 ;; Only add a newline if we break the current line, or
1510 ;; the previous line isn't a blank line.
1512 (and (> (- (point) 2) (point-min))
1513 (not (= (char-after (- (point) 2)) ?
\n)))))
1517 (defun shr-tag-span (dom)
1520 (defun shr-tag-h1 (dom)
1521 (shr-heading dom
(if shr-use-fonts
1522 '(variable-pitch (:height
1.3 :weight bold
))
1525 (defun shr-tag-h2 (dom)
1526 (shr-heading dom
'bold
))
1528 (defun shr-tag-h3 (dom)
1529 (shr-heading dom
'italic
))
1531 (defun shr-tag-h4 (dom)
1534 (defun shr-tag-h5 (dom)
1537 (defun shr-tag-h6 (dom)
1540 (defun shr-tag-hr (_dom)
1541 (shr-ensure-newline)
1542 (insert (make-string (if (not shr-use-fonts
)
1544 (1+ (/ shr-internal-width
1545 shr-table-separator-pixel-width
)))
1549 (defun shr-tag-title (dom)
1550 (shr-heading dom
'bold
'underline
))
1552 (defun shr-tag-font (dom)
1553 (let* ((start (point))
1554 (color (dom-attr dom
'color
))
1555 (shr-stylesheet (nconc (list (cons 'color color
))
1559 (shr-colorize-region start
(point) color
1560 (cdr (assq 'background-color shr-stylesheet
))))))
1562 ;;; Table rendering algorithm.
1564 ;; Table rendering is the only complicated thing here. We do this by
1565 ;; first counting how many TDs there are in each TR, and registering
1566 ;; how wide they think they should be ("width=45%", etc). Then we
1567 ;; render each TD separately (this is done in temporary buffers, so
1568 ;; that we can use all the rendering machinery as if we were in the
1569 ;; main buffer). Now we know how much space each TD really takes, so
1570 ;; we then render everything again with the new widths, and finally
1571 ;; insert all these boxes into the main buffer.
1572 (defun shr-tag-table-1 (dom)
1573 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1574 (let* ((shr-inhibit-images t
)
1575 (shr-table-depth (1+ shr-table-depth
))
1576 (shr-kinsoku-shorten t
)
1577 ;; Find all suggested widths.
1578 (columns (shr-column-specs dom
))
1579 ;; Compute how many pixels wide each TD should be.
1580 (suggested-widths (shr-pro-rate-columns columns
))
1581 ;; Do a "test rendering" to see how big each TD is (this can
1582 ;; be smaller (if there's little text) or bigger (if there's
1583 ;; unbreakable text).
1584 (elems (or (dom-attr dom
'shr-suggested-widths
)
1585 (shr-make-table dom suggested-widths nil
1586 'shr-suggested-widths
)))
1587 (sketch (loop for line in elems
1588 collect
(mapcar #'car line
)))
1589 (natural (loop for line in elems
1590 collect
(mapcar #'cdr line
)))
1591 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1592 ;; This probably won't work very well.
1593 (when (> (+ (loop for width across sketch-widths
1595 shr-indentation shr-table-separator-pixel-width
)
1597 (setq truncate-lines t
))
1598 ;; Then render the table again with these new "hard" widths.
1599 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1601 (defun shr-table-body (dom)
1602 (let ((tbodies (seq-filter (lambda (child)
1603 (eq (dom-tag child
) 'tbody
))
1604 (dom-non-text-children dom
))))
1608 ((= (length tbodies
) 1)
1611 ;; Table with multiple tbodies. Convert into a single tbody.
1612 `(tbody nil
,@(cl-reduce 'append
1613 (mapcar 'dom-non-text-children tbodies
)))))))
1615 (defun shr-tag-table (dom)
1616 (shr-ensure-paragraph)
1617 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1618 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1619 (body (dom-non-text-children (shr-table-body dom
)))
1620 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1621 (bgcolor (dom-attr dom
'bgcolor
))
1623 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1625 (nheader (if header
(shr-max-columns header
)))
1626 (nbody (if body
(shr-max-columns body
) 0))
1627 (nfooter (if footer
(shr-max-columns footer
))))
1628 (if (and (not caption
)
1630 (not (dom-child-by-tag dom
'tbody
))
1631 (not (dom-child-by-tag dom
'tr
))
1633 ;; The table is totally invalid and just contains random junk.
1634 ;; Try to output it anyway.
1636 ;; It's a real table, so render it.
1637 (if (dom-attr dom
'shr-fixed-table
)
1638 (shr-tag-table-1 dom
)
1639 ;; Only fix up the table once.
1643 (if caption
`((tr nil
(td nil
,@caption
))))
1647 ;; header + body + footer
1648 (if (= nheader nbody
)
1649 (if (= nbody nfooter
)
1650 `((tr nil
(td nil
(table nil
1652 ,@body
,@footer
)))))
1653 (nconc `((tr nil
(td nil
(table nil
1658 `((tr nil
(td nil
(table
1660 nil
,@footer
))))))))
1661 (nconc `((tr nil
(td nil
(table nil
(tbody
1663 (if (= nbody nfooter
)
1664 `((tr nil
(td nil
(table
1665 nil
(tbody nil
,@body
1667 (nconc `((tr nil
(td nil
(table
1672 `((tr nil
(td nil
(table
1678 (if (= nheader nbody
)
1679 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1682 `(,@header
(tr nil
(td nil
(table
1683 nil
(tbody nil
,@body
)))))
1684 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1685 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1688 (if (= nbody nfooter
)
1689 `((tr nil
(td nil
(table
1690 nil
(tbody nil
,@body
,@footer
)))))
1691 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1694 `((tr nil
(td nil
(table
1695 nil
(tbody nil
,@footer
)))))))))
1697 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1699 (dom-set-attribute table
'shr-fixed-table t
)
1700 (setcdr dom
(cdr table
))
1701 (shr-tag-table-1 dom
))))
1703 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1705 ;; Finally, insert all the images after the table. The Emacs buffer
1706 ;; model isn't strong enough to allow us to put the images actually
1708 (when (zerop shr-table-depth
)
1710 (shr-expand-alignments start
(point)))
1711 (dolist (elem (dom-by-tag dom
'object
))
1712 (shr-tag-object elem
))
1713 (dolist (elem (dom-by-tag dom
'img
))
1714 (shr-tag-img elem
)))))
1716 (defun shr-insert-table (table widths
)
1717 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
1719 (shr-table-separator-length (if collapse
0 1))
1720 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
1722 (setq shr-table-id
(1+ shr-table-id
))
1724 (shr-insert-table-ruler widths
))
1726 (let ((start (point))
1729 (height (let ((max 0))
1730 (dolist (column row
)
1731 (setq max
(max max
(nth 2 column
))))
1733 (dotimes (i (max height
1))
1735 (insert shr-table-vertical-line
"\n"))
1736 (dolist (column row
)
1737 (when (> (nth 2 column
) -
1)
1739 ;; Sum up all the widths from the column. (There may be
1740 ;; more than one if this is a "colspan" column.)
1741 (dotimes (i (nth 4 column
))
1742 ;; The colspan directive may be wrong and there may not be
1743 ;; that number of columns.
1744 (when (<= column-number
(1- (length widths
)))
1745 (setq align
(+ align
1746 (aref widths column-number
)
1747 (* 2 shr-table-separator-pixel-width
))))
1748 (setq column-number
(1+ column-number
)))
1749 (let ((lines (nth 3 column
))
1750 (pixel-align (if (not shr-use-fonts
)
1751 (* align
(frame-char-width))
1753 (dolist (line lines
)
1755 (let ((start (point))
1756 (background (and (> (length line
) 0)
1757 (shr-face-background
1759 (1- (length line
)) 'face line
))))
1762 'display
`(space :align-to
(,pixel-align
))
1763 'shr-table-indent shr-table-id
)))
1765 (setq space
(propertize space
'face background
)))
1766 (insert line space shr-table-vertical-line
)
1767 (shr-colorize-region
1768 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1770 ;; Add blank lines at padding at the bottom of the TD,
1772 (dotimes (i (- height
(length lines
)))
1774 (let ((start (point)))
1775 (insert (propertize " "
1776 'display
`(space :align-to
(,pixel-align
))
1777 'shr-table-indent shr-table-id
)
1778 shr-table-vertical-line
)
1779 (shr-colorize-region
1780 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1781 (forward-line 1))))))
1783 (shr-insert-table-ruler widths
)))
1784 (unless (= start
(point))
1785 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
1787 (defun shr-face-background (face)
1789 (or (and (plist-get face
:background
)
1790 (list :background
(plist-get face
:background
)))
1791 (let ((background nil
))
1793 (when (and (consp elem
)
1794 (eq (car elem
) :background
)
1796 (setq background
(cadr elem
))))
1798 (list :background background
))))))
1800 (defun shr-expand-alignments (start end
)
1801 (while (< (setq start
(next-single-property-change
1802 start
'shr-table-id nil end
))
1805 (let* ((shr-use-fonts t
)
1806 (id (get-text-property (point) 'shr-table-id
))
1807 (base (shr-pixel-column))
1811 (while (setq elem
(text-property-any
1812 (point) end
'shr-table-indent id
))
1814 (let ((align (get-text-property (point) 'display
)))
1815 (put-text-property (point) (1+ (point)) 'display
1816 `(space :align-to
(,(+ (car (nth 2 align
))
1818 (forward-char 1)))))
1819 (setq start
(1+ start
))))
1821 (defun shr-insert-table-ruler (widths)
1822 (when shr-table-horizontal-line
1824 (> shr-indentation
0))
1826 (insert shr-table-corner
)
1827 (let ((total-width 0))
1828 (dotimes (i (length widths
))
1829 (setq total-width
(+ total-width
(aref widths i
)
1830 (* shr-table-separator-pixel-width
2)))
1831 (insert (make-string (1+ (/ (aref widths i
)
1832 shr-table-separator-pixel-width
))
1833 shr-table-horizontal-line
)
1835 'display
`(space :align-to
(,total-width
))
1836 'shr-table-indent shr-table-id
)
1840 (defun shr-table-widths (table natural-table suggested-widths
)
1841 (let* ((length (length suggested-widths
))
1842 (widths (make-vector length
0))
1843 (natural-widths (make-vector length
0)))
1846 (dolist (column row
)
1847 (aset widths i
(max (aref widths i
) column
))
1849 (dolist (row natural-table
)
1851 (dolist (column row
)
1852 (aset natural-widths i
(max (aref natural-widths i
) column
))
1854 (let ((extra (- (apply '+ (append suggested-widths nil
))
1855 (apply '+ (append widths nil
))
1856 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
1857 (expanded-columns 0))
1858 ;; We have extra, unused space, so divide this space amongst the
1861 ;; If the natural width is wider than the rendered width, we
1862 ;; want to allow the column to expand.
1864 (when (> (aref natural-widths i
) (aref widths i
))
1865 (setq expanded-columns
(1+ expanded-columns
))))
1867 (when (> (aref natural-widths i
) (aref widths i
))
1869 (aref natural-widths i
)
1870 (+ (/ extra expanded-columns
)
1871 (aref widths i
))))))))
1874 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
1875 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
1876 (let ((data (shr-make-table-1 dom widths fill
)))
1877 (push (list (list dom widths fill
) data
)
1879 (when storage-attribute
1880 (dom-set-attribute dom storage-attribute data
))
1883 (defun shr-make-table-1 (dom widths
&optional fill
)
1885 (rowspans (make-vector (length widths
) 0))
1886 (colspan-remaining 0)
1887 colspan-width colspan-count
1889 (dolist (row (dom-non-text-children dom
))
1890 (when (eq (dom-tag row
) 'tr
)
1892 (columns (dom-non-text-children row
))
1896 (while (< i
(length widths
))
1897 ;; If we previously had a rowspan definition, then that
1898 ;; means that we now have a "missing" td/th element here.
1899 ;; So just insert a dummy, empty one to (sort of) emulate
1902 (if (zerop (aref rowspans i
))
1904 (aset rowspans i
(1- (aref rowspans i
)))
1906 (when (and (not (stringp column
))
1907 (or (memq (dom-tag column
) '(td th
))
1909 (when-let (span (dom-attr column
'rowspan
))
1910 (aset rowspans i
(+ (aref rowspans i
)
1911 (1- (string-to-number span
)))))
1912 ;; Sanity check for invalid column-spans.
1913 (when (>= width-column
(length widths
))
1914 (setq width-column
0))
1917 (aref widths width-column
)
1918 (* 10 shr-table-separator-pixel-width
)))
1919 (when (setq colspan
(dom-attr column
'colspan
))
1920 (setq colspan
(min (string-to-number colspan
)
1921 ;; The colspan may be wrong, so
1922 ;; truncate it to the length of the
1923 ;; remaining columns.
1924 (- (length widths
) i
)))
1925 (dotimes (j (1- colspan
))
1927 (if (> (+ i
1 j
) (1- (length widths
)))
1928 ;; If we have a colspan spec that's longer
1929 ;; than the table is wide, just use the last
1930 ;; width as the width.
1931 (aref widths
(1- (length widths
)))
1932 ;; Sum up the widths of the columns we're
1935 shr-table-separator-length
1936 (aref widths
(+ i
1 j
))))))
1937 (setq width-column
(+ width-column
(1- colspan
))
1938 colspan-count colspan
1939 colspan-remaining colspan
))
1941 (let ((data (shr-render-td column width fill
)))
1943 (> colspan-remaining
0))
1945 (setq colspan-width
(car data
))
1946 (let ((this-width (/ colspan-width colspan-count
)))
1947 (push (cons this-width
(cadr data
)) tds
)
1948 (setq colspan-remaining
(1- colspan-remaining
))))
1950 (push (cons (car data
) (cadr data
)) tds
)
1954 (dotimes (c (1- colspan
))
1958 (list 0 0 -
1 nil
1 nil nil
)
1962 width-column
(1+ width-column
))))
1963 (push (nreverse tds
) trs
))))
1966 (defun shr-pixel-buffer-width ()
1967 (if (not shr-use-fonts
)
1969 (goto-char (point-min))
1973 (setq max
(max max
(current-column)))
1976 (if (get-buffer-window)
1977 (car (window-text-pixel-size nil
(point-min) (point-max)))
1978 (save-window-excursion
1979 (set-window-buffer nil
(current-buffer))
1980 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
1982 (defun shr-render-td (dom width fill
)
1983 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
1984 (or (dom-attr dom cache
)
1987 (dolist (attr (dom-attributes dom
))
1988 (let ((name (symbol-name (car attr
))))
1989 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
1990 (let ((cache-width (string-to-number
1991 (match-string 1 name
))))
1992 (when (and (>= cache-width width
)
1993 (<= (car (cdr attr
)) width
))
1994 (setq result
(cdr attr
)))))))
1996 (let ((result (shr-render-td-1 dom width fill
)))
1997 (dom-set-attribute dom cache result
)
2000 (defun shr-render-td-1 (dom width fill
)
2002 (let ((bgcolor (dom-attr dom
'bgcolor
))
2003 (fgcolor (dom-attr dom
'fgcolor
))
2004 (style (dom-attr dom
'style
))
2005 (shr-stylesheet shr-stylesheet
)
2009 (setq style
(and (string-match "color" style
)
2010 (shr-parse-style style
))))
2012 (setq style
(nconc (list (cons 'background-color bgcolor
))
2015 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
2017 (setq shr-stylesheet
(append style shr-stylesheet
)))
2018 (let ((shr-internal-width width
)
2019 (shr-indentation 0))
2021 (save-window-excursion
2022 (set-window-buffer nil
(current-buffer))
2025 (or (dom-attr dom
'shr-td-cache-natural
)
2026 (let ((natural (max (shr-pixel-buffer-width)
2027 (shr-dom-max-natural-width dom
0))))
2028 (dom-set-attribute dom
'shr-td-cache-natural natural
)
2030 (if (and natural-width
2031 (<= natural-width width
))
2032 (setq max-width natural-width
)
2033 (let ((shr-internal-width width
))
2034 (shr-fill-lines (point-min) (point-max))
2035 (setq max-width
(shr-pixel-buffer-width)))))
2036 (goto-char (point-max))
2037 ;; Delete padding at the bottom of the TDs.
2041 (skip-chars-backward " \t\n")
2044 (goto-char (point-min))
2047 (count-lines (point-min) (point-max))
2048 (split-string (buffer-string) "\n")
2049 (if (dom-attr dom
'colspan
)
2050 (string-to-number (dom-attr dom
'colspan
))
2052 (cdr (assq 'color shr-stylesheet
))
2053 (cdr (assq 'background-color shr-stylesheet
))))))
2055 (defun shr-dom-max-natural-width (dom max
)
2056 (if (eq (dom-tag dom
) 'table
)
2058 (loop for line in
(dom-attr dom
'shr-suggested-widths
)
2060 shr-table-separator-length
2061 (loop for elem in line
2064 (* 2 shr-table-separator-length
)))))
2066 (dolist (child (dom-children dom
))
2067 (unless (stringp child
)
2068 (setq max
(max (shr-dom-max-natural-width child max
)))))
2071 (defun shr-buffer-width ()
2072 (goto-char (point-min))
2076 (setq max
(max max
(current-column)))
2080 (defun shr-pro-rate-columns (columns)
2081 (let ((total-percentage 0)
2082 (widths (make-vector (length columns
) 0)))
2083 (dotimes (i (length columns
))
2084 (setq total-percentage
(+ total-percentage
(aref columns i
))))
2085 (setq total-percentage
(/ 1.0 total-percentage
))
2086 (dotimes (i (length columns
))
2087 (aset widths i
(max (truncate (* (aref columns i
)
2089 (- shr-internal-width
2090 (* (1+ (length columns
))
2091 shr-table-separator-pixel-width
))))
2095 ;; Return a summary of the number and shape of the TDs in the table.
2096 (defun shr-column-specs (dom)
2097 (let ((columns (make-vector (shr-max-columns dom
) 1)))
2098 (dolist (row (dom-non-text-children dom
))
2099 (when (eq (dom-tag row
) 'tr
)
2101 (dolist (column (dom-non-text-children row
))
2102 (when (memq (dom-tag column
) '(td th
))
2103 (let ((width (dom-attr column
'width
)))
2105 (string-match "\\([0-9]+\\)%" width
)
2106 (not (zerop (setq width
(string-to-number
2107 (match-string 1 width
))))))
2108 (aset columns i
(/ width
100.0))))
2109 (setq i
(1+ i
)))))))
2112 (defun shr-count (dom elem
)
2114 (dolist (sub (dom-children dom
))
2115 (when (and (not (stringp sub
))
2116 (eq (dom-tag sub
) elem
))
2120 (defun shr-max-columns (dom)
2122 (dolist (row (dom-children dom
))
2123 (when (and (not (stringp row
))
2124 (eq (dom-tag row
) 'tr
))
2125 (setq max
(max max
(+ (shr-count row
'td
)
2126 (shr-count row
'th
))))))
2131 ;;; shr.el ends here