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