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