]> code.delx.au - gnu-emacs/blob - lisp/net/shr.el
(shr-fill-line): Preserve background colours when indenting/folding.
[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 (if (<= (current-column) shr-indentation)
723 (unless (save-excursion
724 (forward-line -1)
725 (looking-at " *$"))
726 (insert "\n"))
727 (if (save-excursion
728 (beginning-of-line)
729 ;; If the current line is totally blank, and doesn't even
730 ;; have any face properties set, then delete the blank
731 ;; space.
732 (and (looking-at " *$")
733 (not (get-text-property (point) 'face))
734 (not (= (next-single-property-change (point) 'face nil
735 (line-end-position))
736 (line-end-position)))))
737 (delete-region (match-beginning 0) (match-end 0))
738 (insert "\n\n")))))
739
740 (defun shr-indent ()
741 (when (> shr-indentation 0)
742 (insert
743 (if (not shr-use-fonts)
744 (make-string shr-indentation ?\s)
745 (propertize " "
746 'display
747 `(space :width (,shr-indentation)))))))
748
749 (defun shr-fontize-dom (dom &rest types)
750 (let ((start (point)))
751 (shr-generic dom)
752 (dolist (type types)
753 (shr-add-font start (point) type))))
754
755 ;; Add face to the region, but avoid putting the font properties on
756 ;; blank text at the start of the line, and the newline at the end, to
757 ;; avoid ugliness.
758 (defun shr-add-font (start end type)
759 (unless shr-inhibit-decoration
760 (save-excursion
761 (goto-char start)
762 (while (< (point) end)
763 (when (bolp)
764 (skip-chars-forward " "))
765 (add-face-text-property (point) (min (line-end-position) end) type t)
766 (if (< (line-end-position) end)
767 (forward-line 1)
768 (goto-char end))))))
769
770 (defun shr-mouse-browse-url (ev)
771 "Browse the URL under the mouse cursor."
772 (interactive "e")
773 (mouse-set-point ev)
774 (shr-browse-url))
775
776 (defun shr-browse-url (&optional external mouse-event)
777 "Browse the URL under point.
778 If EXTERNAL, browse the URL using `shr-external-browser'."
779 (interactive (list current-prefix-arg last-nonmenu-event))
780 (mouse-set-point mouse-event)
781 (let ((url (get-text-property (point) 'shr-url)))
782 (cond
783 ((not url)
784 (message "No link under point"))
785 ((string-match "^mailto:" url)
786 (browse-url-mail url))
787 (t
788 (if external
789 (funcall shr-external-browser url)
790 (browse-url url))))))
791
792 (defun shr-save-contents (directory)
793 "Save the contents from URL in a file."
794 (interactive "DSave contents of URL to directory: ")
795 (let ((url (get-text-property (point) 'shr-url)))
796 (if (not url)
797 (message "No link under point")
798 (url-retrieve (shr-encode-url url)
799 'shr-store-contents (list url directory)
800 nil t))))
801
802 (defun shr-store-contents (status url directory)
803 (unless (plist-get status :error)
804 (when (or (search-forward "\n\n" nil t)
805 (search-forward "\r\n\r\n" nil t))
806 (write-region (point) (point-max)
807 (expand-file-name (file-name-nondirectory url)
808 directory)))))
809
810 (defun shr-image-fetched (status buffer start end &optional flags)
811 (let ((image-buffer (current-buffer)))
812 (when (and (buffer-name buffer)
813 (not (plist-get status :error)))
814 (url-store-in-cache image-buffer)
815 (when (or (search-forward "\n\n" nil t)
816 (search-forward "\r\n\r\n" nil t))
817 (let ((data (shr-parse-image-data)))
818 (with-current-buffer buffer
819 (save-excursion
820 (let ((alt (buffer-substring start end))
821 (properties (text-properties-at start))
822 (inhibit-read-only t))
823 (delete-region start end)
824 (goto-char start)
825 (funcall shr-put-image-function data alt flags)
826 (while properties
827 (let ((type (pop properties))
828 (value (pop properties)))
829 (unless (memq type '(display image-size))
830 (put-text-property start (point) type value))))))))))
831 (kill-buffer image-buffer)))
832
833 (defun shr-image-from-data (data)
834 "Return an image from the data: URI content DATA."
835 (when (string-match
836 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
837 data)
838 (let ((param (match-string 4 data))
839 (payload (url-unhex-string (match-string 5 data))))
840 (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
841 (setq payload (base64-decode-string payload)))
842 payload)))
843
844 ;; Behind display-graphic-p test.
845 (declare-function image-size "image.c" (spec &optional pixels frame))
846 (declare-function image-animate "image" (image &optional index limit))
847
848 (defun shr-put-image (spec alt &optional flags)
849 "Insert image SPEC with a string ALT. Return image.
850 SPEC is either an image data blob, or a list where the first
851 element is the data blob and the second element is the content-type."
852 (if (display-graphic-p)
853 (let* ((size (cdr (assq 'size flags)))
854 (data (if (consp spec)
855 (car spec)
856 spec))
857 (content-type (and (consp spec)
858 (cadr spec)))
859 (start (point))
860 (image (cond
861 ((eq size 'original)
862 (create-image data nil t :ascent 100
863 :format content-type))
864 ((eq content-type 'image/svg+xml)
865 (create-image data 'svg t :ascent 100))
866 ((eq size 'full)
867 (ignore-errors
868 (shr-rescale-image data content-type)))
869 (t
870 (ignore-errors
871 (shr-rescale-image data content-type))))))
872 (when image
873 ;; When inserting big-ish pictures, put them at the
874 ;; beginning of the line.
875 (when (and (> (current-column) 0)
876 (> (car (image-size image t)) 400))
877 (insert "\n"))
878 (if (eq size 'original)
879 (insert-sliced-image image (or alt "*") nil 20 1)
880 (insert-image image (or alt "*")))
881 (put-text-property start (point) 'image-size size)
882 (when (and shr-image-animate
883 (cond ((fboundp 'image-multi-frame-p)
884 ;; Only animate multi-frame things that specify a
885 ;; delay; eg animated gifs as opposed to
886 ;; multi-page tiffs. FIXME?
887 (cdr (image-multi-frame-p image)))
888 ((fboundp 'image-animated-p)
889 (image-animated-p image))))
890 (image-animate image nil 60)))
891 image)
892 (insert alt)))
893
894 (defun shr-rescale-image (data &optional content-type)
895 "Rescale DATA, if too big, to fit the current buffer."
896 (if (not (and (fboundp 'imagemagick-types)
897 (get-buffer-window (current-buffer))))
898 (create-image data nil t :ascent 100)
899 (let ((edges (window-inside-pixel-edges
900 (get-buffer-window (current-buffer)))))
901 (create-image
902 data 'imagemagick t
903 :ascent 100
904 :max-width (truncate (* shr-max-image-proportion
905 (- (nth 2 edges) (nth 0 edges))))
906 :max-height (truncate (* shr-max-image-proportion
907 (- (nth 3 edges) (nth 1 edges))))
908 :format content-type))))
909
910 ;; url-cache-extract autoloads url-cache.
911 (declare-function url-cache-create-filename "url-cache" (url))
912 (autoload 'mm-disable-multibyte "mm-util")
913 (autoload 'browse-url-mail "browse-url")
914
915 (defun shr-get-image-data (url)
916 "Get image data for URL.
917 Return a string with image data."
918 (with-temp-buffer
919 (mm-disable-multibyte)
920 (when (ignore-errors
921 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
922 t)
923 (when (or (search-forward "\n\n" nil t)
924 (search-forward "\r\n\r\n" nil t))
925 (shr-parse-image-data)))))
926
927 (defun shr-parse-image-data ()
928 (let ((data (buffer-substring (point) (point-max)))
929 (content-type
930 (save-excursion
931 (save-restriction
932 (narrow-to-region (point-min) (point))
933 (let ((content-type (mail-fetch-field "content-type")))
934 (and content-type
935 ;; Remove any comments in the type string.
936 (intern (replace-regexp-in-string ";.*" "" content-type)
937 obarray)))))))
938 ;; SVG images may contain references to further images that we may
939 ;; want to block. So special-case these by parsing the XML data
940 ;; and remove the blocked bits.
941 (when (eq content-type 'image/svg+xml)
942 (setq data
943 (shr-dom-to-xml
944 (libxml-parse-xml-region (point) (point-max)))))
945 (list data content-type)))
946
947 (defun shr-image-displayer (content-function)
948 "Return a function to display an image.
949 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
950 is an argument. The function to be returned takes three arguments URL,
951 START, and END. Note that START and END should be markers."
952 `(lambda (url start end)
953 (when url
954 (if (string-match "\\`cid:" url)
955 ,(when content-function
956 `(let ((image (funcall ,content-function
957 (substring url (match-end 0)))))
958 (when image
959 (goto-char start)
960 (funcall shr-put-image-function
961 image (buffer-substring start end))
962 (delete-region (point) end))))
963 (url-retrieve url 'shr-image-fetched
964 (list (current-buffer) start end)
965 t t)))))
966
967 (defun shr-heading (dom &rest types)
968 (shr-ensure-paragraph)
969 (apply #'shr-fontize-dom dom types)
970 (shr-ensure-paragraph))
971
972 (defun shr-urlify (start url &optional title)
973 (shr-add-font start (point) 'shr-link)
974 (add-text-properties
975 start (point)
976 (list 'shr-url url
977 'help-echo (let ((iri (or (ignore-errors
978 (decode-coding-string
979 (url-unhex-string url)
980 'utf-8 t))
981 url)))
982 (if title (format "%s (%s)" iri title) iri))
983 'follow-link t
984 'mouse-face 'highlight
985 'keymap shr-map)))
986
987 (defun shr-encode-url (url)
988 "Encode URL."
989 (browse-url-url-encode-chars url "[)$ ]"))
990
991 (autoload 'shr-color-visible "shr-color")
992 (autoload 'shr-color->hexadecimal "shr-color")
993
994 (defun shr-color-check (fg bg)
995 "Check that FG is visible on BG.
996 Returns (fg bg) with corrected values.
997 Returns nil if the colors that would be used are the default
998 ones, in case fg and bg are nil."
999 (when (or fg bg)
1000 (let ((fixed (cond ((null fg) 'fg)
1001 ((null bg) 'bg))))
1002 ;; Convert colors to hexadecimal, or set them to default.
1003 (let ((fg (or (shr-color->hexadecimal fg)
1004 (frame-parameter nil 'foreground-color)))
1005 (bg (or (shr-color->hexadecimal bg)
1006 (frame-parameter nil 'background-color))))
1007 (cond ((eq fixed 'bg)
1008 ;; Only return the new fg
1009 (list nil (cadr (shr-color-visible bg fg t))))
1010 ((eq fixed 'fg)
1011 ;; Invert args and results and return only the new bg
1012 (list (cadr (shr-color-visible fg bg t)) nil))
1013 (t
1014 (shr-color-visible bg fg)))))))
1015
1016 (defun shr-colorize-region (start end fg &optional bg)
1017 (when (and (not shr-inhibit-decoration)
1018 (or fg bg))
1019 (let ((new-colors (shr-color-check fg bg)))
1020 (when new-colors
1021 (when fg
1022 (add-face-text-property start end
1023 (list :foreground (cadr new-colors))
1024 t))
1025 (when bg
1026 (add-face-text-property start end
1027 (list :background (car new-colors))
1028 t)))
1029 new-colors)))
1030
1031 (defun shr-previous-newline-padding-width (width)
1032 (let ((overlays (overlays-at (point)))
1033 (previous-width 0))
1034 (if (null overlays)
1035 width
1036 (dolist (overlay overlays)
1037 (setq previous-width
1038 (+ previous-width
1039 (length (plist-get (overlay-properties overlay)
1040 'before-string)))))
1041 (+ width previous-width))))
1042
1043 ;;; Tag-specific rendering rules.
1044
1045 (defun shr-tag-body (dom)
1046 (let* ((start (point))
1047 (fgcolor (or (dom-attr dom 'fgcolor) (dom-attr dom 'text)))
1048 (bgcolor (dom-attr dom 'bgcolor))
1049 (shr-stylesheet (list (cons 'color fgcolor)
1050 (cons 'background-color bgcolor))))
1051 (shr-generic dom)
1052 (shr-colorize-region start (point) fgcolor bgcolor)))
1053
1054 (defun shr-tag-style (_dom)
1055 )
1056
1057 (defun shr-tag-script (_dom)
1058 )
1059
1060 (defun shr-tag-comment (_dom)
1061 )
1062
1063 (defun shr-dom-to-xml (dom)
1064 (with-temp-buffer
1065 (shr-dom-print dom)
1066 (buffer-string)))
1067
1068 (defun shr-dom-print (dom)
1069 "Convert DOM into a string containing the xml representation."
1070 (insert (format "<%s" (dom-tag dom)))
1071 (dolist (attr (dom-attributes dom))
1072 ;; Ignore attributes that start with a colon because they are
1073 ;; private elements.
1074 (unless (= (aref (format "%s" (car attr)) 0) ?:)
1075 (insert (format " %s=\"%s\"" (car attr) (cdr attr)))))
1076 (insert ">")
1077 (let (url)
1078 (dolist (elem (dom-children dom))
1079 (cond
1080 ((stringp elem)
1081 (insert elem))
1082 ((eq (dom-tag elem) 'comment)
1083 )
1084 ((or (not (eq (dom-tag elem) 'image))
1085 ;; Filter out blocked elements inside the SVG image.
1086 (not (setq url (dom-attr elem ':xlink:href)))
1087 (not shr-blocked-images)
1088 (not (string-match shr-blocked-images url)))
1089 (insert " ")
1090 (shr-dom-print elem)))))
1091 (insert (format "</%s>" (dom-tag dom))))
1092
1093 (defun shr-tag-svg (dom)
1094 (when (and (image-type-available-p 'svg)
1095 (not shr-inhibit-images))
1096 (funcall shr-put-image-function (list (shr-dom-to-xml dom) 'image/svg+xml)
1097 "SVG Image")))
1098
1099 (defun shr-tag-sup (dom)
1100 (let ((start (point)))
1101 (shr-generic dom)
1102 (put-text-property start (point) 'display '(raise 0.5))))
1103
1104 (defun shr-tag-sub (dom)
1105 (let ((start (point)))
1106 (shr-generic dom)
1107 (put-text-property start (point) 'display '(raise -0.5))))
1108
1109 (defun shr-tag-label (dom)
1110 (shr-generic dom)
1111 (shr-ensure-paragraph))
1112
1113 (defun shr-tag-p (dom)
1114 (shr-ensure-paragraph)
1115 (shr-generic dom)
1116 (shr-ensure-paragraph))
1117
1118 (defun shr-tag-div (dom)
1119 (shr-ensure-newline)
1120 (shr-generic dom)
1121 (shr-ensure-newline))
1122
1123 (defun shr-tag-s (dom)
1124 (shr-fontize-dom dom 'shr-strike-through))
1125
1126 (defun shr-tag-del (dom)
1127 (shr-fontize-dom dom 'shr-strike-through))
1128
1129 (defun shr-tag-b (dom)
1130 (shr-fontize-dom dom 'bold))
1131
1132 (defun shr-tag-i (dom)
1133 (shr-fontize-dom dom 'italic))
1134
1135 (defun shr-tag-em (dom)
1136 (shr-fontize-dom dom 'italic))
1137
1138 (defun shr-tag-strong (dom)
1139 (shr-fontize-dom dom 'bold))
1140
1141 (defun shr-tag-u (dom)
1142 (shr-fontize-dom dom 'underline))
1143
1144 (defun shr-tag-tt (dom)
1145 (let ((shr-current-font 'default))
1146 (shr-generic dom)))
1147
1148 (defun shr-parse-style (style)
1149 (when style
1150 (save-match-data
1151 (when (string-match "\n" style)
1152 (setq style (replace-match " " t t style))))
1153 (let ((plist nil))
1154 (dolist (elem (split-string style ";"))
1155 (when elem
1156 (setq elem (split-string elem ":"))
1157 (when (and (car elem)
1158 (cadr elem))
1159 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1160 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1161 (when (string-match " *!important\\'" value)
1162 (setq value (substring value 0 (match-beginning 0))))
1163 (unless (equal value "inherit")
1164 (push (cons (intern name obarray)
1165 value)
1166 plist))))))
1167 plist)))
1168
1169 (defun shr-tag-base (dom)
1170 (when-let (base (dom-attr dom 'href))
1171 (setq shr-base (shr-parse-base base)))
1172 (shr-generic dom))
1173
1174 (defun shr-tag-a (dom)
1175 (let ((url (dom-attr dom 'href))
1176 (title (dom-attr dom 'title))
1177 (start (point))
1178 shr-start)
1179 (shr-generic dom)
1180 (when (and shr-target-id
1181 (equal (dom-attr dom 'name) shr-target-id))
1182 ;; We have a zero-length <a name="foo"> element, so just
1183 ;; insert... something.
1184 (when (= start (point))
1185 (shr-ensure-newline)
1186 (insert " "))
1187 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
1188 (when (and url
1189 (not shr-inhibit-decoration))
1190 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1191
1192 (defun shr-tag-object (dom)
1193 (unless shr-inhibit-images
1194 (let ((start (point))
1195 url multimedia image)
1196 (when-let (type (dom-attr dom 'type))
1197 (when (string-match "\\`image/svg" type)
1198 (setq url (dom-attr dom 'data)
1199 image t)))
1200 (dolist (child (dom-non-text-children dom))
1201 (cond
1202 ((eq (dom-tag child) 'embed)
1203 (setq url (or url (dom-attr child 'src))
1204 multimedia t))
1205 ((and (eq (dom-tag child) 'param)
1206 (equal (dom-attr child 'name) "movie"))
1207 (setq url (or url (dom-attr child 'value))
1208 multimedia t))))
1209 (when url
1210 (cond
1211 (image
1212 (shr-tag-img dom url)
1213 (setq dom nil))
1214 (multimedia
1215 (shr-insert " [multimedia] ")
1216 (shr-urlify start (shr-expand-url url)))))
1217 (when dom
1218 (shr-generic dom)))))
1219
1220 (defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
1221 ("ogv" . 1.0)
1222 ("ogg" . 1.0)
1223 ("opus" . 1.0)
1224 ("flac" . 0.9)
1225 ("wav" . 0.5))
1226 "Preferences for media types.
1227 The key element should be a regexp matched against the type of the source or
1228 url if no type is specified. The value should be a float in the range 0.0 to
1229 1.0. Media elements with higher value are preferred."
1230 :version "24.4"
1231 :group 'shr
1232 :type '(alist :key-type regexp :value-type float))
1233
1234 (defun shr--get-media-pref (elem)
1235 "Determine the preference for ELEM.
1236 The preference is a float determined from `shr-prefer-media-type'."
1237 (let ((type (dom-attr elem 'type))
1238 (p 0.0))
1239 (unless type
1240 (setq type (dom-attr elem 'src)))
1241 (when type
1242 (dolist (pref shr-prefer-media-type-alist)
1243 (when (and
1244 (> (cdr pref) p)
1245 (string-match-p (car pref) type))
1246 (setq p (cdr pref)))))
1247 p))
1248
1249 (defun shr--extract-best-source (dom &optional url pref)
1250 "Extract the best `:src' property from <source> blocks in DOM."
1251 (setq pref (or pref -1.0))
1252 (let (new-pref)
1253 (dolist (elem (dom-non-text-children dom))
1254 (when (and (eq (dom-tag elem) 'source)
1255 (< pref
1256 (setq new-pref
1257 (shr--get-media-pref elem))))
1258 (setq pref new-pref
1259 url (dom-attr elem 'src))
1260 ;; libxml's html parser isn't HTML5 compliant and non terminated
1261 ;; source tags might end up as children. So recursion it is...
1262 (dolist (child (dom-non-text-children elem))
1263 (when (eq (dom-tag child) 'source)
1264 (let ((ret (shr--extract-best-source (list child) url pref)))
1265 (when (< pref (cdr ret))
1266 (setq url (car ret)
1267 pref (cdr ret)))))))))
1268 (cons url pref))
1269
1270 (defun shr-tag-video (dom)
1271 (let ((image (dom-attr dom 'poster))
1272 (url (dom-attr dom 'src))
1273 (start (point)))
1274 (unless url
1275 (setq url (car (shr--extract-best-source dom))))
1276 (if image
1277 (shr-tag-img nil image)
1278 (shr-insert " [video] "))
1279 (shr-urlify start (shr-expand-url url))))
1280
1281 (defun shr-tag-audio (dom)
1282 (let ((url (dom-attr dom 'src))
1283 (start (point)))
1284 (unless url
1285 (setq url (car (shr--extract-best-source dom))))
1286 (shr-insert " [audio] ")
1287 (shr-urlify start (shr-expand-url url))))
1288
1289 (defun shr-tag-img (dom &optional url)
1290 (when (or url
1291 (and dom
1292 (> (length (dom-attr dom 'src)) 0)))
1293 (when (> (current-column) 0)
1294 (insert "\n"))
1295 (let ((alt (dom-attr dom 'alt))
1296 (url (shr-expand-url (or url (dom-attr dom 'src)))))
1297 (let ((start (point-marker)))
1298 (when (zerop (length alt))
1299 (setq alt "*"))
1300 (cond
1301 ((or (member (dom-attr dom 'height) '("0" "1"))
1302 (member (dom-attr dom 'width) '("0" "1")))
1303 ;; Ignore zero-sized or single-pixel images.
1304 )
1305 ((and (not shr-inhibit-images)
1306 (string-match "\\`data:" url))
1307 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1308 (if image
1309 (funcall shr-put-image-function image alt)
1310 (insert alt))))
1311 ((and (not shr-inhibit-images)
1312 (string-match "\\`cid:" url))
1313 (let ((url (substring url (match-end 0)))
1314 image)
1315 (if (or (not shr-content-function)
1316 (not (setq image (funcall shr-content-function url))))
1317 (insert alt)
1318 (funcall shr-put-image-function image alt))))
1319 ((or shr-inhibit-images
1320 (and shr-blocked-images
1321 (string-match shr-blocked-images url)))
1322 (setq shr-start (point))
1323 (if (> (string-width alt) 8)
1324 (shr-insert (truncate-string-to-width alt 8))
1325 (shr-insert alt)))
1326 ((and (not shr-ignore-cache)
1327 (url-is-cached (shr-encode-url url)))
1328 (funcall shr-put-image-function (shr-get-image-data url) alt))
1329 (t
1330 (insert alt " ")
1331 (when (and shr-ignore-cache
1332 (url-is-cached (shr-encode-url url)))
1333 (let ((file (url-cache-create-filename (shr-encode-url url))))
1334 (when (file-exists-p file)
1335 (delete-file file))))
1336 (url-queue-retrieve
1337 (shr-encode-url url) 'shr-image-fetched
1338 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1339 t t)))
1340 (when (zerop shr-table-depth) ;; We are not in a table.
1341 (put-text-property start (point) 'keymap shr-map)
1342 (put-text-property start (point) 'shr-alt alt)
1343 (put-text-property start (point) 'image-url url)
1344 (put-text-property start (point) 'image-displayer
1345 (shr-image-displayer shr-content-function))
1346 (put-text-property start (point) 'help-echo
1347 (shr-fill-text
1348 (or (dom-attr dom 'title) alt))))))))
1349
1350 (defun shr-tag-pre (dom)
1351 (let ((shr-folding-mode 'none)
1352 (shr-current-font 'default))
1353 (shr-ensure-newline)
1354 (shr-generic dom)
1355 (shr-ensure-newline)))
1356
1357 (defun shr-tag-blockquote (dom)
1358 (shr-ensure-paragraph)
1359 (let ((start (point))
1360 (shr-indentation (+ shr-indentation
1361 (* 4 shr-table-separator-pixel-width))))
1362 (shr-generic dom)
1363 (shr-ensure-paragraph)
1364 (shr-mark-fill start)))
1365
1366 (defun shr-tag-dl (dom)
1367 (shr-ensure-paragraph)
1368 (shr-generic dom)
1369 (shr-ensure-paragraph))
1370
1371 (defun shr-tag-dt (dom)
1372 (shr-ensure-newline)
1373 (shr-generic dom)
1374 (shr-ensure-newline))
1375
1376 (defun shr-tag-dd (dom)
1377 (shr-ensure-newline)
1378 (let ((shr-indentation (+ shr-indentation
1379 (* 4 shr-table-separator-pixel-width))))
1380 (shr-generic dom)))
1381
1382 (defun shr-tag-ul (dom)
1383 (shr-ensure-paragraph)
1384 (let ((shr-list-mode 'ul))
1385 (shr-generic dom))
1386 (shr-ensure-paragraph))
1387
1388 (defun shr-tag-ol (dom)
1389 (shr-ensure-paragraph)
1390 (let ((shr-list-mode 1))
1391 (shr-generic dom))
1392 (shr-ensure-paragraph))
1393
1394 (defun shr-tag-li (dom)
1395 (shr-ensure-newline)
1396 (let ((start (point)))
1397 (let* ((bullet
1398 (if (numberp shr-list-mode)
1399 (prog1
1400 (format "%d " shr-list-mode)
1401 (setq shr-list-mode (1+ shr-list-mode)))
1402 shr-bullet)))
1403 (insert bullet)
1404 (shr-mark-fill start)
1405 (let ((shr-indentation (+ shr-indentation
1406 (shr-string-pixel-width bullet))))
1407 (put-text-property start (1+ start)
1408 'shr-continuation-indentation shr-indentation)
1409 (shr-generic dom)))))
1410
1411 (defun shr-mark-fill (start)
1412 ;; We may not have inserted any text to fill.
1413 (unless (= start (point))
1414 (put-text-property start (1+ start)
1415 'shr-indentation shr-indentation)))
1416
1417 (defun shr-tag-br (dom)
1418 (when (and (not (bobp))
1419 ;; Only add a newline if we break the current line, or
1420 ;; the previous line isn't a blank line.
1421 (or (not (bolp))
1422 (and (> (- (point) 2) (point-min))
1423 (not (= (char-after (- (point) 2)) ?\n)))))
1424 (insert "\n"))
1425 (shr-generic dom))
1426
1427 (defun shr-tag-span (dom)
1428 (shr-generic dom))
1429
1430 (defun shr-tag-h1 (dom)
1431 (shr-heading dom (if shr-use-fonts
1432 '(variable-pitch (:height 1.3 :weight bold))
1433 'bold)))
1434
1435 (defun shr-tag-h2 (dom)
1436 (shr-heading dom 'bold))
1437
1438 (defun shr-tag-h3 (dom)
1439 (shr-heading dom 'italic))
1440
1441 (defun shr-tag-h4 (dom)
1442 (shr-heading dom))
1443
1444 (defun shr-tag-h5 (dom)
1445 (shr-heading dom))
1446
1447 (defun shr-tag-h6 (dom)
1448 (shr-heading dom))
1449
1450 (defun shr-tag-hr (_dom)
1451 (shr-ensure-newline)
1452 (insert (make-string (if (not shr-use-fonts)
1453 shr-internal-width
1454 (1+ (/ shr-internal-width
1455 shr-table-separator-pixel-width)))
1456 shr-hr-line)
1457 "\n"))
1458
1459 (defun shr-tag-title (dom)
1460 (shr-heading dom 'bold 'underline))
1461
1462 (defun shr-tag-font (dom)
1463 (let* ((start (point))
1464 (color (dom-attr dom 'color))
1465 (shr-stylesheet (nconc (list (cons 'color color))
1466 shr-stylesheet)))
1467 (shr-generic dom)
1468 (when color
1469 (shr-colorize-region start (point) color
1470 (cdr (assq 'background-color shr-stylesheet))))))
1471
1472 ;;; Table rendering algorithm.
1473
1474 ;; Table rendering is the only complicated thing here. We do this by
1475 ;; first counting how many TDs there are in each TR, and registering
1476 ;; how wide they think they should be ("width=45%", etc). Then we
1477 ;; render each TD separately (this is done in temporary buffers, so
1478 ;; that we can use all the rendering machinery as if we were in the
1479 ;; main buffer). Now we know how much space each TD really takes, so
1480 ;; we then render everything again with the new widths, and finally
1481 ;; insert all these boxes into the main buffer.
1482 (defun shr-tag-table-1 (dom)
1483 (setq dom (or (dom-child-by-tag dom 'tbody) dom))
1484 (let* ((shr-inhibit-images t)
1485 (shr-table-depth (1+ shr-table-depth))
1486 (shr-kinsoku-shorten t)
1487 ;; Find all suggested widths.
1488 (columns (shr-column-specs dom))
1489 ;; Compute how many pixels wide each TD should be.
1490 (suggested-widths (shr-pro-rate-columns columns))
1491 ;; Do a "test rendering" to see how big each TD is (this can
1492 ;; be smaller (if there's little text) or bigger (if there's
1493 ;; unbreakable text).
1494 (elems (or (dom-attr dom 'shr-suggested-widths)
1495 (shr-make-table dom suggested-widths nil
1496 'shr-suggested-widths)))
1497 (sketch (loop for line in elems
1498 collect (mapcar #'car line)))
1499 (natural (loop for line in elems
1500 collect (mapcar #'cdr line)))
1501 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1502 ;; This probably won't work very well.
1503 (when (> (+ (loop for width across sketch-widths
1504 summing (1+ width))
1505 shr-indentation shr-table-separator-pixel-width)
1506 (frame-width))
1507 (setq truncate-lines t))
1508 ;; Then render the table again with these new "hard" widths.
1509 (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
1510
1511 (defun shr-tag-table (dom)
1512 (shr-ensure-paragraph)
1513 (let* ((caption (dom-children (dom-child-by-tag dom 'caption)))
1514 (header (dom-non-text-children (dom-child-by-tag dom 'thead)))
1515 (body (dom-non-text-children (or (dom-child-by-tag dom 'tbody)
1516 dom)))
1517 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot)))
1518 (bgcolor (dom-attr dom 'bgcolor))
1519 (start (point))
1520 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1521 shr-stylesheet))
1522 (nheader (if header (shr-max-columns header)))
1523 (nbody (if body (shr-max-columns body)))
1524 (nfooter (if footer (shr-max-columns footer))))
1525 (if (and (not caption)
1526 (not header)
1527 (not (dom-child-by-tag dom 'tbody))
1528 (not (dom-child-by-tag dom 'tr))
1529 (not footer))
1530 ;; The table is totally invalid and just contains random junk.
1531 ;; Try to output it anyway.
1532 (shr-generic dom)
1533 ;; It's a real table, so render it.
1534 (if (dom-attr dom 'shr-fixed-table)
1535 (shr-tag-table-1 dom)
1536 ;; Only fix up the table once.
1537 (let ((table
1538 (nconc
1539 (list 'table nil)
1540 (if caption `((tr nil (td nil ,@caption))))
1541 (cond
1542 (header
1543 (if footer
1544 ;; header + body + footer
1545 (if (= nheader nbody)
1546 (if (= nbody nfooter)
1547 `((tr nil (td nil (table nil
1548 (tbody nil ,@header
1549 ,@body ,@footer)))))
1550 (nconc `((tr nil (td nil (table nil
1551 (tbody nil ,@header
1552 ,@body)))))
1553 (if (= nfooter 1)
1554 footer
1555 `((tr nil (td nil (table
1556 nil (tbody
1557 nil ,@footer))))))))
1558 (nconc `((tr nil (td nil (table nil (tbody
1559 nil ,@header)))))
1560 (if (= nbody nfooter)
1561 `((tr nil (td nil (table
1562 nil (tbody nil ,@body
1563 ,@footer)))))
1564 (nconc `((tr nil (td nil (table
1565 nil (tbody nil
1566 ,@body)))))
1567 (if (= nfooter 1)
1568 footer
1569 `((tr nil (td nil (table
1570 nil
1571 (tbody
1572 nil
1573 ,@footer))))))))))
1574 ;; header + body
1575 (if (= nheader nbody)
1576 `((tr nil (td nil (table nil (tbody nil ,@header
1577 ,@body)))))
1578 (if (= nheader 1)
1579 `(,@header (tr nil (td nil (table
1580 nil (tbody nil ,@body)))))
1581 `((tr nil (td nil (table nil (tbody nil ,@header))))
1582 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
1583 (footer
1584 ;; body + footer
1585 (if (= nbody nfooter)
1586 `((tr nil (td nil (table
1587 nil (tbody nil ,@body ,@footer)))))
1588 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
1589 (if (= nfooter 1)
1590 footer
1591 `((tr nil (td nil (table
1592 nil (tbody nil ,@footer)))))))))
1593 (caption
1594 `((tr nil (td nil (table nil (tbody nil ,@body))))))
1595 (body)))))
1596 (dom-set-attribute table 'shr-fixed-table t)
1597 (setcdr dom (cdr table))
1598 (shr-tag-table-1 dom))))
1599 (when bgcolor
1600 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1601 bgcolor))
1602 ;; Finally, insert all the images after the table. The Emacs buffer
1603 ;; model isn't strong enough to allow us to put the images actually
1604 ;; into the tables.
1605 (when (zerop shr-table-depth)
1606 (save-excursion
1607 (shr-expand-alignments start (point)))
1608 (dolist (elem (dom-by-tag dom 'object))
1609 (shr-tag-object elem))
1610 (dolist (elem (dom-by-tag dom 'img))
1611 (shr-tag-img elem)))))
1612
1613 (defun shr-insert-table (table widths)
1614 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1615 "collapse"))
1616 (shr-table-separator-length (if collapse 0 1))
1617 (shr-table-vertical-line (if collapse "" shr-table-vertical-line))
1618 (start (point)))
1619 (setq shr-table-id (1+ shr-table-id))
1620 (unless collapse
1621 (shr-insert-table-ruler widths))
1622 (dolist (row table)
1623 (let ((start (point))
1624 (align 0)
1625 (column-number 0)
1626 (height (let ((max 0))
1627 (dolist (column row)
1628 (setq max (max max (nth 2 column))))
1629 max)))
1630 (dotimes (i (max height 1))
1631 (shr-indent)
1632 (insert shr-table-vertical-line "\n"))
1633 (dolist (column row)
1634 (when (> (nth 2 column) -1)
1635 (goto-char start)
1636 ;; Sum up all the widths from the column. (There may be
1637 ;; more than one if this is a "colspan" column.)
1638 (dotimes (i (nth 4 column))
1639 ;; The colspan directive may be wrong and there may not be
1640 ;; that number of columns.
1641 (when (<= column-number (1- (length widths)))
1642 (setq align (+ align
1643 (aref widths column-number)
1644 (* 2 shr-table-separator-pixel-width))))
1645 (setq column-number (1+ column-number)))
1646 (let ((lines (nth 3 column))
1647 (pixel-align (if (not shr-use-fonts)
1648 (* align (frame-char-width))
1649 align)))
1650 (dolist (line lines)
1651 (end-of-line)
1652 (let ((start (point)))
1653 (insert
1654 line
1655 (propertize " "
1656 'display `(space :align-to (,pixel-align))
1657 'face (and (> (length line) 0)
1658 (shr-face-background
1659 (get-text-property
1660 (1- (length line)) 'face line)))
1661 'shr-table-indent shr-table-id)
1662 shr-table-vertical-line)
1663 (shr-colorize-region
1664 start (1- (point)) (nth 5 column) (nth 6 column)))
1665 (forward-line 1))
1666 ;; Add blank lines at padding at the bottom of the TD,
1667 ;; possibly.
1668 (dotimes (i (- height (length lines)))
1669 (end-of-line)
1670 (let ((start (point)))
1671 (insert (propertize " "
1672 'display `(space :align-to (,pixel-align))
1673 'shr-table-indent shr-table-id)
1674 shr-table-vertical-line)
1675 (shr-colorize-region
1676 start (1- (point)) (nth 5 column) (nth 6 column)))
1677 (forward-line 1))))))
1678 (unless collapse
1679 (shr-insert-table-ruler widths)))
1680 (unless (= start (point))
1681 (put-text-property start (1+ start) 'shr-table-id shr-table-id))))
1682
1683 (defun shr-face-background (face)
1684 (and (consp face)
1685 (let ((background nil))
1686 (dolist (elem face)
1687 (when (and (consp elem)
1688 (eq (car elem) :background))
1689 (setq background (cadr elem))))
1690 (and background
1691 (list :background background)))))
1692
1693 (defun shr-expand-alignments (start end)
1694 (while (< (setq start (next-single-property-change
1695 start 'shr-table-id nil end))
1696 end)
1697 (goto-char start)
1698 (let* ((shr-use-fonts t)
1699 (id (get-text-property (point) 'shr-table-id))
1700 (base (shr-pixel-column))
1701 elem)
1702 (when id
1703 (save-excursion
1704 (while (setq elem (text-property-any
1705 (point) end 'shr-table-indent id))
1706 (goto-char elem)
1707 (let ((align (get-text-property (point) 'display)))
1708 (put-text-property (point) (1+ (point)) 'display
1709 `(space :align-to (,(+ (car (nth 2 align))
1710 base)))))
1711 (forward-char 1)))))
1712 (setq start (1+ start))))
1713
1714 (defun shr-insert-table-ruler (widths)
1715 (when shr-table-horizontal-line
1716 (when (and (bolp)
1717 (> shr-indentation 0))
1718 (shr-indent))
1719 (insert shr-table-corner)
1720 (let ((total-width 0))
1721 (dotimes (i (length widths))
1722 (setq total-width (+ total-width (aref widths i)
1723 (* shr-table-separator-pixel-width 2)))
1724 (insert (make-string (1+ (/ (aref widths i)
1725 shr-table-separator-pixel-width))
1726 shr-table-horizontal-line)
1727 (propertize " "
1728 'display `(space :align-to (,total-width))
1729 'shr-table-indent shr-table-id)
1730 shr-table-corner)))
1731 (insert "\n")))
1732
1733 (defun shr-table-widths (table natural-table suggested-widths)
1734 (let* ((length (length suggested-widths))
1735 (widths (make-vector length 0))
1736 (natural-widths (make-vector length 0)))
1737 (dolist (row table)
1738 (let ((i 0))
1739 (dolist (column row)
1740 (aset widths i (max (aref widths i) column))
1741 (setq i (1+ i)))))
1742 (dolist (row natural-table)
1743 (let ((i 0))
1744 (dolist (column row)
1745 (aset natural-widths i (max (aref natural-widths i) column))
1746 (setq i (1+ i)))))
1747 (let ((extra (- (apply '+ (append suggested-widths nil))
1748 (apply '+ (append widths nil))
1749 (* shr-table-separator-pixel-width (1+ (length widths)))))
1750 (expanded-columns 0))
1751 ;; We have extra, unused space, so divide this space amongst the
1752 ;; columns.
1753 (when (> extra 0)
1754 ;; If the natural width is wider than the rendered width, we
1755 ;; want to allow the column to expand.
1756 (dotimes (i length)
1757 (when (> (aref natural-widths i) (aref widths i))
1758 (setq expanded-columns (1+ expanded-columns))))
1759 (dotimes (i length)
1760 (when (> (aref natural-widths i) (aref widths i))
1761 (aset widths i (min
1762 (aref natural-widths i)
1763 (+ (/ extra expanded-columns)
1764 (aref widths i))))))))
1765 widths))
1766
1767 (defun shr-make-table (dom widths &optional fill storage-attribute)
1768 (or (cadr (assoc (list dom widths fill) shr-content-cache))
1769 (let ((data (shr-make-table-1 dom widths fill)))
1770 (push (list (list dom widths fill) data)
1771 shr-content-cache)
1772 (when storage-attribute
1773 (dom-set-attribute dom storage-attribute data))
1774 data)))
1775
1776 (defun shr-make-table-1 (dom widths &optional fill)
1777 (let ((trs nil)
1778 (shr-inhibit-decoration (not fill))
1779 (rowspans (make-vector (length widths) 0))
1780 (colspan-remaining 0)
1781 colspan-width colspan-count
1782 width colspan)
1783 (dolist (row (dom-non-text-children dom))
1784 (when (eq (dom-tag row) 'tr)
1785 (let ((tds nil)
1786 (columns (dom-non-text-children row))
1787 (i 0)
1788 (width-column 0)
1789 column)
1790 (while (< i (length widths))
1791 ;; If we previously had a rowspan definition, then that
1792 ;; means that we now have a "missing" td/th element here.
1793 ;; So just insert a dummy, empty one to (sort of) emulate
1794 ;; rowspan.
1795 (setq column
1796 (if (zerop (aref rowspans i))
1797 (pop columns)
1798 (aset rowspans i (1- (aref rowspans i)))
1799 '(td)))
1800 (when (and (not (stringp column))
1801 (or (memq (dom-tag column) '(td th))
1802 (not column)))
1803 (when-let (span (dom-attr column 'rowspan))
1804 (aset rowspans i (+ (aref rowspans i)
1805 (1- (string-to-number span)))))
1806 ;; Sanity check for invalid column-spans.
1807 (when (>= width-column (length widths))
1808 (setq width-column 0))
1809 (setq width
1810 (if column
1811 (aref widths width-column)
1812 (* 10 shr-table-separator-pixel-width)))
1813 (when (setq colspan (dom-attr column 'colspan))
1814 (setq colspan (min (string-to-number colspan)
1815 ;; The colspan may be wrong, so
1816 ;; truncate it to the length of the
1817 ;; remaining columns.
1818 (- (length widths) i)))
1819 (dotimes (j (1- colspan))
1820 (setq width
1821 (if (> (+ i 1 j) (1- (length widths)))
1822 ;; If we have a colspan spec that's longer
1823 ;; than the table is wide, just use the last
1824 ;; width as the width.
1825 (aref widths (1- (length widths)))
1826 ;; Sum up the widths of the columns we're
1827 ;; spanning.
1828 (+ width
1829 shr-table-separator-length
1830 (aref widths (+ i 1 j))))))
1831 (setq width-column (+ width-column (1- colspan))
1832 colspan-count colspan
1833 colspan-remaining colspan))
1834 (when column
1835 (let ((data (shr-render-td column width fill)))
1836 (if (and (not fill)
1837 (> colspan-remaining 0))
1838 (progn
1839 (setq colspan-width (car data))
1840 (let ((this-width (/ colspan-width colspan-count)))
1841 (push (cons this-width (cadr data)) tds)
1842 (setq colspan-remaining (1- colspan-remaining))))
1843 (if (not fill)
1844 (push (cons (car data) (cadr data)) tds)
1845 (push data tds)))))
1846 (when (and colspan
1847 (> colspan 1))
1848 (dotimes (c (1- colspan))
1849 (setq i (1+ i))
1850 (push
1851 (if fill
1852 (list 0 0 -1 nil 1 nil nil)
1853 '(0 . 0))
1854 tds)))
1855 (setq i (1+ i)
1856 width-column (1+ width-column))))
1857 (push (nreverse tds) trs))))
1858 (nreverse trs)))
1859
1860 (defun shr-pixel-buffer-width ()
1861 (if (not shr-use-fonts)
1862 (save-excursion
1863 (goto-char (point-min))
1864 (let ((max 0))
1865 (while (not (eobp))
1866 (end-of-line)
1867 (setq max (max max (current-column)))
1868 (forward-line 1))
1869 max))
1870 (if (get-buffer-window)
1871 (car (window-text-pixel-size nil (point-min) (point-max)))
1872 (save-window-excursion
1873 (set-window-buffer nil (current-buffer))
1874 (car (window-text-pixel-size nil (point-min) (point-max)))))))
1875
1876 (defun shr-render-td (dom width fill)
1877 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill))))
1878 (or (dom-attr dom cache)
1879 (and fill
1880 (let (result)
1881 (dolist (attr (dom-attributes dom))
1882 (let ((name (symbol-name (car attr))))
1883 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name)
1884 (let ((cache-width (string-to-number
1885 (match-string 1 name))))
1886 (when (and (>= cache-width width)
1887 (<= (car (cdr attr)) width))
1888 (setq result (cdr attr)))))))
1889 result))
1890 (let ((result (shr-render-td-1 dom width fill)))
1891 (dom-set-attribute dom cache result)
1892 result))))
1893
1894 (defun shr-render-td-1 (dom width fill)
1895 (with-temp-buffer
1896 (let ((bgcolor (dom-attr dom 'bgcolor))
1897 (fgcolor (dom-attr dom 'fgcolor))
1898 (style (dom-attr dom 'style))
1899 (shr-stylesheet shr-stylesheet)
1900 (max-width 0)
1901 natural-width)
1902 (when style
1903 (setq style (and (string-match "color" style)
1904 (shr-parse-style style))))
1905 (when bgcolor
1906 (setq style (nconc (list (cons 'background-color bgcolor))
1907 style)))
1908 (when fgcolor
1909 (setq style (nconc (list (cons 'color fgcolor)) style)))
1910 (when style
1911 (setq shr-stylesheet (append style shr-stylesheet)))
1912 (let ((shr-internal-width width)
1913 (shr-indentation 0))
1914 (shr-descend dom))
1915 (save-window-excursion
1916 (set-window-buffer nil (current-buffer))
1917 (unless fill
1918 (setq natural-width
1919 (or (dom-attr dom 'shr-td-cache-natural)
1920 (let ((natural (max (shr-pixel-buffer-width)
1921 (shr-dom-max-natural-width dom 0))))
1922 (dom-set-attribute dom 'shr-td-cache-natural natural)
1923 natural))))
1924 (if (and natural-width
1925 (<= natural-width width))
1926 (setq max-width natural-width)
1927 (let ((shr-internal-width width))
1928 (shr-fill-lines (point-min) (point-max))
1929 (setq max-width (shr-pixel-buffer-width)))))
1930 (goto-char (point-max))
1931 ;; Delete padding at the bottom of the TDs.
1932 (delete-region
1933 (point)
1934 (progn
1935 (skip-chars-backward " \t\n")
1936 (end-of-line)
1937 (point)))
1938 (goto-char (point-min))
1939 (list max-width
1940 natural-width
1941 (count-lines (point-min) (point-max))
1942 (split-string (buffer-string) "\n")
1943 (if (dom-attr dom 'colspan)
1944 (string-to-number (dom-attr dom 'colspan))
1945 1)
1946 (cdr (assq 'color shr-stylesheet))
1947 (cdr (assq 'background-color shr-stylesheet))))))
1948
1949 (defun shr-dom-max-natural-width (dom max)
1950 (if (eq (dom-tag dom) 'table)
1951 (max max (or
1952 (loop for line in (dom-attr dom 'shr-suggested-widths)
1953 maximize (+
1954 shr-table-separator-length
1955 (loop for elem in line
1956 summing
1957 (+ (cdr elem)
1958 (* 2 shr-table-separator-length)))))
1959 0))
1960 (dolist (child (dom-children dom))
1961 (unless (stringp child)
1962 (setq max (max (shr-dom-max-natural-width child max)))))
1963 max))
1964
1965 (defun shr-buffer-width ()
1966 (goto-char (point-min))
1967 (let ((max 0))
1968 (while (not (eobp))
1969 (end-of-line)
1970 (setq max (max max (current-column)))
1971 (forward-line 1))
1972 max))
1973
1974 (defun shr-pro-rate-columns (columns)
1975 (let ((total-percentage 0)
1976 (widths (make-vector (length columns) 0)))
1977 (dotimes (i (length columns))
1978 (setq total-percentage (+ total-percentage (aref columns i))))
1979 (setq total-percentage (/ 1.0 total-percentage))
1980 (dotimes (i (length columns))
1981 (aset widths i (max (truncate (* (aref columns i)
1982 total-percentage
1983 (- shr-internal-width
1984 (* (1+ (length columns))
1985 shr-table-separator-pixel-width))))
1986 10)))
1987 widths))
1988
1989 ;; Return a summary of the number and shape of the TDs in the table.
1990 (defun shr-column-specs (dom)
1991 (let ((columns (make-vector (shr-max-columns dom) 1)))
1992 (dolist (row (dom-non-text-children dom))
1993 (when (eq (dom-tag row) 'tr)
1994 (let ((i 0))
1995 (dolist (column (dom-non-text-children row))
1996 (when (memq (dom-tag column) '(td th))
1997 (let ((width (dom-attr column 'width)))
1998 (when (and width
1999 (string-match "\\([0-9]+\\)%" width)
2000 (not (zerop (setq width (string-to-number
2001 (match-string 1 width))))))
2002 (aset columns i (/ width 100.0))))
2003 (setq i (1+ i)))))))
2004 columns))
2005
2006 (defun shr-count (dom elem)
2007 (let ((i 0))
2008 (dolist (sub (dom-children dom))
2009 (when (and (not (stringp sub))
2010 (eq (dom-tag sub) elem))
2011 (setq i (1+ i))))
2012 i))
2013
2014 (defun shr-max-columns (dom)
2015 (let ((max 0))
2016 (dolist (row (dom-children dom))
2017 (when (and (not (stringp row))
2018 (eq (dom-tag row) 'tr))
2019 (setq max (max max (+ (shr-count row 'td)
2020 (shr-count row 'th))))))
2021 max))
2022
2023 (provide 'shr)
2024
2025 ;; Local Variables:
2026 ;; coding: utf-8
2027 ;; End:
2028
2029 ;;; shr.el ends here