1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2015 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.
40 "Simple HTML Renderer"
44 (defcustom shr-max-image-proportion
0.9
45 "How big pictures displayed are in relation to the window they're in.
46 A value of 0.7 means that they are allowed to take up 70% of the
47 width and height of the window. If they are larger than this,
48 and Emacs supports it, then the images will be rescaled down to
54 (defcustom shr-blocked-images nil
55 "Images that have URLs matching this regexp will be blocked."
58 :type
'(choice (const nil
) regexp
))
60 (defcustom shr-use-fonts t
61 "If non-nil, use proportional fonts for text."
66 (defcustom shr-table-horizontal-line nil
67 "Character used to draw horizontal table lines.
68 If nil, don't draw horizontal table lines."
70 :type
'(choice (const nil
) character
))
72 (defcustom shr-table-vertical-line ?\s
73 "Character used to draw vertical table lines."
77 (defcustom shr-table-corner ?\s
78 "Character used to draw table corners."
82 (defcustom shr-hr-line ?-
83 "Character used to draw hr lines."
87 (defcustom shr-width nil
88 "Frame width to use for rendering.
89 May either be an integer specifying a fixed width in characters,
90 or nil, meaning that the full width of the window should be
93 :type
'(choice (integer :tag
"Fixed width in characters")
94 (const :tag
"Use the width of the window" nil
))
97 (defcustom shr-bullet
"* "
98 "Bullet used for unordered lists.
99 Alternative suggestions are:
106 (defcustom shr-external-browser
'browse-url-default-browser
107 "Function used to launch an external browser."
112 (defcustom shr-image-animate t
113 "Non nil means that images that can be animated will be."
118 (defvar shr-content-function nil
119 "If bound, this should be a function that will return the content.
120 This is used for cid: URLs, and the function is called with the
121 cid: URL as the argument.")
123 (defvar shr-put-image-function
'shr-put-image
124 "Function called to put image and alt string.")
126 (defface shr-strike-through
'((t (:strike-through t
)))
127 "Font for <s> elements."
131 '((t (:inherit link
)))
132 "Font for link elements."
135 (defvar shr-inhibit-images nil
136 "If non-nil, inhibit loading images.")
138 ;;; Internal variables.
140 (defvar shr-folding-mode nil
)
141 (defvar shr-start nil
)
142 (defvar shr-indentation
0)
143 (defvar shr-internal-width nil
)
144 (defvar shr-list-mode nil
)
145 (defvar shr-content-cache nil
)
146 (defvar shr-kinsoku-shorten nil
)
147 (defvar shr-table-depth
0)
148 (defvar shr-stylesheet nil
)
149 (defvar shr-base nil
)
151 (defvar shr-warning nil
)
152 (defvar shr-ignore-cache nil
)
153 (defvar shr-external-rendering-functions nil
)
154 (defvar shr-target-id nil
)
155 (defvar shr-table-separator-length
1)
156 (defvar shr-table-separator-pixel-width
0)
157 (defvar shr-table-id nil
)
158 (defvar shr-current-font nil
)
159 (defvar shr-internal-bullet nil
)
162 (let ((map (make-sparse-keymap)))
163 (define-key map
"a" 'shr-show-alt-text
)
164 (define-key map
"i" 'shr-browse-image
)
165 (define-key map
"z" 'shr-zoom-image
)
166 (define-key map
[?
\t] 'shr-next-link
)
167 (define-key map
[?\M-
\t] 'shr-previous-link
)
168 (define-key map
[follow-link
] 'mouse-face
)
169 (define-key map
[mouse-2
] 'shr-browse-url
)
170 (define-key map
"I" 'shr-insert-image
)
171 (define-key map
"w" 'shr-copy-url
)
172 (define-key map
"u" 'shr-copy-url
)
173 (define-key map
"v" 'shr-browse-url
)
174 (define-key map
"o" 'shr-save-contents
)
175 (define-key map
"\r" 'shr-browse-url
)
178 ;; Public functions and commands.
179 (declare-function libxml-parse-html-region
"xml.c"
180 (start end
&optional base-url discard-comments
))
182 (defun shr-render-buffer (buffer)
183 "Display the HTML rendering of the current buffer."
184 (interactive (list (current-buffer)))
185 (or (fboundp 'libxml-parse-html-region
)
186 (error "This function requires Emacs to be compiled with libxml2"))
187 (pop-to-buffer "*html*")
190 (with-current-buffer buffer
191 (libxml-parse-html-region (point-min) (point-max))))
192 (goto-char (point-min)))
195 (defun shr-render-region (begin end
&optional buffer
)
196 "Display the HTML rendering of the region between BEGIN and END."
198 (unless (fboundp 'libxml-parse-html-region
)
199 (error "This function requires Emacs to be compiled with libxml2"))
200 (with-current-buffer (or buffer
(current-buffer))
201 (let ((dom (libxml-parse-html-region begin end
)))
202 (delete-region begin end
)
204 (shr-insert-document dom
))))
206 (defun shr--have-one-fringe-p ()
207 "Return non-nil if we know at least one of the fringes has non-zero width."
208 (and (fboundp 'fringe-columns
)
209 (or (not (zerop (fringe-columns 'right
)))
210 (not (zerop (fringe-columns 'left
))))))
213 (defun shr-insert-document (dom)
214 "Render the parsed document DOM into the current buffer.
215 DOM should be a parse tree as generated by
216 `libxml-parse-html-region' or similar."
217 (setq shr-content-cache nil
)
218 (let ((start (point))
224 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
225 (shr-internal-bullet (cons shr-bullet
226 (shr-string-pixel-width shr-bullet
)))
227 (shr-internal-width (or (and shr-width
228 (if (not shr-use-fonts
)
230 (* shr-width
(frame-char-width))))
231 ;; We need to adjust the available
232 ;; width for when the user disables
233 ;; the fringes, which will cause the
234 ;; display engine usurp one column for
235 ;; the continuation glyph.
236 (if (not shr-use-fonts
)
237 (- (window-body-width) 1
238 (if (and (null shr-width
)
239 (not (shr--have-one-fringe-p)))
242 (- (window-body-width nil t
)
243 (* 2 (frame-char-width))
244 (if (and (null shr-width
)
245 (not (shr--have-one-fringe-p)))
246 (* (frame-char-width) 2)
249 (shr-fill-lines start
(point))
250 (shr-remove-trailing-whitespace start
(point))
252 (message "%s" shr-warning
))))
254 (defun shr-remove-trailing-whitespace (start end
)
255 (let ((width (window-width)))
257 (narrow-to-region start end
)
261 (when (> (shr-previous-newline-padding-width (current-column)) width
)
262 (dolist (overlay (overlays-at (point)))
263 (when (overlay-get overlay
'before-string
)
264 (overlay-put overlay
'before-string nil
))))
267 (defun shr-copy-url (&optional image-url
)
268 "Copy the URL under point to the kill ring.
269 If IMAGE-URL (the prefix) is non-nil, or there is no link under
270 point, but there is an image under point then copy the URL of the
271 image under point instead.
272 If called twice, then try to fetch the URL and see whether it
273 redirects somewhere else."
275 (let ((url (or (get-text-property (point) 'shr-url
)
276 (get-text-property (point) 'image-url
))))
279 (message "No URL under point"))
280 ;; Resolve redirected URLs.
281 ((equal url
(car kill-ring
))
286 (eq (car a
) :redirect
))
289 (goto-char (point-min))
290 ;; Remove common tracking junk from the URL.
291 (when (re-search-forward ".utm_.*" nil t
)
292 (replace-match "" t t
))
293 (message "Copied %s" (buffer-string))
294 (copy-region-as-kill (point-min) (point-max)))))
296 ;; Copy the URL to the kill ring.
299 (insert (url-encode-url url
))
300 (copy-region-as-kill (point-min) (point-max))
301 (message "Copied %s" (buffer-string)))))))
303 (defun shr-next-link ()
304 "Skip to the next link."
306 (let ((skip (text-property-any (point) (point-max) 'help-echo nil
)))
308 (not (setq skip
(text-property-not-all skip
(point-max)
310 (message "No next link")
312 (message "%s" (get-text-property (point) 'help-echo
)))))
314 (defun shr-previous-link ()
315 "Skip to the previous link."
317 (let ((start (point))
319 ;; Skip past the current link.
320 (while (and (not (bobp))
321 (get-text-property (point) 'help-echo
))
323 ;; Find the previous link.
324 (while (and (not (bobp))
325 (not (setq found
(get-text-property (point) 'help-echo
))))
329 (message "No previous link")
331 ;; Put point at the start of the link.
332 (while (and (not (bobp))
333 (get-text-property (point) 'help-echo
))
336 (message "%s" (get-text-property (point) 'help-echo
)))))
338 (defun shr-show-alt-text ()
339 "Show the ALT text of the image under point."
341 (let ((text (get-text-property (point) 'shr-alt
)))
343 (message "No image under point")
344 (message "%s" (shr-fill-text text
)))))
346 (defun shr-browse-image (&optional copy-url
)
347 "Browse the image under point.
348 If COPY-URL (the prefix if called interactively) is non-nil, copy
349 the URL of the image to the kill buffer instead."
351 (let ((url (get-text-property (point) 'image-url
)))
354 (message "No image under point"))
358 (copy-region-as-kill (point-min) (point-max))
359 (message "Copied %s" url
)))
361 (message "Browsing %s..." url
)
364 (defun shr-insert-image ()
365 "Insert the image under point into the buffer."
367 (let ((url (get-text-property (point) 'image-url
)))
369 (message "No image under point")
370 (message "Inserting %s..." url
)
371 (url-retrieve url
'shr-image-fetched
372 (list (current-buffer) (1- (point)) (point-marker))
375 (defun shr-zoom-image ()
376 "Toggle the image size.
377 The size will be rotated between the default size, the original
378 size, and full-buffer size."
380 (let ((url (get-text-property (point) 'image-url
))
381 (size (get-text-property (point) 'image-size
))
382 (buffer-read-only nil
))
384 (message "No image under point")
385 ;; Delete the old picture.
386 (while (get-text-property (point) 'image-url
)
389 (let ((start (point)))
390 (while (get-text-property (point) 'image-url
)
393 (put-text-property start
(point) 'display nil
)
394 (when (> (- (point) start
) 2)
395 (delete-region start
(1- (point)))))
396 (message "Inserting %s..." url
)
397 (url-retrieve url
'shr-image-fetched
398 (list (current-buffer) (1- (point)) (point-marker)
400 (cond ((or (eq size
'default
)
409 ;;; Utility functions.
411 (defsubst shr-generic
(dom)
412 (dolist (sub (dom-children dom
))
417 (defun shr-descend (dom)
420 ;; Allow other packages to override (or provide) rendering
422 (cdr (assq (dom-tag dom
) shr-external-rendering-functions
))
423 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
)))
424 (style (dom-attr dom
'style
))
425 (shr-stylesheet shr-stylesheet
)
426 (shr-depth (1+ shr-depth
))
428 ;; shr uses about 12 frames per nested node.
429 (if (> shr-depth
(/ max-specpdl-size
12))
430 (setq shr-warning
"Too deeply nested to render properly; consider increasing `max-specpdl-size'")
432 (if (string-match "color\\|display\\|border-collapse" style
)
433 (setq shr-stylesheet
(nconc (shr-parse-style style
)
436 ;; If we have a display:none, then just ignore this part of the DOM.
437 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
438 (if (fboundp function
)
439 (funcall function dom
)
441 (when (and shr-target-id
442 (equal (dom-attr dom
'id
) shr-target-id
))
443 ;; If the element was empty, we don't have anything to put the
444 ;; anchor on. So just insert a dummy character.
445 (when (= start
(point))
447 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
448 ;; If style is set, then this node has set the color.
452 (cdr (assq 'color shr-stylesheet
))
453 (cdr (assq 'background-color shr-stylesheet
))))))))
455 (defun shr-fill-text (text)
456 (if (zerop (length text
))
459 (let ((shr-indentation 0)
461 (shr-internal-width (- (window-body-width nil t
)
462 (* 2 (frame-char-width))
463 ;; Adjust the window width for when
464 ;; the user disables the fringes,
465 ;; which causes the display engine
466 ;; to usurp one column for the
467 ;; continuation glyph.
468 (if (and (null shr-width
)
469 (not (shr--have-one-fringe-p)))
470 (* (frame-char-width) 2)
475 (define-inline shr-char-breakable-p
(char)
476 "Return non-nil if a line can be broken before and after CHAR."
477 (inline-quote (aref fill-find-break-point-function-table
,char
)))
478 (define-inline shr-char-nospace-p
(char)
479 "Return non-nil if no space is required before and after CHAR."
480 (inline-quote (aref fill-nospace-between-words-table
,char
)))
482 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
483 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
484 ;; parentheses, and so on, that should not be placed in the beginning
485 ;; of a line or the end of a line.
486 (define-inline shr-char-kinsoku-bol-p
(char)
487 "Return non-nil if a line ought not to begin with CHAR."
488 (inline-letevals (char)
489 (inline-quote (and (not (eq ,char ?
'))
490 (aref (char-category-set ,char
) ?
>)))))
491 (define-inline shr-char-kinsoku-eol-p
(char)
492 "Return non-nil if a line ought not to end with CHAR."
493 (inline-quote (aref (char-category-set ,char
) ?
<)))
494 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
495 (load "kinsoku" nil t
))
497 (defun shr-pixel-column ()
498 (if (not shr-use-fonts
)
500 (if (not (get-buffer-window (current-buffer)))
501 (save-window-excursion
502 (set-window-buffer nil
(current-buffer))
503 (car (window-text-pixel-size nil
(line-beginning-position) (point))))
504 (car (window-text-pixel-size nil
(line-beginning-position) (point))))))
506 (defun shr-pixel-region ()
507 (- (shr-pixel-column)
510 (shr-pixel-column))))
512 (defun shr-string-pixel-width (string)
513 (if (not shr-use-fonts
)
517 (shr-pixel-column))))
519 (defun shr-insert (text)
520 (when (and (not (bolp))
521 (get-text-property (1- (point)) 'image-url
))
524 ((eq shr-folding-mode
'none
)
525 (let ((start (point)))
528 (narrow-to-region start
(point))
529 ;; Remove soft hyphens.
530 (goto-char (point-min))
531 (while (search-forward "Â" nil t
)
532 (replace-match "" t t
))
533 (goto-char (point-max)))))
535 (let ((font-start (point)))
536 (when (and (string-match "\\`[ \t\n\r ]" text
)
538 (not (eq (char-after (1- (point))) ?
)))
540 (let ((start (point))
544 (narrow-to-region start
(point))
546 (when (looking-at "[ \t\n\r ]+")
547 (replace-match "" t t
))
548 (while (re-search-forward "[ \t\n\r ]+" nil t
)
549 (replace-match " " t t
))
550 ;; Remove soft hyphens.
551 (goto-char (point-min))
552 (while (search-forward "Â" nil t
)
553 (replace-match "" t t
))
554 (goto-char (point-max)))
555 ;; We may have removed everything we inserted if if was just
557 (unless (= font-start
(point))
558 ;; Mark all lines that should possibly be folded afterwards.
560 (shr-mark-fill start
))
562 (put-text-property font-start
(point)
564 (or shr-current-font
'variable-pitch
)))))))))
566 (defun shr-fill-lines (start end
)
567 (if (<= shr-internal-width
0)
570 (narrow-to-region start end
)
572 (when (get-text-property (point) 'shr-indentation
)
574 (while (setq start
(next-single-property-change start
'shr-indentation
))
578 (goto-char (point-max)))))
580 (defun shr-vertical-motion (column)
581 (if (not shr-use-fonts
)
582 (move-to-column column
)
585 (vertical-motion (cons (/ column
(frame-char-width)) 0))
589 (defun shr-fill-line ()
590 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
591 (continuation (get-text-property
592 (point) 'shr-continuation-indentation
))
594 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
595 (let ((face (get-text-property (point) 'face
))
596 (background-start (point)))
599 (put-text-property background-start
(point) 'face
600 `,(shr-face-background face
))))
602 (setq shr-indentation
(or continuation shr-indentation
))
603 (shr-vertical-motion shr-internal-width
)
604 (when (looking-at " $")
605 (delete-region (point) (line-end-position)))
607 ;; We have to do some folding. First find the first
608 ;; previous point suitable for folding.
609 (if (or (not (shr-find-fill-point (line-beginning-position)))
611 ;; We had unbreakable text (for this width), so just go to
612 ;; the first space and carry on.
615 (skip-chars-forward " ")
616 (search-forward " " (line-end-position) 'move
)))
617 ;; Success; continue.
618 (when (= (preceding-char) ?\s
)
620 (let ((face (get-text-property (point) 'face
))
621 (background-start (point)))
625 (put-text-property background-start
(point) 'face
626 `,(shr-face-background face
))))
628 (shr-vertical-motion shr-internal-width
)
629 (when (looking-at " $")
630 (delete-region (point) (line-end-position))))))
632 (defun shr-find-fill-point (start)
636 (while (not (or (setq failed
(<= (point) start
))
637 (eq (preceding-char) ?
)
638 (eq (following-char) ?
)
639 (shr-char-breakable-p (preceding-char))
640 (shr-char-breakable-p (following-char))
641 (and (shr-char-kinsoku-bol-p (preceding-char))
642 (shr-char-breakable-p (following-char))
643 (not (shr-char-kinsoku-bol-p (following-char))))
644 (shr-char-kinsoku-eol-p (following-char))
648 ;; There's no breakable point, so we give it up.
651 ;; Don't overflow the window edge, even if
652 ;; shr-kinsoku-shorten is nil.
653 (unless (or shr-kinsoku-shorten
(null shr-width
))
654 (while (setq found
(re-search-forward
655 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
656 (line-end-position) 'move
)))
658 (not (match-beginning 1)))
659 (goto-char (match-beginning 0)))))
662 ;; Don't put kinsoku-bol characters at the beginning of a line,
663 ;; or kinsoku-eol characters at the end of a line.
665 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
667 ((or shr-kinsoku-shorten
(null shr-width
))
668 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
669 (or (shr-char-kinsoku-eol-p (preceding-char))
670 (shr-char-kinsoku-bol-p (following-char))))
672 (when (setq failed
(<= (point) start
))
673 ;; There's no breakable point that doesn't violate kinsoku,
674 ;; so we look for the second best position.
680 (shr-char-kinsoku-eol-p (following-char)))))
682 ((shr-char-kinsoku-eol-p (preceding-char))
683 ;; Find backward the point where kinsoku-eol characters begin.
688 (and (> (setq count
(1- count
)) 0)
689 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
690 (or (shr-char-kinsoku-eol-p (preceding-char))
691 (shr-char-kinsoku-bol-p (following-char)))))))
692 (when (setq failed
(<= (point) start
))
693 ;; There's no breakable point that doesn't violate kinsoku,
694 ;; so we go to the second best position.
695 (if (looking-at "\\(\\c<+\\)\\c<")
696 (goto-char (match-end 1))
698 ((shr-char-kinsoku-bol-p (following-char))
699 ;; Find forward the point where kinsoku-bol characters end.
703 (and (>= (setq count
(1- count
)) 0)
704 (shr-char-kinsoku-bol-p (following-char))
705 (shr-char-breakable-p (following-char))))))))
706 (when (eq (following-char) ?
)
710 (defun shr-parse-base (url)
711 ;; Always chop off anchors.
712 (when (string-match "#.*" url
)
713 (setq url
(substring url
0 (match-beginning 0))))
714 ;; NB: <base href="" > URI may itself be relative to the document s URI
715 (setq url
(shr-expand-url url
))
716 (let* ((parsed (url-generic-parse-url url
))
717 (local (url-filename parsed
)))
718 (setf (url-filename parsed
) "")
719 ;; Chop off the bit after the last slash.
720 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
721 (setq local
(match-string 1 local
)))
722 ;; Always make the local bit end with a slash.
723 (when (and (not (zerop (length local
)))
724 (not (eq (aref local
(1- (length local
))) ?
/)))
725 (setq local
(concat local
"/")))
726 (list (url-recreate-url parsed
)
731 (autoload 'url-expand-file-name
"url-expand")
733 ;; FIXME This needs some tests writing.
734 ;; Does it even need to exist, given that url-expand-file-name does?
735 (defun shr-expand-url (url &optional base
)
738 ;; shr-parse-base should never call this with non-nil base!
739 (shr-parse-base base
)
740 ;; Bound by the parser.
742 (when (zerop (length url
))
744 ;; Strip leading whitespace
745 (and url
(string-match "\\`\\s-+" url
)
746 (setq url
(substring url
(match-end 0))))
749 (string-match "\\`[a-z]*:" url
))
750 ;; Absolute or empty URI
751 (or url
(nth 3 base
)))
752 ((eq (aref url
0) ?
/)
753 (if (and (> (length url
) 1)
754 (eq (aref url
1) ?
/))
755 ;; //host...; just use the protocol
756 (concat (nth 2 base
) ":" url
)
757 ;; Just use the host name part.
758 (concat (car base
) url
)))
759 ((eq (aref url
0) ?
#)
760 ;; A link to an anchor.
761 (concat (nth 3 base
) url
))
764 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
766 (defun shr-ensure-newline ()
767 (unless (zerop (current-column))
770 (defun shr-ensure-paragraph ()
772 (let ((prefix (get-text-property (line-beginning-position)
773 'shr-prefix-length
)))
779 ;; We're already at a new paragraph; do nothing.
782 (= prefix
(- (point) (line-beginning-position))))
783 ;; Do nothing; we're at the start of a <li>.
787 ;; If the current line is totally blank, and doesn't even
788 ;; have any face properties set, then delete the blank
790 (and (looking-at " *$")
791 (not (get-text-property (point) 'face
))
792 (not (= (next-single-property-change (point) 'face nil
794 (line-end-position)))))
795 (delete-region (match-beginning 0) (match-end 0)))
800 (when (> shr-indentation
0)
802 (if (not shr-use-fonts
)
803 (make-string shr-indentation ?\s
)
806 `(space :width
(,shr-indentation
)))))))
808 (defun shr-fontize-dom (dom &rest types
)
809 (let ((start (point)))
812 (shr-add-font start
(point) type
))))
814 ;; Add face to the region, but avoid putting the font properties on
815 ;; blank text at the start of the line, and the newline at the end, to
817 (defun shr-add-font (start end type
)
820 (while (< (point) end
)
822 (skip-chars-forward " "))
823 (add-face-text-property (point) (min (line-end-position) end
) type t
)
824 (if (< (line-end-position) end
)
828 (defun shr-mouse-browse-url (ev)
829 "Browse the URL under the mouse cursor."
834 (defun shr-browse-url (&optional external mouse-event
)
835 "Browse the URL under point.
836 If EXTERNAL, browse the URL using `shr-external-browser'."
837 (interactive (list current-prefix-arg last-nonmenu-event
))
838 (mouse-set-point mouse-event
)
839 (let ((url (get-text-property (point) 'shr-url
)))
842 (message "No link under point"))
843 ((string-match "^mailto:" url
)
844 (browse-url-mail url
))
847 (funcall shr-external-browser url
)
848 (browse-url url
))))))
850 (defun shr-save-contents (directory)
851 "Save the contents from URL in a file."
852 (interactive "DSave contents of URL to directory: ")
853 (let ((url (get-text-property (point) 'shr-url
)))
855 (message "No link under point")
856 (url-retrieve (shr-encode-url url
)
857 'shr-store-contents
(list url directory
)
860 (defun shr-store-contents (status url directory
)
861 (unless (plist-get status
:error
)
862 (when (or (search-forward "\n\n" nil t
)
863 (search-forward "\r\n\r\n" nil t
))
864 (write-region (point) (point-max)
865 (expand-file-name (file-name-nondirectory url
)
868 (defun shr-image-fetched (status buffer start end
&optional flags
)
869 (let ((image-buffer (current-buffer)))
870 (when (and (buffer-name buffer
)
871 (not (plist-get status
:error
)))
872 (url-store-in-cache image-buffer
)
873 (when (or (search-forward "\n\n" nil t
)
874 (search-forward "\r\n\r\n" nil t
))
875 (let ((data (shr-parse-image-data)))
876 (with-current-buffer buffer
878 (let ((alt (buffer-substring start end
))
879 (properties (text-properties-at start
))
880 (inhibit-read-only t
))
881 (delete-region start end
)
883 (funcall shr-put-image-function data alt flags
)
885 (let ((type (pop properties
))
886 (value (pop properties
)))
887 (unless (memq type
'(display image-size
))
888 (put-text-property start
(point) type value
))))))))))
889 (kill-buffer image-buffer
)))
891 (defun shr-image-from-data (data)
892 "Return an image from the data: URI content DATA."
894 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
896 (let ((param (match-string 4 data
))
897 (payload (url-unhex-string (match-string 5 data
))))
898 (when (string-match "^.*\\(;[ \t]*base64\\)$" param
)
899 (setq payload
(base64-decode-string payload
)))
902 ;; Behind display-graphic-p test.
903 (declare-function image-size
"image.c" (spec &optional pixels frame
))
904 (declare-function image-animate
"image" (image &optional index limit
))
906 (defun shr-put-image (spec alt
&optional flags
)
907 "Insert image SPEC with a string ALT. Return image.
908 SPEC is either an image data blob, or a list where the first
909 element is the data blob and the second element is the content-type."
910 (if (display-graphic-p)
911 (let* ((size (cdr (assq 'size flags
)))
912 (data (if (consp spec
)
915 (content-type (and (consp spec
)
920 (create-image data nil t
:ascent
100
921 :format content-type
))
922 ((eq content-type
'image
/svg
+xml
)
923 (create-image data
'svg t
:ascent
100))
926 (shr-rescale-image data content-type
)))
929 (shr-rescale-image data content-type
))))))
931 ;; When inserting big-ish pictures, put them at the
932 ;; beginning of the line.
933 (when (and (> (current-column) 0)
934 (> (car (image-size image t
)) 400))
936 (if (eq size
'original
)
937 (insert-sliced-image image
(or alt
"*") nil
20 1)
938 (insert-image image
(or alt
"*")))
939 (put-text-property start
(point) 'image-size size
)
940 (when (and shr-image-animate
941 (cond ((fboundp 'image-multi-frame-p
)
942 ;; Only animate multi-frame things that specify a
943 ;; delay; eg animated gifs as opposed to
944 ;; multi-page tiffs. FIXME?
945 (cdr (image-multi-frame-p image
)))
946 ((fboundp 'image-animated-p
)
947 (image-animated-p image
))))
948 (image-animate image nil
60)))
952 (defun shr-rescale-image (data &optional content-type
)
953 "Rescale DATA, if too big, to fit the current buffer."
954 (if (not (and (fboundp 'imagemagick-types
)
955 (get-buffer-window (current-buffer))))
956 (create-image data nil t
:ascent
100)
957 (let ((edges (window-inside-pixel-edges
958 (get-buffer-window (current-buffer)))))
962 :max-width
(truncate (* shr-max-image-proportion
963 (- (nth 2 edges
) (nth 0 edges
))))
964 :max-height
(truncate (* shr-max-image-proportion
965 (- (nth 3 edges
) (nth 1 edges
))))
966 :format content-type
))))
968 ;; url-cache-extract autoloads url-cache.
969 (declare-function url-cache-create-filename
"url-cache" (url))
970 (autoload 'mm-disable-multibyte
"mm-util")
971 (autoload 'browse-url-mail
"browse-url")
973 (defun shr-get-image-data (url)
974 "Get image data for URL.
975 Return a string with image data."
977 (mm-disable-multibyte)
979 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
981 (when (or (search-forward "\n\n" nil t
)
982 (search-forward "\r\n\r\n" nil t
))
983 (shr-parse-image-data)))))
985 (declare-function libxml-parse-xml-region
"xml.c"
986 (start end
&optional base-url discard-comments
))
988 (defun shr-parse-image-data ()
989 (let ((data (buffer-substring (point) (point-max)))
993 (narrow-to-region (point-min) (point))
994 (let ((content-type (mail-fetch-field "content-type")))
996 ;; Remove any comments in the type string.
997 (intern (replace-regexp-in-string ";.*" "" content-type
)
999 ;; SVG images may contain references to further images that we may
1000 ;; want to block. So special-case these by parsing the XML data
1001 ;; and remove the blocked bits.
1002 (when (eq content-type
'image
/svg
+xml
)
1005 (libxml-parse-xml-region (point) (point-max)))))
1006 (list data content-type
)))
1008 (defun shr-image-displayer (content-function)
1009 "Return a function to display an image.
1010 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1011 is an argument. The function to be returned takes three arguments URL,
1012 START, and END. Note that START and END should be markers."
1013 `(lambda (url start end
)
1015 (if (string-match "\\`cid:" url
)
1016 ,(when content-function
1017 `(let ((image (funcall ,content-function
1018 (substring url
(match-end 0)))))
1021 (funcall shr-put-image-function
1022 image
(buffer-substring start end
))
1023 (delete-region (point) end
))))
1024 (url-retrieve url
'shr-image-fetched
1025 (list (current-buffer) start end
)
1028 (defun shr-heading (dom &rest types
)
1029 (shr-ensure-paragraph)
1030 (apply #'shr-fontize-dom dom types
)
1031 (shr-ensure-paragraph))
1033 (defun shr-urlify (start url
&optional title
)
1034 (shr-add-font start
(point) 'shr-link
)
1035 (add-text-properties
1038 'help-echo
(let ((iri (or (ignore-errors
1039 (decode-coding-string
1040 (url-unhex-string url
)
1043 (if title
(format "%s (%s)" iri title
) iri
))
1045 'mouse-face
'highlight
1048 (defun shr-encode-url (url)
1050 (browse-url-url-encode-chars url
"[)$ ]"))
1052 (autoload 'shr-color-visible
"shr-color")
1053 (autoload 'shr-color-
>hexadecimal
"shr-color")
1055 (defun shr-color-check (fg bg
)
1056 "Check that FG is visible on BG.
1057 Returns (fg bg) with corrected values.
1058 Returns nil if the colors that would be used are the default
1059 ones, in case fg and bg are nil."
1061 (let ((fixed (cond ((null fg
) 'fg
)
1063 ;; Convert colors to hexadecimal, or set them to default.
1064 (let ((fg (or (shr-color->hexadecimal fg
)
1065 (frame-parameter nil
'foreground-color
)))
1066 (bg (or (shr-color->hexadecimal bg
)
1067 (frame-parameter nil
'background-color
))))
1068 (cond ((eq fixed
'bg
)
1069 ;; Only return the new fg
1070 (list nil
(cadr (shr-color-visible bg fg t
))))
1072 ;; Invert args and results and return only the new bg
1073 (list (cadr (shr-color-visible fg bg t
)) nil
))
1075 (shr-color-visible bg fg
)))))))
1077 (defun shr-colorize-region (start end fg
&optional bg
)
1078 (when (and (or fg bg
) (>= (display-color-cells) 88))
1079 (let ((new-colors (shr-color-check fg bg
)))
1082 (add-face-text-property start end
1083 (list :foreground
(cadr new-colors
))
1086 (add-face-text-property start end
1087 (list :background
(car new-colors
))
1091 (defun shr-previous-newline-padding-width (width)
1092 (let ((overlays (overlays-at (point)))
1096 (dolist (overlay overlays
)
1097 (setq previous-width
1099 (length (plist-get (overlay-properties overlay
)
1101 (+ width previous-width
))))
1103 ;;; Tag-specific rendering rules.
1105 (defun shr-tag-body (dom)
1106 (let* ((start (point))
1107 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1108 (bgcolor (dom-attr dom
'bgcolor
))
1109 (shr-stylesheet (list (cons 'color fgcolor
)
1110 (cons 'background-color bgcolor
))))
1112 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1114 (defun shr-tag-style (_dom)
1117 (defun shr-tag-script (_dom)
1120 (defun shr-tag-comment (_dom)
1123 (defun shr-dom-to-xml (dom)
1128 (defun shr-dom-print (dom)
1129 "Convert DOM into a string containing the xml representation."
1130 (insert (format "<%s" (dom-tag dom
)))
1131 (dolist (attr (dom-attributes dom
))
1132 ;; Ignore attributes that start with a colon because they are
1133 ;; private elements.
1134 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1135 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1138 (dolist (elem (dom-children dom
))
1142 ((eq (dom-tag elem
) 'comment
)
1144 ((or (not (eq (dom-tag elem
) 'image
))
1145 ;; Filter out blocked elements inside the SVG image.
1146 (not (setq url
(dom-attr elem
':xlink
:href
)))
1147 (not shr-blocked-images
)
1148 (not (string-match shr-blocked-images url
)))
1150 (shr-dom-print elem
)))))
1151 (insert (format "</%s>" (dom-tag dom
))))
1153 (defun shr-tag-svg (dom)
1154 (when (and (image-type-available-p 'svg
)
1155 (not shr-inhibit-images
))
1156 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1159 (defun shr-tag-sup (dom)
1160 (let ((start (point)))
1162 (put-text-property start
(point) 'display
'(raise 0.5))))
1164 (defun shr-tag-sub (dom)
1165 (let ((start (point)))
1167 (put-text-property start
(point) 'display
'(raise -
0.5))))
1169 (defun shr-tag-label (dom)
1171 (shr-ensure-paragraph))
1173 (defun shr-tag-p (dom)
1174 (shr-ensure-paragraph)
1176 (shr-ensure-paragraph))
1178 (defun shr-tag-div (dom)
1179 (shr-ensure-newline)
1181 (shr-ensure-newline))
1183 (defun shr-tag-s (dom)
1184 (shr-fontize-dom dom
'shr-strike-through
))
1186 (defun shr-tag-del (dom)
1187 (shr-fontize-dom dom
'shr-strike-through
))
1189 (defun shr-tag-b (dom)
1190 (shr-fontize-dom dom
'bold
))
1192 (defun shr-tag-i (dom)
1193 (shr-fontize-dom dom
'italic
))
1195 (defun shr-tag-em (dom)
1196 (shr-fontize-dom dom
'italic
))
1198 (defun shr-tag-strong (dom)
1199 (shr-fontize-dom dom
'bold
))
1201 (defun shr-tag-u (dom)
1202 (shr-fontize-dom dom
'underline
))
1204 (defun shr-tag-tt (dom)
1205 (let ((shr-current-font 'default
))
1208 (defun shr-parse-style (style)
1211 (when (string-match "\n" style
)
1212 (setq style
(replace-match " " t t style
))))
1214 (dolist (elem (split-string style
";"))
1216 (setq elem
(split-string elem
":"))
1217 (when (and (car elem
)
1219 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1220 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1221 (when (string-match " *!important\\'" value
)
1222 (setq value
(substring value
0 (match-beginning 0))))
1223 (unless (equal value
"inherit")
1224 (push (cons (intern name obarray
)
1229 (defun shr-tag-base (dom)
1230 (when-let (base (dom-attr dom
'href
))
1231 (setq shr-base
(shr-parse-base base
)))
1234 (defun shr-tag-a (dom)
1235 (let ((url (dom-attr dom
'href
))
1236 (title (dom-attr dom
'title
))
1240 (when (and shr-target-id
1241 (equal (dom-attr dom
'name
) shr-target-id
))
1242 ;; We have a zero-length <a name="foo"> element, so just
1243 ;; insert... something.
1244 (when (= start
(point))
1245 (shr-ensure-newline)
1247 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1249 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1251 (defun shr-tag-object (dom)
1252 (unless shr-inhibit-images
1253 (let ((start (point))
1254 url multimedia image
)
1255 (when-let (type (dom-attr dom
'type
))
1256 (when (string-match "\\`image/svg" type
)
1257 (setq url
(dom-attr dom
'data
)
1259 (dolist (child (dom-non-text-children dom
))
1261 ((eq (dom-tag child
) 'embed
)
1262 (setq url
(or url
(dom-attr child
'src
))
1264 ((and (eq (dom-tag child
) 'param
)
1265 (equal (dom-attr child
'name
) "movie"))
1266 (setq url
(or url
(dom-attr child
'value
))
1271 (shr-tag-img dom url
)
1274 (shr-insert " [multimedia] ")
1275 (shr-urlify start
(shr-expand-url url
)))))
1277 (shr-generic dom
)))))
1279 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1285 "Preferences for media types.
1286 The key element should be a regexp matched against the type of the source or
1287 url if no type is specified. The value should be a float in the range 0.0 to
1288 1.0. Media elements with higher value are preferred."
1291 :type
'(alist :key-type regexp
:value-type float
))
1293 (defun shr--get-media-pref (elem)
1294 "Determine the preference for ELEM.
1295 The preference is a float determined from `shr-prefer-media-type'."
1296 (let ((type (dom-attr elem
'type
))
1299 (setq type
(dom-attr elem
'src
)))
1301 (dolist (pref shr-prefer-media-type-alist
)
1304 (string-match-p (car pref
) type
))
1305 (setq p
(cdr pref
)))))
1308 (defun shr--extract-best-source (dom &optional url pref
)
1309 "Extract the best `:src' property from <source> blocks in DOM."
1310 (setq pref
(or pref -
1.0))
1312 (dolist (elem (dom-non-text-children dom
))
1313 (when (and (eq (dom-tag elem
) 'source
)
1316 (shr--get-media-pref elem
))))
1318 url
(dom-attr elem
'src
))
1319 ;; libxml's html parser isn't HTML5 compliant and non terminated
1320 ;; source tags might end up as children. So recursion it is...
1321 (dolist (child (dom-non-text-children elem
))
1322 (when (eq (dom-tag child
) 'source
)
1323 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1324 (when (< pref
(cdr ret
))
1326 pref
(cdr ret
)))))))))
1329 (defun shr-tag-video (dom)
1330 (let ((image (dom-attr dom
'poster
))
1331 (url (dom-attr dom
'src
))
1334 (setq url
(car (shr--extract-best-source dom
))))
1336 (shr-tag-img nil image
)
1337 (shr-insert " [video] "))
1338 (shr-urlify start
(shr-expand-url url
))))
1340 (defun shr-tag-audio (dom)
1341 (let ((url (dom-attr dom
'src
))
1344 (setq url
(car (shr--extract-best-source dom
))))
1345 (shr-insert " [audio] ")
1346 (shr-urlify start
(shr-expand-url url
))))
1348 (defun shr-tag-img (dom &optional url
)
1351 (> (length (dom-attr dom
'src
)) 0)))
1352 (when (> (current-column) 0)
1354 (let ((alt (dom-attr dom
'alt
))
1355 (url (shr-expand-url (or url
(dom-attr dom
'src
)))))
1356 (let ((start (point-marker)))
1357 (when (zerop (length alt
))
1360 ((or (member (dom-attr dom
'height
) '("0" "1"))
1361 (member (dom-attr dom
'width
) '("0" "1")))
1362 ;; Ignore zero-sized or single-pixel images.
1364 ((and (not shr-inhibit-images
)
1365 (string-match "\\`data:" url
))
1366 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1368 (funcall shr-put-image-function image alt
)
1370 ((and (not shr-inhibit-images
)
1371 (string-match "\\`cid:" url
))
1372 (let ((url (substring url
(match-end 0)))
1374 (if (or (not shr-content-function
)
1375 (not (setq image
(funcall shr-content-function url
))))
1377 (funcall shr-put-image-function image alt
))))
1378 ((or shr-inhibit-images
1379 (and shr-blocked-images
1380 (string-match shr-blocked-images url
)))
1381 (setq shr-start
(point))
1382 (if (> (string-width alt
) 8)
1383 (shr-insert (truncate-string-to-width alt
8))
1385 ((and (not shr-ignore-cache
)
1386 (url-is-cached (shr-encode-url url
)))
1387 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
1390 (when (and shr-ignore-cache
1391 (url-is-cached (shr-encode-url url
)))
1392 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1393 (when (file-exists-p file
)
1394 (delete-file file
))))
1396 (shr-encode-url url
) 'shr-image-fetched
1397 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
1399 (when (zerop shr-table-depth
) ;; We are not in a table.
1400 (put-text-property start
(point) 'keymap shr-map
)
1401 (put-text-property start
(point) 'shr-alt alt
)
1402 (put-text-property start
(point) 'image-url url
)
1403 (put-text-property start
(point) 'image-displayer
1404 (shr-image-displayer shr-content-function
))
1405 (put-text-property start
(point) 'help-echo
1407 (or (dom-attr dom
'title
) alt
))))))))
1409 (defun shr-tag-pre (dom)
1410 (let ((shr-folding-mode 'none
)
1411 (shr-current-font 'default
))
1412 (shr-ensure-newline)
1414 (shr-ensure-newline)))
1416 (defun shr-tag-blockquote (dom)
1417 (shr-ensure-paragraph)
1418 (let ((start (point))
1419 (shr-indentation (+ shr-indentation
1420 (* 4 shr-table-separator-pixel-width
))))
1422 (shr-ensure-paragraph)
1423 (shr-mark-fill start
)))
1425 (defun shr-tag-dl (dom)
1426 (shr-ensure-paragraph)
1428 (shr-ensure-paragraph))
1430 (defun shr-tag-dt (dom)
1431 (shr-ensure-newline)
1433 (shr-ensure-newline))
1435 (defun shr-tag-dd (dom)
1436 (shr-ensure-newline)
1437 (let ((shr-indentation (+ shr-indentation
1438 (* 4 shr-table-separator-pixel-width
))))
1441 (defun shr-tag-ul (dom)
1442 (shr-ensure-paragraph)
1443 (let ((shr-list-mode 'ul
))
1445 (shr-ensure-paragraph))
1447 (defun shr-tag-ol (dom)
1448 (shr-ensure-paragraph)
1449 (let ((shr-list-mode 1))
1451 (shr-ensure-paragraph))
1453 (defun shr-tag-li (dom)
1454 (shr-ensure-newline)
1455 (let ((start (point)))
1457 (if (numberp shr-list-mode
)
1459 (format "%d " shr-list-mode
)
1460 (setq shr-list-mode
(1+ shr-list-mode
)))
1461 (car shr-internal-bullet
)))
1462 (width (if (numberp shr-list-mode
)
1463 (shr-string-pixel-width bullet
)
1464 (cdr shr-internal-bullet
))))
1466 (shr-mark-fill start
)
1467 (let ((shr-indentation (+ shr-indentation width
)))
1468 (put-text-property start
(1+ start
)
1469 'shr-continuation-indentation shr-indentation
)
1470 (put-text-property start
(1+ start
) 'shr-prefix-length
(length bullet
))
1471 (shr-generic dom
)))))
1473 (defun shr-mark-fill (start)
1474 ;; We may not have inserted any text to fill.
1475 (unless (= start
(point))
1476 (put-text-property start
(1+ start
)
1477 'shr-indentation shr-indentation
)))
1479 (defun shr-tag-br (dom)
1480 (when (and (not (bobp))
1481 ;; Only add a newline if we break the current line, or
1482 ;; the previous line isn't a blank line.
1484 (and (> (- (point) 2) (point-min))
1485 (not (= (char-after (- (point) 2)) ?
\n)))))
1489 (defun shr-tag-span (dom)
1492 (defun shr-tag-h1 (dom)
1493 (shr-heading dom
(if shr-use-fonts
1494 '(variable-pitch (:height
1.3 :weight bold
))
1497 (defun shr-tag-h2 (dom)
1498 (shr-heading dom
'bold
))
1500 (defun shr-tag-h3 (dom)
1501 (shr-heading dom
'italic
))
1503 (defun shr-tag-h4 (dom)
1506 (defun shr-tag-h5 (dom)
1509 (defun shr-tag-h6 (dom)
1512 (defun shr-tag-hr (_dom)
1513 (shr-ensure-newline)
1514 (insert (make-string (if (not shr-use-fonts
)
1516 (1+ (/ shr-internal-width
1517 shr-table-separator-pixel-width
)))
1521 (defun shr-tag-title (dom)
1522 (shr-heading dom
'bold
'underline
))
1524 (defun shr-tag-font (dom)
1525 (let* ((start (point))
1526 (color (dom-attr dom
'color
))
1527 (shr-stylesheet (nconc (list (cons 'color color
))
1531 (shr-colorize-region start
(point) color
1532 (cdr (assq 'background-color shr-stylesheet
))))))
1534 ;;; Table rendering algorithm.
1536 ;; Table rendering is the only complicated thing here. We do this by
1537 ;; first counting how many TDs there are in each TR, and registering
1538 ;; how wide they think they should be ("width=45%", etc). Then we
1539 ;; render each TD separately (this is done in temporary buffers, so
1540 ;; that we can use all the rendering machinery as if we were in the
1541 ;; main buffer). Now we know how much space each TD really takes, so
1542 ;; we then render everything again with the new widths, and finally
1543 ;; insert all these boxes into the main buffer.
1544 (defun shr-tag-table-1 (dom)
1545 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1546 (let* ((shr-inhibit-images t
)
1547 (shr-table-depth (1+ shr-table-depth
))
1548 (shr-kinsoku-shorten t
)
1549 ;; Find all suggested widths.
1550 (columns (shr-column-specs dom
))
1551 ;; Compute how many pixels wide each TD should be.
1552 (suggested-widths (shr-pro-rate-columns columns
))
1553 ;; Do a "test rendering" to see how big each TD is (this can
1554 ;; be smaller (if there's little text) or bigger (if there's
1555 ;; unbreakable text).
1556 (elems (or (dom-attr dom
'shr-suggested-widths
)
1557 (shr-make-table dom suggested-widths nil
1558 'shr-suggested-widths
)))
1559 (sketch (loop for line in elems
1560 collect
(mapcar #'car line
)))
1561 (natural (loop for line in elems
1562 collect
(mapcar #'cdr line
)))
1563 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1564 ;; This probably won't work very well.
1565 (when (> (+ (loop for width across sketch-widths
1567 shr-indentation shr-table-separator-pixel-width
)
1569 (setq truncate-lines t
))
1570 ;; Then render the table again with these new "hard" widths.
1571 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1573 (defun shr-tag-table (dom)
1574 (shr-ensure-paragraph)
1575 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1576 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1577 (body (dom-non-text-children (or (dom-child-by-tag dom
'tbody
)
1579 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1580 (bgcolor (dom-attr dom
'bgcolor
))
1582 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1584 (nheader (if header
(shr-max-columns header
)))
1585 (nbody (if body
(shr-max-columns body
)))
1586 (nfooter (if footer
(shr-max-columns footer
))))
1587 (if (and (not caption
)
1589 (not (dom-child-by-tag dom
'tbody
))
1590 (not (dom-child-by-tag dom
'tr
))
1592 ;; The table is totally invalid and just contains random junk.
1593 ;; Try to output it anyway.
1595 ;; It's a real table, so render it.
1596 (if (dom-attr dom
'shr-fixed-table
)
1597 (shr-tag-table-1 dom
)
1598 ;; Only fix up the table once.
1602 (if caption
`((tr nil
(td nil
,@caption
))))
1606 ;; header + body + footer
1607 (if (= nheader nbody
)
1608 (if (= nbody nfooter
)
1609 `((tr nil
(td nil
(table nil
1611 ,@body
,@footer
)))))
1612 (nconc `((tr nil
(td nil
(table nil
1617 `((tr nil
(td nil
(table
1619 nil
,@footer
))))))))
1620 (nconc `((tr nil
(td nil
(table nil
(tbody
1622 (if (= nbody nfooter
)
1623 `((tr nil
(td nil
(table
1624 nil
(tbody nil
,@body
1626 (nconc `((tr nil
(td nil
(table
1631 `((tr nil
(td nil
(table
1637 (if (= nheader nbody
)
1638 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1641 `(,@header
(tr nil
(td nil
(table
1642 nil
(tbody nil
,@body
)))))
1643 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1644 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1647 (if (= nbody nfooter
)
1648 `((tr nil
(td nil
(table
1649 nil
(tbody nil
,@body
,@footer
)))))
1650 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1653 `((tr nil
(td nil
(table
1654 nil
(tbody nil
,@footer
)))))))))
1656 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1658 (dom-set-attribute table
'shr-fixed-table t
)
1659 (setcdr dom
(cdr table
))
1660 (shr-tag-table-1 dom
))))
1662 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1664 ;; Finally, insert all the images after the table. The Emacs buffer
1665 ;; model isn't strong enough to allow us to put the images actually
1667 (when (zerop shr-table-depth
)
1669 (shr-expand-alignments start
(point)))
1670 (dolist (elem (dom-by-tag dom
'object
))
1671 (shr-tag-object elem
))
1672 (dolist (elem (dom-by-tag dom
'img
))
1673 (shr-tag-img elem
)))))
1675 (defun shr-insert-table (table widths
)
1676 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
1678 (shr-table-separator-length (if collapse
0 1))
1679 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
1681 (setq shr-table-id
(1+ shr-table-id
))
1683 (shr-insert-table-ruler widths
))
1685 (let ((start (point))
1688 (height (let ((max 0))
1689 (dolist (column row
)
1690 (setq max
(max max
(nth 2 column
))))
1692 (dotimes (i (max height
1))
1694 (insert shr-table-vertical-line
"\n"))
1695 (dolist (column row
)
1696 (when (> (nth 2 column
) -
1)
1698 ;; Sum up all the widths from the column. (There may be
1699 ;; more than one if this is a "colspan" column.)
1700 (dotimes (i (nth 4 column
))
1701 ;; The colspan directive may be wrong and there may not be
1702 ;; that number of columns.
1703 (when (<= column-number
(1- (length widths
)))
1704 (setq align
(+ align
1705 (aref widths column-number
)
1706 (* 2 shr-table-separator-pixel-width
))))
1707 (setq column-number
(1+ column-number
)))
1708 (let ((lines (nth 3 column
))
1709 (pixel-align (if (not shr-use-fonts
)
1710 (* align
(frame-char-width))
1712 (dolist (line lines
)
1714 (let ((start (point)))
1718 'display
`(space :align-to
(,pixel-align
))
1719 'face
(and (> (length line
) 0)
1720 (shr-face-background
1722 (1- (length line
)) 'face line
)))
1723 'shr-table-indent shr-table-id
)
1724 shr-table-vertical-line
)
1725 (shr-colorize-region
1726 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1728 ;; Add blank lines at padding at the bottom of the TD,
1730 (dotimes (i (- height
(length lines
)))
1732 (let ((start (point)))
1733 (insert (propertize " "
1734 'display
`(space :align-to
(,pixel-align
))
1735 'shr-table-indent shr-table-id
)
1736 shr-table-vertical-line
)
1737 (shr-colorize-region
1738 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1739 (forward-line 1))))))
1741 (shr-insert-table-ruler widths
)))
1742 (unless (= start
(point))
1743 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
1745 (defun shr-face-background (face)
1747 (let ((background nil
))
1749 (when (and (consp elem
)
1750 (eq (car elem
) :background
))
1751 (setq background
(cadr elem
))))
1753 (list :background background
)))))
1755 (defun shr-expand-alignments (start end
)
1756 (while (< (setq start
(next-single-property-change
1757 start
'shr-table-id nil end
))
1760 (let* ((shr-use-fonts t
)
1761 (id (get-text-property (point) 'shr-table-id
))
1762 (base (shr-pixel-column))
1766 (while (setq elem
(text-property-any
1767 (point) end
'shr-table-indent id
))
1769 (let ((align (get-text-property (point) 'display
)))
1770 (put-text-property (point) (1+ (point)) 'display
1771 `(space :align-to
(,(+ (car (nth 2 align
))
1773 (forward-char 1)))))
1774 (setq start
(1+ start
))))
1776 (defun shr-insert-table-ruler (widths)
1777 (when shr-table-horizontal-line
1779 (> shr-indentation
0))
1781 (insert shr-table-corner
)
1782 (let ((total-width 0))
1783 (dotimes (i (length widths
))
1784 (setq total-width
(+ total-width
(aref widths i
)
1785 (* shr-table-separator-pixel-width
2)))
1786 (insert (make-string (1+ (/ (aref widths i
)
1787 shr-table-separator-pixel-width
))
1788 shr-table-horizontal-line
)
1790 'display
`(space :align-to
(,total-width
))
1791 'shr-table-indent shr-table-id
)
1795 (defun shr-table-widths (table natural-table suggested-widths
)
1796 (let* ((length (length suggested-widths
))
1797 (widths (make-vector length
0))
1798 (natural-widths (make-vector length
0)))
1801 (dolist (column row
)
1802 (aset widths i
(max (aref widths i
) column
))
1804 (dolist (row natural-table
)
1806 (dolist (column row
)
1807 (aset natural-widths i
(max (aref natural-widths i
) column
))
1809 (let ((extra (- (apply '+ (append suggested-widths nil
))
1810 (apply '+ (append widths nil
))
1811 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
1812 (expanded-columns 0))
1813 ;; We have extra, unused space, so divide this space amongst the
1816 ;; If the natural width is wider than the rendered width, we
1817 ;; want to allow the column to expand.
1819 (when (> (aref natural-widths i
) (aref widths i
))
1820 (setq expanded-columns
(1+ expanded-columns
))))
1822 (when (> (aref natural-widths i
) (aref widths i
))
1824 (aref natural-widths i
)
1825 (+ (/ extra expanded-columns
)
1826 (aref widths i
))))))))
1829 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
1830 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
1831 (let ((data (shr-make-table-1 dom widths fill
)))
1832 (push (list (list dom widths fill
) data
)
1834 (when storage-attribute
1835 (dom-set-attribute dom storage-attribute data
))
1838 (defun shr-make-table-1 (dom widths
&optional fill
)
1840 (rowspans (make-vector (length widths
) 0))
1841 (colspan-remaining 0)
1842 colspan-width colspan-count
1844 (dolist (row (dom-non-text-children dom
))
1845 (when (eq (dom-tag row
) 'tr
)
1847 (columns (dom-non-text-children row
))
1851 (while (< i
(length widths
))
1852 ;; If we previously had a rowspan definition, then that
1853 ;; means that we now have a "missing" td/th element here.
1854 ;; So just insert a dummy, empty one to (sort of) emulate
1857 (if (zerop (aref rowspans i
))
1859 (aset rowspans i
(1- (aref rowspans i
)))
1861 (when (and (not (stringp column
))
1862 (or (memq (dom-tag column
) '(td th
))
1864 (when-let (span (dom-attr column
'rowspan
))
1865 (aset rowspans i
(+ (aref rowspans i
)
1866 (1- (string-to-number span
)))))
1867 ;; Sanity check for invalid column-spans.
1868 (when (>= width-column
(length widths
))
1869 (setq width-column
0))
1872 (aref widths width-column
)
1873 (* 10 shr-table-separator-pixel-width
)))
1874 (when (setq colspan
(dom-attr column
'colspan
))
1875 (setq colspan
(min (string-to-number colspan
)
1876 ;; The colspan may be wrong, so
1877 ;; truncate it to the length of the
1878 ;; remaining columns.
1879 (- (length widths
) i
)))
1880 (dotimes (j (1- colspan
))
1882 (if (> (+ i
1 j
) (1- (length widths
)))
1883 ;; If we have a colspan spec that's longer
1884 ;; than the table is wide, just use the last
1885 ;; width as the width.
1886 (aref widths
(1- (length widths
)))
1887 ;; Sum up the widths of the columns we're
1890 shr-table-separator-length
1891 (aref widths
(+ i
1 j
))))))
1892 (setq width-column
(+ width-column
(1- colspan
))
1893 colspan-count colspan
1894 colspan-remaining colspan
))
1896 (let ((data (shr-render-td column width fill
)))
1898 (> colspan-remaining
0))
1900 (setq colspan-width
(car data
))
1901 (let ((this-width (/ colspan-width colspan-count
)))
1902 (push (cons this-width
(cadr data
)) tds
)
1903 (setq colspan-remaining
(1- colspan-remaining
))))
1905 (push (cons (car data
) (cadr data
)) tds
)
1909 (dotimes (c (1- colspan
))
1913 (list 0 0 -
1 nil
1 nil nil
)
1917 width-column
(1+ width-column
))))
1918 (push (nreverse tds
) trs
))))
1921 (defun shr-pixel-buffer-width ()
1922 (if (not shr-use-fonts
)
1924 (goto-char (point-min))
1928 (setq max
(max max
(current-column)))
1931 (if (get-buffer-window)
1932 (car (window-text-pixel-size nil
(point-min) (point-max)))
1933 (save-window-excursion
1934 (set-window-buffer nil
(current-buffer))
1935 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
1937 (defun shr-render-td (dom width fill
)
1938 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
1939 (or (dom-attr dom cache
)
1942 (dolist (attr (dom-attributes dom
))
1943 (let ((name (symbol-name (car attr
))))
1944 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
1945 (let ((cache-width (string-to-number
1946 (match-string 1 name
))))
1947 (when (and (>= cache-width width
)
1948 (<= (car (cdr attr
)) width
))
1949 (setq result
(cdr attr
)))))))
1951 (let ((result (shr-render-td-1 dom width fill
)))
1952 (dom-set-attribute dom cache result
)
1955 (defun shr-render-td-1 (dom width fill
)
1957 (let ((bgcolor (dom-attr dom
'bgcolor
))
1958 (fgcolor (dom-attr dom
'fgcolor
))
1959 (style (dom-attr dom
'style
))
1960 (shr-stylesheet shr-stylesheet
)
1964 (setq style
(and (string-match "color" style
)
1965 (shr-parse-style style
))))
1967 (setq style
(nconc (list (cons 'background-color bgcolor
))
1970 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1972 (setq shr-stylesheet
(append style shr-stylesheet
)))
1973 (let ((shr-internal-width width
)
1974 (shr-indentation 0))
1976 (save-window-excursion
1977 (set-window-buffer nil
(current-buffer))
1980 (or (dom-attr dom
'shr-td-cache-natural
)
1981 (let ((natural (max (shr-pixel-buffer-width)
1982 (shr-dom-max-natural-width dom
0))))
1983 (dom-set-attribute dom
'shr-td-cache-natural natural
)
1985 (if (and natural-width
1986 (<= natural-width width
))
1987 (setq max-width natural-width
)
1988 (let ((shr-internal-width width
))
1989 (shr-fill-lines (point-min) (point-max))
1990 (setq max-width
(shr-pixel-buffer-width)))))
1991 (goto-char (point-max))
1992 ;; Delete padding at the bottom of the TDs.
1996 (skip-chars-backward " \t\n")
1999 (goto-char (point-min))
2002 (count-lines (point-min) (point-max))
2003 (split-string (buffer-string) "\n")
2004 (if (dom-attr dom
'colspan
)
2005 (string-to-number (dom-attr dom
'colspan
))
2007 (cdr (assq 'color shr-stylesheet
))
2008 (cdr (assq 'background-color shr-stylesheet
))))))
2010 (defun shr-dom-max-natural-width (dom max
)
2011 (if (eq (dom-tag dom
) 'table
)
2013 (loop for line in
(dom-attr dom
'shr-suggested-widths
)
2015 shr-table-separator-length
2016 (loop for elem in line
2019 (* 2 shr-table-separator-length
)))))
2021 (dolist (child (dom-children dom
))
2022 (unless (stringp child
)
2023 (setq max
(max (shr-dom-max-natural-width child max
)))))
2026 (defun shr-buffer-width ()
2027 (goto-char (point-min))
2031 (setq max
(max max
(current-column)))
2035 (defun shr-pro-rate-columns (columns)
2036 (let ((total-percentage 0)
2037 (widths (make-vector (length columns
) 0)))
2038 (dotimes (i (length columns
))
2039 (setq total-percentage
(+ total-percentage
(aref columns i
))))
2040 (setq total-percentage
(/ 1.0 total-percentage
))
2041 (dotimes (i (length columns
))
2042 (aset widths i
(max (truncate (* (aref columns i
)
2044 (- shr-internal-width
2045 (* (1+ (length columns
))
2046 shr-table-separator-pixel-width
))))
2050 ;; Return a summary of the number and shape of the TDs in the table.
2051 (defun shr-column-specs (dom)
2052 (let ((columns (make-vector (shr-max-columns dom
) 1)))
2053 (dolist (row (dom-non-text-children dom
))
2054 (when (eq (dom-tag row
) 'tr
)
2056 (dolist (column (dom-non-text-children row
))
2057 (when (memq (dom-tag column
) '(td th
))
2058 (let ((width (dom-attr column
'width
)))
2060 (string-match "\\([0-9]+\\)%" width
)
2061 (not (zerop (setq width
(string-to-number
2062 (match-string 1 width
))))))
2063 (aset columns i
(/ width
100.0))))
2064 (setq i
(1+ i
)))))))
2067 (defun shr-count (dom elem
)
2069 (dolist (sub (dom-children dom
))
2070 (when (and (not (stringp sub
))
2071 (eq (dom-tag sub
) elem
))
2075 (defun shr-max-columns (dom)
2077 (dolist (row (dom-children dom
))
2078 (when (and (not (stringp row
))
2079 (eq (dom-tag row
) 'tr
))
2080 (setq max
(max max
(+ (shr-count row
'td
)
2081 (shr-count row
'th
))))))
2086 ;;; shr.el ends here