]> code.delx.au - gnu-emacs/blob - lisp/net/shr.el
Merge branch 'master' into cairo
[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 t
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-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)
160
161 (defvar shr-map
162 (let ((map (make-sparse-keymap)))
163 (define-key map "a" 'shr-show-alt-text)
164 (define-key map "i" 'shr-browse-image)
165 (define-key map "z" 'shr-zoom-image)
166 (define-key map [?\t] 'shr-next-link)
167 (define-key map [?\M-\t] 'shr-previous-link)
168 (define-key map [follow-link] 'mouse-face)
169 (define-key map [mouse-2] 'shr-browse-url)
170 (define-key map "I" 'shr-insert-image)
171 (define-key map "w" 'shr-copy-url)
172 (define-key map "u" 'shr-copy-url)
173 (define-key map "v" 'shr-browse-url)
174 (define-key map "o" 'shr-save-contents)
175 (define-key map "\r" 'shr-browse-url)
176 map))
177
178 ;; Public functions and commands.
179 (declare-function libxml-parse-html-region "xml.c"
180 (start end &optional base-url discard-comments))
181
182 (defun shr-render-buffer (buffer)
183 "Display the HTML rendering of the current buffer."
184 (interactive (list (current-buffer)))
185 (or (fboundp 'libxml-parse-html-region)
186 (error "This function requires Emacs to be compiled with libxml2"))
187 (pop-to-buffer "*html*")
188 (erase-buffer)
189 (shr-insert-document
190 (with-current-buffer buffer
191 (libxml-parse-html-region (point-min) (point-max))))
192 (goto-char (point-min)))
193
194 ;;;###autoload
195 (defun shr-render-region (begin end &optional buffer)
196 "Display the HTML rendering of the region between BEGIN and END."
197 (interactive "r")
198 (unless (fboundp 'libxml-parse-html-region)
199 (error "This function requires Emacs to be compiled with libxml2"))
200 (with-current-buffer (or buffer (current-buffer))
201 (let ((dom (libxml-parse-html-region begin end)))
202 (delete-region begin end)
203 (goto-char begin)
204 (shr-insert-document dom))))
205
206 ;;;###autoload
207 (defun shr-insert-document (dom)
208 "Render the parsed document DOM into the current buffer.
209 DOM should be a parse tree as generated by
210 `libxml-parse-html-region' or similar."
211 (setq shr-content-cache nil)
212 (let ((start (point))
213 (shr-start nil)
214 (shr-base nil)
215 (shr-depth 0)
216 (shr-table-id 0)
217 (shr-warning nil)
218 (shr-table-separator-pixel-width (shr-string-pixel-width "-"))
219 (shr-internal-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))))
234
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)))))
247
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
264 url
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.
278 (t
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)))))))
283
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)))))
294
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)))))
318
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)))))
326
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)))
341 (t
342 (message "Browsing %s..." url)
343 (browse-url url)))))
344
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))))
355
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))))
389
390 ;;; Utility functions.
391
392 (defsubst shr-generic (dom)
393 (dolist (sub (dom-children dom))
394 (if (stringp sub)
395 (shr-insert sub)
396 (shr-descend sub))))
397
398 (defun shr-descend (dom)
399 (let ((function
400 (or
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))))))))
435
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)))))
446
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)))
453
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))
468
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))))))
477
478 (defun shr-pixel-region ()
479 (- (shr-pixel-column)
480 (save-excursion
481 (goto-char (mark))
482 (shr-pixel-column))))
483
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))))
490
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)))))
506 (t
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)))))))))
537
538 (defun shr-fill-lines (start end)
539 (if (<= shr-internal-width 0)
540 nil
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)))))
551
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))))
560
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))))))
603
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)))))
630 (or
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)))
676
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)))
697
698 (autoload 'url-expand-file-name "url-expand")
699
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))
729 (t
730 ;; Totally relative.
731 (url-expand-file-name url (concat (car base) (cadr base))))))
732
733 (defun shr-ensure-newline ()
734 (unless (zerop (current-column))
735 (insert "\n")))
736
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.
747 )
748 ((and prefix
749 (= prefix (- (point) (line-beginning-position))))
750 ;; Do nothing; we're at the start of a <li>.
751 )
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)))
763 (t
764 (insert "\n\n"))))))
765
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)))))))
774
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))))
780
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)))))
794
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))
800
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))
812 (t
813 (if external
814 (funcall shr-external-browser url)
815 (browse-url url))))))
816
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))))
826
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)))))
834
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)))
857
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)))
868
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))
872
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)))
894 (t
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)))
918
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))))
934
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")
939
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)))
947 t)
948 (when (or (search-forward "\n\n" nil t)
949 (search-forward "\r\n\r\n" nil t))
950 (shr-parse-image-data)))))
951
952 (declare-function libxml-parse-xml-region "xml.c"
953 (start end &optional base-url discard-comments))
954
955 (defun shr-parse-image-data ()
956 (let ((data (buffer-substring (point) (point-max)))
957 (content-type
958 (save-excursion
959 (save-restriction
960 (narrow-to-region (point-min) (point))
961 (let ((content-type (mail-fetch-field "content-type")))
962 (and content-type
963 ;; Remove any comments in the type string.
964 (intern (replace-regexp-in-string ";.*" "" content-type)
965 obarray)))))))
966 ;; SVG images may contain references to further images that we may
967 ;; want to block. So special-case these by parsing the XML data
968 ;; and remove the blocked bits.
969 (when (eq content-type 'image/svg+xml)
970 (setq data
971 (shr-dom-to-xml
972 (libxml-parse-xml-region (point) (point-max)))))
973 (list data content-type)))
974
975 (defun shr-image-displayer (content-function)
976 "Return a function to display an image.
977 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
978 is an argument. The function to be returned takes three arguments URL,
979 START, and END. Note that START and END should be markers."
980 `(lambda (url start end)
981 (when url
982 (if (string-match "\\`cid:" url)
983 ,(when content-function
984 `(let ((image (funcall ,content-function
985 (substring url (match-end 0)))))
986 (when image
987 (goto-char start)
988 (funcall shr-put-image-function
989 image (buffer-substring start end))
990 (delete-region (point) end))))
991 (url-retrieve url 'shr-image-fetched
992 (list (current-buffer) start end)
993 t t)))))
994
995 (defun shr-heading (dom &rest types)
996 (shr-ensure-paragraph)
997 (apply #'shr-fontize-dom dom types)
998 (shr-ensure-paragraph))
999
1000 (defun shr-urlify (start url &optional title)
1001 (shr-add-font start (point) 'shr-link)
1002 (add-text-properties
1003 start (point)
1004 (list 'shr-url url
1005 'help-echo (let ((iri (or (ignore-errors
1006 (decode-coding-string
1007 (url-unhex-string url)
1008 'utf-8 t))
1009 url)))
1010 (if title (format "%s (%s)" iri title) iri))
1011 'follow-link t
1012 'mouse-face 'highlight
1013 'keymap shr-map)))
1014
1015 (defun shr-encode-url (url)
1016 "Encode URL."
1017 (browse-url-url-encode-chars url "[)$ ]"))
1018
1019 (autoload 'shr-color-visible "shr-color")
1020 (autoload 'shr-color->hexadecimal "shr-color")
1021
1022 (defun shr-color-check (fg bg)
1023 "Check that FG is visible on BG.
1024 Returns (fg bg) with corrected values.
1025 Returns nil if the colors that would be used are the default
1026 ones, in case fg and bg are nil."
1027 (when (or fg bg)
1028 (let ((fixed (cond ((null fg) 'fg)
1029 ((null bg) 'bg))))
1030 ;; Convert colors to hexadecimal, or set them to default.
1031 (let ((fg (or (shr-color->hexadecimal fg)
1032 (frame-parameter nil 'foreground-color)))
1033 (bg (or (shr-color->hexadecimal bg)
1034 (frame-parameter nil 'background-color))))
1035 (cond ((eq fixed 'bg)
1036 ;; Only return the new fg
1037 (list nil (cadr (shr-color-visible bg fg t))))
1038 ((eq fixed 'fg)
1039 ;; Invert args and results and return only the new bg
1040 (list (cadr (shr-color-visible fg bg t)) nil))
1041 (t
1042 (shr-color-visible bg fg)))))))
1043
1044 (defun shr-colorize-region (start end fg &optional bg)
1045 (when (or fg bg)
1046 (let ((new-colors (shr-color-check fg bg)))
1047 (when new-colors
1048 (when fg
1049 (add-face-text-property start end
1050 (list :foreground (cadr new-colors))
1051 t))
1052 (when bg
1053 (add-face-text-property start end
1054 (list :background (car new-colors))
1055 t)))
1056 new-colors)))
1057
1058 (defun shr-previous-newline-padding-width (width)
1059 (let ((overlays (overlays-at (point)))
1060 (previous-width 0))
1061 (if (null overlays)
1062 width
1063 (dolist (overlay overlays)
1064 (setq previous-width
1065 (+ previous-width
1066 (length (plist-get (overlay-properties overlay)
1067 'before-string)))))
1068 (+ width previous-width))))
1069
1070 ;;; Tag-specific rendering rules.
1071
1072 (defun shr-tag-body (dom)
1073 (let* ((start (point))
1074 (fgcolor (or (dom-attr dom 'fgcolor) (dom-attr dom 'text)))
1075 (bgcolor (dom-attr dom 'bgcolor))
1076 (shr-stylesheet (list (cons 'color fgcolor)
1077 (cons 'background-color bgcolor))))
1078 (shr-generic dom)
1079 (shr-colorize-region start (point) fgcolor bgcolor)))
1080
1081 (defun shr-tag-style (_dom)
1082 )
1083
1084 (defun shr-tag-script (_dom)
1085 )
1086
1087 (defun shr-tag-comment (_dom)
1088 )
1089
1090 (defun shr-dom-to-xml (dom)
1091 (with-temp-buffer
1092 (shr-dom-print dom)
1093 (buffer-string)))
1094
1095 (defun shr-dom-print (dom)
1096 "Convert DOM into a string containing the xml representation."
1097 (insert (format "<%s" (dom-tag dom)))
1098 (dolist (attr (dom-attributes dom))
1099 ;; Ignore attributes that start with a colon because they are
1100 ;; private elements.
1101 (unless (= (aref (format "%s" (car attr)) 0) ?:)
1102 (insert (format " %s=\"%s\"" (car attr) (cdr attr)))))
1103 (insert ">")
1104 (let (url)
1105 (dolist (elem (dom-children dom))
1106 (cond
1107 ((stringp elem)
1108 (insert elem))
1109 ((eq (dom-tag elem) 'comment)
1110 )
1111 ((or (not (eq (dom-tag elem) 'image))
1112 ;; Filter out blocked elements inside the SVG image.
1113 (not (setq url (dom-attr elem ':xlink:href)))
1114 (not shr-blocked-images)
1115 (not (string-match shr-blocked-images url)))
1116 (insert " ")
1117 (shr-dom-print elem)))))
1118 (insert (format "</%s>" (dom-tag dom))))
1119
1120 (defun shr-tag-svg (dom)
1121 (when (and (image-type-available-p 'svg)
1122 (not shr-inhibit-images))
1123 (funcall shr-put-image-function (list (shr-dom-to-xml dom) 'image/svg+xml)
1124 "SVG Image")))
1125
1126 (defun shr-tag-sup (dom)
1127 (let ((start (point)))
1128 (shr-generic dom)
1129 (put-text-property start (point) 'display '(raise 0.5))))
1130
1131 (defun shr-tag-sub (dom)
1132 (let ((start (point)))
1133 (shr-generic dom)
1134 (put-text-property start (point) 'display '(raise -0.5))))
1135
1136 (defun shr-tag-label (dom)
1137 (shr-generic dom)
1138 (shr-ensure-paragraph))
1139
1140 (defun shr-tag-p (dom)
1141 (shr-ensure-paragraph)
1142 (shr-generic dom)
1143 (shr-ensure-paragraph))
1144
1145 (defun shr-tag-div (dom)
1146 (shr-ensure-newline)
1147 (shr-generic dom)
1148 (shr-ensure-newline))
1149
1150 (defun shr-tag-s (dom)
1151 (shr-fontize-dom dom 'shr-strike-through))
1152
1153 (defun shr-tag-del (dom)
1154 (shr-fontize-dom dom 'shr-strike-through))
1155
1156 (defun shr-tag-b (dom)
1157 (shr-fontize-dom dom 'bold))
1158
1159 (defun shr-tag-i (dom)
1160 (shr-fontize-dom dom 'italic))
1161
1162 (defun shr-tag-em (dom)
1163 (shr-fontize-dom dom 'italic))
1164
1165 (defun shr-tag-strong (dom)
1166 (shr-fontize-dom dom 'bold))
1167
1168 (defun shr-tag-u (dom)
1169 (shr-fontize-dom dom 'underline))
1170
1171 (defun shr-tag-tt (dom)
1172 (let ((shr-current-font 'default))
1173 (shr-generic dom)))
1174
1175 (defun shr-parse-style (style)
1176 (when style
1177 (save-match-data
1178 (when (string-match "\n" style)
1179 (setq style (replace-match " " t t style))))
1180 (let ((plist nil))
1181 (dolist (elem (split-string style ";"))
1182 (when elem
1183 (setq elem (split-string elem ":"))
1184 (when (and (car elem)
1185 (cadr elem))
1186 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
1187 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
1188 (when (string-match " *!important\\'" value)
1189 (setq value (substring value 0 (match-beginning 0))))
1190 (unless (equal value "inherit")
1191 (push (cons (intern name obarray)
1192 value)
1193 plist))))))
1194 plist)))
1195
1196 (defun shr-tag-base (dom)
1197 (when-let (base (dom-attr dom 'href))
1198 (setq shr-base (shr-parse-base base)))
1199 (shr-generic dom))
1200
1201 (defun shr-tag-a (dom)
1202 (let ((url (dom-attr dom 'href))
1203 (title (dom-attr dom 'title))
1204 (start (point))
1205 shr-start)
1206 (shr-generic dom)
1207 (when (and shr-target-id
1208 (equal (dom-attr dom 'name) shr-target-id))
1209 ;; We have a zero-length <a name="foo"> element, so just
1210 ;; insert... something.
1211 (when (= start (point))
1212 (shr-ensure-newline)
1213 (insert " "))
1214 (put-text-property start (1+ start) 'shr-target-id shr-target-id))
1215 (when url
1216 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
1217
1218 (defun shr-tag-object (dom)
1219 (unless shr-inhibit-images
1220 (let ((start (point))
1221 url multimedia image)
1222 (when-let (type (dom-attr dom 'type))
1223 (when (string-match "\\`image/svg" type)
1224 (setq url (dom-attr dom 'data)
1225 image t)))
1226 (dolist (child (dom-non-text-children dom))
1227 (cond
1228 ((eq (dom-tag child) 'embed)
1229 (setq url (or url (dom-attr child 'src))
1230 multimedia t))
1231 ((and (eq (dom-tag child) 'param)
1232 (equal (dom-attr child 'name) "movie"))
1233 (setq url (or url (dom-attr child 'value))
1234 multimedia t))))
1235 (when url
1236 (cond
1237 (image
1238 (shr-tag-img dom url)
1239 (setq dom nil))
1240 (multimedia
1241 (shr-insert " [multimedia] ")
1242 (shr-urlify start (shr-expand-url url)))))
1243 (when dom
1244 (shr-generic dom)))))
1245
1246 (defcustom shr-prefer-media-type-alist '(("webm" . 1.0)
1247 ("ogv" . 1.0)
1248 ("ogg" . 1.0)
1249 ("opus" . 1.0)
1250 ("flac" . 0.9)
1251 ("wav" . 0.5))
1252 "Preferences for media types.
1253 The key element should be a regexp matched against the type of the source or
1254 url if no type is specified. The value should be a float in the range 0.0 to
1255 1.0. Media elements with higher value are preferred."
1256 :version "24.4"
1257 :group 'shr
1258 :type '(alist :key-type regexp :value-type float))
1259
1260 (defun shr--get-media-pref (elem)
1261 "Determine the preference for ELEM.
1262 The preference is a float determined from `shr-prefer-media-type'."
1263 (let ((type (dom-attr elem 'type))
1264 (p 0.0))
1265 (unless type
1266 (setq type (dom-attr elem 'src)))
1267 (when type
1268 (dolist (pref shr-prefer-media-type-alist)
1269 (when (and
1270 (> (cdr pref) p)
1271 (string-match-p (car pref) type))
1272 (setq p (cdr pref)))))
1273 p))
1274
1275 (defun shr--extract-best-source (dom &optional url pref)
1276 "Extract the best `:src' property from <source> blocks in DOM."
1277 (setq pref (or pref -1.0))
1278 (let (new-pref)
1279 (dolist (elem (dom-non-text-children dom))
1280 (when (and (eq (dom-tag elem) 'source)
1281 (< pref
1282 (setq new-pref
1283 (shr--get-media-pref elem))))
1284 (setq pref new-pref
1285 url (dom-attr elem 'src))
1286 ;; libxml's html parser isn't HTML5 compliant and non terminated
1287 ;; source tags might end up as children. So recursion it is...
1288 (dolist (child (dom-non-text-children elem))
1289 (when (eq (dom-tag child) 'source)
1290 (let ((ret (shr--extract-best-source (list child) url pref)))
1291 (when (< pref (cdr ret))
1292 (setq url (car ret)
1293 pref (cdr ret)))))))))
1294 (cons url pref))
1295
1296 (defun shr-tag-video (dom)
1297 (let ((image (dom-attr dom 'poster))
1298 (url (dom-attr dom 'src))
1299 (start (point)))
1300 (unless url
1301 (setq url (car (shr--extract-best-source dom))))
1302 (if image
1303 (shr-tag-img nil image)
1304 (shr-insert " [video] "))
1305 (shr-urlify start (shr-expand-url url))))
1306
1307 (defun shr-tag-audio (dom)
1308 (let ((url (dom-attr dom 'src))
1309 (start (point)))
1310 (unless url
1311 (setq url (car (shr--extract-best-source dom))))
1312 (shr-insert " [audio] ")
1313 (shr-urlify start (shr-expand-url url))))
1314
1315 (defun shr-tag-img (dom &optional url)
1316 (when (or url
1317 (and dom
1318 (> (length (dom-attr dom 'src)) 0)))
1319 (when (> (current-column) 0)
1320 (insert "\n"))
1321 (let ((alt (dom-attr dom 'alt))
1322 (url (shr-expand-url (or url (dom-attr dom 'src)))))
1323 (let ((start (point-marker)))
1324 (when (zerop (length alt))
1325 (setq alt "*"))
1326 (cond
1327 ((or (member (dom-attr dom 'height) '("0" "1"))
1328 (member (dom-attr dom 'width) '("0" "1")))
1329 ;; Ignore zero-sized or single-pixel images.
1330 )
1331 ((and (not shr-inhibit-images)
1332 (string-match "\\`data:" url))
1333 (let ((image (shr-image-from-data (substring url (match-end 0)))))
1334 (if image
1335 (funcall shr-put-image-function image alt)
1336 (insert alt))))
1337 ((and (not shr-inhibit-images)
1338 (string-match "\\`cid:" url))
1339 (let ((url (substring url (match-end 0)))
1340 image)
1341 (if (or (not shr-content-function)
1342 (not (setq image (funcall shr-content-function url))))
1343 (insert alt)
1344 (funcall shr-put-image-function image alt))))
1345 ((or shr-inhibit-images
1346 (and shr-blocked-images
1347 (string-match shr-blocked-images url)))
1348 (setq shr-start (point))
1349 (if (> (string-width alt) 8)
1350 (shr-insert (truncate-string-to-width alt 8))
1351 (shr-insert alt)))
1352 ((and (not shr-ignore-cache)
1353 (url-is-cached (shr-encode-url url)))
1354 (funcall shr-put-image-function (shr-get-image-data url) alt))
1355 (t
1356 (insert alt " ")
1357 (when (and shr-ignore-cache
1358 (url-is-cached (shr-encode-url url)))
1359 (let ((file (url-cache-create-filename (shr-encode-url url))))
1360 (when (file-exists-p file)
1361 (delete-file file))))
1362 (url-queue-retrieve
1363 (shr-encode-url url) 'shr-image-fetched
1364 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1365 t t)))
1366 (when (zerop shr-table-depth) ;; We are not in a table.
1367 (put-text-property start (point) 'keymap shr-map)
1368 (put-text-property start (point) 'shr-alt alt)
1369 (put-text-property start (point) 'image-url url)
1370 (put-text-property start (point) 'image-displayer
1371 (shr-image-displayer shr-content-function))
1372 (put-text-property start (point) 'help-echo
1373 (shr-fill-text
1374 (or (dom-attr dom 'title) alt))))))))
1375
1376 (defun shr-tag-pre (dom)
1377 (let ((shr-folding-mode 'none)
1378 (shr-current-font 'default))
1379 (shr-ensure-newline)
1380 (shr-generic dom)
1381 (shr-ensure-newline)))
1382
1383 (defun shr-tag-blockquote (dom)
1384 (shr-ensure-paragraph)
1385 (let ((start (point))
1386 (shr-indentation (+ shr-indentation
1387 (* 4 shr-table-separator-pixel-width))))
1388 (shr-generic dom)
1389 (shr-ensure-paragraph)
1390 (shr-mark-fill start)))
1391
1392 (defun shr-tag-dl (dom)
1393 (shr-ensure-paragraph)
1394 (shr-generic dom)
1395 (shr-ensure-paragraph))
1396
1397 (defun shr-tag-dt (dom)
1398 (shr-ensure-newline)
1399 (shr-generic dom)
1400 (shr-ensure-newline))
1401
1402 (defun shr-tag-dd (dom)
1403 (shr-ensure-newline)
1404 (let ((shr-indentation (+ shr-indentation
1405 (* 4 shr-table-separator-pixel-width))))
1406 (shr-generic dom)))
1407
1408 (defun shr-tag-ul (dom)
1409 (shr-ensure-paragraph)
1410 (let ((shr-list-mode 'ul))
1411 (shr-generic dom))
1412 (shr-ensure-paragraph))
1413
1414 (defun shr-tag-ol (dom)
1415 (shr-ensure-paragraph)
1416 (let ((shr-list-mode 1))
1417 (shr-generic dom))
1418 (shr-ensure-paragraph))
1419
1420 (defun shr-tag-li (dom)
1421 (shr-ensure-newline)
1422 (let ((start (point)))
1423 (let* ((bullet
1424 (if (numberp shr-list-mode)
1425 (prog1
1426 (format "%d " shr-list-mode)
1427 (setq shr-list-mode (1+ shr-list-mode)))
1428 (car shr-internal-bullet)))
1429 (width (if (numberp shr-list-mode)
1430 (shr-string-pixel-width bullet)
1431 (cdr shr-internal-bullet))))
1432 (insert bullet)
1433 (shr-mark-fill start)
1434 (let ((shr-indentation (+ shr-indentation width)))
1435 (put-text-property start (1+ start)
1436 'shr-continuation-indentation shr-indentation)
1437 (put-text-property start (1+ start) 'shr-prefix-length (length bullet))
1438 (shr-generic dom)))))
1439
1440 (defun shr-mark-fill (start)
1441 ;; We may not have inserted any text to fill.
1442 (unless (= start (point))
1443 (put-text-property start (1+ start)
1444 'shr-indentation shr-indentation)))
1445
1446 (defun shr-tag-br (dom)
1447 (when (and (not (bobp))
1448 ;; Only add a newline if we break the current line, or
1449 ;; the previous line isn't a blank line.
1450 (or (not (bolp))
1451 (and (> (- (point) 2) (point-min))
1452 (not (= (char-after (- (point) 2)) ?\n)))))
1453 (insert "\n"))
1454 (shr-generic dom))
1455
1456 (defun shr-tag-span (dom)
1457 (shr-generic dom))
1458
1459 (defun shr-tag-h1 (dom)
1460 (shr-heading dom (if shr-use-fonts
1461 '(variable-pitch (:height 1.3 :weight bold))
1462 'bold)))
1463
1464 (defun shr-tag-h2 (dom)
1465 (shr-heading dom 'bold))
1466
1467 (defun shr-tag-h3 (dom)
1468 (shr-heading dom 'italic))
1469
1470 (defun shr-tag-h4 (dom)
1471 (shr-heading dom))
1472
1473 (defun shr-tag-h5 (dom)
1474 (shr-heading dom))
1475
1476 (defun shr-tag-h6 (dom)
1477 (shr-heading dom))
1478
1479 (defun shr-tag-hr (_dom)
1480 (shr-ensure-newline)
1481 (insert (make-string (if (not shr-use-fonts)
1482 shr-internal-width
1483 (1+ (/ shr-internal-width
1484 shr-table-separator-pixel-width)))
1485 shr-hr-line)
1486 "\n"))
1487
1488 (defun shr-tag-title (dom)
1489 (shr-heading dom 'bold 'underline))
1490
1491 (defun shr-tag-font (dom)
1492 (let* ((start (point))
1493 (color (dom-attr dom 'color))
1494 (shr-stylesheet (nconc (list (cons 'color color))
1495 shr-stylesheet)))
1496 (shr-generic dom)
1497 (when color
1498 (shr-colorize-region start (point) color
1499 (cdr (assq 'background-color shr-stylesheet))))))
1500
1501 ;;; Table rendering algorithm.
1502
1503 ;; Table rendering is the only complicated thing here. We do this by
1504 ;; first counting how many TDs there are in each TR, and registering
1505 ;; how wide they think they should be ("width=45%", etc). Then we
1506 ;; render each TD separately (this is done in temporary buffers, so
1507 ;; that we can use all the rendering machinery as if we were in the
1508 ;; main buffer). Now we know how much space each TD really takes, so
1509 ;; we then render everything again with the new widths, and finally
1510 ;; insert all these boxes into the main buffer.
1511 (defun shr-tag-table-1 (dom)
1512 (setq dom (or (dom-child-by-tag dom 'tbody) dom))
1513 (let* ((shr-inhibit-images t)
1514 (shr-table-depth (1+ shr-table-depth))
1515 (shr-kinsoku-shorten t)
1516 ;; Find all suggested widths.
1517 (columns (shr-column-specs dom))
1518 ;; Compute how many pixels wide each TD should be.
1519 (suggested-widths (shr-pro-rate-columns columns))
1520 ;; Do a "test rendering" to see how big each TD is (this can
1521 ;; be smaller (if there's little text) or bigger (if there's
1522 ;; unbreakable text).
1523 (elems (or (dom-attr dom 'shr-suggested-widths)
1524 (shr-make-table dom suggested-widths nil
1525 'shr-suggested-widths)))
1526 (sketch (loop for line in elems
1527 collect (mapcar #'car line)))
1528 (natural (loop for line in elems
1529 collect (mapcar #'cdr line)))
1530 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1531 ;; This probably won't work very well.
1532 (when (> (+ (loop for width across sketch-widths
1533 summing (1+ width))
1534 shr-indentation shr-table-separator-pixel-width)
1535 (frame-width))
1536 (setq truncate-lines t))
1537 ;; Then render the table again with these new "hard" widths.
1538 (shr-insert-table (shr-make-table dom sketch-widths t) sketch-widths)))
1539
1540 (defun shr-tag-table (dom)
1541 (shr-ensure-paragraph)
1542 (let* ((caption (dom-children (dom-child-by-tag dom 'caption)))
1543 (header (dom-non-text-children (dom-child-by-tag dom 'thead)))
1544 (body (dom-non-text-children (or (dom-child-by-tag dom 'tbody)
1545 dom)))
1546 (footer (dom-non-text-children (dom-child-by-tag dom 'tfoot)))
1547 (bgcolor (dom-attr dom 'bgcolor))
1548 (start (point))
1549 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1550 shr-stylesheet))
1551 (nheader (if header (shr-max-columns header)))
1552 (nbody (if body (shr-max-columns body)))
1553 (nfooter (if footer (shr-max-columns footer))))
1554 (if (and (not caption)
1555 (not header)
1556 (not (dom-child-by-tag dom 'tbody))
1557 (not (dom-child-by-tag dom 'tr))
1558 (not footer))
1559 ;; The table is totally invalid and just contains random junk.
1560 ;; Try to output it anyway.
1561 (shr-generic dom)
1562 ;; It's a real table, so render it.
1563 (if (dom-attr dom 'shr-fixed-table)
1564 (shr-tag-table-1 dom)
1565 ;; Only fix up the table once.
1566 (let ((table
1567 (nconc
1568 (list 'table nil)
1569 (if caption `((tr nil (td nil ,@caption))))
1570 (cond
1571 (header
1572 (if footer
1573 ;; header + body + footer
1574 (if (= nheader nbody)
1575 (if (= nbody nfooter)
1576 `((tr nil (td nil (table nil
1577 (tbody nil ,@header
1578 ,@body ,@footer)))))
1579 (nconc `((tr nil (td nil (table nil
1580 (tbody nil ,@header
1581 ,@body)))))
1582 (if (= nfooter 1)
1583 footer
1584 `((tr nil (td nil (table
1585 nil (tbody
1586 nil ,@footer))))))))
1587 (nconc `((tr nil (td nil (table nil (tbody
1588 nil ,@header)))))
1589 (if (= nbody nfooter)
1590 `((tr nil (td nil (table
1591 nil (tbody nil ,@body
1592 ,@footer)))))
1593 (nconc `((tr nil (td nil (table
1594 nil (tbody nil
1595 ,@body)))))
1596 (if (= nfooter 1)
1597 footer
1598 `((tr nil (td nil (table
1599 nil
1600 (tbody
1601 nil
1602 ,@footer))))))))))
1603 ;; header + body
1604 (if (= nheader nbody)
1605 `((tr nil (td nil (table nil (tbody nil ,@header
1606 ,@body)))))
1607 (if (= nheader 1)
1608 `(,@header (tr nil (td nil (table
1609 nil (tbody nil ,@body)))))
1610 `((tr nil (td nil (table nil (tbody nil ,@header))))
1611 (tr nil (td nil (table nil (tbody nil ,@body)))))))))
1612 (footer
1613 ;; body + footer
1614 (if (= nbody nfooter)
1615 `((tr nil (td nil (table
1616 nil (tbody nil ,@body ,@footer)))))
1617 (nconc `((tr nil (td nil (table nil (tbody nil ,@body)))))
1618 (if (= nfooter 1)
1619 footer
1620 `((tr nil (td nil (table
1621 nil (tbody nil ,@footer)))))))))
1622 (caption
1623 `((tr nil (td nil (table nil (tbody nil ,@body))))))
1624 (body)))))
1625 (dom-set-attribute table 'shr-fixed-table t)
1626 (setcdr dom (cdr table))
1627 (shr-tag-table-1 dom))))
1628 (when bgcolor
1629 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1630 bgcolor))
1631 ;; Finally, insert all the images after the table. The Emacs buffer
1632 ;; model isn't strong enough to allow us to put the images actually
1633 ;; into the tables.
1634 (when (zerop shr-table-depth)
1635 (save-excursion
1636 (shr-expand-alignments start (point)))
1637 (dolist (elem (dom-by-tag dom 'object))
1638 (shr-tag-object elem))
1639 (dolist (elem (dom-by-tag dom 'img))
1640 (shr-tag-img elem)))))
1641
1642 (defun shr-insert-table (table widths)
1643 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet))
1644 "collapse"))
1645 (shr-table-separator-length (if collapse 0 1))
1646 (shr-table-vertical-line (if collapse "" shr-table-vertical-line))
1647 (start (point)))
1648 (setq shr-table-id (1+ shr-table-id))
1649 (unless collapse
1650 (shr-insert-table-ruler widths))
1651 (dolist (row table)
1652 (let ((start (point))
1653 (align 0)
1654 (column-number 0)
1655 (height (let ((max 0))
1656 (dolist (column row)
1657 (setq max (max max (nth 2 column))))
1658 max)))
1659 (dotimes (i (max height 1))
1660 (shr-indent)
1661 (insert shr-table-vertical-line "\n"))
1662 (dolist (column row)
1663 (when (> (nth 2 column) -1)
1664 (goto-char start)
1665 ;; Sum up all the widths from the column. (There may be
1666 ;; more than one if this is a "colspan" column.)
1667 (dotimes (i (nth 4 column))
1668 ;; The colspan directive may be wrong and there may not be
1669 ;; that number of columns.
1670 (when (<= column-number (1- (length widths)))
1671 (setq align (+ align
1672 (aref widths column-number)
1673 (* 2 shr-table-separator-pixel-width))))
1674 (setq column-number (1+ column-number)))
1675 (let ((lines (nth 3 column))
1676 (pixel-align (if (not shr-use-fonts)
1677 (* align (frame-char-width))
1678 align)))
1679 (dolist (line lines)
1680 (end-of-line)
1681 (let ((start (point)))
1682 (insert
1683 line
1684 (propertize " "
1685 'display `(space :align-to (,pixel-align))
1686 'face (and (> (length line) 0)
1687 (shr-face-background
1688 (get-text-property
1689 (1- (length line)) 'face line)))
1690 'shr-table-indent shr-table-id)
1691 shr-table-vertical-line)
1692 (shr-colorize-region
1693 start (1- (point)) (nth 5 column) (nth 6 column)))
1694 (forward-line 1))
1695 ;; Add blank lines at padding at the bottom of the TD,
1696 ;; possibly.
1697 (dotimes (i (- height (length lines)))
1698 (end-of-line)
1699 (let ((start (point)))
1700 (insert (propertize " "
1701 'display `(space :align-to (,pixel-align))
1702 'shr-table-indent shr-table-id)
1703 shr-table-vertical-line)
1704 (shr-colorize-region
1705 start (1- (point)) (nth 5 column) (nth 6 column)))
1706 (forward-line 1))))))
1707 (unless collapse
1708 (shr-insert-table-ruler widths)))
1709 (unless (= start (point))
1710 (put-text-property start (1+ start) 'shr-table-id shr-table-id))))
1711
1712 (defun shr-face-background (face)
1713 (and (consp face)
1714 (let ((background nil))
1715 (dolist (elem face)
1716 (when (and (consp elem)
1717 (eq (car elem) :background))
1718 (setq background (cadr elem))))
1719 (and background
1720 (list :background background)))))
1721
1722 (defun shr-expand-alignments (start end)
1723 (while (< (setq start (next-single-property-change
1724 start 'shr-table-id nil end))
1725 end)
1726 (goto-char start)
1727 (let* ((shr-use-fonts t)
1728 (id (get-text-property (point) 'shr-table-id))
1729 (base (shr-pixel-column))
1730 elem)
1731 (when id
1732 (save-excursion
1733 (while (setq elem (text-property-any
1734 (point) end 'shr-table-indent id))
1735 (goto-char elem)
1736 (let ((align (get-text-property (point) 'display)))
1737 (put-text-property (point) (1+ (point)) 'display
1738 `(space :align-to (,(+ (car (nth 2 align))
1739 base)))))
1740 (forward-char 1)))))
1741 (setq start (1+ start))))
1742
1743 (defun shr-insert-table-ruler (widths)
1744 (when shr-table-horizontal-line
1745 (when (and (bolp)
1746 (> shr-indentation 0))
1747 (shr-indent))
1748 (insert shr-table-corner)
1749 (let ((total-width 0))
1750 (dotimes (i (length widths))
1751 (setq total-width (+ total-width (aref widths i)
1752 (* shr-table-separator-pixel-width 2)))
1753 (insert (make-string (1+ (/ (aref widths i)
1754 shr-table-separator-pixel-width))
1755 shr-table-horizontal-line)
1756 (propertize " "
1757 'display `(space :align-to (,total-width))
1758 'shr-table-indent shr-table-id)
1759 shr-table-corner)))
1760 (insert "\n")))
1761
1762 (defun shr-table-widths (table natural-table suggested-widths)
1763 (let* ((length (length suggested-widths))
1764 (widths (make-vector length 0))
1765 (natural-widths (make-vector length 0)))
1766 (dolist (row table)
1767 (let ((i 0))
1768 (dolist (column row)
1769 (aset widths i (max (aref widths i) column))
1770 (setq i (1+ i)))))
1771 (dolist (row natural-table)
1772 (let ((i 0))
1773 (dolist (column row)
1774 (aset natural-widths i (max (aref natural-widths i) column))
1775 (setq i (1+ i)))))
1776 (let ((extra (- (apply '+ (append suggested-widths nil))
1777 (apply '+ (append widths nil))
1778 (* shr-table-separator-pixel-width (1+ (length widths)))))
1779 (expanded-columns 0))
1780 ;; We have extra, unused space, so divide this space amongst the
1781 ;; columns.
1782 (when (> extra 0)
1783 ;; If the natural width is wider than the rendered width, we
1784 ;; want to allow the column to expand.
1785 (dotimes (i length)
1786 (when (> (aref natural-widths i) (aref widths i))
1787 (setq expanded-columns (1+ expanded-columns))))
1788 (dotimes (i length)
1789 (when (> (aref natural-widths i) (aref widths i))
1790 (aset widths i (min
1791 (aref natural-widths i)
1792 (+ (/ extra expanded-columns)
1793 (aref widths i))))))))
1794 widths))
1795
1796 (defun shr-make-table (dom widths &optional fill storage-attribute)
1797 (or (cadr (assoc (list dom widths fill) shr-content-cache))
1798 (let ((data (shr-make-table-1 dom widths fill)))
1799 (push (list (list dom widths fill) data)
1800 shr-content-cache)
1801 (when storage-attribute
1802 (dom-set-attribute dom storage-attribute data))
1803 data)))
1804
1805 (defun shr-make-table-1 (dom widths &optional fill)
1806 (let ((trs nil)
1807 (rowspans (make-vector (length widths) 0))
1808 (colspan-remaining 0)
1809 colspan-width colspan-count
1810 width colspan)
1811 (dolist (row (dom-non-text-children dom))
1812 (when (eq (dom-tag row) 'tr)
1813 (let ((tds nil)
1814 (columns (dom-non-text-children row))
1815 (i 0)
1816 (width-column 0)
1817 column)
1818 (while (< i (length widths))
1819 ;; If we previously had a rowspan definition, then that
1820 ;; means that we now have a "missing" td/th element here.
1821 ;; So just insert a dummy, empty one to (sort of) emulate
1822 ;; rowspan.
1823 (setq column
1824 (if (zerop (aref rowspans i))
1825 (pop columns)
1826 (aset rowspans i (1- (aref rowspans i)))
1827 '(td)))
1828 (when (and (not (stringp column))
1829 (or (memq (dom-tag column) '(td th))
1830 (not column)))
1831 (when-let (span (dom-attr column 'rowspan))
1832 (aset rowspans i (+ (aref rowspans i)
1833 (1- (string-to-number span)))))
1834 ;; Sanity check for invalid column-spans.
1835 (when (>= width-column (length widths))
1836 (setq width-column 0))
1837 (setq width
1838 (if column
1839 (aref widths width-column)
1840 (* 10 shr-table-separator-pixel-width)))
1841 (when (setq colspan (dom-attr column 'colspan))
1842 (setq colspan (min (string-to-number colspan)
1843 ;; The colspan may be wrong, so
1844 ;; truncate it to the length of the
1845 ;; remaining columns.
1846 (- (length widths) i)))
1847 (dotimes (j (1- colspan))
1848 (setq width
1849 (if (> (+ i 1 j) (1- (length widths)))
1850 ;; If we have a colspan spec that's longer
1851 ;; than the table is wide, just use the last
1852 ;; width as the width.
1853 (aref widths (1- (length widths)))
1854 ;; Sum up the widths of the columns we're
1855 ;; spanning.
1856 (+ width
1857 shr-table-separator-length
1858 (aref widths (+ i 1 j))))))
1859 (setq width-column (+ width-column (1- colspan))
1860 colspan-count colspan
1861 colspan-remaining colspan))
1862 (when column
1863 (let ((data (shr-render-td column width fill)))
1864 (if (and (not fill)
1865 (> colspan-remaining 0))
1866 (progn
1867 (setq colspan-width (car data))
1868 (let ((this-width (/ colspan-width colspan-count)))
1869 (push (cons this-width (cadr data)) tds)
1870 (setq colspan-remaining (1- colspan-remaining))))
1871 (if (not fill)
1872 (push (cons (car data) (cadr data)) tds)
1873 (push data tds)))))
1874 (when (and colspan
1875 (> colspan 1))
1876 (dotimes (c (1- colspan))
1877 (setq i (1+ i))
1878 (push
1879 (if fill
1880 (list 0 0 -1 nil 1 nil nil)
1881 '(0 . 0))
1882 tds)))
1883 (setq i (1+ i)
1884 width-column (1+ width-column))))
1885 (push (nreverse tds) trs))))
1886 (nreverse trs)))
1887
1888 (defun shr-pixel-buffer-width ()
1889 (if (not shr-use-fonts)
1890 (save-excursion
1891 (goto-char (point-min))
1892 (let ((max 0))
1893 (while (not (eobp))
1894 (end-of-line)
1895 (setq max (max max (current-column)))
1896 (forward-line 1))
1897 max))
1898 (if (get-buffer-window)
1899 (car (window-text-pixel-size nil (point-min) (point-max)))
1900 (save-window-excursion
1901 (set-window-buffer nil (current-buffer))
1902 (car (window-text-pixel-size nil (point-min) (point-max)))))))
1903
1904 (defun shr-render-td (dom width fill)
1905 (let ((cache (intern (format "shr-td-cache-%s-%s" width fill))))
1906 (or (dom-attr dom cache)
1907 (and fill
1908 (let (result)
1909 (dolist (attr (dom-attributes dom))
1910 (let ((name (symbol-name (car attr))))
1911 (when (string-match "shr-td-cache-\\([0-9]+\\)-nil" name)
1912 (let ((cache-width (string-to-number
1913 (match-string 1 name))))
1914 (when (and (>= cache-width width)
1915 (<= (car (cdr attr)) width))
1916 (setq result (cdr attr)))))))
1917 result))
1918 (let ((result (shr-render-td-1 dom width fill)))
1919 (dom-set-attribute dom cache result)
1920 result))))
1921
1922 (defun shr-render-td-1 (dom width fill)
1923 (with-temp-buffer
1924 (let ((bgcolor (dom-attr dom 'bgcolor))
1925 (fgcolor (dom-attr dom 'fgcolor))
1926 (style (dom-attr dom 'style))
1927 (shr-stylesheet shr-stylesheet)
1928 (max-width 0)
1929 natural-width)
1930 (when style
1931 (setq style (and (string-match "color" style)
1932 (shr-parse-style style))))
1933 (when bgcolor
1934 (setq style (nconc (list (cons 'background-color bgcolor))
1935 style)))
1936 (when fgcolor
1937 (setq style (nconc (list (cons 'color fgcolor)) style)))
1938 (when style
1939 (setq shr-stylesheet (append style shr-stylesheet)))
1940 (let ((shr-internal-width width)
1941 (shr-indentation 0))
1942 (shr-descend dom))
1943 (save-window-excursion
1944 (set-window-buffer nil (current-buffer))
1945 (unless fill
1946 (setq natural-width
1947 (or (dom-attr dom 'shr-td-cache-natural)
1948 (let ((natural (max (shr-pixel-buffer-width)
1949 (shr-dom-max-natural-width dom 0))))
1950 (dom-set-attribute dom 'shr-td-cache-natural natural)
1951 natural))))
1952 (if (and natural-width
1953 (<= natural-width width))
1954 (setq max-width natural-width)
1955 (let ((shr-internal-width width))
1956 (shr-fill-lines (point-min) (point-max))
1957 (setq max-width (shr-pixel-buffer-width)))))
1958 (goto-char (point-max))
1959 ;; Delete padding at the bottom of the TDs.
1960 (delete-region
1961 (point)
1962 (progn
1963 (skip-chars-backward " \t\n")
1964 (end-of-line)
1965 (point)))
1966 (goto-char (point-min))
1967 (list max-width
1968 natural-width
1969 (count-lines (point-min) (point-max))
1970 (split-string (buffer-string) "\n")
1971 (if (dom-attr dom 'colspan)
1972 (string-to-number (dom-attr dom 'colspan))
1973 1)
1974 (cdr (assq 'color shr-stylesheet))
1975 (cdr (assq 'background-color shr-stylesheet))))))
1976
1977 (defun shr-dom-max-natural-width (dom max)
1978 (if (eq (dom-tag dom) 'table)
1979 (max max (or
1980 (loop for line in (dom-attr dom 'shr-suggested-widths)
1981 maximize (+
1982 shr-table-separator-length
1983 (loop for elem in line
1984 summing
1985 (+ (cdr elem)
1986 (* 2 shr-table-separator-length)))))
1987 0))
1988 (dolist (child (dom-children dom))
1989 (unless (stringp child)
1990 (setq max (max (shr-dom-max-natural-width child max)))))
1991 max))
1992
1993 (defun shr-buffer-width ()
1994 (goto-char (point-min))
1995 (let ((max 0))
1996 (while (not (eobp))
1997 (end-of-line)
1998 (setq max (max max (current-column)))
1999 (forward-line 1))
2000 max))
2001
2002 (defun shr-pro-rate-columns (columns)
2003 (let ((total-percentage 0)
2004 (widths (make-vector (length columns) 0)))
2005 (dotimes (i (length columns))
2006 (setq total-percentage (+ total-percentage (aref columns i))))
2007 (setq total-percentage (/ 1.0 total-percentage))
2008 (dotimes (i (length columns))
2009 (aset widths i (max (truncate (* (aref columns i)
2010 total-percentage
2011 (- shr-internal-width
2012 (* (1+ (length columns))
2013 shr-table-separator-pixel-width))))
2014 10)))
2015 widths))
2016
2017 ;; Return a summary of the number and shape of the TDs in the table.
2018 (defun shr-column-specs (dom)
2019 (let ((columns (make-vector (shr-max-columns dom) 1)))
2020 (dolist (row (dom-non-text-children dom))
2021 (when (eq (dom-tag row) 'tr)
2022 (let ((i 0))
2023 (dolist (column (dom-non-text-children row))
2024 (when (memq (dom-tag column) '(td th))
2025 (let ((width (dom-attr column 'width)))
2026 (when (and width
2027 (string-match "\\([0-9]+\\)%" width)
2028 (not (zerop (setq width (string-to-number
2029 (match-string 1 width))))))
2030 (aset columns i (/ width 100.0))))
2031 (setq i (1+ i)))))))
2032 columns))
2033
2034 (defun shr-count (dom elem)
2035 (let ((i 0))
2036 (dolist (sub (dom-children dom))
2037 (when (and (not (stringp sub))
2038 (eq (dom-tag sub) elem))
2039 (setq i (1+ i))))
2040 i))
2041
2042 (defun shr-max-columns (dom)
2043 (let ((max 0))
2044 (dolist (row (dom-children dom))
2045 (when (and (not (stringp row))
2046 (eq (dom-tag row) 'tr))
2047 (setq max (max max (+ (shr-count row 'td)
2048 (shr-count row 'th))))))
2049 max))
2050
2051 (provide 'shr)
2052
2053 ;; Local Variables:
2054 ;; coding: utf-8
2055 ;; End:
2056
2057 ;;; shr.el ends here