Remove unnecessary explicit subword-mode use from isearch
[emacs.git] / lisp / net / shr.el
bloba6b8693dd74c8da06df2b590697eec3ae428b3ae
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 (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 ;; Save and restore point across with-temp-buffer, since
595 ;; shr-pixel-column uses save-window-excursion, which can reset
596 ;; point to 1.
597 (let ((pt (point)))
598 (prog1
599 (with-temp-buffer
600 (insert string)
601 (shr-pixel-column))
602 (goto-char pt)))))
604 (defsubst shr--translate-insertion-chars ()
605 ;; Remove soft hyphens.
606 (goto-char (point-min))
607 (while (search-forward "­" nil t)
608 (replace-match "" t t))
609 ;; Translate non-breaking spaces into real spaces.
610 (goto-char (point-min))
611 (while (search-forward " " nil t)
612 (replace-match " " t t)))
614 (defun shr-insert (text)
615 (when (and (not (bolp))
616 (get-text-property (1- (point)) 'image-url))
617 (insert "\n"))
618 (cond
619 ((eq shr-folding-mode 'none)
620 (let ((start (point)))
621 (insert text)
622 (save-restriction
623 (narrow-to-region start (point))
624 (shr--translate-insertion-chars)
625 (goto-char (point-max)))))
627 (let ((font-start (point)))
628 (when (and (string-match "\\`[ \t\n\r]" text)
629 (not (bolp))
630 (not (eq (char-after (1- (point))) ? )))
631 (insert " "))
632 (let ((start (point))
633 (bolp (bolp)))
634 (insert text)
635 (save-restriction
636 (narrow-to-region start (point))
637 (goto-char start)
638 (when (looking-at "[ \t\n\r]+")
639 (replace-match "" t t))
640 (while (re-search-forward "[ \t\n\r]+" nil t)
641 (replace-match " " t t))
642 (shr--translate-insertion-chars)
643 (goto-char (point-max)))
644 ;; We may have removed everything we inserted if it was just
645 ;; spaces.
646 (unless (= font-start (point))
647 ;; Mark all lines that should possibly be folded afterwards.
648 (when bolp
649 (shr-mark-fill start))
650 (when shr-use-fonts
651 (put-text-property font-start (point)
652 'face
653 (or shr-current-font 'variable-pitch)))))))))
655 (defun shr-fill-lines (start end)
656 (if (<= shr-internal-width 0)
658 (save-restriction
659 (narrow-to-region start end)
660 (goto-char start)
661 (when (get-text-property (point) 'shr-indentation)
662 (shr-fill-line))
663 (while (setq start (next-single-property-change start 'shr-indentation))
664 (goto-char start)
665 (when (bolp)
666 (shr-fill-line)))
667 (goto-char (point-max)))))
669 (defun shr-vertical-motion (column)
670 (if (not shr-use-fonts)
671 (move-to-column column)
672 (unless (eolp)
673 (forward-char 1))
674 (vertical-motion (cons (/ column (frame-char-width)) 0))
675 (unless (eolp)
676 (forward-char 1))))
678 (defun shr-fill-line ()
679 (let ((shr-indentation (get-text-property (point) 'shr-indentation))
680 (continuation (get-text-property
681 (point) 'shr-continuation-indentation))
682 start)
683 (put-text-property (point) (1+ (point)) 'shr-indentation nil)
684 (let ((face (get-text-property (point) 'face))
685 (background-start (point)))
686 (shr-indent)
687 (when face
688 (put-text-property background-start (point) 'face
689 `,(shr-face-background face))))
690 (setq start (point))
691 (setq shr-indentation (or continuation shr-indentation))
692 (shr-vertical-motion shr-internal-width)
693 (when (looking-at " $")
694 (delete-region (point) (line-end-position)))
695 (while (not (eolp))
696 ;; We have to do some folding. First find the first
697 ;; previous point suitable for folding.
698 (if (or (not (shr-find-fill-point (line-beginning-position)))
699 (= (point) start))
700 ;; We had unbreakable text (for this width), so just go to
701 ;; the first space and carry on.
702 (progn
703 (beginning-of-line)
704 (skip-chars-forward " ")
705 (search-forward " " (line-end-position) 'move)))
706 ;; Success; continue.
707 (when (= (preceding-char) ?\s)
708 (delete-char -1))
709 (let ((props `(face ,(get-text-property (point) 'face)
710 ;; Don't break the image-displayer property
711 ;; as it will cause `gnus-article-show-images'
712 ;; to show the two or more same images.
713 image-displayer
714 ,(get-text-property (point) 'image-displayer)))
715 (gap-start (point)))
716 (insert "\n")
717 (shr-indent)
718 (add-text-properties gap-start (point) props))
719 (setq start (point))
720 (shr-vertical-motion shr-internal-width)
721 (when (looking-at " $")
722 (delete-region (point) (line-end-position))))))
724 (defun shr-find-fill-point (start)
725 (let ((bp (point))
726 (end (point))
727 failed)
728 (while (not (or (setq failed (<= (point) start))
729 (eq (preceding-char) ? )
730 (eq (following-char) ? )
731 (shr-char-breakable-p (preceding-char))
732 (shr-char-breakable-p (following-char))
733 (and (shr-char-kinsoku-bol-p (preceding-char))
734 (shr-char-breakable-p (following-char))
735 (not (shr-char-kinsoku-bol-p (following-char))))
736 (shr-char-kinsoku-eol-p (following-char))
737 (bolp)))
738 (backward-char 1))
739 (if failed
740 ;; There's no breakable point, so we give it up.
741 (let (found)
742 (goto-char bp)
743 ;; Don't overflow the window edge, even if
744 ;; shr-kinsoku-shorten is nil.
745 (unless (or shr-kinsoku-shorten (null shr-width))
746 (while (setq found (re-search-forward
747 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
748 (line-end-position) 'move)))
749 (if (and found
750 (not (match-beginning 1)))
751 (goto-char (match-beginning 0)))))
753 (eolp)
754 ;; Don't put kinsoku-bol characters at the beginning of a line,
755 ;; or kinsoku-eol characters at the end of a line.
756 (cond
757 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
758 ;; is nil.
759 ((or shr-kinsoku-shorten (null shr-width))
760 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
761 (or (shr-char-kinsoku-eol-p (preceding-char))
762 (shr-char-kinsoku-bol-p (following-char))))
763 (backward-char 1))
764 (when (setq failed (<= (point) start))
765 ;; There's no breakable point that doesn't violate kinsoku,
766 ;; so we look for the second best position.
767 (while (and (progn
768 (forward-char 1)
769 (<= (point) end))
770 (progn
771 (setq bp (point))
772 (shr-char-kinsoku-eol-p (following-char)))))
773 (goto-char bp)))
774 ((shr-char-kinsoku-eol-p (preceding-char))
775 ;; Find backward the point where kinsoku-eol characters begin.
776 (let ((count 4))
777 (while
778 (progn
779 (backward-char 1)
780 (and (> (setq count (1- count)) 0)
781 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
782 (or (shr-char-kinsoku-eol-p (preceding-char))
783 (shr-char-kinsoku-bol-p (following-char)))))))
784 (when (setq failed (<= (point) start))
785 ;; There's no breakable point that doesn't violate kinsoku,
786 ;; so we go to the second best position.
787 (if (looking-at "\\(\\c<+\\)\\c<")
788 (goto-char (match-end 1))
789 (forward-char 1))))
790 ((shr-char-kinsoku-bol-p (following-char))
791 ;; Find forward the point where kinsoku-bol characters end.
792 (let ((count 4))
793 (while (progn
794 (forward-char 1)
795 (and (>= (setq count (1- count)) 0)
796 (shr-char-kinsoku-bol-p (following-char))
797 (shr-char-breakable-p (following-char))))))))
798 (when (eq (following-char) ? )
799 (forward-char 1))))
800 (not failed)))
802 (defun shr-parse-base (url)
803 ;; Always chop off anchors.
804 (when (string-match "#.*" url)
805 (setq url (substring url 0 (match-beginning 0))))
806 ;; NB: <base href="" > URI may itself be relative to the document s URI
807 (setq url (shr-expand-url url))
808 (let* ((parsed (url-generic-parse-url url))
809 (local (url-filename parsed)))
810 (setf (url-filename parsed) "")
811 ;; Chop off the bit after the last slash.
812 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local)
813 (setq local (match-string 1 local)))
814 ;; Always make the local bit end with a slash.
815 (when (and (not (zerop (length local)))
816 (not (eq (aref local (1- (length local))) ?/)))
817 (setq local (concat local "/")))
818 (list (url-recreate-url parsed)
819 local
820 (url-type parsed)
821 url)))
823 (autoload 'url-expand-file-name "url-expand")
825 ;; FIXME This needs some tests writing.
826 ;; Does it even need to exist, given that url-expand-file-name does?
827 (defun shr-expand-url (url &optional base)
828 (setq base
829 (if base
830 ;; shr-parse-base should never call this with non-nil base!
831 (shr-parse-base base)
832 ;; Bound by the parser.
833 shr-base))
834 (when (zerop (length url))
835 (setq url nil))
836 ;; Strip leading whitespace
837 (and url (string-match "\\`\\s-+" url)
838 (setq url (substring url (match-end 0))))
839 (cond ((zerop (length url))
840 (nth 3 base))
841 ((or (not base)
842 (string-match "\\`[a-z]*:" url))
843 ;; Absolute or empty URI
844 url)
845 ((eq (aref url 0) ?/)
846 (if (and (> (length url) 1)
847 (eq (aref url 1) ?/))
848 ;; //host...; just use the protocol
849 (concat (nth 2 base) ":" url)
850 ;; Just use the host name part.
851 (concat (car base) url)))
852 ((eq (aref url 0) ?#)
853 ;; A link to an anchor.
854 (concat (nth 3 base) url))
856 ;; Totally relative.
857 (url-expand-file-name url (concat (car base) (cadr base))))))
859 (defun shr-ensure-newline ()
860 (unless (bobp)
861 (let ((prefix (get-text-property (line-beginning-position)
862 'shr-prefix-length)))
863 (unless (or (zerop (current-column))
864 (and prefix
865 (= prefix (- (point) (line-beginning-position)))))
866 (insert "\n")))))
868 (defun shr-ensure-paragraph ()
869 (unless (bobp)
870 (let ((prefix (get-text-property (line-beginning-position)
871 'shr-prefix-length)))
872 (cond
873 ((and (bolp)
874 (save-excursion
875 (forward-line -1)
876 (looking-at " *$")))
877 ;; We're already at a new paragraph; do nothing.
879 ((and prefix
880 (= prefix (- (point) (line-beginning-position))))
881 ;; Do nothing; we're at the start of a <li>.
883 ((save-excursion
884 (beginning-of-line)
885 ;; If the current line is totally blank, and doesn't even
886 ;; have any face properties set, then delete the blank
887 ;; space.
888 (and (looking-at " *$")
889 (not (get-text-property (point) 'face))
890 (not (= (next-single-property-change (point) 'face nil
891 (line-end-position))
892 (line-end-position)))))
893 (delete-region (match-beginning 0) (match-end 0)))
894 ;; We have a single blank line.
895 ((and (eolp) (bolp))
896 (insert "\n"))
897 ;; Insert new paragraph.
899 (insert "\n\n"))))))
901 (defun shr-indent ()
902 (when (> shr-indentation 0)
903 (insert
904 (if (not shr-use-fonts)
905 (make-string shr-indentation ?\s)
906 (propertize " "
907 'display
908 `(space :width (,shr-indentation)))))))
910 (defun shr-fontize-dom (dom &rest types)
911 (let ((start (point)))
912 (shr-generic dom)
913 (dolist (type types)
914 (shr-add-font start (point) type))))
916 ;; Add face to the region, but avoid putting the font properties on
917 ;; blank text at the start of the line, and the newline at the end, to
918 ;; avoid ugliness.
919 (defun shr-add-font (start end type)
920 (save-excursion
921 (goto-char start)
922 (while (< (point) end)
923 (when (bolp)
924 (skip-chars-forward " "))
925 (add-face-text-property (point) (min (line-end-position) end) type t)
926 (if (< (line-end-position) end)
927 (forward-line 1)
928 (goto-char end)))))
930 (defun shr-mouse-browse-url (ev)
931 "Browse the URL under the mouse cursor."
932 (interactive "e")
933 (mouse-set-point ev)
934 (shr-browse-url))
936 (defun shr-browse-url (&optional external mouse-event)
937 "Browse the URL under point.
938 If EXTERNAL, browse the URL using `shr-external-browser'."
939 (interactive (list current-prefix-arg last-nonmenu-event))
940 (mouse-set-point mouse-event)
941 (let ((url (get-text-property (point) 'shr-url)))
942 (cond
943 ((not url)
944 (message "No link under point"))
945 ((string-match "^mailto:" url)
946 (browse-url-mail url))
948 (if external
949 (funcall shr-external-browser url)
950 (browse-url url))))))
952 (defun shr-save-contents (directory)
953 "Save the contents from URL in a file."
954 (interactive "DSave contents of URL to directory: ")
955 (let ((url (get-text-property (point) 'shr-url)))
956 (if (not url)
957 (message "No link under point")
958 (url-retrieve (shr-encode-url url)
959 'shr-store-contents (list url directory)
960 nil t))))
962 (defun shr-store-contents (status url directory)
963 (unless (plist-get status :error)
964 (when (or (search-forward "\n\n" nil t)
965 (search-forward "\r\n\r\n" nil t))
966 (write-region (point) (point-max)
967 (expand-file-name (file-name-nondirectory url)
968 directory)))))
970 (defun shr-image-fetched (status buffer start end &optional flags)
971 (let ((image-buffer (current-buffer)))
972 (when (and (buffer-name buffer)
973 (not (plist-get status :error)))
974 (url-store-in-cache image-buffer)
975 (goto-char (point-min))
976 (when (or (search-forward "\n\n" nil t)
977 (search-forward "\r\n\r\n" nil t))
978 (let ((data (shr-parse-image-data)))
979 (with-current-buffer buffer
980 (save-excursion
981 (save-restriction
982 (widen)
983 (let ((alt (buffer-substring start end))
984 (properties (text-properties-at start))
985 (inhibit-read-only t))
986 (delete-region start end)
987 (goto-char start)
988 (funcall shr-put-image-function data alt flags)
989 (while properties
990 (let ((type (pop properties))
991 (value (pop properties)))
992 (unless (memq type '(display image-size))
993 (put-text-property start (point) type value)))))))))))
994 (kill-buffer image-buffer)))
996 (defun shr-image-from-data (data)
997 "Return an image from the data: URI content DATA."
998 (when (string-match
999 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
1000 data)
1001 (let ((param (match-string 4 data))
1002 (payload (url-unhex-string (match-string 5 data))))
1003 (when (and param
1004 (string-match "^.*\\(;[ \t]*base64\\)$" param))
1005 (setq payload (ignore-errors
1006 (base64-decode-string payload))))
1007 payload)))
1009 ;; Behind display-graphic-p test.
1010 (declare-function image-size "image.c" (spec &optional pixels frame))
1011 (declare-function image-animate "image" (image &optional index limit))
1013 (defun shr-put-image (spec alt &optional flags)
1014 "Insert image SPEC with a string ALT. Return image.
1015 SPEC is either an image data blob, or a list where the first
1016 element is the data blob and the second element is the content-type."
1017 (if (display-graphic-p)
1018 (let* ((size (cdr (assq 'size flags)))
1019 (data (if (consp spec)
1020 (car spec)
1021 spec))
1022 (content-type (and (consp spec)
1023 (cadr spec)))
1024 (start (point))
1025 (image (cond
1026 ((eq size 'original)
1027 (create-image data nil t :ascent 100
1028 :format content-type))
1029 ((eq content-type 'image/svg+xml)
1030 (create-image data 'svg t :ascent 100))
1031 ((eq size 'full)
1032 (ignore-errors
1033 (shr-rescale-image data content-type
1034 (plist-get flags :width)
1035 (plist-get flags :height))))
1037 (ignore-errors
1038 (shr-rescale-image data content-type
1039 (plist-get flags :width)
1040 (plist-get flags :height)))))))
1041 (when image
1042 ;; When inserting big-ish pictures, put them at the
1043 ;; beginning of the line.
1044 (when (and (> (current-column) 0)
1045 (> (car (image-size image t)) 400))
1046 (insert "\n"))
1047 (if (eq size 'original)
1048 (insert-sliced-image image (or alt "*") nil 20 1)
1049 (insert-image image (or alt "*")))
1050 (put-text-property start (point) 'image-size size)
1051 (when (and shr-image-animate
1052 (cond ((fboundp 'image-multi-frame-p)
1053 ;; Only animate multi-frame things that specify a
1054 ;; delay; eg animated gifs as opposed to
1055 ;; multi-page tiffs. FIXME?
1056 (cdr (image-multi-frame-p image)))
1057 ((fboundp 'image-animated-p)
1058 (image-animated-p image))))
1059 (image-animate image nil 60)))
1060 image)
1061 (insert (or alt ""))))
1063 (defun shr-rescale-image (data content-type width height
1064 &optional max-width max-height)
1065 "Rescale DATA, if too big, to fit the current buffer.
1066 WIDTH and HEIGHT are the sizes given in the HTML data, if any.
1068 The size of the displayed image will not exceed
1069 MAX-WIDTH/MAX-HEIGHT. If not given, use the current window
1070 width/height instead."
1071 (if (or (not (fboundp 'imagemagick-types))
1072 (not (get-buffer-window (current-buffer))))
1073 (create-image data nil t :ascent 100)
1074 (let* ((edges (window-inside-pixel-edges
1075 (get-buffer-window (current-buffer))))
1076 (max-width (truncate (* shr-max-image-proportion
1077 (or max-width
1078 (- (nth 2 edges) (nth 0 edges))))))
1079 (max-height (truncate (* shr-max-image-proportion
1080 (or max-height
1081 (- (nth 3 edges) (nth 1 edges))))))
1082 (scaling (image-compute-scaling-factor image-scaling-factor)))
1083 (when (or (and width
1084 (> width max-width))
1085 (and height
1086 (> height max-height)))
1087 (setq width nil
1088 height nil))
1089 (if (and width height
1090 (< (* width scaling) max-width)
1091 (< (* height scaling) max-height))
1092 (create-image
1093 data 'imagemagick t
1094 :ascent 100
1095 :width width
1096 :height height
1097 :format content-type)
1098 (create-image
1099 data 'imagemagick t
1100 :ascent 100
1101 :max-width max-width
1102 :max-height max-height
1103 :format content-type)))))
1105 ;; url-cache-extract autoloads url-cache.
1106 (declare-function url-cache-create-filename "url-cache" (url))
1107 (autoload 'mm-disable-multibyte "mm-util")
1108 (autoload 'browse-url-mail "browse-url")
1110 (defun shr-get-image-data (url)
1111 "Get image data for URL.
1112 Return a string with image data."
1113 (with-temp-buffer
1114 (mm-disable-multibyte)
1115 (when (ignore-errors
1116 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
1118 (when (re-search-forward "\r?\n\r?\n" nil t)
1119 (shr-parse-image-data)))))
1121 (declare-function libxml-parse-xml-region "xml.c"
1122 (start end &optional base-url discard-comments))
1124 (defun shr-parse-image-data ()
1125 (let ((data (buffer-substring (point) (point-max)))
1126 (content-type
1127 (save-excursion
1128 (save-restriction
1129 (narrow-to-region (point-min) (point))
1130 (let ((content-type (mail-fetch-field "content-type")))
1131 (and content-type
1132 ;; Remove any comments in the type string.
1133 (intern (replace-regexp-in-string ";.*" "" content-type)
1134 obarray)))))))
1135 ;; SVG images may contain references to further images that we may
1136 ;; want to block. So special-case these by parsing the XML data
1137 ;; and remove anything that looks like a blocked bit.
1138 (when (and shr-blocked-images
1139 (eq content-type 'image/svg+xml))
1140 (setq data
1141 ;; Note that libxml2 doesn't parse everything perfectly,
1142 ;; so glitches may occur during this transformation.
1143 (shr-dom-to-xml
1144 (libxml-parse-xml-region (point) (point-max)))))
1145 (list data content-type)))
1147 (defun shr-image-displayer (content-function)
1148 "Return a function to display an image.
1149 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1150 is an argument. The function to be returned takes three arguments URL,
1151 START, and END. Note that START and END should be markers."
1152 `(lambda (url start end)
1153 (when url
1154 (if (string-match "\\`cid:" url)
1155 ,(when content-function
1156 `(let ((image (funcall ,content-function
1157 (substring url (match-end 0)))))
1158 (when image
1159 (goto-char start)
1160 (funcall shr-put-image-function
1161 image (buffer-substring start end))
1162 (delete-region (point) end))))
1163 (url-retrieve url 'shr-image-fetched
1164 (list (current-buffer) start end)
1165 t t)))))
1167 (defun shr-heading (dom &rest types)
1168 (shr-ensure-paragraph)
1169 (apply #'shr-fontize-dom dom types)
1170 (shr-ensure-paragraph))
1172 (defun shr-urlify (start url &optional title)
1173 (shr-add-font start (point) 'shr-link)
1174 (add-text-properties
1175 start (point)
1176 (list 'shr-url url
1177 'help-echo (let ((iri (or (ignore-errors
1178 (decode-coding-string
1179 (url-unhex-string url)
1180 'utf-8 t))
1181 url)))
1182 (if title (format "%s (%s)" iri title) iri))
1183 'follow-link t
1184 'mouse-face 'highlight))
1185 ;; Don't overwrite any keymaps that are already in the buffer (i.e.,
1186 ;; image keymaps).
1187 (while (and start
1188 (< start (point)))
1189 (let ((next (next-single-property-change start 'keymap nil (point))))
1190 (if (get-text-property start 'keymap)
1191 (setq start next)
1192 (put-text-property start (or next (point)) 'keymap shr-map)))))
1194 (defun shr-encode-url (url)
1195 "Encode URL."
1196 (browse-url-url-encode-chars url "[)$ ]"))
1198 (autoload 'shr-color-visible "shr-color")
1199 (autoload 'shr-color->hexadecimal "shr-color")
1201 (defun shr-color-check (fg bg)
1202 "Check that FG is visible on BG.
1203 Returns (fg bg) with corrected values.
1204 Returns nil if the colors that would be used are the default
1205 ones, in case fg and bg are nil."
1206 (when (or fg bg)
1207 (let ((fixed (cond ((null fg) 'fg)
1208 ((null bg) 'bg))))
1209 ;; Convert colors to hexadecimal, or set them to default.
1210 (let ((fg (or (shr-color->hexadecimal fg)
1211 (frame-parameter nil 'foreground-color)))
1212 (bg (or (shr-color->hexadecimal bg)
1213 (frame-parameter nil 'background-color))))
1214 (cond ((eq fixed 'bg)
1215 ;; Only return the new fg
1216 (list nil (cadr (shr-color-visible bg fg t))))
1217 ((eq fixed 'fg)
1218 ;; Invert args and results and return only the new bg
1219 (list (cadr (shr-color-visible fg bg t)) nil))
1221 (shr-color-visible bg fg)))))))
1223 (defun shr-colorize-region (start end fg &optional bg)
1224 (when (and shr-use-colors
1225 (or fg bg)
1226 (>= (display-color-cells) 88))
1227 (let ((new-colors (shr-color-check fg bg)))
1228 (when new-colors
1229 (when fg
1230 (add-face-text-property start end
1231 (list :foreground (cadr new-colors))
1233 (when bg
1234 (add-face-text-property start end
1235 (list :background (car new-colors))
1236 t)))
1237 new-colors)))
1239 ;;; Tag-specific rendering rules.
1241 (defun shr-tag-html (dom)
1242 (let ((dir (dom-attr dom 'dir)))
1243 (cond
1244 ((equal dir "ltr")
1245 (setq bidi-paragraph-direction 'left-to-right))
1246 ((equal dir "rtl")
1247 (setq bidi-paragraph-direction 'right-to-left))
1248 ((equal dir "auto")
1249 (setq bidi-paragraph-direction nil))))
1250 (shr-generic dom))
1252 (defun shr-tag-body (dom)
1253 (let* ((start (point))
1254 (fgcolor (or (dom-attr dom 'fgcolor) (dom-attr dom 'text)))
1255 (bgcolor (dom-attr dom 'bgcolor))
1256 (shr-stylesheet (list (cons 'color fgcolor)
1257 (cons 'background-color bgcolor))))
1258 (shr-generic dom)
1259 (shr-colorize-region start (point) fgcolor bgcolor)))
1261 (defun shr-tag-style (_dom)
1264 (defun shr-tag-script (_dom)
1267 (defun shr-tag-comment (_dom)
1270 (defun shr-dom-to-xml (dom)
1271 (with-temp-buffer
1272 (shr-dom-print dom)
1273 (buffer-string)))
1275 (defun shr-dom-print (dom)
1276 "Convert DOM into a string containing the xml representation."
1277 (insert (format "<%s" (dom-tag dom)))
1278 (dolist (attr (dom-attributes dom))
1279 ;; Ignore attributes that start with a colon because they are
1280 ;; private elements.
1281 (unless (= (aref (format "%s" (car attr)) 0) ?:)
1282 (insert (format " %s=\"%s\"" (car attr) (cdr attr)))))
1283 (insert ">")
1284 (let (url)
1285 (dolist (elem (dom-children dom))
1286 (cond
1287 ((stringp elem)
1288 (insert elem))
1289 ((eq (dom-tag elem) 'comment)
1291 ((or (not (eq (dom-tag elem) 'image))
1292 ;; Filter out blocked elements inside the SVG image.
1293 (not (setq url (dom-attr elem ':xlink:href)))
1294 (not shr-blocked-images)
1295 (not (string-match shr-blocked-images url)))
1296 (insert " ")
1297 (shr-dom-print elem)))))
1298 (insert (format "</%s>" (dom-tag dom))))
1300 (defun shr-tag-svg (dom)
1301 (when (and (image-type-available-p 'svg)
1302 (not shr-inhibit-images)
1303 (dom-attr dom 'width)
1304 (dom-attr dom 'height))
1305 (funcall shr-put-image-function (list (shr-dom-to-xml dom) 'image/svg+xml)
1306 "SVG Image")))
1308 (defun shr-tag-sup (dom)
1309 (let ((start (point)))
1310 (shr-generic dom)
1311 (put-text-property start (point) 'display '(raise 0.5))))
1313 (defun shr-tag-sub (dom)
1314 (let ((start (point)))
1315 (shr-generic dom)
1316 (put-text-property start (point) 'display '(raise -0.5))))
1318 (defun shr-tag-label (dom)
1319 (shr-generic dom)
1320 (shr-ensure-paragraph))
1322 (defun shr-tag-p (dom)
1323 (shr-ensure-paragraph)
1324 (shr-generic dom)
1325 (shr-ensure-paragraph))
1327 (defun shr-tag-div (dom)
1328 (shr-ensure-newline)
1329 (shr-generic dom)
1330 (shr-ensure-newline))
1332 (defun shr-tag-s (dom)
1333 (shr-fontize-dom dom 'shr-strike-through))
1335 (defun shr-tag-b (dom)
1336 (shr-fontize-dom dom 'bold))
1338 (defun shr-tag-i (dom)
1339 (shr-fontize-dom dom 'italic))
1341 (defun shr-tag-em (dom)
1342 (shr-fontize-dom dom 'italic))
1344 (defun shr-tag-strong (dom)
1345 (shr-fontize-dom dom 'bold))
1347 (defun shr-tag-u (dom)
1348 (shr-fontize-dom dom 'underline))
1350 (defun shr-tag-tt (dom)
1351 (let ((shr-current-font 'default))
1352 (shr-generic dom)))
1354 (defun shr-tag-ins (cont)
1355 (let* ((start (point))
1356 (color "green")
1357 (shr-stylesheet (nconc (list (cons 'color color))
1358 shr-stylesheet)))
1359 (shr-generic cont)
1360 (shr-colorize-region start (point) color
1361 (cdr (assq 'background-color shr-stylesheet)))))
1363 (defun shr-tag-del (cont)
1364 (let* ((start (point))
1365 (color "red")
1366 (shr-stylesheet (nconc (list (cons 'color color))
1367 shr-stylesheet)))
1368 (shr-fontize-dom cont 'shr-strike-through)
1369 (shr-colorize-region start (point) color
1370 (cdr (assq 'background-color shr-stylesheet)))))
1372 (defun shr-parse-style (style)
1373 (when style
1374 (save-match-data
1375 (when (string-match "\n" style)
1376 (setq style (replace-match " " t t style))))
1377 (let ((plist nil))
1378 (dolist (elem (split-string style ";"))
1379 (when elem
1380 (setq elem (split-string elem ":"))
1381 (when (and (car elem)
1382 (cadr elem))
1383 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1384 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1385 (when (string-match " *!important\\'" value)
1386 (setq value (substring value 0 (match-beginning 0))))
1387 (unless (equal value "inherit")
1388 (push (cons (intern name obarray)
1389 value)
1390 plist))))))
1391 plist)))
1393 (defun shr-tag-base (dom)
1394 (when-let* ((base (dom-attr dom 'href)))
1395 (setq shr-base (shr-parse-base base)))
1396 (shr-generic dom))
1398 (defun shr-tag-a (dom)
1399 (let ((url (dom-attr dom 'href))
1400 (title (dom-attr dom 'title))
1401 (start (point))
1402 shr-start)
1403 (shr-generic dom)
1404 (when (and shr-target-id
1405 (equal (dom-attr dom 'name) shr-target-id))
1406 ;; We have a zero-length <a name="foo"> element, so just
1407 ;; insert... something.
1408 (when (= start (point))
1409 (shr-ensure-newline)
1410 (insert " "))
1411 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
1412 (when url
1413 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1415 (defun shr-tag-object (dom)
1416 (unless shr-inhibit-images
1417 (let ((start (point))
1418 url multimedia image)
1419 (when-let* ((type (dom-attr dom 'type)))
1420 (when (string-match "\\`image/svg" type)
1421 (setq url (dom-attr dom 'data)
1422 image t)))
1423 (dolist (child (dom-non-text-children dom))
1424 (cond
1425 ((eq (dom-tag child) 'embed)
1426 (setq url (or url (dom-attr child 'src))
1427 multimedia t))
1428 ((and (eq (dom-tag child) 'param)
1429 (equal (dom-attr child 'name) "movie"))
1430 (setq url (or url (dom-attr child 'value))
1431 multimedia t))))
1432 (when url
1433 (cond
1434 (image
1435 (shr-indirect-call 'img dom url)
1436 (setq dom nil))
1437 (multimedia
1438 (shr-insert " [multimedia] ")
1439 (shr-urlify start (shr-expand-url url)))))
1440 (when dom
1441 (shr-generic dom)))))
1443 (defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
1444 ("ogv" . 1.0)
1445 ("ogg" . 1.0)
1446 ("opus" . 1.0)
1447 ("flac" . 0.9)
1448 ("wav" . 0.5))
1449 "Preferences for media types.
1450 The key element should be a regexp matched against the type of the source or
1451 url if no type is specified. The value should be a float in the range 0.0 to
1452 1.0. Media elements with higher value are preferred."
1453 :version "24.4"
1454 :group 'shr
1455 :type '(alist :key-type regexp :value-type float))
1457 (defun shr--get-media-pref (elem)
1458 "Determine the preference for ELEM.
1459 The preference is a float determined from `shr-prefer-media-type'."
1460 (let ((type (dom-attr elem 'type))
1461 (p 0.0))
1462 (unless type
1463 (setq type (dom-attr elem 'src)))
1464 (when type
1465 (dolist (pref shr-prefer-media-type-alist)
1466 (when (and
1467 (> (cdr pref) p)
1468 (string-match-p (car pref) type))
1469 (setq p (cdr pref)))))
1472 (defun shr--extract-best-source (dom &optional url pref)
1473 "Extract the best `:src' property from <source> blocks in DOM."
1474 (setq pref (or pref -1.0))
1475 (let (new-pref)
1476 (dolist (elem (dom-non-text-children dom))
1477 (when (and (eq (dom-tag elem) 'source)
1478 (< pref
1479 (setq new-pref
1480 (shr--get-media-pref elem))))
1481 (setq pref new-pref
1482 url (dom-attr elem 'src))
1483 ;; libxml's html parser isn't HTML5 compliant and non terminated
1484 ;; source tags might end up as children. So recursion it is...
1485 (dolist (child (dom-non-text-children elem))
1486 (when (eq (dom-tag child) 'source)
1487 (let ((ret (shr--extract-best-source (list child) url pref)))
1488 (when (< pref (cdr ret))
1489 (setq url (car ret)
1490 pref (cdr ret)))))))))
1491 (cons url pref))
1493 (defun shr-tag-video (dom)
1494 (let ((image (dom-attr dom 'poster))
1495 (url (dom-attr dom 'src))
1496 (start (point)))
1497 (unless url
1498 (setq url (car (shr--extract-best-source dom))))
1499 (if (> (length image) 0)
1500 (shr-indirect-call 'img nil image)
1501 (shr-insert " [video] "))
1502 (shr-urlify start (shr-expand-url url))))
1504 (defun shr-tag-audio (dom)
1505 (let ((url (dom-attr dom 'src))
1506 (start (point)))
1507 (unless url
1508 (setq url (car (shr--extract-best-source dom))))
1509 (shr-insert " [audio] ")
1510 (shr-urlify start (shr-expand-url url))))
1512 (defun shr-tag-img (dom &optional url)
1513 (when (or url
1514 (and dom
1515 (or (> (length (dom-attr dom 'src)) 0)
1516 (> (length (dom-attr dom 'srcset)) 0))))
1517 (when (> (current-column) 0)
1518 (insert "\n"))
1519 (let ((alt (dom-attr dom 'alt))
1520 (width (shr-string-number (dom-attr dom 'width)))
1521 (height (shr-string-number (dom-attr dom 'height)))
1522 (url (shr-expand-url (or url (shr--preferred-image dom)))))
1523 (let ((start (point-marker)))
1524 (when (zerop (length alt))
1525 (setq alt "*"))
1526 (cond
1527 ((or (member (dom-attr dom 'height) '("0" "1"))
1528 (member (dom-attr dom 'width) '("0" "1")))
1529 ;; Ignore zero-sized or single-pixel images.
1531 ((and (not shr-inhibit-images)
1532 (string-match "\\`data:" url))
1533 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1534 (if image
1535 (funcall shr-put-image-function image alt
1536 (list :width width :height height))
1537 (insert alt))))
1538 ((and (not shr-inhibit-images)
1539 (string-match "\\`cid:" url))
1540 (let ((url (substring url (match-end 0)))
1541 image)
1542 (if (or (not shr-content-function)
1543 (not (setq image (funcall shr-content-function url))))
1544 (insert alt)
1545 (funcall shr-put-image-function image alt
1546 (list :width width :height height)))))
1547 ((or shr-inhibit-images
1548 (and shr-blocked-images
1549 (string-match shr-blocked-images url)))
1550 (setq shr-start (point))
1551 (shr-insert alt))
1552 ((and (not shr-ignore-cache)
1553 (url-is-cached (shr-encode-url url)))
1554 (funcall shr-put-image-function (shr-get-image-data url) alt
1555 (list :width width :height height)))
1557 (when (and shr-ignore-cache
1558 (url-is-cached (shr-encode-url url)))
1559 (let ((file (url-cache-create-filename (shr-encode-url url))))
1560 (when (file-exists-p file)
1561 (delete-file file))))
1562 (when (image-type-available-p 'svg)
1563 (insert-image
1564 (shr-make-placeholder-image dom)
1565 (or alt "")))
1566 (insert " ")
1567 (url-queue-retrieve
1568 (shr-encode-url url) 'shr-image-fetched
1569 (list (current-buffer) start (set-marker (make-marker) (point))
1570 (list :width width :height height))
1571 t t)))
1572 (when (zerop shr-table-depth) ;; We are not in a table.
1573 (put-text-property start (point) 'keymap shr-image-map)
1574 (put-text-property start (point) 'shr-alt alt)
1575 (put-text-property start (point) 'image-url url)
1576 (put-text-property start (point) 'image-displayer
1577 (shr-image-displayer shr-content-function))
1578 (put-text-property start (point) 'help-echo
1579 (shr-fill-text
1580 (or (dom-attr dom 'title) alt))))))))
1582 (defun shr--preferred-image (dom)
1583 (let ((srcset (dom-attr dom 'srcset))
1584 (frame-width (frame-pixel-width))
1585 (width (string-to-number (or (dom-attr dom 'width) "100")))
1586 candidate)
1587 (when (> (length srcset) 0)
1588 ;; srcset consist of a series of URL/size specifications
1589 ;; separated by the ", " string.
1590 (setq srcset
1591 (sort (mapcar
1592 (lambda (elem)
1593 (let ((spec (split-string elem "[\t\n\r ]+")))
1594 (cond
1595 ((= (length spec) 1)
1596 ;; Make sure it's well formed.
1597 (list (car spec) 0))
1598 ((string-match "\\([0-9]+\\)x\\'" (cadr spec))
1599 ;; If we have an "x" form, then use the width
1600 ;; spec to compute the real width.
1601 (list (car spec)
1602 (* width (string-to-number
1603 (match-string 1 (cadr spec))))))
1605 (list (car spec)
1606 (string-to-number (cadr spec)))))))
1607 (split-string (replace-regexp-in-string
1608 "\\`[\t\n\r ]+\\|[\t\n\r ]+\\'" "" srcset)
1609 "[\t\n\r ]*,[\t\n\r ]*"))
1610 (lambda (e1 e2)
1611 (> (cadr e1) (cadr e2)))))
1612 ;; Choose the smallest picture that's bigger than the current
1613 ;; frame.
1614 (setq candidate (caar srcset))
1615 (while (and srcset
1616 (> (cadr (car srcset)) frame-width))
1617 (setq candidate (caar srcset))
1618 (pop srcset)))
1619 (or candidate (dom-attr dom 'src))))
1621 (defun shr-string-number (string)
1622 (if (null string)
1624 (setq string (replace-regexp-in-string "[^0-9]" "" string))
1625 (if (zerop (length string))
1627 (string-to-number string))))
1629 (defun shr-make-placeholder-image (dom)
1630 (let* ((edges (and
1631 (get-buffer-window (current-buffer))
1632 (window-inside-pixel-edges
1633 (get-buffer-window (current-buffer)))))
1634 (scaling (image-compute-scaling-factor image-scaling-factor))
1635 (width (truncate
1636 (* (or (shr-string-number (dom-attr dom 'width)) 100)
1637 scaling)))
1638 (height (truncate
1639 (* (or (shr-string-number (dom-attr dom 'height)) 100)
1640 scaling)))
1641 (max-width
1642 (and edges
1643 (truncate (* shr-max-image-proportion
1644 (- (nth 2 edges) (nth 0 edges))))))
1645 (max-height (and edges
1646 (truncate (* shr-max-image-proportion
1647 (- (nth 3 edges) (nth 1 edges))))))
1648 svg)
1649 (when (and max-width
1650 (> width max-width))
1651 (setq height (truncate (* (/ (float max-width) width) height))
1652 width max-width))
1653 (when (and max-height
1654 (> height max-height))
1655 (setq width (truncate (* (/ (float max-height) height) width))
1656 height max-height))
1657 (setq svg (svg-create width height))
1658 (svg-gradient svg "background" 'linear '((0 . "#b0b0b0") (100 . "#808080")))
1659 (svg-rectangle svg 0 0 width height :gradient "background"
1660 :stroke-width 2 :stroke-color "black")
1661 (let ((image (svg-image svg)))
1662 (setf (image-property image :ascent) 100)
1663 image)))
1665 (defun shr-tag-pre (dom)
1666 (let ((shr-folding-mode 'none)
1667 (shr-current-font 'default))
1668 (shr-ensure-newline)
1669 (shr-generic dom)
1670 (shr-ensure-newline)))
1672 (defun shr-tag-blockquote (dom)
1673 (shr-ensure-paragraph)
1674 (let ((start (point))
1675 (shr-indentation (+ shr-indentation
1676 (* 4 shr-table-separator-pixel-width))))
1677 (shr-generic dom)
1678 (shr-ensure-paragraph)
1679 (shr-mark-fill start)))
1681 (defun shr-tag-dl (dom)
1682 (shr-ensure-paragraph)
1683 (shr-generic dom)
1684 (shr-ensure-paragraph))
1686 (defun shr-tag-dt (dom)
1687 (shr-ensure-newline)
1688 (shr-generic dom)
1689 (shr-ensure-newline))
1691 (defun shr-tag-dd (dom)
1692 (shr-ensure-newline)
1693 (let ((shr-indentation (+ shr-indentation
1694 (* 4 shr-table-separator-pixel-width))))
1695 (shr-generic dom)))
1697 (defun shr-tag-ul (dom)
1698 (shr-ensure-paragraph)
1699 (let ((shr-list-mode 'ul))
1700 (shr-generic dom))
1701 ;; If we end on an empty <li>, then make sure we really end on a new
1702 ;; paragraph.
1703 (unless (bolp)
1704 (insert "\n"))
1705 (shr-ensure-paragraph))
1707 (defun shr-tag-ol (dom)
1708 (shr-ensure-paragraph)
1709 (let ((shr-list-mode 1))
1710 (shr-generic dom))
1711 (shr-ensure-paragraph))
1713 (defun shr-tag-li (dom)
1714 (shr-ensure-newline)
1715 (let ((start (point)))
1716 (let* ((bullet
1717 (if (numberp shr-list-mode)
1718 (prog1
1719 (format "%d " shr-list-mode)
1720 (setq shr-list-mode (1+ shr-list-mode)))
1721 (car shr-internal-bullet)))
1722 (width (if (numberp shr-list-mode)
1723 (shr-string-pixel-width bullet)
1724 (cdr shr-internal-bullet))))
1725 (insert bullet)
1726 (shr-mark-fill start)
1727 (let ((shr-indentation (+ shr-indentation width)))
1728 (put-text-property start (1+ start)
1729 'shr-continuation-indentation shr-indentation)
1730 (put-text-property start (1+ start) 'shr-prefix-length (length bullet))
1731 (shr-generic dom))))
1732 (unless (bolp)
1733 (insert "\n")))
1735 (defun shr-mark-fill (start)
1736 ;; We may not have inserted any text to fill.
1737 (unless (= start (point))
1738 (put-text-property start (1+ start)
1739 'shr-indentation shr-indentation)))
1741 (defun shr-tag-br (dom)
1742 (when (and (not (bobp))
1743 ;; Only add a newline if we break the current line, or
1744 ;; the previous line isn't a blank line.
1745 (or (not (bolp))
1746 (and (> (- (point) 2) (point-min))
1747 (not (= (char-after (- (point) 2)) ?\n)))))
1748 (insert "\n"))
1749 (shr-generic dom))
1751 (defun shr-tag-span (dom)
1752 (shr-generic dom))
1754 (defun shr-tag-h1 (dom)
1755 (shr-heading dom (if shr-use-fonts
1756 '(variable-pitch (:height 1.3 :weight bold))
1757 'bold)))
1759 (defun shr-tag-h2 (dom)
1760 (shr-heading dom 'bold))
1762 (defun shr-tag-h3 (dom)
1763 (shr-heading dom 'italic))
1765 (defun shr-tag-h4 (dom)
1766 (shr-heading dom))
1768 (defun shr-tag-h5 (dom)
1769 (shr-heading dom))
1771 (defun shr-tag-h6 (dom)
1772 (shr-heading dom))
1774 (defun shr-tag-hr (_dom)
1775 (shr-ensure-newline)
1776 (insert (make-string (if (not shr-use-fonts)
1777 shr-internal-width
1778 (1+ (/ shr-internal-width
1779 shr-table-separator-pixel-width)))
1780 shr-hr-line)
1781 "\n"))
1783 (defun shr-tag-title (dom)
1784 (shr-heading dom 'bold 'underline))
1786 (defun shr-tag-font (dom)
1787 (let* ((start (point))
1788 (color (dom-attr dom 'color))
1789 (shr-stylesheet (nconc (list (cons 'color color))
1790 shr-stylesheet)))
1791 (shr-generic dom)
1792 (when color
1793 (shr-colorize-region start (point) color
1794 (cdr (assq 'background-color shr-stylesheet))))))
1796 (defun shr-tag-bdo (dom)
1797 (let* ((direction (dom-attr dom 'dir))
1798 (char (cond
1799 ((equal direction "ltr")
1800 ?\N{LEFT-TO-RIGHT OVERRIDE})
1801 ((equal direction "rtl")
1802 ?\N{RIGHT-TO-LEFT OVERRIDE}))))
1803 (when char
1804 (insert ?\N{FIRST STRONG ISOLATE} char))
1805 (shr-generic dom)
1806 (when char
1807 (insert ?\N{POP DIRECTIONAL FORMATTING} ?\N{POP DIRECTIONAL ISOLATE}))))
1809 (defun shr-tag-bdi (dom)
1810 (insert ?\N{FIRST STRONG ISOLATE})
1811 (shr-generic dom)
1812 (insert ?\N{POP DIRECTIONAL ISOLATE}))
1814 ;;; Table rendering algorithm.
1816 ;; Table rendering is the only complicated thing here. We do this by
1817 ;; first counting how many TDs there are in each TR, and registering
1818 ;; how wide they think they should be ("width=45%", etc). Then we
1819 ;; render each TD separately (this is done in temporary buffers, so
1820 ;; that we can use all the rendering machinery as if we were in the
1821 ;; main buffer). Now we know how much space each TD really takes, so
1822 ;; we then render everything again with the new widths, and finally
1823 ;; insert all these boxes into the main buffer.
1824 (defun shr-tag-table-1 (dom)
1825 (setq dom (or (dom-child-by-tag dom 'tbody) dom))
1826 (let* ((shr-inhibit-images t)
1827 (shr-table-depth (1+ shr-table-depth))
1828 (shr-kinsoku-shorten t)
1829 ;; Find all suggested widths.
1830 (columns (shr-column-specs dom))
1831 ;; Compute how many pixels wide each TD should be.
1832 (suggested-widths (shr-pro-rate-columns columns))
1833 ;; Do a "test rendering" to see how big each TD is (this can
1834 ;; be smaller (if there's little text) or bigger (if there's
1835 ;; unbreakable text).
1836 (elems (or (dom-attr dom 'shr-suggested-widths)
1837 (shr-make-table dom suggested-widths nil
1838 'shr-suggested-widths)))
1839 (sketch (cl-loop for line in elems
1840 collect (mapcar #'car line)))
1841 (natural (cl-loop for line in elems
1842 collect (mapcar #'cdr line)))
1843 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1844 ;; This probably won't work very well.
1845 (when (> (+ (cl-loop for width across sketch-widths
1846 summing (1+ width))
1847 shr-indentation shr-table-separator-pixel-width)
1848 (frame-width))
1849 (setq truncate-lines t))
1850 ;; Then render the table again with these new "hard" widths.
1851 (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
1853 (defun shr-table-body (dom)
1854 (let ((tbodies (seq-filter (lambda (child)
1855 (eq (dom-tag child) 'tbody))
1856 (dom-non-text-children dom))))
1857 (cond
1858 ((null tbodies)
1859 dom)
1860 ((= (length tbodies) 1)
1861 (car tbodies))
1863 ;; Table with multiple tbodies. Convert into a single tbody.
1864 `(tbody nil ,@(cl-reduce 'append
1865 (mapcar 'dom-non-text-children tbodies)))))))
1867 (defun shr-tag-table (dom)
1868 (shr-ensure-paragraph)
1869 (let* ((caption (dom-children (dom-child-by-tag dom 'caption)))
1870 (header (dom-non-text-children (dom-child-by-tag dom 'thead)))
1871 (body (dom-non-text-children (shr-table-body dom)))
1872 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot)))
1873 (bgcolor (dom-attr dom 'bgcolor))
1874 (start (point))
1875 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1876 shr-stylesheet))
1877 (nheader (if header (shr-max-columns header)))
1878 (nbody (if body (shr-max-columns body) 0))
1879 (nfooter (if footer (shr-max-columns footer))))
1880 (if (and (not caption)
1881 (not header)
1882 (not (dom-child-by-tag dom 'tbody))
1883 (not (dom-child-by-tag dom 'tr))
1884 (not footer))
1885 ;; The table is totally invalid and just contains random junk.
1886 ;; Try to output it anyway.
1887 (shr-generic dom)
1888 ;; It's a real table, so render it.
1889 (if (dom-attr dom 'shr-fixed-table)
1890 (shr-tag-table-1 dom)
1891 ;; Only fix up the table once.
1892 (let ((table
1893 (nconc
1894 (list 'table nil)
1895 (if caption `((tr nil (td nil ,@caption))))
1896 (cond
1897 (header
1898 (if footer
1899 ;; header + body + footer
1900 (if (= nheader nbody)
1901 (if (= nbody nfooter)
1902 `((tr nil (td nil (table nil
1903 (tbody nil ,@header
1904 ,@body ,@footer)))))
1905 (nconc `((tr nil (td nil (table nil
1906 (tbody nil ,@header
1907 ,@body)))))
1908 (if (= nfooter 1)
1909 footer
1910 `((tr nil (td nil (table
1911 nil (tbody
1912 nil ,@footer))))))))
1913 (nconc `((tr nil (td nil (table nil (tbody
1914 nil ,@header)))))
1915 (if (= nbody nfooter)
1916 `((tr nil (td nil (table
1917 nil (tbody nil ,@body
1918 ,@footer)))))
1919 (nconc `((tr nil (td nil (table
1920 nil (tbody nil
1921 ,@body)))))
1922 (if (= nfooter 1)
1923 footer
1924 `((tr nil (td nil (table
1926 (tbody
1928 ,@footer))))))))))
1929 ;; header + body
1930 (if (= nheader nbody)
1931 `((tr nil (td nil (table nil (tbody nil ,@header
1932 ,@body)))))
1933 (if (= nheader 1)
1934 `(,@header (tr nil (td nil (table
1935 nil (tbody nil ,@body)))))
1936 `((tr nil (td nil (table nil (tbody nil ,@header))))
1937 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
1938 (footer
1939 ;; body + footer
1940 (if (= nbody nfooter)
1941 `((tr nil (td nil (table
1942 nil (tbody nil ,@body ,@footer)))))
1943 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
1944 (if (= nfooter 1)
1945 footer
1946 `((tr nil (td nil (table
1947 nil (tbody nil ,@footer)))))))))
1948 (caption
1949 `((tr nil (td nil (table nil (tbody nil ,@body))))))
1950 (body)))))
1951 (dom-set-attribute table 'shr-fixed-table t)
1952 (setcdr dom (cdr table))
1953 (shr-tag-table-1 dom))))
1954 (when bgcolor
1955 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1956 bgcolor))
1957 ;; Finally, insert all the images after the table. The Emacs buffer
1958 ;; model isn't strong enough to allow us to put the images actually
1959 ;; into the tables. It inserts also non-td/th objects.
1960 (when (zerop shr-table-depth)
1961 (save-excursion
1962 (shr-expand-alignments start (point)))
1963 (let ((strings (shr-collect-extra-strings-in-table dom)))
1964 (when strings
1965 (save-restriction
1966 (narrow-to-region (point) (point))
1967 (insert (mapconcat #'identity strings "\n"))
1968 (shr-fill-lines (point-min) (point-max))))))))
1970 (defun shr-collect-extra-strings-in-table (dom &optional flags)
1971 "Return extra strings in DOM of which the root is a table clause.
1972 Render <img>s and <object>s, and strings and child <table>s of which
1973 the parent <td> or <th> is lacking. FLAGS is a cons of two boolean
1974 flags that control whether to collect or render objects."
1975 ;; This function runs recursively and collects strings if the cdr of
1976 ;; FLAGS is nil and the car is not nil, and it renders also child
1977 ;; <table>s if the cdr is nil. Note: FLAGS may be nil, not a cons.
1978 ;; FLAGS becomes (t . nil) if a <tr> clause is found in the children
1979 ;; of DOM, and becomes (t . t) if a <td> or a <th> clause is found
1980 ;; and the car is t then. When a <table> clause is found, FLAGS
1981 ;; becomes nil if the cdr is t then. But if FLAGS is (t . nil) then,
1982 ;; it renders the <table>.
1983 (cl-loop for child in (dom-children dom) with recurse with tag
1984 do (setq recurse nil)
1985 if (stringp child)
1986 unless (or (not (car flags)) (cdr flags))
1987 when (string-match "\\(?:[^\t\n\r ]+[\t\n\r ]+\\)*[^\t\n\r ]+"
1988 child)
1989 collect (match-string 0 child)
1990 end end
1991 else if (consp child)
1992 do (setq tag (dom-tag child)) and
1993 unless (memq tag '(comment style))
1994 if (eq tag 'img)
1995 do (shr-indirect-call 'img child)
1996 else if (eq tag 'object)
1997 do (shr-indirect-call 'object child)
1998 else
1999 do (setq recurse t) and
2000 if (eq tag 'tr)
2001 do (setq flags '(t . nil))
2002 else if (memq tag '(td th))
2003 when (car flags)
2004 do (setq flags '(t . t))
2006 else if (eq tag 'table)
2007 if (cdr flags)
2008 do (setq flags nil)
2009 else if (car flags)
2010 do (setq recurse nil)
2011 (shr-indirect-call 'table child)
2012 end end end end end end end end end end
2013 when recurse
2014 append (shr-collect-extra-strings-in-table child flags)))
2016 (defun shr-insert-table (table widths)
2017 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
2018 "collapse"))
2019 (shr-table-separator-length (if collapse 0 1))
2020 (shr-table-vertical-line (if collapse "" shr-table-vertical-line))
2021 (start (point)))
2022 (setq shr-table-id (1+ shr-table-id))
2023 (unless collapse
2024 (shr-insert-table-ruler widths))
2025 (dolist (row table)
2026 (let ((start (point))
2027 (align 0)
2028 (column-number 0)
2029 (height (let ((max 0))
2030 (dolist (column row)
2031 (setq max (max max (nth 2 column))))
2032 max)))
2033 (dotimes (_ (max height 1))
2034 (shr-indent)
2035 (insert shr-table-vertical-line "\n"))
2036 (dolist (column row)
2037 (when (> (nth 2 column) -1)
2038 (goto-char start)
2039 ;; Sum up all the widths from the column. (There may be
2040 ;; more than one if this is a "colspan" column.)
2041 (dotimes (_ (nth 4 column))
2042 ;; The colspan directive may be wrong and there may not be
2043 ;; that number of columns.
2044 (when (<= column-number (1- (length widths)))
2045 (setq align (+ align
2046 (aref widths column-number)
2047 (* 2 shr-table-separator-pixel-width))))
2048 (setq column-number (1+ column-number)))
2049 (let ((lines (nth 3 column))
2050 (pixel-align (if (not shr-use-fonts)
2051 (* align (frame-char-width))
2052 align)))
2053 (dolist (line lines)
2054 (end-of-line)
2055 (let ((start (point))
2056 (background (and (> (length line) 0)
2057 (shr-face-background
2058 (get-text-property
2059 (1- (length line)) 'face line))))
2060 (space (propertize
2062 'display `(space :align-to (,pixel-align))
2063 'shr-table-indent shr-table-id)))
2064 (when background
2065 (setq space (propertize space 'face background)))
2066 (insert line space shr-table-vertical-line)
2067 (shr-colorize-region
2068 start (1- (point)) (nth 5 column) (nth 6 column)))
2069 (forward-line 1))
2070 ;; Add blank lines at padding at the bottom of the TD,
2071 ;; possibly.
2072 (dotimes (_ (- height (length lines)))
2073 (end-of-line)
2074 (let ((start (point)))
2075 (insert (propertize " "
2076 'display `(space :align-to (,pixel-align))
2077 'shr-table-indent shr-table-id)
2078 shr-table-vertical-line)
2079 (shr-colorize-region
2080 start (1- (point)) (nth 5 column) (nth 6 column)))
2081 (forward-line 1))))))
2082 (unless collapse
2083 (shr-insert-table-ruler widths)))
2084 (unless (= start (point))
2085 (put-text-property start (1+ start) 'shr-table-id shr-table-id))))
2087 (defun shr-face-background (face)
2088 (and (consp face)
2089 (or (and (plist-get face :background)
2090 (list :background (plist-get face :background)))
2091 (let ((background nil))
2092 (dolist (elem face)
2093 (when (and (consp elem)
2094 (eq (car elem) :background)
2095 (not background))
2096 (setq background (cadr elem))))
2097 (and background
2098 (list :background background))))))
2100 (defun shr-expand-alignments (start end)
2101 (while (< (setq start (next-single-property-change
2102 start 'shr-table-id nil end))
2103 end)
2104 (goto-char start)
2105 (let* ((shr-use-fonts t)
2106 (id (get-text-property (point) 'shr-table-id))
2107 (base (shr-pixel-column))
2108 elem)
2109 (when id
2110 (save-excursion
2111 (while (setq elem (text-property-any
2112 (point) end 'shr-table-indent id))
2113 (goto-char elem)
2114 (let ((align (get-text-property (point) 'display)))
2115 (put-text-property (point) (1+ (point)) 'display
2116 `(space :align-to (,(+ (car (nth 2 align))
2117 base)))))
2118 (forward-char 1)))))
2119 (setq start (1+ start))))
2121 (defun shr-insert-table-ruler (widths)
2122 (when shr-table-horizontal-line
2123 (when (and (bolp)
2124 (> shr-indentation 0))
2125 (shr-indent))
2126 (insert shr-table-corner)
2127 (let ((total-width 0))
2128 (dotimes (i (length widths))
2129 (setq total-width (+ total-width (aref widths i)
2130 (* shr-table-separator-pixel-width 2)))
2131 (insert (make-string (1+ (/ (aref widths i)
2132 shr-table-separator-pixel-width))
2133 shr-table-horizontal-line)
2134 (propertize " "
2135 'display `(space :align-to (,total-width))
2136 'shr-table-indent shr-table-id)
2137 shr-table-corner)))
2138 (insert "\n")))
2140 (defun shr-table-widths (table natural-table suggested-widths)
2141 (let* ((length (length suggested-widths))
2142 (widths (make-vector length 0))
2143 (natural-widths (make-vector length 0)))
2144 (dolist (row table)
2145 (let ((i 0))
2146 (dolist (column row)
2147 (aset widths i (max (aref widths i) column))
2148 (setq i (1+ i)))))
2149 (dolist (row natural-table)
2150 (let ((i 0))
2151 (dolist (column row)
2152 (aset natural-widths i (max (aref natural-widths i) column))
2153 (setq i (1+ i)))))
2154 (let ((extra (- (apply '+ (append suggested-widths nil))
2155 (apply '+ (append widths nil))
2156 (* shr-table-separator-pixel-width (1+ (length widths)))))
2157 (expanded-columns 0))
2158 ;; We have extra, unused space, so divide this space amongst the
2159 ;; columns.
2160 (when (> extra 0)
2161 ;; If the natural width is wider than the rendered width, we
2162 ;; want to allow the column to expand.
2163 (dotimes (i length)
2164 (when (> (aref natural-widths i) (aref widths i))
2165 (setq expanded-columns (1+ expanded-columns))))
2166 (dotimes (i length)
2167 (when (> (aref natural-widths i) (aref widths i))
2168 (aset widths i (min
2169 (aref natural-widths i)
2170 (+ (/ extra expanded-columns)
2171 (aref widths i))))))))
2172 widths))
2174 (defun shr-make-table (dom widths &optional fill storage-attribute)
2175 (or (cadr (assoc (list dom widths fill) shr-content-cache))
2176 (let ((data (shr-make-table-1 dom widths fill)))
2177 (push (list (list dom widths fill) data)
2178 shr-content-cache)
2179 (when storage-attribute
2180 (dom-set-attribute dom storage-attribute data))
2181 data)))
2183 (defun shr-make-table-1 (dom widths &optional fill)
2184 (let ((trs nil)
2185 (rowspans (make-vector (length widths) 0))
2186 (colspan-remaining 0)
2187 colspan-width colspan-count
2188 width colspan)
2189 (dolist (row (dom-non-text-children dom))
2190 (when (eq (dom-tag row) 'tr)
2191 (let ((tds nil)
2192 (columns (dom-non-text-children row))
2193 (i 0)
2194 (width-column 0)
2195 column)
2196 (while (< i (length widths))
2197 ;; If we previously had a rowspan definition, then that
2198 ;; means that we now have a "missing" td/th element here.
2199 ;; So just insert a dummy, empty one to (sort of) emulate
2200 ;; rowspan.
2201 (setq column
2202 (if (zerop (aref rowspans i))
2203 (pop columns)
2204 (aset rowspans i (1- (aref rowspans i)))
2205 '(td)))
2206 (when (and (not (stringp column))
2207 (or (memq (dom-tag column) '(td th))
2208 (not column)))
2209 (when-let* ((span (dom-attr column 'rowspan)))
2210 (aset rowspans i (+ (aref rowspans i)
2211 (1- (string-to-number span)))))
2212 ;; Sanity check for invalid column-spans.
2213 (when (>= width-column (length widths))
2214 (setq width-column 0))
2215 (setq width
2216 (if column
2217 (aref widths width-column)
2218 (* 10 shr-table-separator-pixel-width)))
2219 (when (setq colspan (dom-attr column 'colspan))
2220 (setq colspan (min (string-to-number colspan)
2221 ;; The colspan may be wrong, so
2222 ;; truncate it to the length of the
2223 ;; remaining columns.
2224 (- (length widths) i)))
2225 (dotimes (j (1- colspan))
2226 (setq width
2227 (if (> (+ i 1 j) (1- (length widths)))
2228 ;; If we have a colspan spec that's longer
2229 ;; than the table is wide, just use the last
2230 ;; width as the width.
2231 (aref widths (1- (length widths)))
2232 ;; Sum up the widths of the columns we're
2233 ;; spanning.
2234 (+ width
2235 shr-table-separator-length
2236 (aref widths (+ i 1 j))))))
2237 (setq width-column (+ width-column (1- colspan))
2238 colspan-count colspan
2239 colspan-remaining colspan))
2240 (when column
2241 (let ((data (shr-render-td column width fill)))
2242 (if (and (not fill)
2243 (> colspan-remaining 0))
2244 (progn
2245 (setq colspan-width (car data))
2246 (let ((this-width (/ colspan-width colspan-count)))
2247 (push (cons this-width (cadr data)) tds)
2248 (setq colspan-remaining (1- colspan-remaining))))
2249 (if (not fill)
2250 (push (cons (car data) (cadr data)) tds)
2251 (push data tds)))))
2252 (when (and colspan
2253 (> colspan 1))
2254 (dotimes (_ (1- colspan))
2255 (setq i (1+ i))
2256 (push
2257 (if fill
2258 (list 0 0 -1 nil 1 nil nil)
2259 '(0 . 0))
2260 tds)))
2261 (setq i (1+ i)
2262 width-column (1+ width-column))))
2263 (push (nreverse tds) trs))))
2264 (nreverse trs)))
2266 (defun shr-pixel-buffer-width ()
2267 (if (not shr-use-fonts)
2268 (save-excursion
2269 (goto-char (point-min))
2270 (let ((max 0))
2271 (while (not (eobp))
2272 (end-of-line)
2273 (setq max (max max (current-column)))
2274 (forward-line 1))
2275 max))
2276 (if (get-buffer-window)
2277 (car (window-text-pixel-size nil (point-min) (point-max)))
2278 (save-window-excursion
2279 ;; Avoid errors if the selected window is a dedicated one,
2280 ;; and they just want to insert a document into it.
2281 (set-window-dedicated-p nil nil)
2282 (set-window-buffer nil (current-buffer))
2283 (car (window-text-pixel-size nil (point-min) (point-max)))))))
2285 (defun shr-render-td (dom width fill)
2286 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill))))
2287 (or (dom-attr dom cache)
2288 (and fill
2289 (let (result)
2290 (dolist (attr (dom-attributes dom))
2291 (let ((name (symbol-name (car attr))))
2292 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name)
2293 (let ((cache-width (string-to-number
2294 (match-string 1 name))))
2295 (when (and (>= cache-width width)
2296 (<= (car (cdr attr)) width))
2297 (setq result (cdr attr)))))))
2298 result))
2299 (let* ((pt (point))
2300 (result (shr-render-td-1 dom width fill)))
2301 (dom-set-attribute dom cache result)
2302 (goto-char pt)
2303 result))))
2305 (defun shr-render-td-1 (dom width fill)
2306 (with-temp-buffer
2307 (let ((bgcolor (dom-attr dom 'bgcolor))
2308 (fgcolor (dom-attr dom 'fgcolor))
2309 (style (dom-attr dom 'style))
2310 (shr-stylesheet shr-stylesheet)
2311 (max-width 0)
2312 natural-width)
2313 (when style
2314 (setq style (and (string-match "color" style)
2315 (shr-parse-style style))))
2316 (when bgcolor
2317 (setq style (nconc (list (cons 'background-color bgcolor))
2318 style)))
2319 (when fgcolor
2320 (setq style (nconc (list (cons 'color fgcolor)) style)))
2321 (when style
2322 (setq shr-stylesheet (append style shr-stylesheet)))
2323 (let ((shr-internal-width width)
2324 (shr-indentation 0))
2325 (shr-descend dom))
2326 (save-window-excursion
2327 ;; Avoid errors if the selected window is a dedicated one,
2328 ;; and they just want to insert a document into it.
2329 (set-window-dedicated-p nil nil)
2330 (set-window-buffer nil (current-buffer))
2331 (unless fill
2332 (setq natural-width
2333 (or (dom-attr dom 'shr-td-cache-natural)
2334 (let ((natural (max (shr-pixel-buffer-width)
2335 (shr-dom-max-natural-width dom 0))))
2336 (dom-set-attribute dom 'shr-td-cache-natural natural)
2337 natural))))
2338 (if (and natural-width
2339 (<= natural-width width))
2340 (setq max-width natural-width)
2341 (let ((shr-internal-width width))
2342 (shr-fill-lines (point-min) (point-max))
2343 (setq max-width (shr-pixel-buffer-width)))))
2344 (goto-char (point-max))
2345 ;; Delete padding at the bottom of the TDs.
2346 (delete-region
2347 (point)
2348 (progn
2349 (skip-chars-backward " \t\n")
2350 (end-of-line)
2351 (point)))
2352 (goto-char (point-min))
2353 (list max-width
2354 natural-width
2355 (count-lines (point-min) (point-max))
2356 (split-string (buffer-string) "\n")
2357 (if (dom-attr dom 'colspan)
2358 (string-to-number (dom-attr dom 'colspan))
2360 (cdr (assq 'color shr-stylesheet))
2361 (cdr (assq 'background-color shr-stylesheet))))))
2363 (defun shr-dom-max-natural-width (dom max)
2364 (if (eq (dom-tag dom) 'table)
2365 (max max (or
2366 (cl-loop
2367 for line in (dom-attr dom 'shr-suggested-widths)
2368 maximize (+
2369 shr-table-separator-length
2370 (cl-loop for elem in line
2371 summing
2372 (+ (cdr elem)
2373 (* 2 shr-table-separator-length)))))
2375 (dolist (child (dom-children dom))
2376 (unless (stringp child)
2377 (setq max (max (shr-dom-max-natural-width child max)))))
2378 max))
2380 (defun shr-buffer-width ()
2381 (goto-char (point-min))
2382 (let ((max 0))
2383 (while (not (eobp))
2384 (end-of-line)
2385 (setq max (max max (current-column)))
2386 (forward-line 1))
2387 max))
2389 (defun shr-pro-rate-columns (columns)
2390 (let ((total-percentage 0)
2391 (widths (make-vector (length columns) 0)))
2392 (dotimes (i (length columns))
2393 (setq total-percentage (+ total-percentage (aref columns i))))
2394 (setq total-percentage (/ 1.0 total-percentage))
2395 (dotimes (i (length columns))
2396 (aset widths i (max (truncate (* (aref columns i)
2397 total-percentage
2398 (- shr-internal-width
2399 (* (1+ (length columns))
2400 shr-table-separator-pixel-width))))
2401 10)))
2402 widths))
2404 ;; Return a summary of the number and shape of the TDs in the table.
2405 (defun shr-column-specs (dom)
2406 (let ((columns (make-vector (shr-max-columns dom) 1)))
2407 (dolist (row (dom-non-text-children dom))
2408 (when (eq (dom-tag row) 'tr)
2409 (let ((i 0))
2410 (dolist (column (dom-non-text-children row))
2411 (when (memq (dom-tag column) '(td th))
2412 (let ((width (dom-attr column 'width)))
2413 (when (and width
2414 (string-match "\\([0-9]+\\)%" width)
2415 (not (zerop (setq width (string-to-number
2416 (match-string 1 width))))))
2417 (aset columns i (/ width 100.0))))
2418 (setq i (1+ i)))))))
2419 columns))
2421 (defun shr-count (dom elem)
2422 (let ((i 0))
2423 (dolist (sub (dom-children dom))
2424 (when (and (not (stringp sub))
2425 (eq (dom-tag sub) elem))
2426 (setq i (1+ i))))
2429 (defun shr-max-columns (dom)
2430 (let ((max 0))
2431 (dolist (row (dom-children dom))
2432 (when (and (not (stringp row))
2433 (eq (dom-tag row) 'tr))
2434 (setq max (max max (+ (shr-count row 'td)
2435 (shr-count row 'th))))))
2436 max))
2438 (provide 'shr)
2440 ;;; shr.el ends here