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