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