(compose-mail): Give a better error message for `mail-user-agent'
[emacs.git] / lisp / net / shr.el
blob275b36f90097b1e9a33e676cb46a66e4cfca39bc
1 ;;; shr.el --- Simple HTML Renderer -*- lexical-binding: t -*-
3 ;; Copyright (C) 2010-2018 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ;;; Commentary:
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.
31 ;;; Code:
33 (require 'cl-lib)
34 (eval-when-compile (require 'url)) ;For url-filename's setf handler.
35 (require 'browse-url)
36 (eval-when-compile (require 'subr-x))
37 (require 'dom)
38 (require 'seq)
39 (require 'svg)
40 (require 'image)
41 (require 'puny)
43 (defgroup shr nil
44 "Simple HTML Renderer"
45 :version "25.1"
46 :group 'web)
48 (defcustom shr-max-image-proportion 0.9
49 "How big pictures displayed are in relation to the window they're in.
50 A value of 0.7 means that they are allowed to take up 70% of the
51 width and height of the window. If they are larger than this,
52 and Emacs supports it, then the images will be rescaled down to
53 fit these criteria."
54 :version "24.1"
55 :group 'shr
56 :type 'float)
58 (defcustom shr-blocked-images nil
59 "Images that have URLs matching this regexp will be blocked."
60 :version "24.1"
61 :group 'shr
62 :type '(choice (const nil) regexp))
64 (defcustom shr-use-fonts t
65 "If non-nil, use proportional fonts for text."
66 :version "25.1"
67 :group 'shr
68 :type 'boolean)
70 (defcustom shr-use-colors t
71 "If non-nil, respect color specifications in the HTML."
72 :version "26.1"
73 :group 'shr
74 :type 'boolean)
76 (defcustom shr-table-horizontal-line nil
77 "Character used to draw horizontal table lines.
78 If nil, don't draw horizontal table lines."
79 :group 'shr
80 :type '(choice (const nil) character))
82 (defcustom shr-table-vertical-line ?\s
83 "Character used to draw vertical table lines."
84 :group 'shr
85 :type 'character)
87 (defcustom shr-table-corner ?\s
88 "Character used to draw table corners."
89 :group 'shr
90 :type 'character)
92 (defcustom shr-hr-line ?-
93 "Character used to draw hr lines."
94 :group 'shr
95 :type 'character)
97 (defcustom shr-width nil
98 "Frame width to use for rendering.
99 May either be an integer specifying a fixed width in characters,
100 or nil, meaning that the full width of the window should be used.
101 If `shr-use-fonts' is set, the mean character width is used to
102 compute the pixel width, which is used instead."
103 :version "25.1"
104 :type '(choice (integer :tag "Fixed width in characters")
105 (const :tag "Use the width of the window" nil))
106 :group 'shr)
108 (defcustom shr-bullet "* "
109 "Bullet used for unordered lists.
110 Alternative suggestions are:
111 - \" \"
112 - \" \""
113 :version "24.4"
114 :type 'string
115 :group 'shr)
117 (defcustom shr-external-browser 'browse-url-default-browser
118 "Function used to launch an external browser."
119 :version "24.4"
120 :group 'shr
121 :type 'function)
123 (defcustom shr-image-animate t
124 "Non nil means that images that can be animated will be."
125 :version "24.4"
126 :group 'shr
127 :type 'boolean)
129 (defvar shr-content-function nil
130 "If bound, this should be a function that will return the content.
131 This is used for cid: URLs, and the function is called with the
132 cid: URL as the argument.")
134 (defvar shr-put-image-function 'shr-put-image
135 "Function called to put image and alt string.")
137 (defface shr-strike-through '((t (:strike-through t)))
138 "Font for <s> elements."
139 :group 'shr)
141 (defface shr-link
142 '((t (:inherit link)))
143 "Font for link elements."
144 :group 'shr)
146 (defface shr-selected-link
147 '((t (:inherit shr-link :background "red")))
148 "Font for link elements."
149 :group 'shr)
151 (defvar shr-inhibit-images nil
152 "If non-nil, inhibit loading images.")
154 (defvar shr-external-rendering-functions nil
155 "Alist of tag/function pairs used to alter how shr renders certain tags.
156 For instance, eww uses this to alter rendering of title, forms
157 and other things:
158 \((title . eww-tag-title)
159 (form . eww-tag-form)
160 ...)")
162 ;;; Internal variables.
164 (defvar shr-folding-mode nil)
165 (defvar shr-start nil)
166 (defvar shr-indentation 0)
167 (defvar shr-internal-width nil)
168 (defvar shr-list-mode nil)
169 (defvar shr-content-cache nil)
170 (defvar shr-kinsoku-shorten nil)
171 (defvar shr-table-depth 0)
172 (defvar shr-stylesheet nil)
173 (defvar shr-base nil)
174 (defvar shr-depth 0)
175 (defvar shr-warning nil)
176 (defvar shr-ignore-cache nil)
177 (defvar shr-target-id nil)
178 (defvar shr-table-separator-length 1)
179 (defvar shr-table-separator-pixel-width 0)
180 (defvar shr-table-id nil)
181 (defvar shr-current-font nil)
182 (defvar shr-internal-bullet nil)
184 (defvar shr-map
185 (let ((map (make-sparse-keymap)))
186 (define-key map "a" 'shr-show-alt-text)
187 (define-key map "i" 'shr-browse-image)
188 (define-key map "z" 'shr-zoom-image)
189 (define-key map [?\t] 'shr-next-link)
190 (define-key map [?\M-\t] 'shr-previous-link)
191 (define-key map [follow-link] 'mouse-face)
192 (define-key map [mouse-2] 'shr-browse-url)
193 (define-key map "I" 'shr-insert-image)
194 (define-key map "w" 'shr-maybe-probe-and-copy-url)
195 (define-key map "u" 'shr-maybe-probe-and-copy-url)
196 (define-key map "v" 'shr-browse-url)
197 (define-key map "O" 'shr-save-contents)
198 (define-key map "\r" 'shr-browse-url)
199 map))
201 (defvar shr-image-map
202 (let ((map (copy-keymap shr-map)))
203 (when (boundp 'image-map)
204 (set-keymap-parent map image-map))
205 map))
207 ;; Public functions and commands.
208 (declare-function libxml-parse-html-region "xml.c"
209 (start end &optional base-url discard-comments))
211 (defun shr-render-buffer (buffer)
212 "Display the HTML rendering of the current buffer."
213 (interactive (list (current-buffer)))
214 (or (fboundp 'libxml-parse-html-region)
215 (error "This function requires Emacs to be compiled with libxml2"))
216 (pop-to-buffer "*html*")
217 (erase-buffer)
218 (shr-insert-document
219 (with-current-buffer buffer
220 (libxml-parse-html-region (point-min) (point-max))))
221 (goto-char (point-min)))
223 ;;;###autoload
224 (defun shr-render-region (begin end &optional buffer)
225 "Display the HTML rendering of the region between BEGIN and END."
226 (interactive "r")
227 (unless (fboundp 'libxml-parse-html-region)
228 (error "This function requires Emacs to be compiled with libxml2"))
229 (with-current-buffer (or buffer (current-buffer))
230 (let ((dom (libxml-parse-html-region begin end)))
231 (delete-region begin end)
232 (goto-char begin)
233 (shr-insert-document dom))))
235 (defun shr--have-one-fringe-p ()
236 "Return non-nil if we know at least one of the fringes has non-zero width."
237 (and (fboundp 'fringe-columns)
238 (or (not (zerop (fringe-columns 'right)))
239 (not (zerop (fringe-columns 'left))))))
241 ;;;###autoload
242 (defun shr-insert-document (dom)
243 "Render the parsed document DOM into the current buffer.
244 DOM should be a parse tree as generated by
245 `libxml-parse-html-region' or similar."
246 (setq shr-content-cache nil)
247 (let ((start (point))
248 (shr-start nil)
249 (shr-base nil)
250 (shr-depth 0)
251 (shr-table-id 0)
252 (shr-warning nil)
253 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
254 (shr-internal-bullet (cons shr-bullet
255 (shr-string-pixel-width shr-bullet)))
256 (shr-internal-width (or (and shr-width
257 (if (not shr-use-fonts)
258 shr-width
259 (* shr-width (frame-char-width))))
260 ;; We need to adjust the available
261 ;; width for when the user disables
262 ;; the fringes, which will cause the
263 ;; display engine usurp one column for
264 ;; the continuation glyph.
265 (if (not shr-use-fonts)
266 (- (window-body-width) 1
267 (if (and (null shr-width)
268 (not (shr--have-one-fringe-p)))
271 (- (window-body-width nil t)
272 (* 2 (frame-char-width))
273 (if (and (null shr-width)
274 (not (shr--have-one-fringe-p)))
275 (* (frame-char-width) 2)
276 0)))))
277 (max-specpdl-size max-specpdl-size)
278 bidi-display-reordering)
279 ;; If the window was hscrolled for some reason, shr-fill-lines
280 ;; below will misbehave, because it silently assumes that it
281 ;; starts with a non-hscrolled window (vertical-motion will move
282 ;; to a wrong place otherwise).
283 (set-window-hscroll nil 0)
284 (shr-descend dom)
285 (shr-fill-lines start (point))
286 (shr--remove-blank-lines-at-the-end start (point))
287 (when shr-warning
288 (message "%s" shr-warning))))
290 (defun shr--remove-blank-lines-at-the-end (start end)
291 (save-restriction
292 (save-excursion
293 (narrow-to-region start end)
294 (goto-char end)
295 (when (and (re-search-backward "[^ \n]" nil t)
296 (not (eobp)))
297 (forward-line 1)
298 (delete-region (point) (point-max))))))
300 (defun shr-url-at-point (image-url)
301 "Return the URL under point as a string.
302 If IMAGE-URL is non-nil, or there is no link under point, but
303 there is an image under point then copy the URL of the image
304 under point instead."
305 (if image-url
306 (get-text-property (point) 'image-url)
307 (or (get-text-property (point) 'shr-url)
308 (get-text-property (point) 'image-url))))
310 (defun shr-copy-url (url)
311 "Copy the URL under point to the kill ring.
312 If IMAGE-URL (the prefix) is non-nil, or there is no link under
313 point, but there is an image under point then copy the URL of the
314 image under point instead."
315 (interactive (list (shr-url-at-point current-prefix-arg)))
316 (if (not url)
317 (message "No URL under point")
318 (setq url (url-encode-url url))
319 (kill-new url)
320 (message "Copied %s" url)))
322 (defun shr-probe-url (url cont)
323 "Pass URL's redirect destination to CONT, if it has one.
324 CONT should be a function of one argument, the redirect
325 destination URL. If URL is not redirected, then CONT is never
326 called."
327 (interactive "P")
328 (url-retrieve
329 url (lambda (a)
330 (pcase a
331 (`(:redirect ,destination . ,_)
332 ;; Remove common tracking junk from the URL.
333 (funcall cont (replace-regexp-in-string
334 ".utm_.*" "" destination)))))
335 nil t))
337 (defun shr-probe-and-copy-url (url)
338 "Copy the URL under point to the kill ring.
339 Like `shr-copy-url', but additionally fetch URL and use its
340 redirection destination if it has one."
341 (interactive (list (shr-url-at-point current-prefix-arg)))
342 (if url (shr-probe-url url #'shr-copy-url)
343 (shr-copy-url url)))
345 (defun shr-maybe-probe-and-copy-url (url)
346 "Copy the URL under point to the kill ring.
347 If the URL is already at the front of the kill ring act like
348 `shr-probe-and-copy-url', otherwise like `shr-copy-url'."
349 (interactive (list (shr-url-at-point current-prefix-arg)))
350 (if (equal url (car kill-ring))
351 (shr-probe-and-copy-url url)
352 (shr-copy-url url)))
354 (defun shr--current-link-region ()
355 (let ((current (get-text-property (point) 'shr-url))
356 start)
357 (save-excursion
358 ;; Go to the beginning.
359 (while (and (not (bobp))
360 (equal (get-text-property (point) 'shr-url) current))
361 (forward-char -1))
362 (unless (equal (get-text-property (point) 'shr-url) current)
363 (forward-char 1))
364 (setq start (point))
365 ;; Go to the end.
366 (while (and (not (eobp))
367 (equal (get-text-property (point) 'shr-url) current))
368 (forward-char 1))
369 (list start (point)))))
371 (defun shr--blink-link ()
372 (let* ((region (shr--current-link-region))
373 (overlay (make-overlay (car region) (cadr region))))
374 (overlay-put overlay 'face 'shr-selected-link)
375 (run-at-time 1 nil (lambda ()
376 (delete-overlay overlay)))))
378 (defun shr-next-link ()
379 "Skip to the next link."
380 (interactive)
381 (let ((current (get-text-property (point) 'shr-url))
382 (start (point))
383 skip)
384 (while (and (not (eobp))
385 (equal (get-text-property (point) 'shr-url) current))
386 (forward-char 1))
387 (cond
388 ((and (not (eobp))
389 (get-text-property (point) 'shr-url))
390 ;; The next link is adjacent.
391 (message "%s" (get-text-property (point) 'help-echo)))
392 ((or (eobp)
393 (not (setq skip (text-property-not-all (point) (point-max)
394 'shr-url nil))))
395 (goto-char start)
396 (message "No next link"))
398 (goto-char skip)
399 (message "%s" (get-text-property (point) 'help-echo))))))
401 (defun shr-previous-link ()
402 "Skip to the previous link."
403 (interactive)
404 (let ((start (point))
405 (found nil))
406 ;; Skip past the current link.
407 (while (and (not (bobp))
408 (get-text-property (point) 'help-echo))
409 (forward-char -1))
410 ;; Find the previous link.
411 (while (and (not (bobp))
412 (not (setq found (get-text-property (point) 'help-echo))))
413 (forward-char -1))
414 (if (not found)
415 (progn
416 (message "No previous link")
417 (goto-char start))
418 ;; Put point at the start of the link.
419 (while (and (not (bobp))
420 (get-text-property (point) 'help-echo))
421 (forward-char -1))
422 (forward-char 1)
423 (message "%s" (get-text-property (point) 'help-echo)))))
425 (defun shr-show-alt-text ()
426 "Show the ALT text of the image under point."
427 (interactive)
428 (let ((text (get-text-property (point) 'shr-alt)))
429 (if (not text)
430 (message "No image under point")
431 (message "%s" (shr-fill-text text)))))
433 (defun shr-browse-image (&optional copy-url)
434 "Browse the image under point.
435 If COPY-URL (the prefix if called interactively) is non-nil, copy
436 the URL of the image to the kill buffer instead."
437 (interactive "P")
438 (let ((url (get-text-property (point) 'image-url)))
439 (cond
440 ((not url)
441 (message "No image under point"))
442 (copy-url
443 (with-temp-buffer
444 (insert url)
445 (copy-region-as-kill (point-min) (point-max))
446 (message "Copied %s" url)))
448 (message "Browsing %s..." url)
449 (browse-url url)))))
451 (defun shr-insert-image ()
452 "Insert the image under point into the buffer."
453 (interactive)
454 (let ((url (get-text-property (point) 'image-url)))
455 (if (not url)
456 (message "No image under point")
457 (message "Inserting %s..." url)
458 (url-retrieve url 'shr-image-fetched
459 (list (current-buffer) (1- (point)) (point-marker))
460 t t))))
462 (defun shr-zoom-image ()
463 "Toggle the image size.
464 The size will be rotated between the default size, the original
465 size, and full-buffer size."
466 (interactive)
467 (let ((url (get-text-property (point) 'image-url))
468 (size (get-text-property (point) 'image-size))
469 (buffer-read-only nil))
470 (if (not url)
471 (message "No image under point")
472 ;; Delete the old picture.
473 (while (get-text-property (point) 'image-url)
474 (forward-char -1))
475 (forward-char 1)
476 (let ((start (point)))
477 (while (get-text-property (point) 'image-url)
478 (forward-char 1))
479 (forward-char -1)
480 (put-text-property start (point) 'display nil)
481 (when (> (- (point) start) 2)
482 (delete-region start (1- (point)))))
483 (message "Inserting %s..." url)
484 (url-retrieve url 'shr-image-fetched
485 (list (current-buffer) (1- (point)) (point-marker)
486 (list (cons 'size
487 (cond ((or (eq size 'default)
488 (null size))
489 'original)
490 ((eq size 'original)
491 'full)
492 ((eq size 'full)
493 'default)))))
494 t))))
496 ;;; Utility functions.
498 (defsubst shr-generic (dom)
499 (dolist (sub (dom-children dom))
500 (if (stringp sub)
501 (shr-insert sub)
502 (shr-descend sub))))
504 (defun shr-indirect-call (tag-name dom &rest args)
505 (let ((function (intern (concat "shr-tag-" (symbol-name tag-name)) obarray))
506 ;; Allow other packages to override (or provide) rendering
507 ;; of elements.
508 (external (cdr (assq tag-name shr-external-rendering-functions))))
509 (cond (external
510 (apply external dom args))
511 ((fboundp function)
512 (apply function dom args))
514 (apply 'shr-generic dom args)))))
516 (defun shr-descend (dom)
517 (let ((function
518 (intern (concat "shr-tag-" (symbol-name (dom-tag dom))) obarray))
519 ;; Allow other packages to override (or provide) rendering
520 ;; of elements.
521 (external (cdr (assq (dom-tag dom) shr-external-rendering-functions)))
522 (style (dom-attr dom 'style))
523 (shr-stylesheet shr-stylesheet)
524 (shr-depth (1+ shr-depth))
525 (start (point)))
526 ;; shr uses many frames per nested node.
527 (if (and (> shr-depth (/ max-specpdl-size 15))
528 (not (and (y-or-n-p "Too deeply nested to render properly; increase `max-specpdl-size'?")
529 (setq max-specpdl-size (* max-specpdl-size 2)))))
530 (setq shr-warning
531 "Not rendering the complete page because of too-deep nesting")
532 (when style
533 (if (string-match "color\\|display\\|border-collapse" style)
534 (setq shr-stylesheet (nconc (shr-parse-style style)
535 shr-stylesheet))
536 (setq style nil)))
537 ;; If we have a display:none, then just ignore this part of the DOM.
538 (unless (equal (cdr (assq 'display shr-stylesheet)) "none")
539 ;; We don't use shr-indirect-call here, since shr-descend is
540 ;; the central bit of shr.el, and should be as fast as
541 ;; possible. Having one more level of indirection with its
542 ;; negative effect on performance is deemed unjustified in
543 ;; this case.
544 (cond (external
545 (funcall external dom))
546 ((fboundp function)
547 (funcall function dom))
549 (shr-generic dom)))
550 (when (and shr-target-id
551 (equal (dom-attr dom 'id) shr-target-id))
552 ;; If the element was empty, we don't have anything to put the
553 ;; anchor on. So just insert a dummy character.
554 (when (= start (point))
555 (insert "*"))
556 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
557 ;; If style is set, then this node has set the color.
558 (when style
559 (shr-colorize-region
560 start (point)
561 (cdr (assq 'color shr-stylesheet))
562 (cdr (assq 'background-color shr-stylesheet))))))))
564 (defun shr-fill-text (text)
565 (if (zerop (length text))
566 text
567 (with-temp-buffer
568 (let ((shr-indentation 0)
569 (shr-start nil)
570 (shr-internal-width (- (window-body-width nil t)
571 (* 2 (frame-char-width))
572 ;; Adjust the window width for when
573 ;; the user disables the fringes,
574 ;; which causes the display engine
575 ;; to usurp one column for the
576 ;; continuation glyph.
577 (if (and (null shr-width)
578 (not (shr--have-one-fringe-p)))
579 (* (frame-char-width) 2)
580 0))))
581 (shr-insert text)
582 (shr-fill-lines (point-min) (point-max))
583 (buffer-string)))))
585 (define-inline shr-char-breakable-p (char)
586 "Return non-nil if a line can be broken before and after CHAR."
587 (inline-quote (aref fill-find-break-point-function-table ,char)))
588 (define-inline shr-char-nospace-p (char)
589 "Return non-nil if no space is required before and after CHAR."
590 (inline-quote (aref fill-nospace-between-words-table ,char)))
592 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
593 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
594 ;; parentheses, and so on, that should not be placed in the beginning
595 ;; of a line or the end of a line.
596 (define-inline shr-char-kinsoku-bol-p (char)
597 "Return non-nil if a line ought not to begin with CHAR."
598 (inline-letevals (char)
599 (inline-quote (and (not (eq ,char ?'))
600 (aref (char-category-set ,char) ?>)))))
601 (define-inline shr-char-kinsoku-eol-p (char)
602 "Return non-nil if a line ought not to end with CHAR."
603 (inline-quote (aref (char-category-set ,char) ?<)))
604 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
605 (load "kinsoku" nil t))
607 (defun shr-pixel-column ()
608 (if (not shr-use-fonts)
609 (current-column)
610 (if (not (get-buffer-window (current-buffer)))
611 (save-window-excursion
612 ;; Avoid errors if the selected window is a dedicated one,
613 ;; and they just want to insert a document into it.
614 (set-window-dedicated-p nil nil)
615 (set-window-buffer nil (current-buffer))
616 (car (window-text-pixel-size nil (line-beginning-position) (point))))
617 (car (window-text-pixel-size nil (line-beginning-position) (point))))))
619 (defun shr-pixel-region ()
620 (- (shr-pixel-column)
621 (save-excursion
622 (goto-char (mark))
623 (shr-pixel-column))))
625 (defun shr-string-pixel-width (string)
626 (if (not shr-use-fonts)
627 (length string)
628 ;; Save and restore point across with-temp-buffer, since
629 ;; shr-pixel-column uses save-window-excursion, which can reset
630 ;; point to 1.
631 (let ((pt (point)))
632 (prog1
633 (with-temp-buffer
634 (insert string)
635 (shr-pixel-column))
636 (goto-char pt)))))
638 (defsubst shr--translate-insertion-chars ()
639 ;; Remove soft hyphens.
640 (goto-char (point-min))
641 (while (search-forward "­" nil t)
642 (replace-match "" t t))
643 ;; Translate non-breaking spaces into real spaces.
644 (goto-char (point-min))
645 (while (search-forward " " nil t)
646 (replace-match " " t t)))
648 (defun shr-insert (text)
649 (when (and (not (bolp))
650 (get-text-property (1- (point)) 'image-url))
651 (insert "\n"))
652 (cond
653 ((eq shr-folding-mode 'none)
654 (let ((start (point)))
655 (insert text)
656 (save-restriction
657 (narrow-to-region start (point))
658 (shr--translate-insertion-chars)
659 (goto-char (point-max)))))
661 (let ((font-start (point)))
662 (when (and (string-match "\\`[ \t\n\r]" text)
663 (not (bolp))
664 (not (eq (char-after (1- (point))) ? )))
665 (insert " "))
666 (let ((start (point))
667 (bolp (bolp)))
668 (insert text)
669 (save-restriction
670 (narrow-to-region start (point))
671 (goto-char start)
672 (when (looking-at "[ \t\n\r]+")
673 (replace-match "" t t))
674 (while (re-search-forward "[ \t\n\r]+" nil t)
675 (replace-match " " t t))
676 (shr--translate-insertion-chars)
677 (goto-char (point-max)))
678 ;; We may have removed everything we inserted if it was just
679 ;; spaces.
680 (unless (= font-start (point))
681 ;; Mark all lines that should possibly be folded afterwards.
682 (when bolp
683 (shr-mark-fill start))
684 (when shr-use-fonts
685 (put-text-property font-start (point)
686 'face
687 (or shr-current-font 'variable-pitch)))))))))
689 (defun shr-fill-lines (start end)
690 (if (<= shr-internal-width 0)
692 (save-restriction
693 (narrow-to-region start end)
694 (goto-char start)
695 (when (get-text-property (point) 'shr-indentation)
696 (shr-fill-line))
697 (while (setq start (next-single-property-change start 'shr-indentation))
698 (goto-char start)
699 (when (bolp)
700 (shr-fill-line)))
701 (goto-char (point-max)))))
703 (defun shr-vertical-motion (column)
704 (if (not shr-use-fonts)
705 (move-to-column column)
706 (unless (eolp)
707 (forward-char 1))
708 (vertical-motion (cons (/ column (frame-char-width)) 0))
709 (unless (eolp)
710 (forward-char 1))))
712 (defun shr-fill-line ()
713 (let ((shr-indentation (get-text-property (point) 'shr-indentation))
714 (continuation (get-text-property
715 (point) 'shr-continuation-indentation))
716 start)
717 (put-text-property (point) (1+ (point)) 'shr-indentation nil)
718 (let ((face (get-text-property (point) 'face))
719 (background-start (point)))
720 (shr-indent)
721 (when face
722 (put-text-property background-start (point) 'face
723 `,(shr-face-background face))))
724 (setq start (point))
725 (setq shr-indentation (or continuation shr-indentation))
726 ;; If we have an indentation that's wider than the width we're
727 ;; trying to fill to, then just give up and don't do any filling.
728 (when (< shr-indentation shr-internal-width)
729 (shr-vertical-motion shr-internal-width)
730 (when (looking-at " $")
731 (delete-region (point) (line-end-position)))
732 (while (not (eolp))
733 ;; We have to do some folding. First find the first
734 ;; previous point suitable for folding.
735 (if (or (not (shr-find-fill-point (line-beginning-position)))
736 (= (point) start))
737 ;; We had unbreakable text (for this width), so just go to
738 ;; the first space and carry on.
739 (progn
740 (beginning-of-line)
741 (skip-chars-forward " ")
742 (search-forward " " (line-end-position) 'move)))
743 ;; Success; continue.
744 (when (= (preceding-char) ?\s)
745 (delete-char -1))
746 (let ((gap-start (point)))
747 (insert "\n")
748 (shr-indent)
749 (when (and (> (1- gap-start) (point-min))
750 ;; The link on both sides of the newline are the
751 ;; same...
752 (equal (get-text-property (point) 'shr-url)
753 (get-text-property (1- gap-start) 'shr-url)))
754 ;; ... so we join the two bits into one link logically, but
755 ;; not visually. This makes navigation between links work
756 ;; well, but avoids underscores before the link on the next
757 ;; line when indented.
758 (let ((props (copy-sequence (text-properties-at (point)))))
759 ;; We don't want to use the faces on the indentation, because
760 ;; that's ugly.
761 (setq props (plist-put props 'face nil))
762 (add-text-properties gap-start (point) props))))
763 (setq start (point))
764 (shr-vertical-motion shr-internal-width)
765 (when (looking-at " $")
766 (delete-region (point) (line-end-position)))))))
768 (defun shr-find-fill-point (start)
769 (let ((bp (point))
770 (end (point))
771 failed)
772 (while (not (or (setq failed (<= (point) start))
773 (eq (preceding-char) ? )
774 (eq (following-char) ? )
775 (shr-char-breakable-p (preceding-char))
776 (shr-char-breakable-p (following-char))
777 (and (shr-char-kinsoku-bol-p (preceding-char))
778 (shr-char-breakable-p (following-char))
779 (not (shr-char-kinsoku-bol-p (following-char))))
780 (shr-char-kinsoku-eol-p (following-char))
781 (bolp)))
782 (backward-char 1))
783 (if failed
784 ;; There's no breakable point, so we give it up.
785 (let (found)
786 (goto-char bp)
787 ;; Don't overflow the window edge, even if
788 ;; shr-kinsoku-shorten is nil.
789 (unless (or shr-kinsoku-shorten (null shr-width))
790 (while (setq found (re-search-forward
791 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
792 (line-end-position) 'move)))
793 (if (and found
794 (not (match-beginning 1)))
795 (goto-char (match-beginning 0)))))
797 (eolp)
798 ;; Don't put kinsoku-bol characters at the beginning of a line,
799 ;; or kinsoku-eol characters at the end of a line.
800 (cond
801 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
802 ;; is nil.
803 ((or shr-kinsoku-shorten (null shr-width))
804 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
805 (or (shr-char-kinsoku-eol-p (preceding-char))
806 (shr-char-kinsoku-bol-p (following-char))))
807 (backward-char 1))
808 (when (setq failed (<= (point) start))
809 ;; There's no breakable point that doesn't violate kinsoku,
810 ;; so we look for the second best position.
811 (while (and (progn
812 (forward-char 1)
813 (<= (point) end))
814 (progn
815 (setq bp (point))
816 (shr-char-kinsoku-eol-p (following-char)))))
817 (goto-char bp)))
818 ((shr-char-kinsoku-eol-p (preceding-char))
819 ;; Find backward the point where kinsoku-eol characters begin.
820 (let ((count 4))
821 (while
822 (progn
823 (backward-char 1)
824 (and (> (setq count (1- count)) 0)
825 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
826 (or (shr-char-kinsoku-eol-p (preceding-char))
827 (shr-char-kinsoku-bol-p (following-char)))))))
828 (when (setq failed (<= (point) start))
829 ;; There's no breakable point that doesn't violate kinsoku,
830 ;; so we go to the second best position.
831 (if (looking-at "\\(\\c<+\\)\\c<")
832 (goto-char (match-end 1))
833 (forward-char 1))))
834 ((shr-char-kinsoku-bol-p (following-char))
835 ;; Find forward the point where kinsoku-bol characters end.
836 (let ((count 4))
837 (while (progn
838 (forward-char 1)
839 (and (>= (setq count (1- count)) 0)
840 (shr-char-kinsoku-bol-p (following-char))
841 (shr-char-breakable-p (following-char))))))))
842 (when (eq (following-char) ? )
843 (forward-char 1))))
844 (not failed)))
846 (defun shr-parse-base (url)
847 ;; Always chop off anchors.
848 (when (string-match "#.*" url)
849 (setq url (substring url 0 (match-beginning 0))))
850 ;; NB: <base href="" > URI may itself be relative to the document s URI
851 (setq url (shr-expand-url url))
852 (let* ((parsed (url-generic-parse-url url))
853 (local (url-filename parsed)))
854 (setf (url-filename parsed) "")
855 ;; Chop off the bit after the last slash.
856 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local)
857 (setq local (match-string 1 local)))
858 ;; Always make the local bit end with a slash.
859 (when (and (not (zerop (length local)))
860 (not (eq (aref local (1- (length local))) ?/)))
861 (setq local (concat local "/")))
862 (list (url-recreate-url parsed)
863 local
864 (url-type parsed)
865 url)))
867 (autoload 'url-expand-file-name "url-expand")
869 ;; FIXME This needs some tests writing.
870 ;; Does it even need to exist, given that url-expand-file-name does?
871 (defun shr-expand-url (url &optional base)
872 (setq base
873 (if base
874 ;; shr-parse-base should never call this with non-nil base!
875 (shr-parse-base base)
876 ;; Bound by the parser.
877 shr-base))
878 (when (zerop (length url))
879 (setq url nil))
880 ;; Strip leading whitespace
881 (and url (string-match "\\`\\s-+" url)
882 (setq url (substring url (match-end 0))))
883 (cond ((zerop (length url))
884 (nth 3 base))
885 ((or (not base)
886 (string-match "\\`[a-z]*:" url))
887 ;; Absolute or empty URI
888 url)
889 ((eq (aref url 0) ?/)
890 (if (and (> (length url) 1)
891 (eq (aref url 1) ?/))
892 ;; //host...; just use the protocol
893 (concat (nth 2 base) ":" url)
894 ;; Just use the host name part.
895 (concat (car base) url)))
896 ((eq (aref url 0) ?#)
897 ;; A link to an anchor.
898 (concat (nth 3 base) url))
900 ;; Totally relative.
901 (url-expand-file-name url (concat (car base) (cadr base))))))
903 (defun shr-ensure-newline ()
904 (unless (bobp)
905 (let ((prefix (get-text-property (line-beginning-position)
906 'shr-prefix-length)))
907 (unless (or (zerop (current-column))
908 (and prefix
909 (= prefix (- (point) (line-beginning-position)))))
910 (insert "\n")))))
912 (defun shr-ensure-paragraph ()
913 (unless (bobp)
914 (let ((prefix (get-text-property (line-beginning-position)
915 'shr-prefix-length)))
916 (cond
917 ((and (bolp)
918 (save-excursion
919 (forward-line -1)
920 (looking-at " *$")))
921 ;; We're already at a new paragraph; do nothing.
923 ((and prefix
924 (= prefix (- (point) (line-beginning-position))))
925 ;; Do nothing; we're at the start of a <li>.
927 ((save-excursion
928 (beginning-of-line)
929 ;; If the current line is totally blank, and doesn't even
930 ;; have any face properties set, then delete the blank
931 ;; space.
932 (and (looking-at " *$")
933 (not (get-text-property (point) 'face))
934 (not (= (next-single-property-change (point) 'face nil
935 (line-end-position))
936 (line-end-position)))))
937 (delete-region (match-beginning 0) (match-end 0)))
938 ;; We have a single blank line.
939 ((and (eolp) (bolp))
940 (insert "\n"))
941 ;; Insert new paragraph.
943 (insert "\n\n"))))))
945 (defun shr-indent ()
946 (when (> shr-indentation 0)
947 (insert
948 (if (not shr-use-fonts)
949 (make-string shr-indentation ?\s)
950 (propertize " "
951 'display
952 `(space :width (,shr-indentation)))))))
954 (defun shr-fontize-dom (dom &rest types)
955 (let ((start (point)))
956 (shr-generic dom)
957 (dolist (type types)
958 (shr-add-font start (point) type))))
960 ;; Add face to the region, but avoid putting the font properties on
961 ;; blank text at the start of the line, and the newline at the end, to
962 ;; avoid ugliness.
963 (defun shr-add-font (start end type)
964 (save-excursion
965 (goto-char start)
966 (while (< (point) end)
967 (when (bolp)
968 (skip-chars-forward " "))
969 (add-face-text-property (point) (min (line-end-position) end) type t)
970 (if (< (line-end-position) end)
971 (forward-line 1)
972 (goto-char end)))))
974 (defun shr-mouse-browse-url (ev)
975 "Browse the URL under the mouse cursor."
976 (interactive "e")
977 (mouse-set-point ev)
978 (shr-browse-url))
980 (defun shr-browse-url (&optional external mouse-event)
981 "Browse the URL at point using `browse-url'.
982 If EXTERNAL is non-nil (interactively, the prefix argument), browse
983 the URL using `shr-external-browser'.
984 If this function is invoked by a mouse click, it will browse the URL
985 at the position of the click. Optional argument MOUSE-EVENT describes
986 the mouse click event."
987 (interactive (list current-prefix-arg last-nonmenu-event))
988 (mouse-set-point mouse-event)
989 (let ((url (get-text-property (point) 'shr-url)))
990 (cond
991 ((not url)
992 (message "No link under point"))
993 ((string-match "^mailto:" url)
994 (browse-url-mail url))
996 (if external
997 (progn
998 (funcall shr-external-browser url)
999 (shr--blink-link))
1000 (browse-url url))))))
1002 (defun shr-save-contents (directory)
1003 "Save the contents from URL in a file."
1004 (interactive "DSave contents of URL to directory: ")
1005 (let ((url (get-text-property (point) 'shr-url)))
1006 (if (not url)
1007 (message "No link under point")
1008 (url-retrieve (shr-encode-url url)
1009 'shr-store-contents (list url directory)
1010 nil t))))
1012 (defun shr-store-contents (status url directory)
1013 (unless (plist-get status :error)
1014 (when (or (search-forward "\n\n" nil t)
1015 (search-forward "\r\n\r\n" nil t))
1016 (write-region (point) (point-max)
1017 (expand-file-name (file-name-nondirectory url)
1018 directory)))))
1020 (defun shr-image-fetched (status buffer start end &optional flags)
1021 (let ((image-buffer (current-buffer)))
1022 (when (and (buffer-name buffer)
1023 (not (plist-get status :error)))
1024 (url-store-in-cache image-buffer)
1025 (goto-char (point-min))
1026 (when (or (search-forward "\n\n" nil t)
1027 (search-forward "\r\n\r\n" nil t))
1028 (let ((data (shr-parse-image-data)))
1029 (with-current-buffer buffer
1030 (save-excursion
1031 (save-restriction
1032 (widen)
1033 (let ((alt (buffer-substring start end))
1034 (properties (text-properties-at start))
1035 (inhibit-read-only t))
1036 (delete-region start end)
1037 (goto-char start)
1038 (funcall shr-put-image-function data alt flags)
1039 (while properties
1040 (let ((type (pop properties))
1041 (value (pop properties)))
1042 (unless (memq type '(display image-size))
1043 (put-text-property start (point) type value)))))))))))
1044 (kill-buffer image-buffer)))
1046 (defun shr-image-from-data (data)
1047 "Return an image from the data: URI content DATA."
1048 (when (string-match
1049 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
1050 data)
1051 (let ((param (match-string 4 data))
1052 (payload (url-unhex-string (match-string 5 data))))
1053 (when (and param
1054 (string-match "^.*\\(;[ \t]*base64\\)$" param))
1055 (setq payload (ignore-errors
1056 (base64-decode-string payload))))
1057 payload)))
1059 ;; Behind display-graphic-p test.
1060 (declare-function image-size "image.c" (spec &optional pixels frame))
1061 (declare-function image-animate "image" (image &optional index limit))
1063 (defun shr-put-image (spec alt &optional flags)
1064 "Insert image SPEC with a string ALT. Return image.
1065 SPEC is either an image data blob, or a list where the first
1066 element is the data blob and the second element is the content-type."
1067 (if (display-graphic-p)
1068 (let* ((size (cdr (assq 'size flags)))
1069 (data (if (consp spec)
1070 (car spec)
1071 spec))
1072 (content-type (and (consp spec)
1073 (cadr spec)))
1074 (start (point))
1075 (image (cond
1076 ((eq size 'original)
1077 (create-image data nil t :ascent 100
1078 :format content-type))
1079 ((eq content-type 'image/svg+xml)
1080 (create-image data 'svg t :ascent 100))
1081 ((eq size 'full)
1082 (ignore-errors
1083 (shr-rescale-image data content-type
1084 (plist-get flags :width)
1085 (plist-get flags :height))))
1087 (ignore-errors
1088 (shr-rescale-image data content-type
1089 (plist-get flags :width)
1090 (plist-get flags :height)))))))
1091 (when image
1092 ;; When inserting big-ish pictures, put them at the
1093 ;; beginning of the line.
1094 (when (and (> (current-column) 0)
1095 (> (car (image-size image t)) 400))
1096 (insert "\n"))
1097 (if (eq size 'original)
1098 (insert-sliced-image image (or alt "*") nil 20 1)
1099 (insert-image image (or alt "*")))
1100 (put-text-property start (point) 'image-size size)
1101 (when (and shr-image-animate
1102 (cond ((fboundp 'image-multi-frame-p)
1103 ;; Only animate multi-frame things that specify a
1104 ;; delay; eg animated gifs as opposed to
1105 ;; multi-page tiffs. FIXME?
1106 (cdr (image-multi-frame-p image)))
1107 ((fboundp 'image-animated-p)
1108 (image-animated-p image))))
1109 (image-animate image nil 60)))
1110 image)
1111 (insert (or alt ""))))
1113 (defun shr-rescale-image (data content-type width height
1114 &optional max-width max-height)
1115 "Rescale DATA, if too big, to fit the current buffer.
1116 WIDTH and HEIGHT are the sizes given in the HTML data, if any.
1118 The size of the displayed image will not exceed
1119 MAX-WIDTH/MAX-HEIGHT. If not given, use the current window
1120 width/height instead."
1121 (if (or (not (fboundp 'imagemagick-types))
1122 (not (get-buffer-window (current-buffer))))
1123 (create-image data nil t :ascent 100)
1124 (let* ((edges (window-inside-pixel-edges
1125 (get-buffer-window (current-buffer))))
1126 (max-width (truncate (* shr-max-image-proportion
1127 (or max-width
1128 (- (nth 2 edges) (nth 0 edges))))))
1129 (max-height (truncate (* shr-max-image-proportion
1130 (or max-height
1131 (- (nth 3 edges) (nth 1 edges))))))
1132 (scaling (image-compute-scaling-factor image-scaling-factor)))
1133 (when (or (and width
1134 (> width max-width))
1135 (and height
1136 (> height max-height)))
1137 (setq width nil
1138 height nil))
1139 (if (and width height
1140 (< (* width scaling) max-width)
1141 (< (* height scaling) max-height))
1142 (create-image
1143 data 'imagemagick t
1144 :ascent 100
1145 :width width
1146 :height height
1147 :format content-type)
1148 (create-image
1149 data 'imagemagick t
1150 :ascent 100
1151 :max-width max-width
1152 :max-height max-height
1153 :format content-type)))))
1155 ;; url-cache-extract autoloads url-cache.
1156 (declare-function url-cache-create-filename "url-cache" (url))
1157 (autoload 'mm-disable-multibyte "mm-util")
1158 (autoload 'browse-url-mail "browse-url")
1160 (defun shr-get-image-data (url)
1161 "Get image data for URL.
1162 Return a string with image data."
1163 (with-temp-buffer
1164 (mm-disable-multibyte)
1165 (when (ignore-errors
1166 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
1168 (when (re-search-forward "\r?\n\r?\n" nil t)
1169 (shr-parse-image-data)))))
1171 (declare-function libxml-parse-xml-region "xml.c"
1172 (start end &optional base-url discard-comments))
1174 (defun shr-parse-image-data ()
1175 (let ((data (buffer-substring (point) (point-max)))
1176 (content-type
1177 (save-excursion
1178 (save-restriction
1179 (narrow-to-region (point-min) (point))
1180 (let ((content-type (mail-fetch-field "content-type")))
1181 (and content-type
1182 ;; Remove any comments in the type string.
1183 (intern (replace-regexp-in-string ";.*" "" content-type)
1184 obarray)))))))
1185 ;; SVG images may contain references to further images that we may
1186 ;; want to block. So special-case these by parsing the XML data
1187 ;; and remove anything that looks like a blocked bit.
1188 (when (and shr-blocked-images
1189 (eq content-type 'image/svg+xml))
1190 (setq data
1191 ;; Note that libxml2 doesn't parse everything perfectly,
1192 ;; so glitches may occur during this transformation.
1193 (shr-dom-to-xml
1194 (libxml-parse-xml-region (point) (point-max)))))
1195 (list data content-type)))
1197 (defun shr-image-displayer (content-function)
1198 "Return a function to display an image.
1199 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1200 is an argument. The function to be returned takes three arguments URL,
1201 START, and END. Note that START and END should be markers."
1202 `(lambda (url start end)
1203 (when url
1204 (if (string-match "\\`cid:" url)
1205 ,(when content-function
1206 `(let ((image (funcall ,content-function
1207 (substring url (match-end 0)))))
1208 (when image
1209 (goto-char start)
1210 (funcall shr-put-image-function
1211 image (buffer-substring start end))
1212 (delete-region (point) end))))
1213 (url-retrieve url 'shr-image-fetched
1214 (list (current-buffer) start end)
1215 t t)))))
1217 (defun shr-heading (dom &rest types)
1218 (shr-ensure-paragraph)
1219 (apply #'shr-fontize-dom dom types)
1220 (shr-ensure-paragraph))
1222 (defun shr-urlify (start url &optional title)
1223 (shr-add-font start (point) 'shr-link)
1224 (add-text-properties
1225 start (point)
1226 (list 'shr-url url
1227 'help-echo (let ((parsed (url-generic-parse-url
1228 (or (ignore-errors
1229 (decode-coding-string
1230 (url-unhex-string url)
1231 'utf-8 t))
1232 url)))
1233 iri)
1234 ;; If we have an IDNA domain, then show the
1235 ;; decoded version in the mouseover to let the
1236 ;; user know that there's something possibly
1237 ;; fishy.
1238 (when (url-host parsed)
1239 (setf (url-host parsed)
1240 (puny-encode-domain (url-host parsed))))
1241 (setq iri (url-recreate-url parsed))
1242 (if title
1243 (format "%s (%s)" iri title)
1244 iri))
1245 'follow-link t
1246 'mouse-face 'highlight))
1247 ;; Don't overwrite any keymaps that are already in the buffer (i.e.,
1248 ;; image keymaps).
1249 (while (and start
1250 (< start (point)))
1251 (let ((next (next-single-property-change start 'keymap nil (point))))
1252 (if (get-text-property start 'keymap)
1253 (setq start next)
1254 (put-text-property start (or next (point)) 'keymap shr-map)))))
1256 (defun shr-encode-url (url)
1257 "Encode URL."
1258 (browse-url-url-encode-chars url "[)$ ]"))
1260 (autoload 'shr-color-visible "shr-color")
1261 (autoload 'shr-color->hexadecimal "shr-color")
1263 (defun shr-color-check (fg bg)
1264 "Check that FG is visible on BG.
1265 Returns (fg bg) with corrected values.
1266 Returns nil if the colors that would be used are the default
1267 ones, in case fg and bg are nil."
1268 (when (or fg bg)
1269 (let ((fixed (cond ((null fg) 'fg)
1270 ((null bg) 'bg))))
1271 ;; Convert colors to hexadecimal, or set them to default.
1272 (let ((fg (or (shr-color->hexadecimal fg)
1273 (frame-parameter nil 'foreground-color)))
1274 (bg (or (shr-color->hexadecimal bg)
1275 (frame-parameter nil 'background-color))))
1276 (cond ((eq fixed 'bg)
1277 ;; Only return the new fg
1278 (list nil (cadr (shr-color-visible bg fg t))))
1279 ((eq fixed 'fg)
1280 ;; Invert args and results and return only the new bg
1281 (list (cadr (shr-color-visible fg bg t)) nil))
1283 (shr-color-visible bg fg)))))))
1285 (defun shr-colorize-region (start end fg &optional bg)
1286 (when (and shr-use-colors
1287 (or fg bg)
1288 (>= (display-color-cells) 88))
1289 (let ((new-colors (shr-color-check fg bg)))
1290 (when new-colors
1291 (when fg
1292 (add-face-text-property start end
1293 (list :foreground (cadr new-colors))
1295 (when bg
1296 (add-face-text-property start end
1297 (list :background (car new-colors))
1298 t)))
1299 new-colors)))
1301 ;;; Tag-specific rendering rules.
1303 (defun shr-tag-html (dom)
1304 (let ((dir (dom-attr dom 'dir)))
1305 (cond
1306 ((equal dir "ltr")
1307 (setq bidi-paragraph-direction 'left-to-right))
1308 ((equal dir "rtl")
1309 (setq bidi-paragraph-direction 'right-to-left))
1310 ((equal dir "auto")
1311 (setq bidi-paragraph-direction nil))))
1312 (shr-generic dom))
1314 (defun shr-tag-body (dom)
1315 (let* ((start (point))
1316 (fgcolor (or (dom-attr dom 'fgcolor) (dom-attr dom 'text)))
1317 (bgcolor (dom-attr dom 'bgcolor))
1318 (shr-stylesheet (list (cons 'color fgcolor)
1319 (cons 'background-color bgcolor))))
1320 (shr-generic dom)
1321 (shr-colorize-region start (point) fgcolor bgcolor)))
1323 (defun shr-tag-style (_dom)
1326 (defun shr-tag-script (_dom)
1329 (defun shr-tag-comment (_dom)
1332 (defun shr-dom-to-xml (dom)
1333 (with-temp-buffer
1334 (shr-dom-print dom)
1335 (buffer-string)))
1337 (defun shr-dom-print (dom)
1338 "Convert DOM into a string containing the xml representation."
1339 (insert (format "<%s" (dom-tag dom)))
1340 (dolist (attr (dom-attributes dom))
1341 ;; Ignore attributes that start with a colon because they are
1342 ;; private elements.
1343 (unless (= (aref (format "%s" (car attr)) 0) ?:)
1344 (insert (format " %s=\"%s\"" (car attr) (cdr attr)))))
1345 (insert ">")
1346 (let (url)
1347 (dolist (elem (dom-children dom))
1348 (cond
1349 ((stringp elem)
1350 (insert elem))
1351 ((eq (dom-tag elem) 'comment)
1353 ((or (not (eq (dom-tag elem) 'image))
1354 ;; Filter out blocked elements inside the SVG image.
1355 (not (setq url (dom-attr elem ':xlink:href)))
1356 (not shr-blocked-images)
1357 (not (string-match shr-blocked-images url)))
1358 (insert " ")
1359 (shr-dom-print elem)))))
1360 (insert (format "</%s>" (dom-tag dom))))
1362 (defun shr-tag-svg (dom)
1363 (when (and (image-type-available-p 'svg)
1364 (not shr-inhibit-images)
1365 (dom-attr dom 'width)
1366 (dom-attr dom 'height))
1367 (funcall shr-put-image-function (list (shr-dom-to-xml dom) 'image/svg+xml)
1368 "SVG Image")))
1370 (defun shr-tag-sup (dom)
1371 (let ((start (point)))
1372 (shr-generic dom)
1373 (put-text-property start (point) 'display '(raise 0.5))))
1375 (defun shr-tag-sub (dom)
1376 (let ((start (point)))
1377 (shr-generic dom)
1378 (put-text-property start (point) 'display '(raise -0.5))))
1380 (defun shr-tag-p (dom)
1381 (shr-ensure-paragraph)
1382 (shr-generic dom)
1383 (shr-ensure-paragraph))
1385 (defun shr-tag-div (dom)
1386 (let ((display (cdr (assq 'display shr-stylesheet))))
1387 (if (or (equal display "inline")
1388 (equal display "inline-block"))
1389 (shr-generic dom)
1390 (shr-ensure-newline)
1391 (shr-generic dom)
1392 (shr-ensure-newline))))
1394 (defun shr-tag-s (dom)
1395 (shr-fontize-dom dom 'shr-strike-through))
1397 (defun shr-tag-b (dom)
1398 (shr-fontize-dom dom 'bold))
1400 (defun shr-tag-i (dom)
1401 (shr-fontize-dom dom 'italic))
1403 (defun shr-tag-em (dom)
1404 (shr-fontize-dom dom 'italic))
1406 (defun shr-tag-strong (dom)
1407 (shr-fontize-dom dom 'bold))
1409 (defun shr-tag-u (dom)
1410 (shr-fontize-dom dom 'underline))
1412 (defun shr-tag-tt (dom)
1413 (let ((shr-current-font 'default))
1414 (shr-generic dom)))
1416 (defun shr-tag-ins (cont)
1417 (let* ((start (point))
1418 (color "green")
1419 (shr-stylesheet (nconc (list (cons 'color color))
1420 shr-stylesheet)))
1421 (shr-generic cont)
1422 (shr-colorize-region start (point) color
1423 (cdr (assq 'background-color shr-stylesheet)))))
1425 (defun shr-tag-del (cont)
1426 (let* ((start (point))
1427 (color "red")
1428 (shr-stylesheet (nconc (list (cons 'color color))
1429 shr-stylesheet)))
1430 (shr-fontize-dom cont 'shr-strike-through)
1431 (shr-colorize-region start (point) color
1432 (cdr (assq 'background-color shr-stylesheet)))))
1434 (defun shr-parse-style (style)
1435 (when style
1436 (save-match-data
1437 (when (string-match "\n" style)
1438 (setq style (replace-match " " t t style))))
1439 (let ((plist nil))
1440 (dolist (elem (split-string style ";"))
1441 (when elem
1442 (setq elem (split-string elem ":"))
1443 (when (and (car elem)
1444 (cadr elem))
1445 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1446 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1447 (when (string-match " *!important\\'" value)
1448 (setq value (substring value 0 (match-beginning 0))))
1449 (unless (equal value "inherit")
1450 (push (cons (intern name obarray)
1451 value)
1452 plist))))))
1453 plist)))
1455 (defun shr-tag-base (dom)
1456 (when-let* ((base (dom-attr dom 'href)))
1457 (setq shr-base (shr-parse-base base)))
1458 (shr-generic dom))
1460 (defun shr-tag-a (dom)
1461 (let ((url (dom-attr dom 'href))
1462 (title (dom-attr dom 'title))
1463 (start (point))
1464 shr-start)
1465 (shr-generic dom)
1466 (when (and shr-target-id
1467 (equal (dom-attr dom 'name) shr-target-id))
1468 ;; We have a zero-length <a name="foo"> element, so just
1469 ;; insert... something.
1470 (when (= start (point))
1471 (shr-ensure-newline)
1472 (insert " "))
1473 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
1474 (when url
1475 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1477 (defun shr-tag-object (dom)
1478 (unless shr-inhibit-images
1479 (let ((start (point))
1480 url multimedia image)
1481 (when-let* ((type (dom-attr dom 'type)))
1482 (when (string-match "\\`image/svg" type)
1483 (setq url (dom-attr dom 'data)
1484 image t)))
1485 (dolist (child (dom-non-text-children dom))
1486 (cond
1487 ((eq (dom-tag child) 'embed)
1488 (setq url (or url (dom-attr child 'src))
1489 multimedia t))
1490 ((and (eq (dom-tag child) 'param)
1491 (equal (dom-attr child 'name) "movie"))
1492 (setq url (or url (dom-attr child 'value))
1493 multimedia t))))
1494 (when url
1495 (cond
1496 (image
1497 (shr-indirect-call 'img dom url)
1498 (setq dom nil))
1499 (multimedia
1500 (shr-insert " [multimedia] ")
1501 (shr-urlify start (shr-expand-url url)))))
1502 (when dom
1503 (shr-generic dom)))))
1505 (defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
1506 ("ogv" . 1.0)
1507 ("ogg" . 1.0)
1508 ("opus" . 1.0)
1509 ("flac" . 0.9)
1510 ("wav" . 0.5))
1511 "Preferences for media types.
1512 The key element should be a regexp matched against the type of the source or
1513 url if no type is specified. The value should be a float in the range 0.0 to
1514 1.0. Media elements with higher value are preferred."
1515 :version "24.4"
1516 :group 'shr
1517 :type '(alist :key-type regexp :value-type float))
1519 (defun shr--get-media-pref (elem)
1520 "Determine the preference for ELEM.
1521 The preference is a float determined from `shr-prefer-media-type'."
1522 (let ((type (dom-attr elem 'type))
1523 (p 0.0))
1524 (unless type
1525 (setq type (dom-attr elem 'src)))
1526 (when type
1527 (dolist (pref shr-prefer-media-type-alist)
1528 (when (and
1529 (> (cdr pref) p)
1530 (string-match-p (car pref) type))
1531 (setq p (cdr pref)))))
1534 (defun shr--extract-best-source (dom &optional url pref)
1535 "Extract the best `:src' property from <source> blocks in DOM."
1536 (setq pref (or pref -1.0))
1537 (let (new-pref)
1538 (dolist (elem (dom-non-text-children dom))
1539 (when (and (eq (dom-tag elem) 'source)
1540 (< pref
1541 (setq new-pref
1542 (shr--get-media-pref elem))))
1543 (setq pref new-pref
1544 url (dom-attr elem 'src))
1545 ;; libxml's html parser isn't HTML5 compliant and non terminated
1546 ;; source tags might end up as children. So recursion it is...
1547 (dolist (child (dom-non-text-children elem))
1548 (when (eq (dom-tag child) 'source)
1549 (let ((ret (shr--extract-best-source (list child) url pref)))
1550 (when (< pref (cdr ret))
1551 (setq url (car ret)
1552 pref (cdr ret)))))))))
1553 (cons url pref))
1555 (defun shr-tag-video (dom)
1556 (let ((image (dom-attr dom 'poster))
1557 (url (dom-attr dom 'src))
1558 (start (point)))
1559 (unless url
1560 (setq url (car (shr--extract-best-source dom))))
1561 (if (> (length image) 0)
1562 (shr-indirect-call 'img nil image)
1563 (shr-insert " [video] "))
1564 (shr-urlify start (shr-expand-url url))))
1566 (defun shr-tag-audio (dom)
1567 (let ((url (dom-attr dom 'src))
1568 (start (point)))
1569 (unless url
1570 (setq url (car (shr--extract-best-source dom))))
1571 (shr-insert " [audio] ")
1572 (shr-urlify start (shr-expand-url url))))
1574 (defun shr-tag-img (dom &optional url)
1575 (when (or url
1576 (and dom
1577 (or (> (length (dom-attr dom 'src)) 0)
1578 (> (length (dom-attr dom 'srcset)) 0))))
1579 (when (> (current-column) 0)
1580 (insert "\n"))
1581 (let ((alt (dom-attr dom 'alt))
1582 (width (shr-string-number (dom-attr dom 'width)))
1583 (height (shr-string-number (dom-attr dom 'height)))
1584 (url (shr-expand-url (or url (shr--preferred-image dom)))))
1585 (let ((start (point-marker)))
1586 (when (zerop (length alt))
1587 (setq alt "*"))
1588 (cond
1589 ((or (member (dom-attr dom 'height) '("0" "1"))
1590 (member (dom-attr dom 'width) '("0" "1")))
1591 ;; Ignore zero-sized or single-pixel images.
1593 ((and (not shr-inhibit-images)
1594 (string-match "\\`data:" url))
1595 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1596 (if image
1597 (funcall shr-put-image-function image alt
1598 (list :width width :height height))
1599 (insert alt))))
1600 ((and (not shr-inhibit-images)
1601 (string-match "\\`cid:" url))
1602 (let ((url (substring url (match-end 0)))
1603 image)
1604 (if (or (not shr-content-function)
1605 (not (setq image (funcall shr-content-function url))))
1606 (insert alt)
1607 (funcall shr-put-image-function image alt
1608 (list :width width :height height)))))
1609 ((or shr-inhibit-images
1610 (and shr-blocked-images
1611 (string-match shr-blocked-images url)))
1612 (setq shr-start (point))
1613 (shr-insert alt))
1614 ((and (not shr-ignore-cache)
1615 (url-is-cached (shr-encode-url url)))
1616 (funcall shr-put-image-function (shr-get-image-data url) alt
1617 (list :width width :height height)))
1619 (when (and shr-ignore-cache
1620 (url-is-cached (shr-encode-url url)))
1621 (let ((file (url-cache-create-filename (shr-encode-url url))))
1622 (when (file-exists-p file)
1623 (delete-file file))))
1624 (when (image-type-available-p 'svg)
1625 (insert-image
1626 (shr-make-placeholder-image dom)
1627 (or alt "")))
1628 (insert " ")
1629 (url-queue-retrieve
1630 (shr-encode-url url) 'shr-image-fetched
1631 (list (current-buffer) start (set-marker (make-marker) (point))
1632 (list :width width :height height))
1633 t t)))
1634 (when (zerop shr-table-depth) ;; We are not in a table.
1635 (put-text-property start (point) 'keymap shr-image-map)
1636 (put-text-property start (point) 'shr-alt alt)
1637 (put-text-property start (point) 'image-url url)
1638 (put-text-property start (point) 'image-displayer
1639 (shr-image-displayer shr-content-function))
1640 (put-text-property start (point) 'help-echo
1641 (shr-fill-text
1642 (or (dom-attr dom 'title) alt))))))))
1644 (defun shr--preferred-image (dom)
1645 (let ((srcset (dom-attr dom 'srcset))
1646 (frame-width (frame-pixel-width))
1647 (width (string-to-number (or (dom-attr dom 'width) "100")))
1648 candidate)
1649 (when (> (length srcset) 0)
1650 ;; srcset consist of a series of URL/size specifications
1651 ;; separated by the ", " string.
1652 (setq srcset
1653 (sort (mapcar
1654 (lambda (elem)
1655 (let ((spec (split-string elem "[\t\n\r ]+")))
1656 (cond
1657 ((= (length spec) 1)
1658 ;; Make sure it's well formed.
1659 (list (car spec) 0))
1660 ((string-match "\\([0-9]+\\)x\\'" (cadr spec))
1661 ;; If we have an "x" form, then use the width
1662 ;; spec to compute the real width.
1663 (list (car spec)
1664 (* width (string-to-number
1665 (match-string 1 (cadr spec))))))
1667 (list (car spec)
1668 (string-to-number (cadr spec)))))))
1669 (split-string (replace-regexp-in-string
1670 "\\`[\t\n\r ]+\\|[\t\n\r ]+\\'" "" srcset)
1671 "[\t\n\r ]*,[\t\n\r ]*"))
1672 (lambda (e1 e2)
1673 (> (cadr e1) (cadr e2)))))
1674 ;; Choose the smallest picture that's bigger than the current
1675 ;; frame.
1676 (setq candidate (caar srcset))
1677 (while (and srcset
1678 (> (cadr (car srcset)) frame-width))
1679 (setq candidate (caar srcset))
1680 (pop srcset)))
1681 (or candidate (dom-attr dom 'src))))
1683 (defun shr-string-number (string)
1684 (if (null string)
1686 (setq string (replace-regexp-in-string "[^0-9]" "" string))
1687 (if (zerop (length string))
1689 (string-to-number string))))
1691 (defun shr-make-placeholder-image (dom)
1692 (let* ((edges (and
1693 (get-buffer-window (current-buffer))
1694 (window-inside-pixel-edges
1695 (get-buffer-window (current-buffer)))))
1696 (scaling (image-compute-scaling-factor image-scaling-factor))
1697 (width (truncate
1698 (* (or (shr-string-number (dom-attr dom 'width)) 100)
1699 scaling)))
1700 (height (truncate
1701 (* (or (shr-string-number (dom-attr dom 'height)) 100)
1702 scaling)))
1703 (max-width
1704 (and edges
1705 (truncate (* shr-max-image-proportion
1706 (- (nth 2 edges) (nth 0 edges))))))
1707 (max-height (and edges
1708 (truncate (* shr-max-image-proportion
1709 (- (nth 3 edges) (nth 1 edges))))))
1710 svg)
1711 (when (and max-width
1712 (> width max-width))
1713 (setq height (truncate (* (/ (float max-width) width) height))
1714 width max-width))
1715 (when (and max-height
1716 (> height max-height))
1717 (setq width (truncate (* (/ (float max-height) height) width))
1718 height max-height))
1719 (setq svg (svg-create width height))
1720 (svg-gradient svg "background" 'linear '((0 . "#b0b0b0") (100 . "#808080")))
1721 (svg-rectangle svg 0 0 width height :gradient "background"
1722 :stroke-width 2 :stroke-color "black")
1723 (let ((image (svg-image svg)))
1724 (setf (image-property image :ascent) 100)
1725 image)))
1727 (defun shr-tag-pre (dom)
1728 (let ((shr-folding-mode 'none)
1729 (shr-current-font 'default))
1730 (shr-ensure-newline)
1731 (shr-generic dom)
1732 (shr-ensure-newline)))
1734 (defun shr-tag-blockquote (dom)
1735 (shr-ensure-paragraph)
1736 (let ((start (point))
1737 (shr-indentation (+ shr-indentation
1738 (* 4 shr-table-separator-pixel-width))))
1739 (shr-generic dom)
1740 (shr-ensure-paragraph)
1741 (shr-mark-fill start)))
1743 (defun shr-tag-dl (dom)
1744 (shr-ensure-paragraph)
1745 (shr-generic dom)
1746 (shr-ensure-paragraph))
1748 (defun shr-tag-dt (dom)
1749 (shr-ensure-newline)
1750 (shr-generic dom)
1751 (shr-ensure-newline))
1753 (defun shr-tag-dd (dom)
1754 (shr-ensure-newline)
1755 (let ((shr-indentation (+ shr-indentation
1756 (* 4 shr-table-separator-pixel-width))))
1757 (shr-generic dom)))
1759 (defun shr-tag-ul (dom)
1760 (shr-ensure-paragraph)
1761 (let ((shr-list-mode 'ul))
1762 (shr-generic dom))
1763 ;; If we end on an empty <li>, then make sure we really end on a new
1764 ;; paragraph.
1765 (unless (bolp)
1766 (insert "\n"))
1767 (shr-ensure-paragraph))
1769 (defun shr-tag-ol (dom)
1770 (shr-ensure-paragraph)
1771 (let ((shr-list-mode 1))
1772 (shr-generic dom))
1773 (shr-ensure-paragraph))
1775 (defun shr-tag-li (dom)
1776 (shr-ensure-newline)
1777 (let ((start (point)))
1778 (let* ((bullet
1779 (if (numberp shr-list-mode)
1780 (prog1
1781 (format "%d " shr-list-mode)
1782 (setq shr-list-mode (1+ shr-list-mode)))
1783 (car shr-internal-bullet)))
1784 (width (if (numberp shr-list-mode)
1785 (shr-string-pixel-width bullet)
1786 (cdr shr-internal-bullet))))
1787 (insert bullet)
1788 (shr-mark-fill start)
1789 (let ((shr-indentation (+ shr-indentation width)))
1790 (put-text-property start (1+ start)
1791 'shr-continuation-indentation shr-indentation)
1792 (put-text-property start (1+ start) 'shr-prefix-length (length bullet))
1793 (shr-generic dom))))
1794 (unless (bolp)
1795 (insert "\n")))
1797 (defun shr-mark-fill (start)
1798 ;; We may not have inserted any text to fill.
1799 (unless (= start (point))
1800 (put-text-property start (1+ start)
1801 'shr-indentation shr-indentation)))
1803 (defun shr-tag-br (dom)
1804 (when (and (not (bobp))
1805 ;; Only add a newline if we break the current line, or
1806 ;; the previous line isn't a blank line.
1807 (or (not (bolp))
1808 (and (> (- (point) 2) (point-min))
1809 (not (= (char-after (- (point) 2)) ?\n)))))
1810 (insert "\n"))
1811 (shr-generic dom))
1813 (defun shr-tag-span (dom)
1814 (shr-generic dom))
1816 (defun shr-tag-h1 (dom)
1817 (shr-heading dom (if shr-use-fonts
1818 '(variable-pitch (:height 1.3 :weight bold))
1819 'bold)))
1821 (defun shr-tag-h2 (dom)
1822 (shr-heading dom 'bold))
1824 (defun shr-tag-h3 (dom)
1825 (shr-heading dom 'italic))
1827 (defun shr-tag-h4 (dom)
1828 (shr-heading dom))
1830 (defun shr-tag-h5 (dom)
1831 (shr-heading dom))
1833 (defun shr-tag-h6 (dom)
1834 (shr-heading dom))
1836 (defun shr-tag-hr (_dom)
1837 (shr-ensure-newline)
1838 (insert (make-string (if (not shr-use-fonts)
1839 shr-internal-width
1840 (1+ (/ shr-internal-width
1841 shr-table-separator-pixel-width)))
1842 shr-hr-line)
1843 "\n"))
1845 (defun shr-tag-title (dom)
1846 (shr-heading dom 'bold 'underline))
1848 (defun shr-tag-font (dom)
1849 (let* ((start (point))
1850 (color (dom-attr dom 'color))
1851 (shr-stylesheet (nconc (list (cons 'color color))
1852 shr-stylesheet)))
1853 (shr-generic dom)
1854 (when color
1855 (shr-colorize-region start (point) color
1856 (cdr (assq 'background-color shr-stylesheet))))))
1858 (defun shr-tag-bdo (dom)
1859 (let* ((direction (dom-attr dom 'dir))
1860 (char (cond
1861 ((equal direction "ltr")
1862 ?\N{LEFT-TO-RIGHT OVERRIDE})
1863 ((equal direction "rtl")
1864 ?\N{RIGHT-TO-LEFT OVERRIDE}))))
1865 (when char
1866 (insert ?\N{FIRST STRONG ISOLATE} char))
1867 (shr-generic dom)
1868 (when char
1869 (insert ?\N{POP DIRECTIONAL FORMATTING} ?\N{POP DIRECTIONAL ISOLATE}))))
1871 (defun shr-tag-bdi (dom)
1872 (insert ?\N{FIRST STRONG ISOLATE})
1873 (shr-generic dom)
1874 (insert ?\N{POP DIRECTIONAL ISOLATE}))
1876 ;;; Table rendering algorithm.
1878 ;; Table rendering is the only complicated thing here. We do this by
1879 ;; first counting how many TDs there are in each TR, and registering
1880 ;; how wide they think they should be ("width=45%", etc). Then we
1881 ;; render each TD separately (this is done in temporary buffers, so
1882 ;; that we can use all the rendering machinery as if we were in the
1883 ;; main buffer). Now we know how much space each TD really takes, so
1884 ;; we then render everything again with the new widths, and finally
1885 ;; insert all these boxes into the main buffer.
1886 (defun shr-tag-table-1 (dom)
1887 (setq dom (or (dom-child-by-tag dom 'tbody) dom))
1888 (let* ((shr-inhibit-images t)
1889 (shr-table-depth (1+ shr-table-depth))
1890 (shr-kinsoku-shorten t)
1891 ;; Find all suggested widths.
1892 (columns (shr-column-specs dom))
1893 ;; Compute how many pixels wide each TD should be.
1894 (suggested-widths (shr-pro-rate-columns columns))
1895 ;; Do a "test rendering" to see how big each TD is (this can
1896 ;; be smaller (if there's little text) or bigger (if there's
1897 ;; unbreakable text).
1898 (elems (or (dom-attr dom 'shr-suggested-widths)
1899 (shr-make-table dom suggested-widths nil
1900 'shr-suggested-widths)))
1901 (sketch (cl-loop for line in elems
1902 collect (mapcar #'car line)))
1903 (natural (cl-loop for line in elems
1904 collect (mapcar #'cdr line)))
1905 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1906 ;; This probably won't work very well.
1907 (when (> (+ (cl-loop for width across sketch-widths
1908 summing (1+ width))
1909 shr-indentation shr-table-separator-pixel-width)
1910 (frame-width))
1911 (setq truncate-lines t))
1912 ;; Then render the table again with these new "hard" widths.
1913 (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
1915 (defun shr-table-body (dom)
1916 (let ((tbodies (seq-filter (lambda (child)
1917 (eq (dom-tag child) 'tbody))
1918 (dom-non-text-children dom))))
1919 (cond
1920 ((null tbodies)
1921 dom)
1922 ((= (length tbodies) 1)
1923 (car tbodies))
1925 ;; Table with multiple tbodies. Convert into a single tbody.
1926 `(tbody nil ,@(cl-reduce 'append
1927 (mapcar 'dom-non-text-children tbodies)))))))
1929 (defun shr-tag-table (dom)
1930 (shr-ensure-paragraph)
1931 (let* ((caption (dom-children (dom-child-by-tag dom 'caption)))
1932 (header (dom-non-text-children (dom-child-by-tag dom 'thead)))
1933 (body (dom-non-text-children (shr-table-body dom)))
1934 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot)))
1935 (bgcolor (dom-attr dom 'bgcolor))
1936 (start (point))
1937 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1938 shr-stylesheet))
1939 (nheader (if header (shr-max-columns header)))
1940 (nbody (if body (shr-max-columns body) 0))
1941 (nfooter (if footer (shr-max-columns footer))))
1942 (if (and (not caption)
1943 (not header)
1944 (not (dom-child-by-tag dom 'tbody))
1945 (not (dom-child-by-tag dom 'tr))
1946 (not footer))
1947 ;; The table is totally invalid and just contains random junk.
1948 ;; Try to output it anyway.
1949 (shr-generic dom)
1950 ;; It's a real table, so render it.
1951 (if (dom-attr dom 'shr-fixed-table)
1952 (shr-tag-table-1 dom)
1953 ;; Only fix up the table once.
1954 (let ((table
1955 (nconc
1956 (list 'table nil)
1957 (if caption `((tr nil (td nil ,@caption))))
1958 (cond
1959 (header
1960 (if footer
1961 ;; header + body + footer
1962 (if (= nheader nbody)
1963 (if (= nbody nfooter)
1964 `((tr nil (td nil (table nil
1965 (tbody nil ,@header
1966 ,@body ,@footer)))))
1967 (nconc `((tr nil (td nil (table nil
1968 (tbody nil ,@header
1969 ,@body)))))
1970 (if (= nfooter 1)
1971 footer
1972 `((tr nil (td nil (table
1973 nil (tbody
1974 nil ,@footer))))))))
1975 (nconc `((tr nil (td nil (table nil (tbody
1976 nil ,@header)))))
1977 (if (= nbody nfooter)
1978 `((tr nil (td nil (table
1979 nil (tbody nil ,@body
1980 ,@footer)))))
1981 (nconc `((tr nil (td nil (table
1982 nil (tbody nil
1983 ,@body)))))
1984 (if (= nfooter 1)
1985 footer
1986 `((tr nil (td nil (table
1988 (tbody
1990 ,@footer))))))))))
1991 ;; header + body
1992 (if (= nheader nbody)
1993 `((tr nil (td nil (table nil (tbody nil ,@header
1994 ,@body)))))
1995 (if (= nheader 1)
1996 `(,@header (tr nil (td nil (table
1997 nil (tbody nil ,@body)))))
1998 `((tr nil (td nil (table nil (tbody nil ,@header))))
1999 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
2000 (footer
2001 ;; body + footer
2002 (if (= nbody nfooter)
2003 `((tr nil (td nil (table
2004 nil (tbody nil ,@body ,@footer)))))
2005 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
2006 (if (= nfooter 1)
2007 footer
2008 `((tr nil (td nil (table
2009 nil (tbody nil ,@footer)))))))))
2010 (caption
2011 `((tr nil (td nil (table nil (tbody nil ,@body))))))
2012 (body)))))
2013 (dom-set-attribute table 'shr-fixed-table t)
2014 (setcdr dom (cdr table))
2015 (shr-tag-table-1 dom))))
2016 (when bgcolor
2017 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
2018 bgcolor))
2019 ;; Finally, insert all the images after the table. The Emacs buffer
2020 ;; model isn't strong enough to allow us to put the images actually
2021 ;; into the tables. It inserts also non-td/th objects.
2022 (when (zerop shr-table-depth)
2023 (save-excursion
2024 (shr-expand-alignments start (point)))
2025 (let ((strings (shr-collect-extra-strings-in-table dom)))
2026 (when strings
2027 (save-restriction
2028 (narrow-to-region (point) (point))
2029 (insert (mapconcat #'identity strings "\n"))
2030 (shr-fill-lines (point-min) (point-max))))))))
2032 (defun shr-collect-extra-strings-in-table (dom &optional flags)
2033 "Return extra strings in DOM of which the root is a table clause.
2034 Render <img>s and <object>s, and strings and child <table>s of which
2035 the parent <td> or <th> is lacking. FLAGS is a cons of two boolean
2036 flags that control whether to collect or render objects."
2037 ;; This function runs recursively and collects strings if the cdr of
2038 ;; FLAGS is nil and the car is not nil, and it renders also child
2039 ;; <table>s if the cdr is nil. Note: FLAGS may be nil, not a cons.
2040 ;; FLAGS becomes (t . nil) if a <tr> clause is found in the children
2041 ;; of DOM, and becomes (t . t) if a <td> or a <th> clause is found
2042 ;; and the car is t then. When a <table> clause is found, FLAGS
2043 ;; becomes nil if the cdr is t then. But if FLAGS is (t . nil) then,
2044 ;; it renders the <table>.
2045 (cl-loop for child in (dom-children dom) with recurse with tag
2046 do (setq recurse nil)
2047 if (stringp child)
2048 unless (or (not (car flags)) (cdr flags))
2049 when (string-match "\\(?:[^\t\n\r ]+[\t\n\r ]+\\)*[^\t\n\r ]+"
2050 child)
2051 collect (match-string 0 child)
2052 end end
2053 else if (consp child)
2054 do (setq tag (dom-tag child)) and
2055 unless (memq tag '(comment style))
2056 if (eq tag 'img)
2057 do (shr-indirect-call 'img child)
2058 else if (eq tag 'object)
2059 do (shr-indirect-call 'object child)
2060 else
2061 do (setq recurse t) and
2062 if (eq tag 'tr)
2063 do (setq flags '(t . nil))
2064 else if (memq tag '(td th))
2065 when (car flags)
2066 do (setq flags '(t . t))
2068 else if (eq tag 'table)
2069 if (cdr flags)
2070 do (setq flags nil)
2071 else if (car flags)
2072 do (setq recurse nil)
2073 (shr-indirect-call 'table child)
2074 end end end end end end end end end end
2075 when recurse
2076 append (shr-collect-extra-strings-in-table child flags)))
2078 (defun shr-insert-table (table widths)
2079 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
2080 "collapse"))
2081 (shr-table-separator-length (if collapse 0 1))
2082 (shr-table-vertical-line (if collapse "" shr-table-vertical-line))
2083 (start (point)))
2084 (setq shr-table-id (1+ shr-table-id))
2085 (unless collapse
2086 (shr-insert-table-ruler widths))
2087 (dolist (row table)
2088 (let ((start (point))
2089 (align 0)
2090 (column-number 0)
2091 (height (let ((max 0))
2092 (dolist (column row)
2093 (setq max (max max (nth 2 column))))
2094 max)))
2095 (dotimes (_ (max height 1))
2096 (shr-indent)
2097 (insert shr-table-vertical-line "\n"))
2098 (dolist (column row)
2099 (when (> (nth 2 column) -1)
2100 (goto-char start)
2101 ;; Sum up all the widths from the column. (There may be
2102 ;; more than one if this is a "colspan" column.)
2103 (dotimes (_ (nth 4 column))
2104 ;; The colspan directive may be wrong and there may not be
2105 ;; that number of columns.
2106 (when (<= column-number (1- (length widths)))
2107 (setq align (+ align
2108 (aref widths column-number)
2109 (* 2 shr-table-separator-pixel-width))))
2110 (setq column-number (1+ column-number)))
2111 (let ((lines (nth 3 column))
2112 (pixel-align (if (not shr-use-fonts)
2113 (* align (frame-char-width))
2114 align)))
2115 (dolist (line lines)
2116 (end-of-line)
2117 (let ((start (point))
2118 (background (and (> (length line) 0)
2119 (shr-face-background
2120 (get-text-property
2121 (1- (length line)) 'face line))))
2122 (space (propertize
2124 'display `(space :align-to (,pixel-align))
2125 'shr-table-indent shr-table-id)))
2126 (when background
2127 (setq space (propertize space 'face background)))
2128 (insert line space shr-table-vertical-line)
2129 (shr-colorize-region
2130 start (1- (point)) (nth 5 column) (nth 6 column)))
2131 (forward-line 1))
2132 ;; Add blank lines at padding at the bottom of the TD,
2133 ;; possibly.
2134 (dotimes (_ (- height (length lines)))
2135 (end-of-line)
2136 (let ((start (point)))
2137 (insert (propertize " "
2138 'display `(space :align-to (,pixel-align))
2139 'shr-table-indent shr-table-id)
2140 shr-table-vertical-line)
2141 (shr-colorize-region
2142 start (1- (point)) (nth 5 column) (nth 6 column)))
2143 (forward-line 1))))))
2144 (unless collapse
2145 (shr-insert-table-ruler widths)))
2146 (unless (= start (point))
2147 (put-text-property start (1+ start) 'shr-table-id shr-table-id))))
2149 (defun shr-face-background (face)
2150 (and (consp face)
2151 (or (and (plist-get face :background)
2152 (list :background (plist-get face :background)))
2153 (let ((background nil))
2154 (dolist (elem face)
2155 (when (and (consp elem)
2156 (eq (car elem) :background)
2157 (not background))
2158 (setq background (cadr elem))))
2159 (and background
2160 (list :background background))))))
2162 (defun shr-expand-alignments (start end)
2163 (while (< (setq start (next-single-property-change
2164 start 'shr-table-id nil end))
2165 end)
2166 (goto-char start)
2167 (let* ((shr-use-fonts t)
2168 (id (get-text-property (point) 'shr-table-id))
2169 (base (shr-pixel-column))
2170 elem)
2171 (when id
2172 (save-excursion
2173 (while (setq elem (text-property-any
2174 (point) end 'shr-table-indent id))
2175 (goto-char elem)
2176 (let ((align (get-text-property (point) 'display)))
2177 (put-text-property (point) (1+ (point)) 'display
2178 `(space :align-to (,(+ (car (nth 2 align))
2179 base)))))
2180 (forward-char 1)))))
2181 (setq start (1+ start))))
2183 (defun shr-insert-table-ruler (widths)
2184 (when shr-table-horizontal-line
2185 (when (and (bolp)
2186 (> shr-indentation 0))
2187 (shr-indent))
2188 (insert shr-table-corner)
2189 (let ((total-width 0))
2190 (dotimes (i (length widths))
2191 (setq total-width (+ total-width (aref widths i)
2192 (* shr-table-separator-pixel-width 2)))
2193 (insert (make-string (1+ (/ (aref widths i)
2194 shr-table-separator-pixel-width))
2195 shr-table-horizontal-line)
2196 (propertize " "
2197 'display `(space :align-to (,total-width))
2198 'shr-table-indent shr-table-id)
2199 shr-table-corner)))
2200 (insert "\n")))
2202 (defun shr-table-widths (table natural-table suggested-widths)
2203 (let* ((length (length suggested-widths))
2204 (widths (make-vector length 0))
2205 (natural-widths (make-vector length 0)))
2206 (dolist (row table)
2207 (let ((i 0))
2208 (dolist (column row)
2209 (aset widths i (max (aref widths i) column))
2210 (setq i (1+ i)))))
2211 (dolist (row natural-table)
2212 (let ((i 0))
2213 (dolist (column row)
2214 (aset natural-widths i (max (aref natural-widths i) column))
2215 (setq i (1+ i)))))
2216 (let ((extra (- (apply '+ (append suggested-widths nil))
2217 (apply '+ (append widths nil))
2218 (* shr-table-separator-pixel-width (1+ (length widths)))))
2219 (expanded-columns 0))
2220 ;; We have extra, unused space, so divide this space amongst the
2221 ;; columns.
2222 (when (> extra 0)
2223 ;; If the natural width is wider than the rendered width, we
2224 ;; want to allow the column to expand.
2225 (dotimes (i length)
2226 (when (> (aref natural-widths i) (aref widths i))
2227 (setq expanded-columns (1+ expanded-columns))))
2228 (dotimes (i length)
2229 (when (> (aref natural-widths i) (aref widths i))
2230 (aset widths i (min
2231 (aref natural-widths i)
2232 (+ (/ extra expanded-columns)
2233 (aref widths i))))))))
2234 widths))
2236 (defun shr-make-table (dom widths &optional fill storage-attribute)
2237 (or (cadr (assoc (list dom widths fill) shr-content-cache))
2238 (let ((data (shr-make-table-1 dom widths fill)))
2239 (push (list (list dom widths fill) data)
2240 shr-content-cache)
2241 (when storage-attribute
2242 (dom-set-attribute dom storage-attribute data))
2243 data)))
2245 (defun shr-make-table-1 (dom widths &optional fill)
2246 (let ((trs nil)
2247 (rowspans (make-vector (length widths) 0))
2248 (colspan-remaining 0)
2249 colspan-width colspan-count
2250 width colspan)
2251 (dolist (row (dom-non-text-children dom))
2252 (when (eq (dom-tag row) 'tr)
2253 (let ((tds nil)
2254 (columns (dom-non-text-children row))
2255 (i 0)
2256 (width-column 0)
2257 column)
2258 (while (< i (length widths))
2259 ;; If we previously had a rowspan definition, then that
2260 ;; means that we now have a "missing" td/th element here.
2261 ;; So just insert a dummy, empty one to (sort of) emulate
2262 ;; rowspan.
2263 (setq column
2264 (if (zerop (aref rowspans i))
2265 (pop columns)
2266 (aset rowspans i (1- (aref rowspans i)))
2267 '(td)))
2268 (when (and (not (stringp column))
2269 (or (memq (dom-tag column) '(td th))
2270 (not column)))
2271 (when-let* ((span (dom-attr column 'rowspan)))
2272 (aset rowspans i (+ (aref rowspans i)
2273 (1- (string-to-number span)))))
2274 ;; Sanity check for invalid column-spans.
2275 (when (>= width-column (length widths))
2276 (setq width-column 0))
2277 (setq width
2278 (if column
2279 (aref widths width-column)
2280 (* 10 shr-table-separator-pixel-width)))
2281 (when (setq colspan (dom-attr column 'colspan))
2282 (setq colspan (min (string-to-number colspan)
2283 ;; The colspan may be wrong, so
2284 ;; truncate it to the length of the
2285 ;; remaining columns.
2286 (- (length widths) i)))
2287 (dotimes (j (1- colspan))
2288 (setq width
2289 (if (> (+ i 1 j) (1- (length widths)))
2290 ;; If we have a colspan spec that's longer
2291 ;; than the table is wide, just use the last
2292 ;; width as the width.
2293 (aref widths (1- (length widths)))
2294 ;; Sum up the widths of the columns we're
2295 ;; spanning.
2296 (+ width
2297 shr-table-separator-length
2298 (aref widths (+ i 1 j))))))
2299 (setq width-column (+ width-column (1- colspan))
2300 colspan-count colspan
2301 colspan-remaining colspan))
2302 (when column
2303 (let ((data (shr-render-td column width fill)))
2304 (if (and (not fill)
2305 (> colspan-remaining 0))
2306 (progn
2307 (setq colspan-width (car data))
2308 (let ((this-width (/ colspan-width colspan-count)))
2309 (push (cons this-width (cadr data)) tds)
2310 (setq colspan-remaining (1- colspan-remaining))))
2311 (if (not fill)
2312 (push (cons (car data) (cadr data)) tds)
2313 (push data tds)))))
2314 (when (and colspan
2315 (> colspan 1))
2316 (dotimes (_ (1- colspan))
2317 (setq i (1+ i))
2318 (push
2319 (if fill
2320 (list 0 0 -1 nil 1 nil nil)
2321 '(0 . 0))
2322 tds)))
2323 (setq i (1+ i)
2324 width-column (1+ width-column))))
2325 (push (nreverse tds) trs))))
2326 (nreverse trs)))
2328 (defun shr-pixel-buffer-width ()
2329 (if (not shr-use-fonts)
2330 (save-excursion
2331 (goto-char (point-min))
2332 (let ((max 0))
2333 (while (not (eobp))
2334 (end-of-line)
2335 (setq max (max max (current-column)))
2336 (forward-line 1))
2337 max))
2338 (if (get-buffer-window)
2339 (car (window-text-pixel-size nil (point-min) (point-max)))
2340 (save-window-excursion
2341 ;; Avoid errors if the selected window is a dedicated one,
2342 ;; and they just want to insert a document into it.
2343 (set-window-dedicated-p nil nil)
2344 (set-window-buffer nil (current-buffer))
2345 (car (window-text-pixel-size nil (point-min) (point-max)))))))
2347 (defun shr-render-td (dom width fill)
2348 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill))))
2349 (or (dom-attr dom cache)
2350 (and fill
2351 (let (result)
2352 (dolist (attr (dom-attributes dom))
2353 (let ((name (symbol-name (car attr))))
2354 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name)
2355 (let ((cache-width (string-to-number
2356 (match-string 1 name))))
2357 (when (and (>= cache-width width)
2358 (<= (car (cdr attr)) width))
2359 (setq result (cdr attr)))))))
2360 result))
2361 (let* ((pt (point))
2362 (result (shr-render-td-1 dom width fill)))
2363 (dom-set-attribute dom cache result)
2364 (goto-char pt)
2365 result))))
2367 (defun shr-render-td-1 (dom width fill)
2368 (with-temp-buffer
2369 (let ((bgcolor (dom-attr dom 'bgcolor))
2370 (fgcolor (dom-attr dom 'fgcolor))
2371 (style (dom-attr dom 'style))
2372 (shr-stylesheet shr-stylesheet)
2373 (max-width 0)
2374 natural-width)
2375 (when style
2376 (setq style (and (string-match "color" style)
2377 (shr-parse-style style))))
2378 (when bgcolor
2379 (setq style (nconc (list (cons 'background-color bgcolor))
2380 style)))
2381 (when fgcolor
2382 (setq style (nconc (list (cons 'color fgcolor)) style)))
2383 (when style
2384 (setq shr-stylesheet (append style shr-stylesheet)))
2385 (let ((shr-internal-width width)
2386 (shr-indentation 0))
2387 (shr-descend dom))
2388 (save-window-excursion
2389 ;; Avoid errors if the selected window is a dedicated one,
2390 ;; and they just want to insert a document into it.
2391 (set-window-dedicated-p nil nil)
2392 (set-window-buffer nil (current-buffer))
2393 (unless fill
2394 (setq natural-width
2395 (or (dom-attr dom 'shr-td-cache-natural)
2396 (let ((natural (max (shr-pixel-buffer-width)
2397 (shr-dom-max-natural-width dom 0))))
2398 (dom-set-attribute dom 'shr-td-cache-natural natural)
2399 natural))))
2400 (if (and natural-width
2401 (<= natural-width width))
2402 (setq max-width natural-width)
2403 (let ((shr-internal-width width))
2404 (shr-fill-lines (point-min) (point-max))
2405 (setq max-width (shr-pixel-buffer-width)))))
2406 (goto-char (point-max))
2407 ;; Delete padding at the bottom of the TDs.
2408 (delete-region
2409 (point)
2410 (progn
2411 (skip-chars-backward " \t\n")
2412 (end-of-line)
2413 (point)))
2414 (goto-char (point-min))
2415 (list max-width
2416 natural-width
2417 (count-lines (point-min) (point-max))
2418 (split-string (buffer-string) "\n")
2419 (if (dom-attr dom 'colspan)
2420 (string-to-number (dom-attr dom 'colspan))
2422 (cdr (assq 'color shr-stylesheet))
2423 (cdr (assq 'background-color shr-stylesheet))))))
2425 (defun shr-dom-max-natural-width (dom max)
2426 (if (eq (dom-tag dom) 'table)
2427 (max max (or
2428 (cl-loop
2429 for line in (dom-attr dom 'shr-suggested-widths)
2430 maximize (+
2431 shr-table-separator-length
2432 (cl-loop for elem in line
2433 summing
2434 (+ (cdr elem)
2435 (* 2 shr-table-separator-length)))))
2437 (dolist (child (dom-children dom))
2438 (unless (stringp child)
2439 (setq max (max (shr-dom-max-natural-width child max)))))
2440 max))
2442 (defun shr-buffer-width ()
2443 (goto-char (point-min))
2444 (let ((max 0))
2445 (while (not (eobp))
2446 (end-of-line)
2447 (setq max (max max (current-column)))
2448 (forward-line 1))
2449 max))
2451 (defun shr-pro-rate-columns (columns)
2452 (let ((total-percentage 0)
2453 (widths (make-vector (length columns) 0)))
2454 (dotimes (i (length columns))
2455 (setq total-percentage (+ total-percentage (aref columns i))))
2456 (setq total-percentage (/ 1.0 total-percentage))
2457 (dotimes (i (length columns))
2458 (aset widths i (max (truncate (* (aref columns i)
2459 total-percentage
2460 (- shr-internal-width
2461 (* (1+ (length columns))
2462 shr-table-separator-pixel-width))))
2463 10)))
2464 widths))
2466 ;; Return a summary of the number and shape of the TDs in the table.
2467 (defun shr-column-specs (dom)
2468 (let ((columns (make-vector (shr-max-columns dom) 1)))
2469 (dolist (row (dom-non-text-children dom))
2470 (when (eq (dom-tag row) 'tr)
2471 (let ((i 0))
2472 (dolist (column (dom-non-text-children row))
2473 (when (memq (dom-tag column) '(td th))
2474 (let ((width (dom-attr column 'width)))
2475 (when (and width
2476 (string-match "\\([0-9]+\\)%" width)
2477 (not (zerop (setq width (string-to-number
2478 (match-string 1 width))))))
2479 (aset columns i (/ width 100.0))))
2480 (setq i (1+ i)))))))
2481 columns))
2483 (defun shr-count (dom elem)
2484 (let ((i 0))
2485 (dolist (sub (dom-children dom))
2486 (when (and (not (stringp sub))
2487 (eq (dom-tag sub) elem))
2488 (setq i (1+ i))))
2491 (defun shr-max-columns (dom)
2492 (let ((max 0))
2493 (dolist (row (dom-children dom))
2494 (when (and (not (stringp row))
2495 (eq (dom-tag row) 'tr))
2496 (setq max (max max (+ (shr-count row 'td)
2497 (shr-count row 'th))))))
2498 max))
2500 (provide 'shr)
2502 ;;; shr.el ends here