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