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