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