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