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