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