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