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
))))
207 (defun shr-insert-document (dom)
208 "Render the parsed document DOM into the current buffer.
209 DOM should be a parse tree as generated by
210 `libxml-parse-html-region' or similar."
211 (setq shr-content-cache nil
)
212 (let ((start (point))
218 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
219 (shr-internal-bullet (cons shr-bullet
220 (shr-string-pixel-width shr-bullet
)))
221 (shr-internal-width (or (and shr-width
222 (if (not shr-use-fonts
)
224 (* shr-width
(frame-char-width))))
225 (if (not shr-use-fonts
)
227 (- (window-pixel-width)
228 (* (frame-fringe-width) 2))))))
230 (shr-fill-lines start
(point))
231 (shr-remove-trailing-whitespace start
(point))
233 (message "%s" shr-warning
))))
235 (defun shr-remove-trailing-whitespace (start end
)
236 (let ((width (window-width)))
238 (narrow-to-region start end
)
242 (when (> (shr-previous-newline-padding-width (current-column)) width
)
243 (dolist (overlay (overlays-at (point)))
244 (when (overlay-get overlay
'before-string
)
245 (overlay-put overlay
'before-string nil
))))
248 (defun shr-copy-url (&optional image-url
)
249 "Copy the URL under point to the kill ring.
250 If IMAGE-URL (the prefix) is non-nil, or there is no link under
251 point, but there is an image under point then copy the URL of the
252 image under point instead.
253 If called twice, then try to fetch the URL and see whether it
254 redirects somewhere else."
256 (let ((url (or (get-text-property (point) 'shr-url
)
257 (get-text-property (point) 'image-url
))))
260 (message "No URL under point"))
261 ;; Resolve redirected URLs.
262 ((equal url
(car kill-ring
))
267 (eq (car a
) :redirect
))
270 (goto-char (point-min))
271 ;; Remove common tracking junk from the URL.
272 (when (re-search-forward ".utm_.*" nil t
)
273 (replace-match "" t t
))
274 (message "Copied %s" (buffer-string))
275 (copy-region-as-kill (point-min) (point-max)))))
277 ;; Copy the URL to the kill ring.
280 (insert (url-encode-url url
))
281 (copy-region-as-kill (point-min) (point-max))
282 (message "Copied %s" (buffer-string)))))))
284 (defun shr-next-link ()
285 "Skip to the next link."
287 (let ((skip (text-property-any (point) (point-max) 'help-echo nil
)))
289 (not (setq skip
(text-property-not-all skip
(point-max)
291 (message "No next link")
293 (message "%s" (get-text-property (point) 'help-echo
)))))
295 (defun shr-previous-link ()
296 "Skip to the previous link."
298 (let ((start (point))
300 ;; Skip past the current link.
301 (while (and (not (bobp))
302 (get-text-property (point) 'help-echo
))
304 ;; Find the previous link.
305 (while (and (not (bobp))
306 (not (setq found
(get-text-property (point) 'help-echo
))))
310 (message "No previous link")
312 ;; Put point at the start of the link.
313 (while (and (not (bobp))
314 (get-text-property (point) 'help-echo
))
317 (message "%s" (get-text-property (point) 'help-echo
)))))
319 (defun shr-show-alt-text ()
320 "Show the ALT text of the image under point."
322 (let ((text (get-text-property (point) 'shr-alt
)))
324 (message "No image under point")
325 (message "%s" (shr-fill-text text
)))))
327 (defun shr-browse-image (&optional copy-url
)
328 "Browse the image under point.
329 If COPY-URL (the prefix if called interactively) is non-nil, copy
330 the URL of the image to the kill buffer instead."
332 (let ((url (get-text-property (point) 'image-url
)))
335 (message "No image under point"))
339 (copy-region-as-kill (point-min) (point-max))
340 (message "Copied %s" url
)))
342 (message "Browsing %s..." url
)
345 (defun shr-insert-image ()
346 "Insert the image under point into the buffer."
348 (let ((url (get-text-property (point) 'image-url
)))
350 (message "No image under point")
351 (message "Inserting %s..." url
)
352 (url-retrieve url
'shr-image-fetched
353 (list (current-buffer) (1- (point)) (point-marker))
356 (defun shr-zoom-image ()
357 "Toggle the image size.
358 The size will be rotated between the default size, the original
359 size, and full-buffer size."
361 (let ((url (get-text-property (point) 'image-url
))
362 (size (get-text-property (point) 'image-size
))
363 (buffer-read-only nil
))
365 (message "No image under point")
366 ;; Delete the old picture.
367 (while (get-text-property (point) 'image-url
)
370 (let ((start (point)))
371 (while (get-text-property (point) 'image-url
)
374 (put-text-property start
(point) 'display nil
)
375 (when (> (- (point) start
) 2)
376 (delete-region start
(1- (point)))))
377 (message "Inserting %s..." url
)
378 (url-retrieve url
'shr-image-fetched
379 (list (current-buffer) (1- (point)) (point-marker)
381 (cond ((or (eq size
'default
)
390 ;;; Utility functions.
392 (defsubst shr-generic
(dom)
393 (dolist (sub (dom-children dom
))
398 (defun shr-descend (dom)
401 ;; Allow other packages to override (or provide) rendering
403 (cdr (assq (dom-tag dom
) shr-external-rendering-functions
))
404 (intern (concat "shr-tag-" (symbol-name (dom-tag dom
))) obarray
)))
405 (style (dom-attr dom
'style
))
406 (shr-stylesheet shr-stylesheet
)
407 (shr-depth (1+ shr-depth
))
409 ;; shr uses about 12 frames per nested node.
410 (if (> shr-depth
(/ max-specpdl-size
12))
411 (setq shr-warning
"Too deeply nested to render properly; consider increasing `max-specpdl-size'")
413 (if (string-match "color\\|display\\|border-collapse" style
)
414 (setq shr-stylesheet
(nconc (shr-parse-style style
)
417 ;; If we have a display:none, then just ignore this part of the DOM.
418 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
419 (if (fboundp function
)
420 (funcall function dom
)
422 (when (and shr-target-id
423 (equal (dom-attr dom
'id
) shr-target-id
))
424 ;; If the element was empty, we don't have anything to put the
425 ;; anchor on. So just insert a dummy character.
426 (when (= start
(point))
428 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
429 ;; If style is set, then this node has set the color.
433 (cdr (assq 'color shr-stylesheet
))
434 (cdr (assq 'background-color shr-stylesheet
))))))))
436 (defun shr-fill-text (text)
437 (if (zerop (length text
))
440 (let ((shr-indentation 0)
442 (shr-internal-width (- (window-pixel-width)
443 (* (frame-fringe-width) 2))))
447 (define-inline shr-char-breakable-p
(char)
448 "Return non-nil if a line can be broken before and after CHAR."
449 (inline-quote (aref fill-find-break-point-function-table
,char
)))
450 (define-inline shr-char-nospace-p
(char)
451 "Return non-nil if no space is required before and after CHAR."
452 (inline-quote (aref fill-nospace-between-words-table
,char
)))
454 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
455 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
456 ;; parentheses, and so on, that should not be placed in the beginning
457 ;; of a line or the end of a line.
458 (define-inline shr-char-kinsoku-bol-p
(char)
459 "Return non-nil if a line ought not to begin with CHAR."
460 (inline-letevals (char)
461 (inline-quote (and (not (eq ,char ?
'))
462 (aref (char-category-set ,char
) ?
>)))))
463 (define-inline shr-char-kinsoku-eol-p
(char)
464 "Return non-nil if a line ought not to end with CHAR."
465 (inline-quote (aref (char-category-set ,char
) ?
<)))
466 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
467 (load "kinsoku" nil t
))
469 (defun shr-pixel-column ()
470 (if (not shr-use-fonts
)
472 (if (not (get-buffer-window (current-buffer)))
473 (save-window-excursion
474 (set-window-buffer nil
(current-buffer))
475 (car (window-text-pixel-size nil
(line-beginning-position) (point))))
476 (car (window-text-pixel-size nil
(line-beginning-position) (point))))))
478 (defun shr-pixel-region ()
479 (- (shr-pixel-column)
482 (shr-pixel-column))))
484 (defun shr-string-pixel-width (string)
485 (if (not shr-use-fonts
)
489 (shr-pixel-column))))
491 (defun shr-insert (text)
492 (when (and (not (bolp))
493 (get-text-property (1- (point)) 'image-url
))
496 ((eq shr-folding-mode
'none
)
497 (let ((start (point)))
500 (narrow-to-region start
(point))
501 ;; Remove soft hyphens.
502 (goto-char (point-min))
503 (while (search-forward "Â" nil t
)
504 (replace-match "" t t
))
505 (goto-char (point-max)))))
507 (let ((font-start (point)))
508 (when (and (string-match "\\`[ \t\n\r ]" text
)
510 (not (eq (char-after (1- (point))) ?
)))
512 (let ((start (point))
516 (narrow-to-region start
(point))
518 (when (looking-at "[ \t\n\r ]+")
519 (replace-match "" t t
))
520 (while (re-search-forward "[ \t\n\r ]+" nil t
)
521 (replace-match " " t t
))
522 ;; Remove soft hyphens.
523 (goto-char (point-min))
524 (while (search-forward "Â" nil t
)
525 (replace-match "" t t
))
526 (goto-char (point-max)))
527 ;; We may have removed everything we inserted if if was just
529 (unless (= font-start
(point))
530 ;; Mark all lines that should possibly be folded afterwards.
532 (shr-mark-fill start
))
534 (put-text-property font-start
(point)
536 (or shr-current-font
'variable-pitch
)))))))))
538 (defun shr-fill-lines (start end
)
539 (if (<= shr-internal-width
0)
542 (narrow-to-region start end
)
544 (when (get-text-property (point) 'shr-indentation
)
546 (while (setq start
(next-single-property-change start
'shr-indentation
))
550 (goto-char (point-max)))))
552 (defun shr-vertical-motion (column)
553 (if (not shr-use-fonts
)
554 (move-to-column column
)
557 (vertical-motion (cons (/ column
(frame-char-width)) 0))
561 (defun shr-fill-line ()
562 (let ((shr-indentation (get-text-property (point) 'shr-indentation
))
563 (continuation (get-text-property
564 (point) 'shr-continuation-indentation
))
566 (put-text-property (point) (1+ (point)) 'shr-indentation nil
)
567 (let ((face (get-text-property (point) 'face
))
568 (background-start (point)))
571 (put-text-property background-start
(point) 'face
572 `,(shr-face-background face
))))
574 (setq shr-indentation
(or continuation shr-indentation
))
575 (shr-vertical-motion shr-internal-width
)
576 (when (looking-at " $")
577 (delete-region (point) (line-end-position)))
579 ;; We have to do some folding. First find the first
580 ;; previous point suitable for folding.
581 (if (or (not (shr-find-fill-point (line-beginning-position)))
583 ;; We had unbreakable text (for this width), so just go to
584 ;; the first space and carry on.
587 (skip-chars-forward " ")
588 (search-forward " " (line-end-position) 'move
)))
589 ;; Success; continue.
590 (when (= (preceding-char) ?\s
)
592 (let ((face (get-text-property (point) 'face
))
593 (background-start (point)))
597 (put-text-property background-start
(point) 'face
598 `,(shr-face-background face
))))
600 (shr-vertical-motion shr-internal-width
)
601 (when (looking-at " $")
602 (delete-region (point) (line-end-position))))))
604 (defun shr-find-fill-point (start)
608 (while (not (or (setq failed
(<= (point) start
))
609 (eq (preceding-char) ?
)
610 (eq (following-char) ?
)
611 (shr-char-breakable-p (preceding-char))
612 (shr-char-breakable-p (following-char))
613 (and (shr-char-kinsoku-bol-p (preceding-char))
614 (shr-char-breakable-p (following-char))
615 (not (shr-char-kinsoku-bol-p (following-char))))
616 (shr-char-kinsoku-eol-p (following-char))
620 ;; There's no breakable point, so we give it up.
623 (unless shr-kinsoku-shorten
624 (while (setq found
(re-search-forward
625 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
626 (line-end-position) 'move
)))
628 (not (match-beginning 1)))
629 (goto-char (match-beginning 0)))))
632 ;; Don't put kinsoku-bol characters at the beginning of a line,
633 ;; or kinsoku-eol characters at the end of a line.
636 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
637 (shr-char-kinsoku-eol-p (preceding-char)))
639 (when (setq failed
(<= (point) start
))
640 ;; There's no breakable point that doesn't violate kinsoku,
641 ;; so we look for the second best position.
647 (shr-char-kinsoku-eol-p (following-char)))))
649 ((shr-char-kinsoku-eol-p (preceding-char))
650 ;; Find backward the point where kinsoku-eol characters begin.
655 (and (> (setq count
(1- count
)) 0)
656 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
657 (or (shr-char-kinsoku-eol-p (preceding-char))
658 (shr-char-kinsoku-bol-p (following-char)))))))
659 (when (setq failed
(<= (point) start
))
660 ;; There's no breakable point that doesn't violate kinsoku,
661 ;; so we go to the second best position.
662 (if (looking-at "\\(\\c<+\\)\\c<")
663 (goto-char (match-end 1))
665 ((shr-char-kinsoku-bol-p (following-char))
666 ;; Find forward the point where kinsoku-bol characters end.
670 (and (>= (setq count
(1- count
)) 0)
671 (shr-char-kinsoku-bol-p (following-char))
672 (shr-char-breakable-p (following-char))))))))
673 (when (eq (following-char) ?
)
677 (defun shr-parse-base (url)
678 ;; Always chop off anchors.
679 (when (string-match "#.*" url
)
680 (setq url
(substring url
0 (match-beginning 0))))
681 ;; NB: <base href="" > URI may itself be relative to the document s URI
682 (setq url
(shr-expand-url url
))
683 (let* ((parsed (url-generic-parse-url url
))
684 (local (url-filename parsed
)))
685 (setf (url-filename parsed
) "")
686 ;; Chop off the bit after the last slash.
687 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
688 (setq local
(match-string 1 local
)))
689 ;; Always make the local bit end with a slash.
690 (when (and (not (zerop (length local
)))
691 (not (eq (aref local
(1- (length local
))) ?
/)))
692 (setq local
(concat local
"/")))
693 (list (url-recreate-url parsed
)
698 (autoload 'url-expand-file-name
"url-expand")
700 ;; FIXME This needs some tests writing.
701 ;; Does it even need to exist, given that url-expand-file-name does?
702 (defun shr-expand-url (url &optional base
)
705 ;; shr-parse-base should never call this with non-nil base!
706 (shr-parse-base base
)
707 ;; Bound by the parser.
709 (when (zerop (length url
))
711 ;; Strip leading whitespace
712 (and url
(string-match "\\`\\s-+" url
)
713 (setq url
(substring url
(match-end 0))))
716 (string-match "\\`[a-z]*:" url
))
717 ;; Absolute or empty URI
718 (or url
(nth 3 base
)))
719 ((eq (aref url
0) ?
/)
720 (if (and (> (length url
) 1)
721 (eq (aref url
1) ?
/))
722 ;; //host...; just use the protocol
723 (concat (nth 2 base
) ":" url
)
724 ;; Just use the host name part.
725 (concat (car base
) url
)))
726 ((eq (aref url
0) ?
#)
727 ;; A link to an anchor.
728 (concat (nth 3 base
) url
))
731 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
733 (defun shr-ensure-newline ()
734 (unless (zerop (current-column))
737 (defun shr-ensure-paragraph ()
739 (let ((prefix (get-text-property (line-beginning-position)
740 'shr-prefix-length
)))
746 ;; We're already at a new paragraph; do nothing.
749 (= prefix
(- (point) (line-beginning-position))))
750 ;; Do nothing; we're at the start of a <li>.
754 ;; If the current line is totally blank, and doesn't even
755 ;; have any face properties set, then delete the blank
757 (and (looking-at " *$")
758 (not (get-text-property (point) 'face
))
759 (not (= (next-single-property-change (point) 'face nil
761 (line-end-position)))))
762 (delete-region (match-beginning 0) (match-end 0)))
767 (when (> shr-indentation
0)
769 (if (not shr-use-fonts
)
770 (make-string shr-indentation ?\s
)
773 `(space :width
(,shr-indentation
)))))))
775 (defun shr-fontize-dom (dom &rest types
)
776 (let ((start (point)))
779 (shr-add-font start
(point) type
))))
781 ;; Add face to the region, but avoid putting the font properties on
782 ;; blank text at the start of the line, and the newline at the end, to
784 (defun shr-add-font (start end type
)
787 (while (< (point) end
)
789 (skip-chars-forward " "))
790 (add-face-text-property (point) (min (line-end-position) end
) type t
)
791 (if (< (line-end-position) end
)
795 (defun shr-mouse-browse-url (ev)
796 "Browse the URL under the mouse cursor."
801 (defun shr-browse-url (&optional external mouse-event
)
802 "Browse the URL under point.
803 If EXTERNAL, browse the URL using `shr-external-browser'."
804 (interactive (list current-prefix-arg last-nonmenu-event
))
805 (mouse-set-point mouse-event
)
806 (let ((url (get-text-property (point) 'shr-url
)))
809 (message "No link under point"))
810 ((string-match "^mailto:" url
)
811 (browse-url-mail url
))
814 (funcall shr-external-browser url
)
815 (browse-url url
))))))
817 (defun shr-save-contents (directory)
818 "Save the contents from URL in a file."
819 (interactive "DSave contents of URL to directory: ")
820 (let ((url (get-text-property (point) 'shr-url
)))
822 (message "No link under point")
823 (url-retrieve (shr-encode-url url
)
824 'shr-store-contents
(list url directory
)
827 (defun shr-store-contents (status url directory
)
828 (unless (plist-get status
:error
)
829 (when (or (search-forward "\n\n" nil t
)
830 (search-forward "\r\n\r\n" nil t
))
831 (write-region (point) (point-max)
832 (expand-file-name (file-name-nondirectory url
)
835 (defun shr-image-fetched (status buffer start end
&optional flags
)
836 (let ((image-buffer (current-buffer)))
837 (when (and (buffer-name buffer
)
838 (not (plist-get status
:error
)))
839 (url-store-in-cache image-buffer
)
840 (when (or (search-forward "\n\n" nil t
)
841 (search-forward "\r\n\r\n" nil t
))
842 (let ((data (shr-parse-image-data)))
843 (with-current-buffer buffer
845 (let ((alt (buffer-substring start end
))
846 (properties (text-properties-at start
))
847 (inhibit-read-only t
))
848 (delete-region start end
)
850 (funcall shr-put-image-function data alt flags
)
852 (let ((type (pop properties
))
853 (value (pop properties
)))
854 (unless (memq type
'(display image-size
))
855 (put-text-property start
(point) type value
))))))))))
856 (kill-buffer image-buffer
)))
858 (defun shr-image-from-data (data)
859 "Return an image from the data: URI content DATA."
861 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
863 (let ((param (match-string 4 data
))
864 (payload (url-unhex-string (match-string 5 data
))))
865 (when (string-match "^.*\\(;[ \t]*base64\\)$" param
)
866 (setq payload
(base64-decode-string payload
)))
869 ;; Behind display-graphic-p test.
870 (declare-function image-size
"image.c" (spec &optional pixels frame
))
871 (declare-function image-animate
"image" (image &optional index limit
))
873 (defun shr-put-image (spec alt
&optional flags
)
874 "Insert image SPEC with a string ALT. Return image.
875 SPEC is either an image data blob, or a list where the first
876 element is the data blob and the second element is the content-type."
877 (if (display-graphic-p)
878 (let* ((size (cdr (assq 'size flags
)))
879 (data (if (consp spec
)
882 (content-type (and (consp spec
)
887 (create-image data nil t
:ascent
100
888 :format content-type
))
889 ((eq content-type
'image
/svg
+xml
)
890 (create-image data
'svg t
:ascent
100))
893 (shr-rescale-image data content-type
)))
896 (shr-rescale-image data content-type
))))))
898 ;; When inserting big-ish pictures, put them at the
899 ;; beginning of the line.
900 (when (and (> (current-column) 0)
901 (> (car (image-size image t
)) 400))
903 (if (eq size
'original
)
904 (insert-sliced-image image
(or alt
"*") nil
20 1)
905 (insert-image image
(or alt
"*")))
906 (put-text-property start
(point) 'image-size size
)
907 (when (and shr-image-animate
908 (cond ((fboundp 'image-multi-frame-p
)
909 ;; Only animate multi-frame things that specify a
910 ;; delay; eg animated gifs as opposed to
911 ;; multi-page tiffs. FIXME?
912 (cdr (image-multi-frame-p image
)))
913 ((fboundp 'image-animated-p
)
914 (image-animated-p image
))))
915 (image-animate image nil
60)))
919 (defun shr-rescale-image (data &optional content-type
)
920 "Rescale DATA, if too big, to fit the current buffer."
921 (if (not (and (fboundp 'imagemagick-types
)
922 (get-buffer-window (current-buffer))))
923 (create-image data nil t
:ascent
100)
924 (let ((edges (window-inside-pixel-edges
925 (get-buffer-window (current-buffer)))))
929 :max-width
(truncate (* shr-max-image-proportion
930 (- (nth 2 edges
) (nth 0 edges
))))
931 :max-height
(truncate (* shr-max-image-proportion
932 (- (nth 3 edges
) (nth 1 edges
))))
933 :format content-type
))))
935 ;; url-cache-extract autoloads url-cache.
936 (declare-function url-cache-create-filename
"url-cache" (url))
937 (autoload 'mm-disable-multibyte
"mm-util")
938 (autoload 'browse-url-mail
"browse-url")
940 (defun shr-get-image-data (url)
941 "Get image data for URL.
942 Return a string with image data."
944 (mm-disable-multibyte)
946 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
948 (when (or (search-forward "\n\n" nil t
)
949 (search-forward "\r\n\r\n" nil t
))
950 (shr-parse-image-data)))))
952 (declare-function libxml-parse-xml-region
"xml.c"
953 (start end
&optional base-url discard-comments
))
955 (defun shr-parse-image-data ()
956 (let ((data (buffer-substring (point) (point-max)))
960 (narrow-to-region (point-min) (point))
961 (let ((content-type (mail-fetch-field "content-type")))
963 ;; Remove any comments in the type string.
964 (intern (replace-regexp-in-string ";.*" "" content-type
)
966 ;; SVG images may contain references to further images that we may
967 ;; want to block. So special-case these by parsing the XML data
968 ;; and remove the blocked bits.
969 (when (eq content-type
'image
/svg
+xml
)
972 (libxml-parse-xml-region (point) (point-max)))))
973 (list data content-type
)))
975 (defun shr-image-displayer (content-function)
976 "Return a function to display an image.
977 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
978 is an argument. The function to be returned takes three arguments URL,
979 START, and END. Note that START and END should be markers."
980 `(lambda (url start end
)
982 (if (string-match "\\`cid:" url
)
983 ,(when content-function
984 `(let ((image (funcall ,content-function
985 (substring url
(match-end 0)))))
988 (funcall shr-put-image-function
989 image
(buffer-substring start end
))
990 (delete-region (point) end
))))
991 (url-retrieve url
'shr-image-fetched
992 (list (current-buffer) start end
)
995 (defun shr-heading (dom &rest types
)
996 (shr-ensure-paragraph)
997 (apply #'shr-fontize-dom dom types
)
998 (shr-ensure-paragraph))
1000 (defun shr-urlify (start url
&optional title
)
1001 (shr-add-font start
(point) 'shr-link
)
1002 (add-text-properties
1005 'help-echo
(let ((iri (or (ignore-errors
1006 (decode-coding-string
1007 (url-unhex-string url
)
1010 (if title
(format "%s (%s)" iri title
) iri
))
1012 'mouse-face
'highlight
1015 (defun shr-encode-url (url)
1017 (browse-url-url-encode-chars url
"[)$ ]"))
1019 (autoload 'shr-color-visible
"shr-color")
1020 (autoload 'shr-color-
>hexadecimal
"shr-color")
1022 (defun shr-color-check (fg bg
)
1023 "Check that FG is visible on BG.
1024 Returns (fg bg) with corrected values.
1025 Returns nil if the colors that would be used are the default
1026 ones, in case fg and bg are nil."
1028 (let ((fixed (cond ((null fg
) 'fg
)
1030 ;; Convert colors to hexadecimal, or set them to default.
1031 (let ((fg (or (shr-color->hexadecimal fg
)
1032 (frame-parameter nil
'foreground-color
)))
1033 (bg (or (shr-color->hexadecimal bg
)
1034 (frame-parameter nil
'background-color
))))
1035 (cond ((eq fixed
'bg
)
1036 ;; Only return the new fg
1037 (list nil
(cadr (shr-color-visible bg fg t
))))
1039 ;; Invert args and results and return only the new bg
1040 (list (cadr (shr-color-visible fg bg t
)) nil
))
1042 (shr-color-visible bg fg
)))))))
1044 (defun shr-colorize-region (start end fg
&optional bg
)
1046 (let ((new-colors (shr-color-check fg bg
)))
1049 (add-face-text-property start end
1050 (list :foreground
(cadr new-colors
))
1053 (add-face-text-property start end
1054 (list :background
(car new-colors
))
1058 (defun shr-previous-newline-padding-width (width)
1059 (let ((overlays (overlays-at (point)))
1063 (dolist (overlay overlays
)
1064 (setq previous-width
1066 (length (plist-get (overlay-properties overlay
)
1068 (+ width previous-width
))))
1070 ;;; Tag-specific rendering rules.
1072 (defun shr-tag-body (dom)
1073 (let* ((start (point))
1074 (fgcolor (or (dom-attr dom
'fgcolor
) (dom-attr dom
'text
)))
1075 (bgcolor (dom-attr dom
'bgcolor
))
1076 (shr-stylesheet (list (cons 'color fgcolor
)
1077 (cons 'background-color bgcolor
))))
1079 (shr-colorize-region start
(point) fgcolor bgcolor
)))
1081 (defun shr-tag-style (_dom)
1084 (defun shr-tag-script (_dom)
1087 (defun shr-tag-comment (_dom)
1090 (defun shr-dom-to-xml (dom)
1095 (defun shr-dom-print (dom)
1096 "Convert DOM into a string containing the xml representation."
1097 (insert (format "<%s" (dom-tag dom
)))
1098 (dolist (attr (dom-attributes dom
))
1099 ;; Ignore attributes that start with a colon because they are
1100 ;; private elements.
1101 (unless (= (aref (format "%s" (car attr
)) 0) ?
:)
1102 (insert (format " %s=\"%s\"" (car attr
) (cdr attr
)))))
1105 (dolist (elem (dom-children dom
))
1109 ((eq (dom-tag elem
) 'comment
)
1111 ((or (not (eq (dom-tag elem
) 'image
))
1112 ;; Filter out blocked elements inside the SVG image.
1113 (not (setq url
(dom-attr elem
':xlink
:href
)))
1114 (not shr-blocked-images
)
1115 (not (string-match shr-blocked-images url
)))
1117 (shr-dom-print elem
)))))
1118 (insert (format "</%s>" (dom-tag dom
))))
1120 (defun shr-tag-svg (dom)
1121 (when (and (image-type-available-p 'svg
)
1122 (not shr-inhibit-images
))
1123 (funcall shr-put-image-function
(list (shr-dom-to-xml dom
) 'image
/svg
+xml
)
1126 (defun shr-tag-sup (dom)
1127 (let ((start (point)))
1129 (put-text-property start
(point) 'display
'(raise 0.5))))
1131 (defun shr-tag-sub (dom)
1132 (let ((start (point)))
1134 (put-text-property start
(point) 'display
'(raise -
0.5))))
1136 (defun shr-tag-label (dom)
1138 (shr-ensure-paragraph))
1140 (defun shr-tag-p (dom)
1141 (shr-ensure-paragraph)
1143 (shr-ensure-paragraph))
1145 (defun shr-tag-div (dom)
1146 (shr-ensure-newline)
1148 (shr-ensure-newline))
1150 (defun shr-tag-s (dom)
1151 (shr-fontize-dom dom
'shr-strike-through
))
1153 (defun shr-tag-del (dom)
1154 (shr-fontize-dom dom
'shr-strike-through
))
1156 (defun shr-tag-b (dom)
1157 (shr-fontize-dom dom
'bold
))
1159 (defun shr-tag-i (dom)
1160 (shr-fontize-dom dom
'italic
))
1162 (defun shr-tag-em (dom)
1163 (shr-fontize-dom dom
'italic
))
1165 (defun shr-tag-strong (dom)
1166 (shr-fontize-dom dom
'bold
))
1168 (defun shr-tag-u (dom)
1169 (shr-fontize-dom dom
'underline
))
1171 (defun shr-tag-tt (dom)
1172 (let ((shr-current-font 'default
))
1175 (defun shr-parse-style (style)
1178 (when (string-match "\n" style
)
1179 (setq style
(replace-match " " t t style
))))
1181 (dolist (elem (split-string style
";"))
1183 (setq elem
(split-string elem
":"))
1184 (when (and (car elem
)
1186 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1187 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1188 (when (string-match " *!important\\'" value
)
1189 (setq value
(substring value
0 (match-beginning 0))))
1190 (unless (equal value
"inherit")
1191 (push (cons (intern name obarray
)
1196 (defun shr-tag-base (dom)
1197 (when-let (base (dom-attr dom
'href
))
1198 (setq shr-base
(shr-parse-base base
)))
1201 (defun shr-tag-a (dom)
1202 (let ((url (dom-attr dom
'href
))
1203 (title (dom-attr dom
'title
))
1207 (when (and shr-target-id
1208 (equal (dom-attr dom
'name
) shr-target-id
))
1209 ;; We have a zero-length <a name="foo"> element, so just
1210 ;; insert... something.
1211 (when (= start
(point))
1212 (shr-ensure-newline)
1214 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1216 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1218 (defun shr-tag-object (dom)
1219 (unless shr-inhibit-images
1220 (let ((start (point))
1221 url multimedia image
)
1222 (when-let (type (dom-attr dom
'type
))
1223 (when (string-match "\\`image/svg" type
)
1224 (setq url
(dom-attr dom
'data
)
1226 (dolist (child (dom-non-text-children dom
))
1228 ((eq (dom-tag child
) 'embed
)
1229 (setq url
(or url
(dom-attr child
'src
))
1231 ((and (eq (dom-tag child
) 'param
)
1232 (equal (dom-attr child
'name
) "movie"))
1233 (setq url
(or url
(dom-attr child
'value
))
1238 (shr-tag-img dom url
)
1241 (shr-insert " [multimedia] ")
1242 (shr-urlify start
(shr-expand-url url
)))))
1244 (shr-generic dom
)))))
1246 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1252 "Preferences for media types.
1253 The key element should be a regexp matched against the type of the source or
1254 url if no type is specified. The value should be a float in the range 0.0 to
1255 1.0. Media elements with higher value are preferred."
1258 :type
'(alist :key-type regexp
:value-type float
))
1260 (defun shr--get-media-pref (elem)
1261 "Determine the preference for ELEM.
1262 The preference is a float determined from `shr-prefer-media-type'."
1263 (let ((type (dom-attr elem
'type
))
1266 (setq type
(dom-attr elem
'src
)))
1268 (dolist (pref shr-prefer-media-type-alist
)
1271 (string-match-p (car pref
) type
))
1272 (setq p
(cdr pref
)))))
1275 (defun shr--extract-best-source (dom &optional url pref
)
1276 "Extract the best `:src' property from <source> blocks in DOM."
1277 (setq pref
(or pref -
1.0))
1279 (dolist (elem (dom-non-text-children dom
))
1280 (when (and (eq (dom-tag elem
) 'source
)
1283 (shr--get-media-pref elem
))))
1285 url
(dom-attr elem
'src
))
1286 ;; libxml's html parser isn't HTML5 compliant and non terminated
1287 ;; source tags might end up as children. So recursion it is...
1288 (dolist (child (dom-non-text-children elem
))
1289 (when (eq (dom-tag child
) 'source
)
1290 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1291 (when (< pref
(cdr ret
))
1293 pref
(cdr ret
)))))))))
1296 (defun shr-tag-video (dom)
1297 (let ((image (dom-attr dom
'poster
))
1298 (url (dom-attr dom
'src
))
1301 (setq url
(car (shr--extract-best-source dom
))))
1303 (shr-tag-img nil image
)
1304 (shr-insert " [video] "))
1305 (shr-urlify start
(shr-expand-url url
))))
1307 (defun shr-tag-audio (dom)
1308 (let ((url (dom-attr dom
'src
))
1311 (setq url
(car (shr--extract-best-source dom
))))
1312 (shr-insert " [audio] ")
1313 (shr-urlify start
(shr-expand-url url
))))
1315 (defun shr-tag-img (dom &optional url
)
1318 (> (length (dom-attr dom
'src
)) 0)))
1319 (when (> (current-column) 0)
1321 (let ((alt (dom-attr dom
'alt
))
1322 (url (shr-expand-url (or url
(dom-attr dom
'src
)))))
1323 (let ((start (point-marker)))
1324 (when (zerop (length alt
))
1327 ((or (member (dom-attr dom
'height
) '("0" "1"))
1328 (member (dom-attr dom
'width
) '("0" "1")))
1329 ;; Ignore zero-sized or single-pixel images.
1331 ((and (not shr-inhibit-images
)
1332 (string-match "\\`data:" url
))
1333 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1335 (funcall shr-put-image-function image alt
)
1337 ((and (not shr-inhibit-images
)
1338 (string-match "\\`cid:" url
))
1339 (let ((url (substring url
(match-end 0)))
1341 (if (or (not shr-content-function
)
1342 (not (setq image
(funcall shr-content-function url
))))
1344 (funcall shr-put-image-function image alt
))))
1345 ((or shr-inhibit-images
1346 (and shr-blocked-images
1347 (string-match shr-blocked-images url
)))
1348 (setq shr-start
(point))
1349 (if (> (string-width alt
) 8)
1350 (shr-insert (truncate-string-to-width alt
8))
1352 ((and (not shr-ignore-cache
)
1353 (url-is-cached (shr-encode-url url
)))
1354 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
1357 (when (and shr-ignore-cache
1358 (url-is-cached (shr-encode-url url
)))
1359 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1360 (when (file-exists-p file
)
1361 (delete-file file
))))
1363 (shr-encode-url url
) 'shr-image-fetched
1364 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
1366 (when (zerop shr-table-depth
) ;; We are not in a table.
1367 (put-text-property start
(point) 'keymap shr-map
)
1368 (put-text-property start
(point) 'shr-alt alt
)
1369 (put-text-property start
(point) 'image-url url
)
1370 (put-text-property start
(point) 'image-displayer
1371 (shr-image-displayer shr-content-function
))
1372 (put-text-property start
(point) 'help-echo
1374 (or (dom-attr dom
'title
) alt
))))))))
1376 (defun shr-tag-pre (dom)
1377 (let ((shr-folding-mode 'none
)
1378 (shr-current-font 'default
))
1379 (shr-ensure-newline)
1381 (shr-ensure-newline)))
1383 (defun shr-tag-blockquote (dom)
1384 (shr-ensure-paragraph)
1385 (let ((start (point))
1386 (shr-indentation (+ shr-indentation
1387 (* 4 shr-table-separator-pixel-width
))))
1389 (shr-ensure-paragraph)
1390 (shr-mark-fill start
)))
1392 (defun shr-tag-dl (dom)
1393 (shr-ensure-paragraph)
1395 (shr-ensure-paragraph))
1397 (defun shr-tag-dt (dom)
1398 (shr-ensure-newline)
1400 (shr-ensure-newline))
1402 (defun shr-tag-dd (dom)
1403 (shr-ensure-newline)
1404 (let ((shr-indentation (+ shr-indentation
1405 (* 4 shr-table-separator-pixel-width
))))
1408 (defun shr-tag-ul (dom)
1409 (shr-ensure-paragraph)
1410 (let ((shr-list-mode 'ul
))
1412 (shr-ensure-paragraph))
1414 (defun shr-tag-ol (dom)
1415 (shr-ensure-paragraph)
1416 (let ((shr-list-mode 1))
1418 (shr-ensure-paragraph))
1420 (defun shr-tag-li (dom)
1421 (shr-ensure-newline)
1422 (let ((start (point)))
1424 (if (numberp shr-list-mode
)
1426 (format "%d " shr-list-mode
)
1427 (setq shr-list-mode
(1+ shr-list-mode
)))
1428 (car shr-internal-bullet
)))
1429 (width (if (numberp shr-list-mode
)
1430 (shr-string-pixel-width bullet
)
1431 (cdr shr-internal-bullet
))))
1433 (shr-mark-fill start
)
1434 (let ((shr-indentation (+ shr-indentation width
)))
1435 (put-text-property start
(1+ start
)
1436 'shr-continuation-indentation shr-indentation
)
1437 (put-text-property start
(1+ start
) 'shr-prefix-length
(length bullet
))
1438 (shr-generic dom
)))))
1440 (defun shr-mark-fill (start)
1441 ;; We may not have inserted any text to fill.
1442 (unless (= start
(point))
1443 (put-text-property start
(1+ start
)
1444 'shr-indentation shr-indentation
)))
1446 (defun shr-tag-br (dom)
1447 (when (and (not (bobp))
1448 ;; Only add a newline if we break the current line, or
1449 ;; the previous line isn't a blank line.
1451 (and (> (- (point) 2) (point-min))
1452 (not (= (char-after (- (point) 2)) ?
\n)))))
1456 (defun shr-tag-span (dom)
1459 (defun shr-tag-h1 (dom)
1460 (shr-heading dom
(if shr-use-fonts
1461 '(variable-pitch (:height
1.3 :weight bold
))
1464 (defun shr-tag-h2 (dom)
1465 (shr-heading dom
'bold
))
1467 (defun shr-tag-h3 (dom)
1468 (shr-heading dom
'italic
))
1470 (defun shr-tag-h4 (dom)
1473 (defun shr-tag-h5 (dom)
1476 (defun shr-tag-h6 (dom)
1479 (defun shr-tag-hr (_dom)
1480 (shr-ensure-newline)
1481 (insert (make-string (if (not shr-use-fonts
)
1483 (1+ (/ shr-internal-width
1484 shr-table-separator-pixel-width
)))
1488 (defun shr-tag-title (dom)
1489 (shr-heading dom
'bold
'underline
))
1491 (defun shr-tag-font (dom)
1492 (let* ((start (point))
1493 (color (dom-attr dom
'color
))
1494 (shr-stylesheet (nconc (list (cons 'color color
))
1498 (shr-colorize-region start
(point) color
1499 (cdr (assq 'background-color shr-stylesheet
))))))
1501 ;;; Table rendering algorithm.
1503 ;; Table rendering is the only complicated thing here. We do this by
1504 ;; first counting how many TDs there are in each TR, and registering
1505 ;; how wide they think they should be ("width=45%", etc). Then we
1506 ;; render each TD separately (this is done in temporary buffers, so
1507 ;; that we can use all the rendering machinery as if we were in the
1508 ;; main buffer). Now we know how much space each TD really takes, so
1509 ;; we then render everything again with the new widths, and finally
1510 ;; insert all these boxes into the main buffer.
1511 (defun shr-tag-table-1 (dom)
1512 (setq dom
(or (dom-child-by-tag dom
'tbody
) dom
))
1513 (let* ((shr-inhibit-images t
)
1514 (shr-table-depth (1+ shr-table-depth
))
1515 (shr-kinsoku-shorten t
)
1516 ;; Find all suggested widths.
1517 (columns (shr-column-specs dom
))
1518 ;; Compute how many pixels wide each TD should be.
1519 (suggested-widths (shr-pro-rate-columns columns
))
1520 ;; Do a "test rendering" to see how big each TD is (this can
1521 ;; be smaller (if there's little text) or bigger (if there's
1522 ;; unbreakable text).
1523 (elems (or (dom-attr dom
'shr-suggested-widths
)
1524 (shr-make-table dom suggested-widths nil
1525 'shr-suggested-widths
)))
1526 (sketch (loop for line in elems
1527 collect
(mapcar #'car line
)))
1528 (natural (loop for line in elems
1529 collect
(mapcar #'cdr line
)))
1530 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1531 ;; This probably won't work very well.
1532 (when (> (+ (loop for width across sketch-widths
1534 shr-indentation shr-table-separator-pixel-width
)
1536 (setq truncate-lines t
))
1537 ;; Then render the table again with these new "hard" widths.
1538 (shr-insert-table (shr-make-table dom sketch-widths t
) sketch-widths
)))
1540 (defun shr-tag-table (dom)
1541 (shr-ensure-paragraph)
1542 (let* ((caption (dom-children (dom-child-by-tag dom
'caption
)))
1543 (header (dom-non-text-children (dom-child-by-tag dom
'thead
)))
1544 (body (dom-non-text-children (or (dom-child-by-tag dom
'tbody
)
1546 (footer (dom-non-text-children (dom-child-by-tag dom
'tfoot
)))
1547 (bgcolor (dom-attr dom
'bgcolor
))
1549 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1551 (nheader (if header
(shr-max-columns header
)))
1552 (nbody (if body
(shr-max-columns body
)))
1553 (nfooter (if footer
(shr-max-columns footer
))))
1554 (if (and (not caption
)
1556 (not (dom-child-by-tag dom
'tbody
))
1557 (not (dom-child-by-tag dom
'tr
))
1559 ;; The table is totally invalid and just contains random junk.
1560 ;; Try to output it anyway.
1562 ;; It's a real table, so render it.
1563 (if (dom-attr dom
'shr-fixed-table
)
1564 (shr-tag-table-1 dom
)
1565 ;; Only fix up the table once.
1569 (if caption
`((tr nil
(td nil
,@caption
))))
1573 ;; header + body + footer
1574 (if (= nheader nbody
)
1575 (if (= nbody nfooter
)
1576 `((tr nil
(td nil
(table nil
1578 ,@body
,@footer
)))))
1579 (nconc `((tr nil
(td nil
(table nil
1584 `((tr nil
(td nil
(table
1586 nil
,@footer
))))))))
1587 (nconc `((tr nil
(td nil
(table nil
(tbody
1589 (if (= nbody nfooter
)
1590 `((tr nil
(td nil
(table
1591 nil
(tbody nil
,@body
1593 (nconc `((tr nil
(td nil
(table
1598 `((tr nil
(td nil
(table
1604 (if (= nheader nbody
)
1605 `((tr nil
(td nil
(table nil
(tbody nil
,@header
1608 `(,@header
(tr nil
(td nil
(table
1609 nil
(tbody nil
,@body
)))))
1610 `((tr nil
(td nil
(table nil
(tbody nil
,@header
))))
1611 (tr nil
(td nil
(table nil
(tbody nil
,@body
)))))))))
1614 (if (= nbody nfooter
)
1615 `((tr nil
(td nil
(table
1616 nil
(tbody nil
,@body
,@footer
)))))
1617 (nconc `((tr nil
(td nil
(table nil
(tbody nil
,@body
)))))
1620 `((tr nil
(td nil
(table
1621 nil
(tbody nil
,@footer
)))))))))
1623 `((tr nil
(td nil
(table nil
(tbody nil
,@body
))))))
1625 (dom-set-attribute table
'shr-fixed-table t
)
1626 (setcdr dom
(cdr table
))
1627 (shr-tag-table-1 dom
))))
1629 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1631 ;; Finally, insert all the images after the table. The Emacs buffer
1632 ;; model isn't strong enough to allow us to put the images actually
1634 (when (zerop shr-table-depth
)
1636 (shr-expand-alignments start
(point)))
1637 (dolist (elem (dom-by-tag dom
'object
))
1638 (shr-tag-object elem
))
1639 (dolist (elem (dom-by-tag dom
'img
))
1640 (shr-tag-img elem
)))))
1642 (defun shr-insert-table (table widths
)
1643 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
1645 (shr-table-separator-length (if collapse
0 1))
1646 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
))
1648 (setq shr-table-id
(1+ shr-table-id
))
1650 (shr-insert-table-ruler widths
))
1652 (let ((start (point))
1655 (height (let ((max 0))
1656 (dolist (column row
)
1657 (setq max
(max max
(nth 2 column
))))
1659 (dotimes (i (max height
1))
1661 (insert shr-table-vertical-line
"\n"))
1662 (dolist (column row
)
1663 (when (> (nth 2 column
) -
1)
1665 ;; Sum up all the widths from the column. (There may be
1666 ;; more than one if this is a "colspan" column.)
1667 (dotimes (i (nth 4 column
))
1668 ;; The colspan directive may be wrong and there may not be
1669 ;; that number of columns.
1670 (when (<= column-number
(1- (length widths
)))
1671 (setq align
(+ align
1672 (aref widths column-number
)
1673 (* 2 shr-table-separator-pixel-width
))))
1674 (setq column-number
(1+ column-number
)))
1675 (let ((lines (nth 3 column
))
1676 (pixel-align (if (not shr-use-fonts
)
1677 (* align
(frame-char-width))
1679 (dolist (line lines
)
1681 (let ((start (point)))
1685 'display
`(space :align-to
(,pixel-align
))
1686 'face
(and (> (length line
) 0)
1687 (shr-face-background
1689 (1- (length line
)) 'face line
)))
1690 'shr-table-indent shr-table-id
)
1691 shr-table-vertical-line
)
1692 (shr-colorize-region
1693 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1695 ;; Add blank lines at padding at the bottom of the TD,
1697 (dotimes (i (- height
(length lines
)))
1699 (let ((start (point)))
1700 (insert (propertize " "
1701 'display
`(space :align-to
(,pixel-align
))
1702 'shr-table-indent shr-table-id
)
1703 shr-table-vertical-line
)
1704 (shr-colorize-region
1705 start
(1- (point)) (nth 5 column
) (nth 6 column
)))
1706 (forward-line 1))))))
1708 (shr-insert-table-ruler widths
)))
1709 (unless (= start
(point))
1710 (put-text-property start
(1+ start
) 'shr-table-id shr-table-id
))))
1712 (defun shr-face-background (face)
1714 (let ((background nil
))
1716 (when (and (consp elem
)
1717 (eq (car elem
) :background
))
1718 (setq background
(cadr elem
))))
1720 (list :background background
)))))
1722 (defun shr-expand-alignments (start end
)
1723 (while (< (setq start
(next-single-property-change
1724 start
'shr-table-id nil end
))
1727 (let* ((shr-use-fonts t
)
1728 (id (get-text-property (point) 'shr-table-id
))
1729 (base (shr-pixel-column))
1733 (while (setq elem
(text-property-any
1734 (point) end
'shr-table-indent id
))
1736 (let ((align (get-text-property (point) 'display
)))
1737 (put-text-property (point) (1+ (point)) 'display
1738 `(space :align-to
(,(+ (car (nth 2 align
))
1740 (forward-char 1)))))
1741 (setq start
(1+ start
))))
1743 (defun shr-insert-table-ruler (widths)
1744 (when shr-table-horizontal-line
1746 (> shr-indentation
0))
1748 (insert shr-table-corner
)
1749 (let ((total-width 0))
1750 (dotimes (i (length widths
))
1751 (setq total-width
(+ total-width
(aref widths i
)
1752 (* shr-table-separator-pixel-width
2)))
1753 (insert (make-string (1+ (/ (aref widths i
)
1754 shr-table-separator-pixel-width
))
1755 shr-table-horizontal-line
)
1757 'display
`(space :align-to
(,total-width
))
1758 'shr-table-indent shr-table-id
)
1762 (defun shr-table-widths (table natural-table suggested-widths
)
1763 (let* ((length (length suggested-widths
))
1764 (widths (make-vector length
0))
1765 (natural-widths (make-vector length
0)))
1768 (dolist (column row
)
1769 (aset widths i
(max (aref widths i
) column
))
1771 (dolist (row natural-table
)
1773 (dolist (column row
)
1774 (aset natural-widths i
(max (aref natural-widths i
) column
))
1776 (let ((extra (- (apply '+ (append suggested-widths nil
))
1777 (apply '+ (append widths nil
))
1778 (* shr-table-separator-pixel-width
(1+ (length widths
)))))
1779 (expanded-columns 0))
1780 ;; We have extra, unused space, so divide this space amongst the
1783 ;; If the natural width is wider than the rendered width, we
1784 ;; want to allow the column to expand.
1786 (when (> (aref natural-widths i
) (aref widths i
))
1787 (setq expanded-columns
(1+ expanded-columns
))))
1789 (when (> (aref natural-widths i
) (aref widths i
))
1791 (aref natural-widths i
)
1792 (+ (/ extra expanded-columns
)
1793 (aref widths i
))))))))
1796 (defun shr-make-table (dom widths
&optional fill storage-attribute
)
1797 (or (cadr (assoc (list dom widths fill
) shr-content-cache
))
1798 (let ((data (shr-make-table-1 dom widths fill
)))
1799 (push (list (list dom widths fill
) data
)
1801 (when storage-attribute
1802 (dom-set-attribute dom storage-attribute data
))
1805 (defun shr-make-table-1 (dom widths
&optional fill
)
1807 (rowspans (make-vector (length widths
) 0))
1808 (colspan-remaining 0)
1809 colspan-width colspan-count
1811 (dolist (row (dom-non-text-children dom
))
1812 (when (eq (dom-tag row
) 'tr
)
1814 (columns (dom-non-text-children row
))
1818 (while (< i
(length widths
))
1819 ;; If we previously had a rowspan definition, then that
1820 ;; means that we now have a "missing" td/th element here.
1821 ;; So just insert a dummy, empty one to (sort of) emulate
1824 (if (zerop (aref rowspans i
))
1826 (aset rowspans i
(1- (aref rowspans i
)))
1828 (when (and (not (stringp column
))
1829 (or (memq (dom-tag column
) '(td th
))
1831 (when-let (span (dom-attr column
'rowspan
))
1832 (aset rowspans i
(+ (aref rowspans i
)
1833 (1- (string-to-number span
)))))
1834 ;; Sanity check for invalid column-spans.
1835 (when (>= width-column
(length widths
))
1836 (setq width-column
0))
1839 (aref widths width-column
)
1840 (* 10 shr-table-separator-pixel-width
)))
1841 (when (setq colspan
(dom-attr column
'colspan
))
1842 (setq colspan
(min (string-to-number colspan
)
1843 ;; The colspan may be wrong, so
1844 ;; truncate it to the length of the
1845 ;; remaining columns.
1846 (- (length widths
) i
)))
1847 (dotimes (j (1- colspan
))
1849 (if (> (+ i
1 j
) (1- (length widths
)))
1850 ;; If we have a colspan spec that's longer
1851 ;; than the table is wide, just use the last
1852 ;; width as the width.
1853 (aref widths
(1- (length widths
)))
1854 ;; Sum up the widths of the columns we're
1857 shr-table-separator-length
1858 (aref widths
(+ i
1 j
))))))
1859 (setq width-column
(+ width-column
(1- colspan
))
1860 colspan-count colspan
1861 colspan-remaining colspan
))
1863 (let ((data (shr-render-td column width fill
)))
1865 (> colspan-remaining
0))
1867 (setq colspan-width
(car data
))
1868 (let ((this-width (/ colspan-width colspan-count
)))
1869 (push (cons this-width
(cadr data
)) tds
)
1870 (setq colspan-remaining
(1- colspan-remaining
))))
1872 (push (cons (car data
) (cadr data
)) tds
)
1876 (dotimes (c (1- colspan
))
1880 (list 0 0 -
1 nil
1 nil nil
)
1884 width-column
(1+ width-column
))))
1885 (push (nreverse tds
) trs
))))
1888 (defun shr-pixel-buffer-width ()
1889 (if (not shr-use-fonts
)
1891 (goto-char (point-min))
1895 (setq max
(max max
(current-column)))
1898 (if (get-buffer-window)
1899 (car (window-text-pixel-size nil
(point-min) (point-max)))
1900 (save-window-excursion
1901 (set-window-buffer nil
(current-buffer))
1902 (car (window-text-pixel-size nil
(point-min) (point-max)))))))
1904 (defun shr-render-td (dom width fill
)
1905 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill
))))
1906 (or (dom-attr dom cache
)
1909 (dolist (attr (dom-attributes dom
))
1910 (let ((name (symbol-name (car attr
))))
1911 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name
)
1912 (let ((cache-width (string-to-number
1913 (match-string 1 name
))))
1914 (when (and (>= cache-width width
)
1915 (<= (car (cdr attr
)) width
))
1916 (setq result
(cdr attr
)))))))
1918 (let ((result (shr-render-td-1 dom width fill
)))
1919 (dom-set-attribute dom cache result
)
1922 (defun shr-render-td-1 (dom width fill
)
1924 (let ((bgcolor (dom-attr dom
'bgcolor
))
1925 (fgcolor (dom-attr dom
'fgcolor
))
1926 (style (dom-attr dom
'style
))
1927 (shr-stylesheet shr-stylesheet
)
1931 (setq style
(and (string-match "color" style
)
1932 (shr-parse-style style
))))
1934 (setq style
(nconc (list (cons 'background-color bgcolor
))
1937 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1939 (setq shr-stylesheet
(append style shr-stylesheet
)))
1940 (let ((shr-internal-width width
)
1941 (shr-indentation 0))
1943 (save-window-excursion
1944 (set-window-buffer nil
(current-buffer))
1947 (or (dom-attr dom
'shr-td-cache-natural
)
1948 (let ((natural (max (shr-pixel-buffer-width)
1949 (shr-dom-max-natural-width dom
0))))
1950 (dom-set-attribute dom
'shr-td-cache-natural natural
)
1952 (if (and natural-width
1953 (<= natural-width width
))
1954 (setq max-width natural-width
)
1955 (let ((shr-internal-width width
))
1956 (shr-fill-lines (point-min) (point-max))
1957 (setq max-width
(shr-pixel-buffer-width)))))
1958 (goto-char (point-max))
1959 ;; Delete padding at the bottom of the TDs.
1963 (skip-chars-backward " \t\n")
1966 (goto-char (point-min))
1969 (count-lines (point-min) (point-max))
1970 (split-string (buffer-string) "\n")
1971 (if (dom-attr dom
'colspan
)
1972 (string-to-number (dom-attr dom
'colspan
))
1974 (cdr (assq 'color shr-stylesheet
))
1975 (cdr (assq 'background-color shr-stylesheet
))))))
1977 (defun shr-dom-max-natural-width (dom max
)
1978 (if (eq (dom-tag dom
) 'table
)
1980 (loop for line in
(dom-attr dom
'shr-suggested-widths
)
1982 shr-table-separator-length
1983 (loop for elem in line
1986 (* 2 shr-table-separator-length
)))))
1988 (dolist (child (dom-children dom
))
1989 (unless (stringp child
)
1990 (setq max
(max (shr-dom-max-natural-width child max
)))))
1993 (defun shr-buffer-width ()
1994 (goto-char (point-min))
1998 (setq max
(max max
(current-column)))
2002 (defun shr-pro-rate-columns (columns)
2003 (let ((total-percentage 0)
2004 (widths (make-vector (length columns
) 0)))
2005 (dotimes (i (length columns
))
2006 (setq total-percentage
(+ total-percentage
(aref columns i
))))
2007 (setq total-percentage
(/ 1.0 total-percentage
))
2008 (dotimes (i (length columns
))
2009 (aset widths i
(max (truncate (* (aref columns i
)
2011 (- shr-internal-width
2012 (* (1+ (length columns
))
2013 shr-table-separator-pixel-width
))))
2017 ;; Return a summary of the number and shape of the TDs in the table.
2018 (defun shr-column-specs (dom)
2019 (let ((columns (make-vector (shr-max-columns dom
) 1)))
2020 (dolist (row (dom-non-text-children dom
))
2021 (when (eq (dom-tag row
) 'tr
)
2023 (dolist (column (dom-non-text-children row
))
2024 (when (memq (dom-tag column
) '(td th
))
2025 (let ((width (dom-attr column
'width
)))
2027 (string-match "\\([0-9]+\\)%" width
)
2028 (not (zerop (setq width
(string-to-number
2029 (match-string 1 width
))))))
2030 (aset columns i
(/ width
100.0))))
2031 (setq i
(1+ i
)))))))
2034 (defun shr-count (dom elem
)
2036 (dolist (sub (dom-children dom
))
2037 (when (and (not (stringp sub
))
2038 (eq (dom-tag sub
) elem
))
2042 (defun shr-max-columns (dom)
2044 (dolist (row (dom-children dom
))
2045 (when (and (not (stringp row
))
2046 (eq (dom-tag row
) 'tr
))
2047 (setq max
(max max
(+ (shr-count row
'td
)
2048 (shr-count row
'th
))))))
2057 ;;; shr.el ends here