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