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