Fix frame height calculations with added menu bar on Windows (Bug#22105)
[emacs.git] / lisp / net / shr.el
bloba48d098fe2605229f1fea801379aed2fa8193fc8
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 (defun shr--have-one-fringe-p ()
207 "Return non-nil if we know at least one of the fringes has non-zero width."
208 (and (fboundp 'fringe-columns)
209 (or (not (zerop (fringe-columns 'right)))
210 (not (zerop (fringe-columns 'left))))))
212 ;;;###autoload
213 (defun shr-insert-document (dom)
214 "Render the parsed document DOM into the current buffer.
215 DOM should be a parse tree as generated by
216 `libxml-parse-html-region' or similar."
217 (setq shr-content-cache nil)
218 (let ((start (point))
219 (shr-start nil)
220 (shr-base nil)
221 (shr-depth 0)
222 (shr-table-id 0)
223 (shr-warning nil)
224 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
225 (shr-internal-bullet (cons shr-bullet
226 (shr-string-pixel-width shr-bullet)))
227 (shr-internal-width (or (and shr-width
228 (if (not shr-use-fonts)
229 shr-width
230 (* shr-width (frame-char-width))))
231 ;; We need to adjust the available
232 ;; width for when the user disables
233 ;; the fringes, which will cause the
234 ;; display engine usurp one column for
235 ;; the continuation glyph.
236 (if (not shr-use-fonts)
237 (- (window-body-width) 1
238 (if (and (null shr-width)
239 (not (shr--have-one-fringe-p)))
242 (- (window-body-width nil t)
243 (* 2 (frame-char-width))
244 (if (and (null shr-width)
245 (not (shr--have-one-fringe-p)))
246 (* (frame-char-width) 2)
247 0))))))
248 (shr-descend dom)
249 (shr-fill-lines start (point))
250 (shr-remove-trailing-whitespace start (point))
251 (when shr-warning
252 (message "%s" shr-warning))))
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 ;; to usurp one column for the
467 ;; continuation glyph.
468 (if (and (null shr-width)
469 (not (shr--have-one-fringe-p)))
470 (* (frame-char-width) 2)
471 0))))
472 (shr-insert text)
473 (buffer-string)))))
475 (define-inline shr-char-breakable-p (char)
476 "Return non-nil if a line can be broken before and after CHAR."
477 (inline-quote (aref fill-find-break-point-function-table ,char)))
478 (define-inline shr-char-nospace-p (char)
479 "Return non-nil if no space is required before and after CHAR."
480 (inline-quote (aref fill-nospace-between-words-table ,char)))
482 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
483 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
484 ;; parentheses, and so on, that should not be placed in the beginning
485 ;; of a line or the end of a line.
486 (define-inline shr-char-kinsoku-bol-p (char)
487 "Return non-nil if a line ought not to begin with CHAR."
488 (inline-letevals (char)
489 (inline-quote (and (not (eq ,char ?'))
490 (aref (char-category-set ,char) ?>)))))
491 (define-inline shr-char-kinsoku-eol-p (char)
492 "Return non-nil if a line ought not to end with CHAR."
493 (inline-quote (aref (char-category-set ,char) ?<)))
494 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
495 (load "kinsoku" nil t))
497 (defun shr-pixel-column ()
498 (if (not shr-use-fonts)
499 (current-column)
500 (if (not (get-buffer-window (current-buffer)))
501 (save-window-excursion
502 (set-window-buffer nil (current-buffer))
503 (car (window-text-pixel-size nil (line-beginning-position) (point))))
504 (car (window-text-pixel-size nil (line-beginning-position) (point))))))
506 (defun shr-pixel-region ()
507 (- (shr-pixel-column)
508 (save-excursion
509 (goto-char (mark))
510 (shr-pixel-column))))
512 (defun shr-string-pixel-width (string)
513 (if (not shr-use-fonts)
514 (length string)
515 (with-temp-buffer
516 (insert string)
517 (shr-pixel-column))))
519 (defun shr-insert (text)
520 (when (and (not (bolp))
521 (get-text-property (1- (point)) 'image-url))
522 (insert "\n"))
523 (cond
524 ((eq shr-folding-mode 'none)
525 (let ((start (point)))
526 (insert text)
527 (save-restriction
528 (narrow-to-region start (point))
529 ;; Remove soft hyphens.
530 (goto-char (point-min))
531 (while (search-forward "­" nil t)
532 (replace-match "" t t))
533 (goto-char (point-max)))))
535 (let ((font-start (point)))
536 (when (and (string-match "\\`[ \t\n\r ]" text)
537 (not (bolp))
538 (not (eq (char-after (1- (point))) ? )))
539 (insert " "))
540 (let ((start (point))
541 (bolp (bolp)))
542 (insert text)
543 (save-restriction
544 (narrow-to-region start (point))
545 (goto-char start)
546 (when (looking-at "[ \t\n\r ]+")
547 (replace-match "" t t))
548 (while (re-search-forward "[ \t\n\r ]+" nil t)
549 (replace-match " " t t))
550 ;; Remove soft hyphens.
551 (goto-char (point-min))
552 (while (search-forward "­" nil t)
553 (replace-match "" t t))
554 (goto-char (point-max)))
555 ;; We may have removed everything we inserted if if was just
556 ;; spaces.
557 (unless (= font-start (point))
558 ;; Mark all lines that should possibly be folded afterwards.
559 (when bolp
560 (shr-mark-fill start))
561 (when shr-use-fonts
562 (put-text-property font-start (point)
563 'face
564 (or shr-current-font 'variable-pitch)))))))))
566 (defun shr-fill-lines (start end)
567 (if (<= shr-internal-width 0)
569 (save-restriction
570 (narrow-to-region start end)
571 (goto-char start)
572 (when (get-text-property (point) 'shr-indentation)
573 (shr-fill-line))
574 (while (setq start (next-single-property-change start 'shr-indentation))
575 (goto-char start)
576 (when (bolp)
577 (shr-fill-line)))
578 (goto-char (point-max)))))
580 (defun shr-vertical-motion (column)
581 (if (not shr-use-fonts)
582 (move-to-column column)
583 (unless (eolp)
584 (forward-char 1))
585 (vertical-motion (cons (/ column (frame-char-width)) 0))
586 (unless (eolp)
587 (forward-char 1))))
589 (defun shr-fill-line ()
590 (let ((shr-indentation (get-text-property (point) 'shr-indentation))
591 (continuation (get-text-property
592 (point) 'shr-continuation-indentation))
593 start)
594 (put-text-property (point) (1+ (point)) 'shr-indentation nil)
595 (let ((face (get-text-property (point) 'face))
596 (background-start (point)))
597 (shr-indent)
598 (when face
599 (put-text-property background-start (point) 'face
600 `,(shr-face-background face))))
601 (setq start (point))
602 (setq shr-indentation (or continuation shr-indentation))
603 (shr-vertical-motion shr-internal-width)
604 (when (looking-at " $")
605 (delete-region (point) (line-end-position)))
606 (while (not (eolp))
607 ;; We have to do some folding. First find the first
608 ;; previous point suitable for folding.
609 (if (or (not (shr-find-fill-point (line-beginning-position)))
610 (= (point) start))
611 ;; We had unbreakable text (for this width), so just go to
612 ;; the first space and carry on.
613 (progn
614 (beginning-of-line)
615 (skip-chars-forward " ")
616 (search-forward " " (line-end-position) 'move)))
617 ;; Success; continue.
618 (when (= (preceding-char) ?\s)
619 (delete-char -1))
620 (let ((face (get-text-property (point) 'face))
621 (background-start (point)))
622 (insert "\n")
623 (shr-indent)
624 (when face
625 (put-text-property background-start (point) 'face
626 `,(shr-face-background face))))
627 (setq start (point))
628 (shr-vertical-motion shr-internal-width)
629 (when (looking-at " $")
630 (delete-region (point) (line-end-position))))))
632 (defun shr-find-fill-point (start)
633 (let ((bp (point))
634 (end (point))
635 failed)
636 (while (not (or (setq failed (<= (point) start))
637 (eq (preceding-char) ? )
638 (eq (following-char) ? )
639 (shr-char-breakable-p (preceding-char))
640 (shr-char-breakable-p (following-char))
641 (and (shr-char-kinsoku-bol-p (preceding-char))
642 (shr-char-breakable-p (following-char))
643 (not (shr-char-kinsoku-bol-p (following-char))))
644 (shr-char-kinsoku-eol-p (following-char))
645 (bolp)))
646 (backward-char 1))
647 (if failed
648 ;; There's no breakable point, so we give it up.
649 (let (found)
650 (goto-char bp)
651 ;; Don't overflow the window edge, even if
652 ;; shr-kinsoku-shorten is nil.
653 (unless (or shr-kinsoku-shorten (null shr-width))
654 (while (setq found (re-search-forward
655 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
656 (line-end-position) 'move)))
657 (if (and found
658 (not (match-beginning 1)))
659 (goto-char (match-beginning 0)))))
661 (eolp)
662 ;; Don't put kinsoku-bol characters at the beginning of a line,
663 ;; or kinsoku-eol characters at the end of a line.
664 (cond
665 ;; Don't overflow the window edge, even if shr-kinsoku-shorten
666 ;; is nil.
667 ((or shr-kinsoku-shorten (null shr-width))
668 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
669 (or (shr-char-kinsoku-eol-p (preceding-char))
670 (shr-char-kinsoku-bol-p (following-char))))
671 (backward-char 1))
672 (when (setq failed (<= (point) start))
673 ;; There's no breakable point that doesn't violate kinsoku,
674 ;; so we look for the second best position.
675 (while (and (progn
676 (forward-char 1)
677 (<= (point) end))
678 (progn
679 (setq bp (point))
680 (shr-char-kinsoku-eol-p (following-char)))))
681 (goto-char bp)))
682 ((shr-char-kinsoku-eol-p (preceding-char))
683 ;; Find backward the point where kinsoku-eol characters begin.
684 (let ((count 4))
685 (while
686 (progn
687 (backward-char 1)
688 (and (> (setq count (1- count)) 0)
689 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
690 (or (shr-char-kinsoku-eol-p (preceding-char))
691 (shr-char-kinsoku-bol-p (following-char)))))))
692 (when (setq failed (<= (point) start))
693 ;; There's no breakable point that doesn't violate kinsoku,
694 ;; so we go to the second best position.
695 (if (looking-at "\\(\\c<+\\)\\c<")
696 (goto-char (match-end 1))
697 (forward-char 1))))
698 ((shr-char-kinsoku-bol-p (following-char))
699 ;; Find forward the point where kinsoku-bol characters end.
700 (let ((count 4))
701 (while (progn
702 (forward-char 1)
703 (and (>= (setq count (1- count)) 0)
704 (shr-char-kinsoku-bol-p (following-char))
705 (shr-char-breakable-p (following-char))))))))
706 (when (eq (following-char) ? )
707 (forward-char 1))))
708 (not failed)))
710 (defun shr-parse-base (url)
711 ;; Always chop off anchors.
712 (when (string-match "#.*" url)
713 (setq url (substring url 0 (match-beginning 0))))
714 ;; NB: <base href="" > URI may itself be relative to the document s URI
715 (setq url (shr-expand-url url))
716 (let* ((parsed (url-generic-parse-url url))
717 (local (url-filename parsed)))
718 (setf (url-filename parsed) "")
719 ;; Chop off the bit after the last slash.
720 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local)
721 (setq local (match-string 1 local)))
722 ;; Always make the local bit end with a slash.
723 (when (and (not (zerop (length local)))
724 (not (eq (aref local (1- (length local))) ?/)))
725 (setq local (concat local "/")))
726 (list (url-recreate-url parsed)
727 local
728 (url-type parsed)
729 url)))
731 (autoload 'url-expand-file-name "url-expand")
733 ;; FIXME This needs some tests writing.
734 ;; Does it even need to exist, given that url-expand-file-name does?
735 (defun shr-expand-url (url &optional base)
736 (setq base
737 (if base
738 ;; shr-parse-base should never call this with non-nil base!
739 (shr-parse-base base)
740 ;; Bound by the parser.
741 shr-base))
742 (when (zerop (length url))
743 (setq url nil))
744 ;; Strip leading whitespace
745 (and url (string-match "\\`\\s-+" url)
746 (setq url (substring url (match-end 0))))
747 (cond ((or (not url)
748 (not base)
749 (string-match "\\`[a-z]*:" url))
750 ;; Absolute or empty URI
751 (or url (nth 3 base)))
752 ((eq (aref url 0) ?/)
753 (if (and (> (length url) 1)
754 (eq (aref url 1) ?/))
755 ;; //host...; just use the protocol
756 (concat (nth 2 base) ":" url)
757 ;; Just use the host name part.
758 (concat (car base) url)))
759 ((eq (aref url 0) ?#)
760 ;; A link to an anchor.
761 (concat (nth 3 base) url))
763 ;; Totally relative.
764 (url-expand-file-name url (concat (car base) (cadr base))))))
766 (defun shr-ensure-newline ()
767 (unless (zerop (current-column))
768 (insert "\n")))
770 (defun shr-ensure-paragraph ()
771 (unless (bobp)
772 (let ((prefix (get-text-property (line-beginning-position)
773 'shr-prefix-length)))
774 (cond
775 ((and (bolp)
776 (save-excursion
777 (forward-line -1)
778 (looking-at " *$")))
779 ;; We're already at a new paragraph; do nothing.
781 ((and prefix
782 (= prefix (- (point) (line-beginning-position))))
783 ;; Do nothing; we're at the start of a <li>.
785 ((save-excursion
786 (beginning-of-line)
787 ;; If the current line is totally blank, and doesn't even
788 ;; have any face properties set, then delete the blank
789 ;; space.
790 (and (looking-at " *$")
791 (not (get-text-property (point) 'face))
792 (not (= (next-single-property-change (point) 'face nil
793 (line-end-position))
794 (line-end-position)))))
795 (delete-region (match-beginning 0) (match-end 0)))
797 (insert "\n\n"))))))
799 (defun shr-indent ()
800 (when (> shr-indentation 0)
801 (insert
802 (if (not shr-use-fonts)
803 (make-string shr-indentation ?\s)
804 (propertize " "
805 'display
806 `(space :width (,shr-indentation)))))))
808 (defun shr-fontize-dom (dom &rest types)
809 (let ((start (point)))
810 (shr-generic dom)
811 (dolist (type types)
812 (shr-add-font start (point) type))))
814 ;; Add face to the region, but avoid putting the font properties on
815 ;; blank text at the start of the line, and the newline at the end, to
816 ;; avoid ugliness.
817 (defun shr-add-font (start end type)
818 (save-excursion
819 (goto-char start)
820 (while (< (point) end)
821 (when (bolp)
822 (skip-chars-forward " "))
823 (add-face-text-property (point) (min (line-end-position) end) type t)
824 (if (< (line-end-position) end)
825 (forward-line 1)
826 (goto-char end)))))
828 (defun shr-mouse-browse-url (ev)
829 "Browse the URL under the mouse cursor."
830 (interactive "e")
831 (mouse-set-point ev)
832 (shr-browse-url))
834 (defun shr-browse-url (&optional external mouse-event)
835 "Browse the URL under point.
836 If EXTERNAL, browse the URL using `shr-external-browser'."
837 (interactive (list current-prefix-arg last-nonmenu-event))
838 (mouse-set-point mouse-event)
839 (let ((url (get-text-property (point) 'shr-url)))
840 (cond
841 ((not url)
842 (message "No link under point"))
843 ((string-match "^mailto:" url)
844 (browse-url-mail url))
846 (if external
847 (funcall shr-external-browser url)
848 (browse-url url))))))
850 (defun shr-save-contents (directory)
851 "Save the contents from URL in a file."
852 (interactive "DSave contents of URL to directory: ")
853 (let ((url (get-text-property (point) 'shr-url)))
854 (if (not url)
855 (message "No link under point")
856 (url-retrieve (shr-encode-url url)
857 'shr-store-contents (list url directory)
858 nil t))))
860 (defun shr-store-contents (status url directory)
861 (unless (plist-get status :error)
862 (when (or (search-forward "\n\n" nil t)
863 (search-forward "\r\n\r\n" nil t))
864 (write-region (point) (point-max)
865 (expand-file-name (file-name-nondirectory url)
866 directory)))))
868 (defun shr-image-fetched (status buffer start end &optional flags)
869 (let ((image-buffer (current-buffer)))
870 (when (and (buffer-name buffer)
871 (not (plist-get status :error)))
872 (url-store-in-cache image-buffer)
873 (when (or (search-forward "\n\n" nil t)
874 (search-forward "\r\n\r\n" nil t))
875 (let ((data (shr-parse-image-data)))
876 (with-current-buffer buffer
877 (save-excursion
878 (let ((alt (buffer-substring start end))
879 (properties (text-properties-at start))
880 (inhibit-read-only t))
881 (delete-region start end)
882 (goto-char start)
883 (funcall shr-put-image-function data alt flags)
884 (while properties
885 (let ((type (pop properties))
886 (value (pop properties)))
887 (unless (memq type '(display image-size))
888 (put-text-property start (point) type value))))))))))
889 (kill-buffer image-buffer)))
891 (defun shr-image-from-data (data)
892 "Return an image from the data: URI content DATA."
893 (when (string-match
894 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
895 data)
896 (let ((param (match-string 4 data))
897 (payload (url-unhex-string (match-string 5 data))))
898 (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
899 (setq payload (base64-decode-string payload)))
900 payload)))
902 ;; Behind display-graphic-p test.
903 (declare-function image-size "image.c" (spec &optional pixels frame))
904 (declare-function image-animate "image" (image &optional index limit))
906 (defun shr-put-image (spec alt &optional flags)
907 "Insert image SPEC with a string ALT. Return image.
908 SPEC is either an image data blob, or a list where the first
909 element is the data blob and the second element is the content-type."
910 (if (display-graphic-p)
911 (let* ((size (cdr (assq 'size flags)))
912 (data (if (consp spec)
913 (car spec)
914 spec))
915 (content-type (and (consp spec)
916 (cadr spec)))
917 (start (point))
918 (image (cond
919 ((eq size 'original)
920 (create-image data nil t :ascent 100
921 :format content-type))
922 ((eq content-type 'image/svg+xml)
923 (create-image data 'svg t :ascent 100))
924 ((eq size 'full)
925 (ignore-errors
926 (shr-rescale-image data content-type)))
928 (ignore-errors
929 (shr-rescale-image data content-type))))))
930 (when image
931 ;; When inserting big-ish pictures, put them at the
932 ;; beginning of the line.
933 (when (and (> (current-column) 0)
934 (> (car (image-size image t)) 400))
935 (insert "\n"))
936 (if (eq size 'original)
937 (insert-sliced-image image (or alt "*") nil 20 1)
938 (insert-image image (or alt "*")))
939 (put-text-property start (point) 'image-size size)
940 (when (and shr-image-animate
941 (cond ((fboundp 'image-multi-frame-p)
942 ;; Only animate multi-frame things that specify a
943 ;; delay; eg animated gifs as opposed to
944 ;; multi-page tiffs. FIXME?
945 (cdr (image-multi-frame-p image)))
946 ((fboundp 'image-animated-p)
947 (image-animated-p image))))
948 (image-animate image nil 60)))
949 image)
950 (insert alt)))
952 (defun shr-rescale-image (data &optional content-type)
953 "Rescale DATA, if too big, to fit the current buffer."
954 (if (not (and (fboundp 'imagemagick-types)
955 (get-buffer-window (current-buffer))))
956 (create-image data nil t :ascent 100)
957 (let ((edges (window-inside-pixel-edges
958 (get-buffer-window (current-buffer)))))
959 (create-image
960 data 'imagemagick t
961 :ascent 100
962 :max-width (truncate (* shr-max-image-proportion
963 (- (nth 2 edges) (nth 0 edges))))
964 :max-height (truncate (* shr-max-image-proportion
965 (- (nth 3 edges) (nth 1 edges))))
966 :format content-type))))
968 ;; url-cache-extract autoloads url-cache.
969 (declare-function url-cache-create-filename "url-cache" (url))
970 (autoload 'mm-disable-multibyte "mm-util")
971 (autoload 'browse-url-mail "browse-url")
973 (defun shr-get-image-data (url)
974 "Get image data for URL.
975 Return a string with image data."
976 (with-temp-buffer
977 (mm-disable-multibyte)
978 (when (ignore-errors
979 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
981 (when (or (search-forward "\n\n" nil t)
982 (search-forward "\r\n\r\n" nil t))
983 (shr-parse-image-data)))))
985 (declare-function libxml-parse-xml-region "xml.c"
986 (start end &optional base-url discard-comments))
988 (defun shr-parse-image-data ()
989 (let ((data (buffer-substring (point) (point-max)))
990 (content-type
991 (save-excursion
992 (save-restriction
993 (narrow-to-region (point-min) (point))
994 (let ((content-type (mail-fetch-field "content-type")))
995 (and content-type
996 ;; Remove any comments in the type string.
997 (intern (replace-regexp-in-string ";.*" "" content-type)
998 obarray)))))))
999 ;; SVG images may contain references to further images that we may
1000 ;; want to block. So special-case these by parsing the XML data
1001 ;; and remove the blocked bits.
1002 (when (eq content-type 'image/svg+xml)
1003 (setq data
1004 (shr-dom-to-xml
1005 (libxml-parse-xml-region (point) (point-max)))))
1006 (list data content-type)))
1008 (defun shr-image-displayer (content-function)
1009 "Return a function to display an image.
1010 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
1011 is an argument. The function to be returned takes three arguments URL,
1012 START, and END. Note that START and END should be markers."
1013 `(lambda (url start end)
1014 (when url
1015 (if (string-match "\\`cid:" url)
1016 ,(when content-function
1017 `(let ((image (funcall ,content-function
1018 (substring url (match-end 0)))))
1019 (when image
1020 (goto-char start)
1021 (funcall shr-put-image-function
1022 image (buffer-substring start end))
1023 (delete-region (point) end))))
1024 (url-retrieve url 'shr-image-fetched
1025 (list (current-buffer) start end)
1026 t t)))))
1028 (defun shr-heading (dom &rest types)
1029 (shr-ensure-paragraph)
1030 (apply #'shr-fontize-dom dom types)
1031 (shr-ensure-paragraph))
1033 (defun shr-urlify (start url &optional title)
1034 (shr-add-font start (point) 'shr-link)
1035 (add-text-properties
1036 start (point)
1037 (list 'shr-url url
1038 'help-echo (let ((iri (or (ignore-errors
1039 (decode-coding-string
1040 (url-unhex-string url)
1041 'utf-8 t))
1042 url)))
1043 (if title (format "%s (%s)" iri title) iri))
1044 'follow-link t
1045 'mouse-face 'highlight
1046 'keymap shr-map)))
1048 (defun shr-encode-url (url)
1049 "Encode URL."
1050 (browse-url-url-encode-chars url "[)$ ]"))
1052 (autoload 'shr-color-visible "shr-color")
1053 (autoload 'shr-color->hexadecimal "shr-color")
1055 (defun shr-color-check (fg bg)
1056 "Check that FG is visible on BG.
1057 Returns (fg bg) with corrected values.
1058 Returns nil if the colors that would be used are the default
1059 ones, in case fg and bg are nil."
1060 (when (or fg bg)
1061 (let ((fixed (cond ((null fg) 'fg)
1062 ((null bg) 'bg))))
1063 ;; Convert colors to hexadecimal, or set them to default.
1064 (let ((fg (or (shr-color->hexadecimal fg)
1065 (frame-parameter nil 'foreground-color)))
1066 (bg (or (shr-color->hexadecimal bg)
1067 (frame-parameter nil 'background-color))))
1068 (cond ((eq fixed 'bg)
1069 ;; Only return the new fg
1070 (list nil (cadr (shr-color-visible bg fg t))))
1071 ((eq fixed 'fg)
1072 ;; Invert args and results and return only the new bg
1073 (list (cadr (shr-color-visible fg bg t)) nil))
1075 (shr-color-visible bg fg)))))))
1077 (defun shr-colorize-region (start end fg &optional bg)
1078 (when (and (or fg bg) (>= (display-color-cells) 88))
1079 (let ((new-colors (shr-color-check fg bg)))
1080 (when new-colors
1081 (when fg
1082 (add-face-text-property start end
1083 (list :foreground (cadr new-colors))
1085 (when bg
1086 (add-face-text-property start end
1087 (list :background (car new-colors))
1088 t)))
1089 new-colors)))
1091 (defun shr-previous-newline-padding-width (width)
1092 (let ((overlays (overlays-at (point)))
1093 (previous-width 0))
1094 (if (null overlays)
1095 width
1096 (dolist (overlay overlays)
1097 (setq previous-width
1098 (+ previous-width
1099 (length (plist-get (overlay-properties overlay)
1100 'before-string)))))
1101 (+ width previous-width))))
1103 ;;; Tag-specific rendering rules.
1105 (defun shr-tag-body (dom)
1106 (let* ((start (point))
1107 (fgcolor (or (dom-attr dom 'fgcolor) (dom-attr dom 'text)))
1108 (bgcolor (dom-attr dom 'bgcolor))
1109 (shr-stylesheet (list (cons 'color fgcolor)
1110 (cons 'background-color bgcolor))))
1111 (shr-generic dom)
1112 (shr-colorize-region start (point) fgcolor bgcolor)))
1114 (defun shr-tag-style (_dom)
1117 (defun shr-tag-script (_dom)
1120 (defun shr-tag-comment (_dom)
1123 (defun shr-dom-to-xml (dom)
1124 (with-temp-buffer
1125 (shr-dom-print dom)
1126 (buffer-string)))
1128 (defun shr-dom-print (dom)
1129 "Convert DOM into a string containing the xml representation."
1130 (insert (format "<%s" (dom-tag dom)))
1131 (dolist (attr (dom-attributes dom))
1132 ;; Ignore attributes that start with a colon because they are
1133 ;; private elements.
1134 (unless (= (aref (format "%s" (car attr)) 0) ?:)
1135 (insert (format " %s=\"%s\"" (car attr) (cdr attr)))))
1136 (insert ">")
1137 (let (url)
1138 (dolist (elem (dom-children dom))
1139 (cond
1140 ((stringp elem)
1141 (insert elem))
1142 ((eq (dom-tag elem) 'comment)
1144 ((or (not (eq (dom-tag elem) 'image))
1145 ;; Filter out blocked elements inside the SVG image.
1146 (not (setq url (dom-attr elem ':xlink:href)))
1147 (not shr-blocked-images)
1148 (not (string-match shr-blocked-images url)))
1149 (insert " ")
1150 (shr-dom-print elem)))))
1151 (insert (format "</%s>" (dom-tag dom))))
1153 (defun shr-tag-svg (dom)
1154 (when (and (image-type-available-p 'svg)
1155 (not shr-inhibit-images))
1156 (funcall shr-put-image-function (list (shr-dom-to-xml dom) 'image/svg+xml)
1157 "SVG Image")))
1159 (defun shr-tag-sup (dom)
1160 (let ((start (point)))
1161 (shr-generic dom)
1162 (put-text-property start (point) 'display '(raise 0.5))))
1164 (defun shr-tag-sub (dom)
1165 (let ((start (point)))
1166 (shr-generic dom)
1167 (put-text-property start (point) 'display '(raise -0.5))))
1169 (defun shr-tag-label (dom)
1170 (shr-generic dom)
1171 (shr-ensure-paragraph))
1173 (defun shr-tag-p (dom)
1174 (shr-ensure-paragraph)
1175 (shr-generic dom)
1176 (shr-ensure-paragraph))
1178 (defun shr-tag-div (dom)
1179 (shr-ensure-newline)
1180 (shr-generic dom)
1181 (shr-ensure-newline))
1183 (defun shr-tag-s (dom)
1184 (shr-fontize-dom dom 'shr-strike-through))
1186 (defun shr-tag-del (dom)
1187 (shr-fontize-dom dom 'shr-strike-through))
1189 (defun shr-tag-b (dom)
1190 (shr-fontize-dom dom 'bold))
1192 (defun shr-tag-i (dom)
1193 (shr-fontize-dom dom 'italic))
1195 (defun shr-tag-em (dom)
1196 (shr-fontize-dom dom 'italic))
1198 (defun shr-tag-strong (dom)
1199 (shr-fontize-dom dom 'bold))
1201 (defun shr-tag-u (dom)
1202 (shr-fontize-dom dom 'underline))
1204 (defun shr-tag-tt (dom)
1205 (let ((shr-current-font 'default))
1206 (shr-generic dom)))
1208 (defun shr-parse-style (style)
1209 (when style
1210 (save-match-data
1211 (when (string-match "\n" style)
1212 (setq style (replace-match " " t t style))))
1213 (let ((plist nil))
1214 (dolist (elem (split-string style ";"))
1215 (when elem
1216 (setq elem (split-string elem ":"))
1217 (when (and (car elem)
1218 (cadr elem))
1219 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1220 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1221 (when (string-match " *!important\\'" value)
1222 (setq value (substring value 0 (match-beginning 0))))
1223 (unless (equal value "inherit")
1224 (push (cons (intern name obarray)
1225 value)
1226 plist))))))
1227 plist)))
1229 (defun shr-tag-base (dom)
1230 (when-let (base (dom-attr dom 'href))
1231 (setq shr-base (shr-parse-base base)))
1232 (shr-generic dom))
1234 (defun shr-tag-a (dom)
1235 (let ((url (dom-attr dom 'href))
1236 (title (dom-attr dom 'title))
1237 (start (point))
1238 shr-start)
1239 (shr-generic dom)
1240 (when (and shr-target-id
1241 (equal (dom-attr dom 'name) shr-target-id))
1242 ;; We have a zero-length <a name="foo"> element, so just
1243 ;; insert... something.
1244 (when (= start (point))
1245 (shr-ensure-newline)
1246 (insert " "))
1247 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
1248 (when url
1249 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1251 (defun shr-tag-object (dom)
1252 (unless shr-inhibit-images
1253 (let ((start (point))
1254 url multimedia image)
1255 (when-let (type (dom-attr dom 'type))
1256 (when (string-match "\\`image/svg" type)
1257 (setq url (dom-attr dom 'data)
1258 image t)))
1259 (dolist (child (dom-non-text-children dom))
1260 (cond
1261 ((eq (dom-tag child) 'embed)
1262 (setq url (or url (dom-attr child 'src))
1263 multimedia t))
1264 ((and (eq (dom-tag child) 'param)
1265 (equal (dom-attr child 'name) "movie"))
1266 (setq url (or url (dom-attr child 'value))
1267 multimedia t))))
1268 (when url
1269 (cond
1270 (image
1271 (shr-tag-img dom url)
1272 (setq dom nil))
1273 (multimedia
1274 (shr-insert " [multimedia] ")
1275 (shr-urlify start (shr-expand-url url)))))
1276 (when dom
1277 (shr-generic dom)))))
1279 (defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
1280 ("ogv" . 1.0)
1281 ("ogg" . 1.0)
1282 ("opus" . 1.0)
1283 ("flac" . 0.9)
1284 ("wav" . 0.5))
1285 "Preferences for media types.
1286 The key element should be a regexp matched against the type of the source or
1287 url if no type is specified. The value should be a float in the range 0.0 to
1288 1.0. Media elements with higher value are preferred."
1289 :version "24.4"
1290 :group 'shr
1291 :type '(alist :key-type regexp :value-type float))
1293 (defun shr--get-media-pref (elem)
1294 "Determine the preference for ELEM.
1295 The preference is a float determined from `shr-prefer-media-type'."
1296 (let ((type (dom-attr elem 'type))
1297 (p 0.0))
1298 (unless type
1299 (setq type (dom-attr elem 'src)))
1300 (when type
1301 (dolist (pref shr-prefer-media-type-alist)
1302 (when (and
1303 (> (cdr pref) p)
1304 (string-match-p (car pref) type))
1305 (setq p (cdr pref)))))
1308 (defun shr--extract-best-source (dom &optional url pref)
1309 "Extract the best `:src' property from <source> blocks in DOM."
1310 (setq pref (or pref -1.0))
1311 (let (new-pref)
1312 (dolist (elem (dom-non-text-children dom))
1313 (when (and (eq (dom-tag elem) 'source)
1314 (< pref
1315 (setq new-pref
1316 (shr--get-media-pref elem))))
1317 (setq pref new-pref
1318 url (dom-attr elem 'src))
1319 ;; libxml's html parser isn't HTML5 compliant and non terminated
1320 ;; source tags might end up as children. So recursion it is...
1321 (dolist (child (dom-non-text-children elem))
1322 (when (eq (dom-tag child) 'source)
1323 (let ((ret (shr--extract-best-source (list child) url pref)))
1324 (when (< pref (cdr ret))
1325 (setq url (car ret)
1326 pref (cdr ret)))))))))
1327 (cons url pref))
1329 (defun shr-tag-video (dom)
1330 (let ((image (dom-attr dom 'poster))
1331 (url (dom-attr dom 'src))
1332 (start (point)))
1333 (unless url
1334 (setq url (car (shr--extract-best-source dom))))
1335 (if image
1336 (shr-tag-img nil image)
1337 (shr-insert " [video] "))
1338 (shr-urlify start (shr-expand-url url))))
1340 (defun shr-tag-audio (dom)
1341 (let ((url (dom-attr dom 'src))
1342 (start (point)))
1343 (unless url
1344 (setq url (car (shr--extract-best-source dom))))
1345 (shr-insert " [audio] ")
1346 (shr-urlify start (shr-expand-url url))))
1348 (defun shr-tag-img (dom &optional url)
1349 (when (or url
1350 (and dom
1351 (> (length (dom-attr dom 'src)) 0)))
1352 (when (> (current-column) 0)
1353 (insert "\n"))
1354 (let ((alt (dom-attr dom 'alt))
1355 (url (shr-expand-url (or url (dom-attr dom 'src)))))
1356 (let ((start (point-marker)))
1357 (when (zerop (length alt))
1358 (setq alt "*"))
1359 (cond
1360 ((or (member (dom-attr dom 'height) '("0" "1"))
1361 (member (dom-attr dom 'width) '("0" "1")))
1362 ;; Ignore zero-sized or single-pixel images.
1364 ((and (not shr-inhibit-images)
1365 (string-match "\\`data:" url))
1366 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1367 (if image
1368 (funcall shr-put-image-function image alt)
1369 (insert alt))))
1370 ((and (not shr-inhibit-images)
1371 (string-match "\\`cid:" url))
1372 (let ((url (substring url (match-end 0)))
1373 image)
1374 (if (or (not shr-content-function)
1375 (not (setq image (funcall shr-content-function url))))
1376 (insert alt)
1377 (funcall shr-put-image-function image alt))))
1378 ((or shr-inhibit-images
1379 (and shr-blocked-images
1380 (string-match shr-blocked-images url)))
1381 (setq shr-start (point))
1382 (if (> (string-width alt) 8)
1383 (shr-insert (truncate-string-to-width alt 8))
1384 (shr-insert alt)))
1385 ((and (not shr-ignore-cache)
1386 (url-is-cached (shr-encode-url url)))
1387 (funcall shr-put-image-function (shr-get-image-data url) alt))
1389 (insert alt " ")
1390 (when (and shr-ignore-cache
1391 (url-is-cached (shr-encode-url url)))
1392 (let ((file (url-cache-create-filename (shr-encode-url url))))
1393 (when (file-exists-p file)
1394 (delete-file file))))
1395 (url-queue-retrieve
1396 (shr-encode-url url) 'shr-image-fetched
1397 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1398 t t)))
1399 (when (zerop shr-table-depth) ;; We are not in a table.
1400 (put-text-property start (point) 'keymap shr-map)
1401 (put-text-property start (point) 'shr-alt alt)
1402 (put-text-property start (point) 'image-url url)
1403 (put-text-property start (point) 'image-displayer
1404 (shr-image-displayer shr-content-function))
1405 (put-text-property start (point) 'help-echo
1406 (shr-fill-text
1407 (or (dom-attr dom 'title) alt))))))))
1409 (defun shr-tag-pre (dom)
1410 (let ((shr-folding-mode 'none)
1411 (shr-current-font 'default))
1412 (shr-ensure-newline)
1413 (shr-generic dom)
1414 (shr-ensure-newline)))
1416 (defun shr-tag-blockquote (dom)
1417 (shr-ensure-paragraph)
1418 (let ((start (point))
1419 (shr-indentation (+ shr-indentation
1420 (* 4 shr-table-separator-pixel-width))))
1421 (shr-generic dom)
1422 (shr-ensure-paragraph)
1423 (shr-mark-fill start)))
1425 (defun shr-tag-dl (dom)
1426 (shr-ensure-paragraph)
1427 (shr-generic dom)
1428 (shr-ensure-paragraph))
1430 (defun shr-tag-dt (dom)
1431 (shr-ensure-newline)
1432 (shr-generic dom)
1433 (shr-ensure-newline))
1435 (defun shr-tag-dd (dom)
1436 (shr-ensure-newline)
1437 (let ((shr-indentation (+ shr-indentation
1438 (* 4 shr-table-separator-pixel-width))))
1439 (shr-generic dom)))
1441 (defun shr-tag-ul (dom)
1442 (shr-ensure-paragraph)
1443 (let ((shr-list-mode 'ul))
1444 (shr-generic dom))
1445 (shr-ensure-paragraph))
1447 (defun shr-tag-ol (dom)
1448 (shr-ensure-paragraph)
1449 (let ((shr-list-mode 1))
1450 (shr-generic dom))
1451 (shr-ensure-paragraph))
1453 (defun shr-tag-li (dom)
1454 (shr-ensure-newline)
1455 (let ((start (point)))
1456 (let* ((bullet
1457 (if (numberp shr-list-mode)
1458 (prog1
1459 (format "%d " shr-list-mode)
1460 (setq shr-list-mode (1+ shr-list-mode)))
1461 (car shr-internal-bullet)))
1462 (width (if (numberp shr-list-mode)
1463 (shr-string-pixel-width bullet)
1464 (cdr shr-internal-bullet))))
1465 (insert bullet)
1466 (shr-mark-fill start)
1467 (let ((shr-indentation (+ shr-indentation width)))
1468 (put-text-property start (1+ start)
1469 'shr-continuation-indentation shr-indentation)
1470 (put-text-property start (1+ start) 'shr-prefix-length (length bullet))
1471 (shr-generic dom)))))
1473 (defun shr-mark-fill (start)
1474 ;; We may not have inserted any text to fill.
1475 (unless (= start (point))
1476 (put-text-property start (1+ start)
1477 'shr-indentation shr-indentation)))
1479 (defun shr-tag-br (dom)
1480 (when (and (not (bobp))
1481 ;; Only add a newline if we break the current line, or
1482 ;; the previous line isn't a blank line.
1483 (or (not (bolp))
1484 (and (> (- (point) 2) (point-min))
1485 (not (= (char-after (- (point) 2)) ?\n)))))
1486 (insert "\n"))
1487 (shr-generic dom))
1489 (defun shr-tag-span (dom)
1490 (shr-generic dom))
1492 (defun shr-tag-h1 (dom)
1493 (shr-heading dom (if shr-use-fonts
1494 '(variable-pitch (:height 1.3 :weight bold))
1495 'bold)))
1497 (defun shr-tag-h2 (dom)
1498 (shr-heading dom 'bold))
1500 (defun shr-tag-h3 (dom)
1501 (shr-heading dom 'italic))
1503 (defun shr-tag-h4 (dom)
1504 (shr-heading dom))
1506 (defun shr-tag-h5 (dom)
1507 (shr-heading dom))
1509 (defun shr-tag-h6 (dom)
1510 (shr-heading dom))
1512 (defun shr-tag-hr (_dom)
1513 (shr-ensure-newline)
1514 (insert (make-string (if (not shr-use-fonts)
1515 shr-internal-width
1516 (1+ (/ shr-internal-width
1517 shr-table-separator-pixel-width)))
1518 shr-hr-line)
1519 "\n"))
1521 (defun shr-tag-title (dom)
1522 (shr-heading dom 'bold 'underline))
1524 (defun shr-tag-font (dom)
1525 (let* ((start (point))
1526 (color (dom-attr dom 'color))
1527 (shr-stylesheet (nconc (list (cons 'color color))
1528 shr-stylesheet)))
1529 (shr-generic dom)
1530 (when color
1531 (shr-colorize-region start (point) color
1532 (cdr (assq 'background-color shr-stylesheet))))))
1534 ;;; Table rendering algorithm.
1536 ;; Table rendering is the only complicated thing here. We do this by
1537 ;; first counting how many TDs there are in each TR, and registering
1538 ;; how wide they think they should be ("width=45%", etc). Then we
1539 ;; render each TD separately (this is done in temporary buffers, so
1540 ;; that we can use all the rendering machinery as if we were in the
1541 ;; main buffer). Now we know how much space each TD really takes, so
1542 ;; we then render everything again with the new widths, and finally
1543 ;; insert all these boxes into the main buffer.
1544 (defun shr-tag-table-1 (dom)
1545 (setq dom (or (dom-child-by-tag dom 'tbody) dom))
1546 (let* ((shr-inhibit-images t)
1547 (shr-table-depth (1+ shr-table-depth))
1548 (shr-kinsoku-shorten t)
1549 ;; Find all suggested widths.
1550 (columns (shr-column-specs dom))
1551 ;; Compute how many pixels wide each TD should be.
1552 (suggested-widths (shr-pro-rate-columns columns))
1553 ;; Do a "test rendering" to see how big each TD is (this can
1554 ;; be smaller (if there's little text) or bigger (if there's
1555 ;; unbreakable text).
1556 (elems (or (dom-attr dom 'shr-suggested-widths)
1557 (shr-make-table dom suggested-widths nil
1558 'shr-suggested-widths)))
1559 (sketch (loop for line in elems
1560 collect (mapcar #'car line)))
1561 (natural (loop for line in elems
1562 collect (mapcar #'cdr line)))
1563 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1564 ;; This probably won't work very well.
1565 (when (> (+ (loop for width across sketch-widths
1566 summing (1+ width))
1567 shr-indentation shr-table-separator-pixel-width)
1568 (frame-width))
1569 (setq truncate-lines t))
1570 ;; Then render the table again with these new "hard" widths.
1571 (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
1573 (defun shr-tag-table (dom)
1574 (shr-ensure-paragraph)
1575 (let* ((caption (dom-children (dom-child-by-tag dom 'caption)))
1576 (header (dom-non-text-children (dom-child-by-tag dom 'thead)))
1577 (body (dom-non-text-children (or (dom-child-by-tag dom 'tbody)
1578 dom)))
1579 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot)))
1580 (bgcolor (dom-attr dom 'bgcolor))
1581 (start (point))
1582 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1583 shr-stylesheet))
1584 (nheader (if header (shr-max-columns header)))
1585 (nbody (if body (shr-max-columns body)))
1586 (nfooter (if footer (shr-max-columns footer))))
1587 (if (and (not caption)
1588 (not header)
1589 (not (dom-child-by-tag dom 'tbody))
1590 (not (dom-child-by-tag dom 'tr))
1591 (not footer))
1592 ;; The table is totally invalid and just contains random junk.
1593 ;; Try to output it anyway.
1594 (shr-generic dom)
1595 ;; It's a real table, so render it.
1596 (if (dom-attr dom 'shr-fixed-table)
1597 (shr-tag-table-1 dom)
1598 ;; Only fix up the table once.
1599 (let ((table
1600 (nconc
1601 (list 'table nil)
1602 (if caption `((tr nil (td nil ,@caption))))
1603 (cond
1604 (header
1605 (if footer
1606 ;; header + body + footer
1607 (if (= nheader nbody)
1608 (if (= nbody nfooter)
1609 `((tr nil (td nil (table nil
1610 (tbody nil ,@header
1611 ,@body ,@footer)))))
1612 (nconc `((tr nil (td nil (table nil
1613 (tbody nil ,@header
1614 ,@body)))))
1615 (if (= nfooter 1)
1616 footer
1617 `((tr nil (td nil (table
1618 nil (tbody
1619 nil ,@footer))))))))
1620 (nconc `((tr nil (td nil (table nil (tbody
1621 nil ,@header)))))
1622 (if (= nbody nfooter)
1623 `((tr nil (td nil (table
1624 nil (tbody nil ,@body
1625 ,@footer)))))
1626 (nconc `((tr nil (td nil (table
1627 nil (tbody nil
1628 ,@body)))))
1629 (if (= nfooter 1)
1630 footer
1631 `((tr nil (td nil (table
1633 (tbody
1635 ,@footer))))))))))
1636 ;; header + body
1637 (if (= nheader nbody)
1638 `((tr nil (td nil (table nil (tbody nil ,@header
1639 ,@body)))))
1640 (if (= nheader 1)
1641 `(,@header (tr nil (td nil (table
1642 nil (tbody nil ,@body)))))
1643 `((tr nil (td nil (table nil (tbody nil ,@header))))
1644 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
1645 (footer
1646 ;; body + footer
1647 (if (= nbody nfooter)
1648 `((tr nil (td nil (table
1649 nil (tbody nil ,@body ,@footer)))))
1650 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
1651 (if (= nfooter 1)
1652 footer
1653 `((tr nil (td nil (table
1654 nil (tbody nil ,@footer)))))))))
1655 (caption
1656 `((tr nil (td nil (table nil (tbody nil ,@body))))))
1657 (body)))))
1658 (dom-set-attribute table 'shr-fixed-table t)
1659 (setcdr dom (cdr table))
1660 (shr-tag-table-1 dom))))
1661 (when bgcolor
1662 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1663 bgcolor))
1664 ;; Finally, insert all the images after the table. The Emacs buffer
1665 ;; model isn't strong enough to allow us to put the images actually
1666 ;; into the tables.
1667 (when (zerop shr-table-depth)
1668 (save-excursion
1669 (shr-expand-alignments start (point)))
1670 (dolist (elem (dom-by-tag dom 'object))
1671 (shr-tag-object elem))
1672 (dolist (elem (dom-by-tag dom 'img))
1673 (shr-tag-img elem)))))
1675 (defun shr-insert-table (table widths)
1676 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1677 "collapse"))
1678 (shr-table-separator-length (if collapse 0 1))
1679 (shr-table-vertical-line (if collapse "" shr-table-vertical-line))
1680 (start (point)))
1681 (setq shr-table-id (1+ shr-table-id))
1682 (unless collapse
1683 (shr-insert-table-ruler widths))
1684 (dolist (row table)
1685 (let ((start (point))
1686 (align 0)
1687 (column-number 0)
1688 (height (let ((max 0))
1689 (dolist (column row)
1690 (setq max (max max (nth 2 column))))
1691 max)))
1692 (dotimes (i (max height 1))
1693 (shr-indent)
1694 (insert shr-table-vertical-line "\n"))
1695 (dolist (column row)
1696 (when (> (nth 2 column) -1)
1697 (goto-char start)
1698 ;; Sum up all the widths from the column. (There may be
1699 ;; more than one if this is a "colspan" column.)
1700 (dotimes (i (nth 4 column))
1701 ;; The colspan directive may be wrong and there may not be
1702 ;; that number of columns.
1703 (when (<= column-number (1- (length widths)))
1704 (setq align (+ align
1705 (aref widths column-number)
1706 (* 2 shr-table-separator-pixel-width))))
1707 (setq column-number (1+ column-number)))
1708 (let ((lines (nth 3 column))
1709 (pixel-align (if (not shr-use-fonts)
1710 (* align (frame-char-width))
1711 align)))
1712 (dolist (line lines)
1713 (end-of-line)
1714 (let ((start (point)))
1715 (insert
1716 line
1717 (propertize " "
1718 'display `(space :align-to (,pixel-align))
1719 'face (and (> (length line) 0)
1720 (shr-face-background
1721 (get-text-property
1722 (1- (length line)) 'face line)))
1723 'shr-table-indent shr-table-id)
1724 shr-table-vertical-line)
1725 (shr-colorize-region
1726 start (1- (point)) (nth 5 column) (nth 6 column)))
1727 (forward-line 1))
1728 ;; Add blank lines at padding at the bottom of the TD,
1729 ;; possibly.
1730 (dotimes (i (- height (length lines)))
1731 (end-of-line)
1732 (let ((start (point)))
1733 (insert (propertize " "
1734 'display `(space :align-to (,pixel-align))
1735 'shr-table-indent shr-table-id)
1736 shr-table-vertical-line)
1737 (shr-colorize-region
1738 start (1- (point)) (nth 5 column) (nth 6 column)))
1739 (forward-line 1))))))
1740 (unless collapse
1741 (shr-insert-table-ruler widths)))
1742 (unless (= start (point))
1743 (put-text-property start (1+ start) 'shr-table-id shr-table-id))))
1745 (defun shr-face-background (face)
1746 (and (consp face)
1747 (let ((background nil))
1748 (dolist (elem face)
1749 (when (and (consp elem)
1750 (eq (car elem) :background))
1751 (setq background (cadr elem))))
1752 (and background
1753 (list :background background)))))
1755 (defun shr-expand-alignments (start end)
1756 (while (< (setq start (next-single-property-change
1757 start 'shr-table-id nil end))
1758 end)
1759 (goto-char start)
1760 (let* ((shr-use-fonts t)
1761 (id (get-text-property (point) 'shr-table-id))
1762 (base (shr-pixel-column))
1763 elem)
1764 (when id
1765 (save-excursion
1766 (while (setq elem (text-property-any
1767 (point) end 'shr-table-indent id))
1768 (goto-char elem)
1769 (let ((align (get-text-property (point) 'display)))
1770 (put-text-property (point) (1+ (point)) 'display
1771 `(space :align-to (,(+ (car (nth 2 align))
1772 base)))))
1773 (forward-char 1)))))
1774 (setq start (1+ start))))
1776 (defun shr-insert-table-ruler (widths)
1777 (when shr-table-horizontal-line
1778 (when (and (bolp)
1779 (> shr-indentation 0))
1780 (shr-indent))
1781 (insert shr-table-corner)
1782 (let ((total-width 0))
1783 (dotimes (i (length widths))
1784 (setq total-width (+ total-width (aref widths i)
1785 (* shr-table-separator-pixel-width 2)))
1786 (insert (make-string (1+ (/ (aref widths i)
1787 shr-table-separator-pixel-width))
1788 shr-table-horizontal-line)
1789 (propertize " "
1790 'display `(space :align-to (,total-width))
1791 'shr-table-indent shr-table-id)
1792 shr-table-corner)))
1793 (insert "\n")))
1795 (defun shr-table-widths (table natural-table suggested-widths)
1796 (let* ((length (length suggested-widths))
1797 (widths (make-vector length 0))
1798 (natural-widths (make-vector length 0)))
1799 (dolist (row table)
1800 (let ((i 0))
1801 (dolist (column row)
1802 (aset widths i (max (aref widths i) column))
1803 (setq i (1+ i)))))
1804 (dolist (row natural-table)
1805 (let ((i 0))
1806 (dolist (column row)
1807 (aset natural-widths i (max (aref natural-widths i) column))
1808 (setq i (1+ i)))))
1809 (let ((extra (- (apply '+ (append suggested-widths nil))
1810 (apply '+ (append widths nil))
1811 (* shr-table-separator-pixel-width (1+ (length widths)))))
1812 (expanded-columns 0))
1813 ;; We have extra, unused space, so divide this space amongst the
1814 ;; columns.
1815 (when (> extra 0)
1816 ;; If the natural width is wider than the rendered width, we
1817 ;; want to allow the column to expand.
1818 (dotimes (i length)
1819 (when (> (aref natural-widths i) (aref widths i))
1820 (setq expanded-columns (1+ expanded-columns))))
1821 (dotimes (i length)
1822 (when (> (aref natural-widths i) (aref widths i))
1823 (aset widths i (min
1824 (aref natural-widths i)
1825 (+ (/ extra expanded-columns)
1826 (aref widths i))))))))
1827 widths))
1829 (defun shr-make-table (dom widths &optional fill storage-attribute)
1830 (or (cadr (assoc (list dom widths fill) shr-content-cache))
1831 (let ((data (shr-make-table-1 dom widths fill)))
1832 (push (list (list dom widths fill) data)
1833 shr-content-cache)
1834 (when storage-attribute
1835 (dom-set-attribute dom storage-attribute data))
1836 data)))
1838 (defun shr-make-table-1 (dom widths &optional fill)
1839 (let ((trs nil)
1840 (rowspans (make-vector (length widths) 0))
1841 (colspan-remaining 0)
1842 colspan-width colspan-count
1843 width colspan)
1844 (dolist (row (dom-non-text-children dom))
1845 (when (eq (dom-tag row) 'tr)
1846 (let ((tds nil)
1847 (columns (dom-non-text-children row))
1848 (i 0)
1849 (width-column 0)
1850 column)
1851 (while (< i (length widths))
1852 ;; If we previously had a rowspan definition, then that
1853 ;; means that we now have a "missing" td/th element here.
1854 ;; So just insert a dummy, empty one to (sort of) emulate
1855 ;; rowspan.
1856 (setq column
1857 (if (zerop (aref rowspans i))
1858 (pop columns)
1859 (aset rowspans i (1- (aref rowspans i)))
1860 '(td)))
1861 (when (and (not (stringp column))
1862 (or (memq (dom-tag column) '(td th))
1863 (not column)))
1864 (when-let (span (dom-attr column 'rowspan))
1865 (aset rowspans i (+ (aref rowspans i)
1866 (1- (string-to-number span)))))
1867 ;; Sanity check for invalid column-spans.
1868 (when (>= width-column (length widths))
1869 (setq width-column 0))
1870 (setq width
1871 (if column
1872 (aref widths width-column)
1873 (* 10 shr-table-separator-pixel-width)))
1874 (when (setq colspan (dom-attr column 'colspan))
1875 (setq colspan (min (string-to-number colspan)
1876 ;; The colspan may be wrong, so
1877 ;; truncate it to the length of the
1878 ;; remaining columns.
1879 (- (length widths) i)))
1880 (dotimes (j (1- colspan))
1881 (setq width
1882 (if (> (+ i 1 j) (1- (length widths)))
1883 ;; If we have a colspan spec that's longer
1884 ;; than the table is wide, just use the last
1885 ;; width as the width.
1886 (aref widths (1- (length widths)))
1887 ;; Sum up the widths of the columns we're
1888 ;; spanning.
1889 (+ width
1890 shr-table-separator-length
1891 (aref widths (+ i 1 j))))))
1892 (setq width-column (+ width-column (1- colspan))
1893 colspan-count colspan
1894 colspan-remaining colspan))
1895 (when column
1896 (let ((data (shr-render-td column width fill)))
1897 (if (and (not fill)
1898 (> colspan-remaining 0))
1899 (progn
1900 (setq colspan-width (car data))
1901 (let ((this-width (/ colspan-width colspan-count)))
1902 (push (cons this-width (cadr data)) tds)
1903 (setq colspan-remaining (1- colspan-remaining))))
1904 (if (not fill)
1905 (push (cons (car data) (cadr data)) tds)
1906 (push data tds)))))
1907 (when (and colspan
1908 (> colspan 1))
1909 (dotimes (c (1- colspan))
1910 (setq i (1+ i))
1911 (push
1912 (if fill
1913 (list 0 0 -1 nil 1 nil nil)
1914 '(0 . 0))
1915 tds)))
1916 (setq i (1+ i)
1917 width-column (1+ width-column))))
1918 (push (nreverse tds) trs))))
1919 (nreverse trs)))
1921 (defun shr-pixel-buffer-width ()
1922 (if (not shr-use-fonts)
1923 (save-excursion
1924 (goto-char (point-min))
1925 (let ((max 0))
1926 (while (not (eobp))
1927 (end-of-line)
1928 (setq max (max max (current-column)))
1929 (forward-line 1))
1930 max))
1931 (if (get-buffer-window)
1932 (car (window-text-pixel-size nil (point-min) (point-max)))
1933 (save-window-excursion
1934 (set-window-buffer nil (current-buffer))
1935 (car (window-text-pixel-size nil (point-min) (point-max)))))))
1937 (defun shr-render-td (dom width fill)
1938 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill))))
1939 (or (dom-attr dom cache)
1940 (and fill
1941 (let (result)
1942 (dolist (attr (dom-attributes dom))
1943 (let ((name (symbol-name (car attr))))
1944 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name)
1945 (let ((cache-width (string-to-number
1946 (match-string 1 name))))
1947 (when (and (>= cache-width width)
1948 (<= (car (cdr attr)) width))
1949 (setq result (cdr attr)))))))
1950 result))
1951 (let ((result (shr-render-td-1 dom width fill)))
1952 (dom-set-attribute dom cache result)
1953 result))))
1955 (defun shr-render-td-1 (dom width fill)
1956 (with-temp-buffer
1957 (let ((bgcolor (dom-attr dom 'bgcolor))
1958 (fgcolor (dom-attr dom 'fgcolor))
1959 (style (dom-attr dom 'style))
1960 (shr-stylesheet shr-stylesheet)
1961 (max-width 0)
1962 natural-width)
1963 (when style
1964 (setq style (and (string-match "color" style)
1965 (shr-parse-style style))))
1966 (when bgcolor
1967 (setq style (nconc (list (cons 'background-color bgcolor))
1968 style)))
1969 (when fgcolor
1970 (setq style (nconc (list (cons 'color fgcolor)) style)))
1971 (when style
1972 (setq shr-stylesheet (append style shr-stylesheet)))
1973 (let ((shr-internal-width width)
1974 (shr-indentation 0))
1975 (shr-descend dom))
1976 (save-window-excursion
1977 (set-window-buffer nil (current-buffer))
1978 (unless fill
1979 (setq natural-width
1980 (or (dom-attr dom 'shr-td-cache-natural)
1981 (let ((natural (max (shr-pixel-buffer-width)
1982 (shr-dom-max-natural-width dom 0))))
1983 (dom-set-attribute dom 'shr-td-cache-natural natural)
1984 natural))))
1985 (if (and natural-width
1986 (<= natural-width width))
1987 (setq max-width natural-width)
1988 (let ((shr-internal-width width))
1989 (shr-fill-lines (point-min) (point-max))
1990 (setq max-width (shr-pixel-buffer-width)))))
1991 (goto-char (point-max))
1992 ;; Delete padding at the bottom of the TDs.
1993 (delete-region
1994 (point)
1995 (progn
1996 (skip-chars-backward " \t\n")
1997 (end-of-line)
1998 (point)))
1999 (goto-char (point-min))
2000 (list max-width
2001 natural-width
2002 (count-lines (point-min) (point-max))
2003 (split-string (buffer-string) "\n")
2004 (if (dom-attr dom 'colspan)
2005 (string-to-number (dom-attr dom 'colspan))
2007 (cdr (assq 'color shr-stylesheet))
2008 (cdr (assq 'background-color shr-stylesheet))))))
2010 (defun shr-dom-max-natural-width (dom max)
2011 (if (eq (dom-tag dom) 'table)
2012 (max max (or
2013 (loop for line in (dom-attr dom 'shr-suggested-widths)
2014 maximize (+
2015 shr-table-separator-length
2016 (loop for elem in line
2017 summing
2018 (+ (cdr elem)
2019 (* 2 shr-table-separator-length)))))
2021 (dolist (child (dom-children dom))
2022 (unless (stringp child)
2023 (setq max (max (shr-dom-max-natural-width child max)))))
2024 max))
2026 (defun shr-buffer-width ()
2027 (goto-char (point-min))
2028 (let ((max 0))
2029 (while (not (eobp))
2030 (end-of-line)
2031 (setq max (max max (current-column)))
2032 (forward-line 1))
2033 max))
2035 (defun shr-pro-rate-columns (columns)
2036 (let ((total-percentage 0)
2037 (widths (make-vector (length columns) 0)))
2038 (dotimes (i (length columns))
2039 (setq total-percentage (+ total-percentage (aref columns i))))
2040 (setq total-percentage (/ 1.0 total-percentage))
2041 (dotimes (i (length columns))
2042 (aset widths i (max (truncate (* (aref columns i)
2043 total-percentage
2044 (- shr-internal-width
2045 (* (1+ (length columns))
2046 shr-table-separator-pixel-width))))
2047 10)))
2048 widths))
2050 ;; Return a summary of the number and shape of the TDs in the table.
2051 (defun shr-column-specs (dom)
2052 (let ((columns (make-vector (shr-max-columns dom) 1)))
2053 (dolist (row (dom-non-text-children dom))
2054 (when (eq (dom-tag row) 'tr)
2055 (let ((i 0))
2056 (dolist (column (dom-non-text-children row))
2057 (when (memq (dom-tag column) '(td th))
2058 (let ((width (dom-attr column 'width)))
2059 (when (and width
2060 (string-match "\\([0-9]+\\)%" width)
2061 (not (zerop (setq width (string-to-number
2062 (match-string 1 width))))))
2063 (aset columns i (/ width 100.0))))
2064 (setq i (1+ i)))))))
2065 columns))
2067 (defun shr-count (dom elem)
2068 (let ((i 0))
2069 (dolist (sub (dom-children dom))
2070 (when (and (not (stringp sub))
2071 (eq (dom-tag sub) elem))
2072 (setq i (1+ i))))
2075 (defun shr-max-columns (dom)
2076 (let ((max 0))
2077 (dolist (row (dom-children dom))
2078 (when (and (not (stringp row))
2079 (eq (dom-tag row) 'tr))
2080 (setq max (max max (+ (shr-count row 'td)
2081 (shr-count row 'th))))))
2082 max))
2084 (provide 'shr)
2086 ;;; shr.el ends here