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