1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
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.
33 (eval-when-compile (require 'cl
))
34 (eval-when-compile (require 'url
)) ;For url-filename's setf handler.
38 "Simple HTML Renderer"
42 (defcustom shr-max-image-proportion
0.9
43 "How big pictures displayed are in relation to the window they're in.
44 A value of 0.7 means that they are allowed to take up 70% of the
45 width and height of the window. If they are larger than this,
46 and Emacs supports it, then the images will be rescaled down to
52 (defcustom shr-blocked-images nil
53 "Images that have URLs matching this regexp will be blocked."
56 :type
'(choice (const nil
) regexp
))
58 (defcustom shr-table-horizontal-line nil
59 "Character used to draw horizontal table lines.
60 If nil, don't draw horizontal table lines."
62 :type
'(choice (const nil
) character
))
64 (defcustom shr-table-vertical-line ?\s
65 "Character used to draw vertical table lines."
69 (defcustom shr-table-corner ?\s
70 "Character used to draw table corners."
74 (defcustom shr-hr-line ?-
75 "Character used to draw hr lines."
79 (defcustom shr-width fill-column
80 "Frame width to use for rendering.
81 May either be an integer specifying a fixed width in characters,
82 or nil, meaning that the full width of the window should be
84 :type
'(choice (integer :tag
"Fixed width in characters")
85 (const :tag
"Use the width of the window" nil
))
88 (defcustom shr-bullet
"* "
89 "Bullet used for unordered lists.
90 Alternative suggestions are:
97 (defcustom shr-external-browser
'browse-url-default-browser
98 "Function used to launch an external browser."
103 (defcustom shr-image-animate t
104 "Non nil means that images that can be animated will be."
109 (defvar shr-content-function nil
110 "If bound, this should be a function that will return the content.
111 This is used for cid: URLs, and the function is called with the
112 cid: URL as the argument.")
114 (defvar shr-put-image-function
'shr-put-image
115 "Function called to put image and alt string.")
117 (defface shr-strike-through
'((t (:strike-through t
)))
118 "Font for <s> elements."
122 '((t (:inherit link
)))
123 "Font for link elements."
126 ;;; Internal variables.
128 (defvar shr-folding-mode nil
)
129 (defvar shr-state nil
)
130 (defvar shr-start nil
)
131 (defvar shr-indentation
0)
132 (defvar shr-inhibit-images nil
)
133 (defvar shr-list-mode nil
)
134 (defvar shr-content-cache nil
)
135 (defvar shr-kinsoku-shorten nil
)
136 (defvar shr-table-depth
0)
137 (defvar shr-stylesheet nil
)
138 (defvar shr-base nil
)
139 (defvar shr-ignore-cache nil
)
140 (defvar shr-external-rendering-functions nil
)
141 (defvar shr-target-id nil
)
142 (defvar shr-inhibit-decoration nil
)
143 (defvar shr-table-separator-length
1)
146 (let ((map (make-sparse-keymap)))
147 (define-key map
"a" 'shr-show-alt-text
)
148 (define-key map
"i" 'shr-browse-image
)
149 (define-key map
"z" 'shr-zoom-image
)
150 (define-key map
[?
\t] 'shr-next-link
)
151 (define-key map
[?\M-
\t] 'shr-previous-link
)
152 (define-key map
[follow-link
] 'mouse-face
)
153 (define-key map
[mouse-2
] 'shr-browse-url
)
154 (define-key map
"I" 'shr-insert-image
)
155 (define-key map
"w" 'shr-copy-url
)
156 (define-key map
"u" 'shr-copy-url
)
157 (define-key map
"v" 'shr-browse-url
)
158 (define-key map
"o" 'shr-save-contents
)
159 (define-key map
"\r" 'shr-browse-url
)
162 ;; Public functions and commands.
163 (declare-function libxml-parse-html-region
"xml.c"
164 (start end
&optional base-url
))
166 (defun shr-render-buffer (buffer)
167 "Display the HTML rendering of the current buffer."
168 (interactive (list (current-buffer)))
169 (or (fboundp 'libxml-parse-html-region
)
170 (error "This function requires Emacs to be compiled with libxml2"))
171 (pop-to-buffer "*html*")
174 (with-current-buffer buffer
175 (libxml-parse-html-region (point-min) (point-max))))
176 (goto-char (point-min)))
179 (defun shr-render-region (begin end
&optional buffer
)
180 "Display the HTML rendering of the region between BEGIN and END."
182 (unless (fboundp 'libxml-parse-html-region
)
183 (error "This function requires Emacs to be compiled with libxml2"))
184 (with-current-buffer (or buffer
(current-buffer))
185 (let ((dom (libxml-parse-html-region begin end
)))
186 (delete-region begin end
)
188 (shr-insert-document dom
))))
191 (defun shr-insert-document (dom)
192 "Render the parsed document DOM into the current buffer.
193 DOM should be a parse tree as generated by
194 `libxml-parse-html-region' or similar."
195 (setq shr-content-cache nil
)
196 (let ((start (point))
200 (shr-width (or shr-width
(1- (window-width)))))
201 (shr-descend (shr-transform-dom dom
))
202 (shr-remove-trailing-whitespace start
(point))))
204 (defun shr-remove-trailing-whitespace (start end
)
205 (let ((width (window-width)))
207 (narrow-to-region start end
)
211 (when (> (shr-previous-newline-padding-width (current-column)) width
)
212 (dolist (overlay (overlays-at (point)))
213 (when (overlay-get overlay
'before-string
)
214 (overlay-put overlay
'before-string nil
))))
217 (defun shr-copy-url ()
218 "Copy the URL under point to the kill ring.
219 If called twice, then try to fetch the URL and see whether it
220 redirects somewhere else."
222 (let ((url (get-text-property (point) 'shr-url
)))
225 (message "No URL under point"))
226 ;; Resolve redirected URLs.
227 ((equal url
(car kill-ring
))
232 (eq (car a
) :redirect
))
235 (goto-char (point-min))
236 ;; Remove common tracking junk from the URL.
237 (when (re-search-forward ".utm_.*" nil t
)
238 (replace-match "" t t
))
239 (message "Copied %s" (buffer-string))
240 (copy-region-as-kill (point-min) (point-max)))))
242 ;; Copy the URL to the kill ring.
246 (copy-region-as-kill (point-min) (point-max))
247 (message "Copied %s" url
))))))
249 (defun shr-next-link ()
250 "Skip to the next link."
252 (let ((skip (text-property-any (point) (point-max) 'help-echo nil
)))
253 (if (not (setq skip
(text-property-not-all skip
(point-max)
255 (message "No next link")
257 (message "%s" (get-text-property (point) 'help-echo
)))))
259 (defun shr-previous-link ()
260 "Skip to the previous link."
262 (let ((start (point))
264 ;; Skip past the current link.
265 (while (and (not (bobp))
266 (get-text-property (point) 'help-echo
))
268 ;; Find the previous link.
269 (while (and (not (bobp))
270 (not (setq found
(get-text-property (point) 'help-echo
))))
274 (message "No previous link")
276 ;; Put point at the start of the link.
277 (while (and (not (bobp))
278 (get-text-property (point) 'help-echo
))
281 (message "%s" (get-text-property (point) 'help-echo
)))))
283 (defun shr-show-alt-text ()
284 "Show the ALT text of the image under point."
286 (let ((text (get-text-property (point) 'shr-alt
)))
288 (message "No image under point")
289 (message "%s" text
))))
291 (defun shr-browse-image (&optional copy-url
)
292 "Browse the image under point.
293 If COPY-URL (the prefix if called interactively) is non-nil, copy
294 the URL of the image to the kill buffer instead."
296 (let ((url (get-text-property (point) 'image-url
)))
299 (message "No image under point"))
303 (copy-region-as-kill (point-min) (point-max))
304 (message "Copied %s" url
)))
306 (message "Browsing %s..." url
)
309 (defun shr-insert-image ()
310 "Insert the image under point into the buffer."
312 (let ((url (get-text-property (point) 'image-url
)))
314 (message "No image under point")
315 (message "Inserting %s..." url
)
316 (url-retrieve url
'shr-image-fetched
317 (list (current-buffer) (1- (point)) (point-marker))
320 (defun shr-zoom-image ()
321 "Toggle the image size.
322 The size will be rotated between the default size, the original
323 size, and full-buffer size."
325 (let ((url (get-text-property (point) 'image-url
))
326 (size (get-text-property (point) 'image-size
))
327 (buffer-read-only nil
))
329 (message "No image under point")
330 ;; Delete the old picture.
331 (while (get-text-property (point) 'image-url
)
334 (let ((start (point)))
335 (while (get-text-property (point) 'image-url
)
338 (put-text-property start
(point) 'display nil
)
339 (when (> (- (point) start
) 2)
340 (delete-region start
(1- (point)))))
341 (message "Inserting %s..." url
)
342 (url-retrieve url
'shr-image-fetched
343 (list (current-buffer) (1- (point)) (point-marker)
345 (cond ((or (eq size
'default
)
354 ;;; Utility functions.
356 (defun shr-transform-dom (dom)
357 (let ((result (list (pop dom
))))
358 (dolist (arg (pop dom
))
359 (push (cons (intern (concat ":" (symbol-name (car arg
))) obarray
)
364 (push (cons 'text sub
) result
)
365 (push (shr-transform-dom sub
) result
)))
368 (defsubst shr-generic
(cont)
371 ((eq (car sub
) 'text
)
372 (shr-insert (cdr sub
)))
374 (shr-descend sub
)))))
376 (defun shr-descend (dom)
379 ;; Allow other packages to override (or provide) rendering
381 (cdr (assq (car dom
) shr-external-rendering-functions
))
382 (intern (concat "shr-tag-" (symbol-name (car dom
))) obarray
)))
383 (style (cdr (assq :style
(cdr dom
))))
384 (shr-stylesheet shr-stylesheet
)
387 (if (string-match "color\\|display\\|border-collapse" style
)
388 (setq shr-stylesheet
(nconc (shr-parse-style style
)
391 ;; If we have a display:none, then just ignore this part of the DOM.
392 (unless (equal (cdr (assq 'display shr-stylesheet
)) "none")
393 (if (fboundp function
)
394 (funcall function
(cdr dom
))
395 (shr-generic (cdr dom
)))
396 (when (and shr-target-id
397 (equal (cdr (assq :id
(cdr dom
))) shr-target-id
))
398 ;; If the element was empty, we don't have anything to put the
399 ;; anchor on. So just insert a dummy character.
400 (when (= start
(point))
402 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
403 ;; If style is set, then this node has set the color.
405 (shr-colorize-region start
(point)
406 (cdr (assq 'color shr-stylesheet
))
407 (cdr (assq 'background-color shr-stylesheet
)))))))
409 (defmacro shr-char-breakable-p
(char)
410 "Return non-nil if a line can be broken before and after CHAR."
411 `(aref fill-find-break-point-function-table
,char
))
412 (defmacro shr-char-nospace-p
(char)
413 "Return non-nil if no space is required before and after CHAR."
414 `(aref fill-nospace-between-words-table
,char
))
416 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
417 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
418 ;; parentheses, and so on, that should not be placed in the beginning
419 ;; of a line or the end of a line.
420 (defmacro shr-char-kinsoku-bol-p
(char)
421 "Return non-nil if a line ought not to begin with CHAR."
423 (and (not (eq char ?
'))
424 (aref (char-category-set char
) ?
>))))
425 (defmacro shr-char-kinsoku-eol-p
(char)
426 "Return non-nil if a line ought not to end with CHAR."
427 `(aref (char-category-set ,char
) ?
<))
428 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
429 (load "kinsoku" nil t
))
431 (defun shr-insert (text)
432 (when (and (eq shr-state
'image
)
434 (not (string-match "\\`[ \t\n]+\\'" text
)))
436 (setq shr-state nil
))
438 ((eq shr-folding-mode
'none
)
441 (when (and (string-match "\\`[ \t\n ]" text
)
443 (not (eq (char-after (1- (point))) ?
)))
445 (dolist (elem (split-string text
"[ \f\t\n\r\v ]+" t
))
447 (> shr-indentation
0))
449 ;; No space is needed behind a wide character categorized as
450 ;; kinsoku-bol, between characters both categorized as nospace,
451 ;; or at the beginning of a line.
453 (when (and (> (current-column) shr-indentation
)
454 (eq (preceding-char) ?
)
455 (or (= (line-beginning-position) (1- (point)))
456 (and (shr-char-breakable-p
457 (setq prev
(char-after (- (point) 2))))
458 (shr-char-kinsoku-bol-p prev
))
459 (and (shr-char-nospace-p prev
)
460 (shr-char-nospace-p (aref elem
0)))))
462 ;; The shr-start is a special variable that is used to pass
463 ;; upwards the first point in the buffer where the text really
466 (setq shr-start
(point)))
470 (while (and (> (current-column) shr-width
)
473 (setq found
(shr-find-fill-point))
475 (when (eq (preceding-char) ?
)
479 ;; No space is needed at the beginning of a line.
480 (when (eq (following-char) ?
)
482 (when (> shr-indentation
0)
485 (if (<= (current-column) shr-width
)
487 ;; In case we couldn't get a valid break point (because of a
488 ;; word that's longer than `shr-width'), just break anyway.
490 (when (> shr-indentation
0)
492 (unless (string-match "[ \t\r\n ]\\'" text
)
495 (defun shr-find-fill-point ()
496 (when (> (move-to-column shr-width
) shr-width
)
500 (while (not (or (setq failed
(<= (current-column) shr-indentation
))
501 (eq (preceding-char) ?
)
502 (eq (following-char) ?
)
503 (shr-char-breakable-p (preceding-char))
504 (shr-char-breakable-p (following-char))
505 (and (shr-char-kinsoku-bol-p (preceding-char))
506 (shr-char-breakable-p (following-char))
507 (not (shr-char-kinsoku-bol-p (following-char))))
508 (shr-char-kinsoku-eol-p (following-char))
512 ;; There's no breakable point, so we give it up.
515 (unless shr-kinsoku-shorten
516 (while (setq found
(re-search-forward
517 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
518 (line-end-position) 'move
)))
520 (not (match-beginning 1)))
521 (goto-char (match-beginning 0)))))
524 ;; Don't put kinsoku-bol characters at the beginning of a line,
525 ;; or kinsoku-eol characters at the end of a line.
528 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
529 (shr-char-kinsoku-eol-p (preceding-char)))
531 (when (setq failed
(<= (current-column) shr-indentation
))
532 ;; There's no breakable point that doesn't violate kinsoku,
533 ;; so we look for the second best position.
536 (<= (current-column) shr-width
))
539 (shr-char-kinsoku-eol-p (following-char)))))
541 ((shr-char-kinsoku-eol-p (preceding-char))
542 ;; Find backward the point where kinsoku-eol characters begin.
547 (and (> (setq count
(1- count
)) 0)
548 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
549 (or (shr-char-kinsoku-eol-p (preceding-char))
550 (shr-char-kinsoku-bol-p (following-char)))))))
551 (when (setq failed
(<= (current-column) shr-indentation
))
552 ;; There's no breakable point that doesn't violate kinsoku,
553 ;; so we go to the second best position.
554 (if (looking-at "\\(\\c<+\\)\\c<")
555 (goto-char (match-end 1))
557 ((shr-char-kinsoku-bol-p (following-char))
558 ;; Find forward the point where kinsoku-bol characters end.
562 (and (>= (setq count
(1- count
)) 0)
563 (shr-char-kinsoku-bol-p (following-char))
564 (shr-char-breakable-p (following-char))))))))
565 (when (eq (following-char) ?
)
569 (defun shr-parse-base (url)
570 ;; Always chop off anchors.
571 (when (string-match "#.*" url
)
572 (setq url
(substring url
0 (match-beginning 0))))
573 (let* ((parsed (url-generic-parse-url url
))
574 (local (url-filename parsed
)))
575 (setf (url-filename parsed
) "")
576 ;; Chop off the bit after the last slash.
577 (when (string-match "\\`\\(.*/\\)[^/]+\\'" local
)
578 (setq local
(match-string 1 local
)))
579 ;; Always make the local bit end with a slash.
580 (when (and (not (zerop (length local
)))
581 (not (eq (aref local
(1- (length local
))) ?
/)))
582 (setq local
(concat local
"/")))
583 (list (url-recreate-url parsed
)
588 (autoload 'url-expand-file-name
"url-expand")
590 ;; FIXME This needs some tests writing.
591 ;; Does it even need to exist, given that url-expand-file-name does?
592 (defun shr-expand-url (url &optional base
)
595 (shr-parse-base base
)
596 ;; Bound by the parser.
598 (when (zerop (length url
))
602 (string-match "\\`[a-z]*:" url
))
605 ((eq (aref url
0) ?
/)
606 (if (and (> (length url
) 1)
607 (eq (aref url
1) ?
/))
608 ;; //host...; just use the protocol
609 (concat (nth 2 base
) ":" url
)
610 ;; Just use the host name part.
611 (concat (car base
) url
)))
612 ((eq (aref url
0) ?
#)
613 ;; A link to an anchor.
614 (concat (nth 3 base
) url
))
617 (url-expand-file-name url
(concat (car base
) (cadr base
))))))
619 (defun shr-ensure-newline ()
620 (unless (zerop (current-column))
623 (defun shr-ensure-paragraph ()
625 (if (<= (current-column) shr-indentation
)
626 (unless (save-excursion
632 ;; If the current line is totally blank, and doesn't even
633 ;; have any face properties set, then delete the blank
635 (and (looking-at " *$")
636 (not (get-text-property (point) 'face
))
637 (not (= (next-single-property-change (point) 'face nil
639 (line-end-position)))))
640 (delete-region (match-beginning 0) (match-end 0))
644 (when (> shr-indentation
0)
645 (insert (make-string shr-indentation ?
))))
647 (defun shr-fontize-cont (cont &rest types
)
651 (shr-add-font (or shr-start
(point)) (point) type
))))
653 ;; Add face to the region, but avoid putting the font properties on
654 ;; blank text at the start of the line, and the newline at the end, to
656 (defun shr-add-font (start end type
)
657 (unless shr-inhibit-decoration
660 (while (< (point) end
)
662 (skip-chars-forward " "))
663 (add-face-text-property (point) (min (line-end-position) end
) type t
)
664 (if (< (line-end-position) end
)
668 (defun shr-mouse-browse-url (ev)
669 "Browse the URL under the mouse cursor."
674 (defun shr-browse-url (&optional external mouse-event
)
675 "Browse the URL under point.
676 If EXTERNAL, browse the URL using `shr-external-browser'."
677 (interactive (list current-prefix-arg last-nonmenu-event
))
678 (mouse-set-point mouse-event
)
679 (let ((url (get-text-property (point) 'shr-url
)))
682 (message "No link under point"))
683 ((string-match "^mailto:" url
)
684 (browse-url-mail url
))
687 (funcall shr-external-browser url
)
688 (browse-url url
))))))
690 (defun shr-save-contents (directory)
691 "Save the contents from URL in a file."
692 (interactive "DSave contents of URL to directory: ")
693 (let ((url (get-text-property (point) 'shr-url
)))
695 (message "No link under point")
696 (url-retrieve (shr-encode-url url
)
697 'shr-store-contents
(list url directory
)
700 (defun shr-store-contents (status url directory
)
701 (unless (plist-get status
:error
)
702 (when (or (search-forward "\n\n" nil t
)
703 (search-forward "\r\n\r\n" nil t
))
704 (write-region (point) (point-max)
705 (expand-file-name (file-name-nondirectory url
)
708 (defun shr-image-fetched (status buffer start end
&optional flags
)
709 (let ((image-buffer (current-buffer)))
710 (when (and (buffer-name buffer
)
711 (not (plist-get status
:error
)))
712 (url-store-in-cache image-buffer
)
713 (when (or (search-forward "\n\n" nil t
)
714 (search-forward "\r\n\r\n" nil t
))
715 (let ((data (shr-parse-image-data)))
716 (with-current-buffer buffer
718 (let ((alt (buffer-substring start end
))
719 (properties (text-properties-at start
))
720 (inhibit-read-only t
))
721 (delete-region start end
)
723 (funcall shr-put-image-function data alt flags
)
725 (let ((type (pop properties
))
726 (value (pop properties
)))
727 (unless (memq type
'(display image-size
))
728 (put-text-property start
(point) type value
))))))))))
729 (kill-buffer image-buffer
)))
731 (defun shr-image-from-data (data)
732 "Return an image from the data: URI content DATA."
734 "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
736 (let ((param (match-string 4 data
))
737 (payload (url-unhex-string (match-string 5 data
))))
738 (when (string-match "^.*\\(;[ \t]*base64\\)$" param
)
739 (setq payload
(base64-decode-string payload
)))
742 ;; Behind display-graphic-p test.
743 (declare-function image-size
"image.c" (spec &optional pixels frame
))
744 (declare-function image-animate
"image" (image &optional index limit
))
746 (defun shr-put-image (spec alt
&optional flags
)
747 "Insert image SPEC with a string ALT. Return image.
748 SPEC is either an image data blob, or a list where the first
749 element is the data blob and the second element is the content-type."
750 (if (display-graphic-p)
751 (let* ((size (cdr (assq 'size flags
)))
752 (data (if (consp spec
)
755 (content-type (and (consp spec
)
760 (create-image data nil t
:ascent
100
761 :format content-type
))
764 (shr-rescale-image data content-type
)))
767 (shr-rescale-image data content-type
))))))
769 ;; When inserting big-ish pictures, put them at the
770 ;; beginning of the line.
771 (when (and (> (current-column) 0)
772 (> (car (image-size image t
)) 400))
774 (if (eq size
'original
)
775 (insert-sliced-image image
(or alt
"*") nil
20 1)
776 (insert-image image
(or alt
"*")))
777 (put-text-property start
(point) 'image-size size
)
778 (when (and shr-image-animate
779 (cond ((fboundp 'image-multi-frame-p
)
780 ;; Only animate multi-frame things that specify a
781 ;; delay; eg animated gifs as opposed to
782 ;; multi-page tiffs. FIXME?
783 (cdr (image-multi-frame-p image
)))
784 ((fboundp 'image-animated-p
)
785 (image-animated-p image
))))
786 (image-animate image nil
60)))
790 (defun shr-rescale-image (data &optional content-type
)
791 "Rescale DATA, if too big, to fit the current buffer."
792 (if (not (and (fboundp 'imagemagick-types
)
793 (get-buffer-window (current-buffer))))
794 (create-image data nil t
:ascent
100)
795 (let ((edges (window-inside-pixel-edges
796 (get-buffer-window (current-buffer)))))
800 :max-width
(truncate (* shr-max-image-proportion
801 (- (nth 2 edges
) (nth 0 edges
))))
802 :max-height
(truncate (* shr-max-image-proportion
803 (- (nth 3 edges
) (nth 1 edges
))))
804 :format content-type
))))
806 ;; url-cache-extract autoloads url-cache.
807 (declare-function url-cache-create-filename
"url-cache" (url))
808 (autoload 'mm-disable-multibyte
"mm-util")
809 (autoload 'browse-url-mail
"browse-url")
811 (defun shr-get-image-data (url)
812 "Get image data for URL.
813 Return a string with image data."
815 (mm-disable-multibyte)
817 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
819 (when (or (search-forward "\n\n" nil t
)
820 (search-forward "\r\n\r\n" nil t
))
821 (shr-parse-image-data)))))
823 (defun shr-parse-image-data ()
825 (buffer-substring (point) (point-max))
828 (narrow-to-region (point-min) (point))
829 (let ((content-type (mail-fetch-field "content-type")))
831 (intern content-type obarray
)))))))
833 (defun shr-image-displayer (content-function)
834 "Return a function to display an image.
835 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
836 is an argument. The function to be returned takes three arguments URL,
837 START, and END. Note that START and END should be markers."
838 `(lambda (url start end
)
840 (if (string-match "\\`cid:" url
)
841 ,(when content-function
842 `(let ((image (funcall ,content-function
843 (substring url
(match-end 0)))))
846 (funcall shr-put-image-function
847 image
(buffer-substring start end
))
848 (delete-region (point) end
))))
849 (url-retrieve url
'shr-image-fetched
850 (list (current-buffer) start end
)
853 (defun shr-heading (cont &rest types
)
854 (shr-ensure-paragraph)
855 (apply #'shr-fontize-cont cont types
)
856 (shr-ensure-paragraph))
858 (defun shr-urlify (start url
&optional title
)
859 (shr-add-font start
(point) 'shr-link
)
863 'help-echo
(if title
(format "%s (%s)" url title
) url
)
865 'mouse-face
'highlight
868 (defun shr-encode-url (url)
870 (browse-url-url-encode-chars url
"[)$ ]"))
872 (autoload 'shr-color-visible
"shr-color")
873 (autoload 'shr-color-
>hexadecimal
"shr-color")
875 (defun shr-color-check (fg bg
)
876 "Check that FG is visible on BG.
877 Returns (fg bg) with corrected values.
878 Returns nil if the colors that would be used are the default
879 ones, in case fg and bg are nil."
881 (let ((fixed (cond ((null fg
) 'fg
)
883 ;; Convert colors to hexadecimal, or set them to default.
884 (let ((fg (or (shr-color->hexadecimal fg
)
885 (frame-parameter nil
'foreground-color
)))
886 (bg (or (shr-color->hexadecimal bg
)
887 (frame-parameter nil
'background-color
))))
888 (cond ((eq fixed
'bg
)
889 ;; Only return the new fg
890 (list nil
(cadr (shr-color-visible bg fg t
))))
892 ;; Invert args and results and return only the new bg
893 (list (cadr (shr-color-visible fg bg t
)) nil
))
895 (shr-color-visible bg fg
)))))))
897 (defun shr-colorize-region (start end fg
&optional bg
)
898 (when (and (not shr-inhibit-decoration
)
900 (let ((new-colors (shr-color-check fg bg
)))
903 (add-face-text-property start end
904 (list :foreground
(cadr new-colors
))
907 (add-face-text-property start end
908 (list :background
(car new-colors
))
912 (defun shr-expand-newlines (start end color
)
914 ;; Skip past all white space at the start and ends.
916 (skip-chars-forward " \t\n")
920 (skip-chars-backward " \t\n")
923 (narrow-to-region start end
)
924 (let ((width (shr-buffer-width))
926 (goto-char (point-min))
929 (when (and (< (setq column
(current-column)) width
)
930 (< (setq column
(shr-previous-newline-padding-width column
))
932 (let ((overlay (make-overlay (point) (1+ (point)))))
933 (overlay-put overlay
'before-string
937 (let ((string (plist-get
938 (overlay-properties overlay
)
942 (overlay-put overlay
'before-string
"")
944 (overlays-at (point))
946 (propertize (make-string (- width column
) ?
)
947 'face
(list :background color
))))))
950 (defun shr-previous-newline-padding-width (width)
951 (let ((overlays (overlays-at (point)))
955 (dolist (overlay overlays
)
958 (length (plist-get (overlay-properties overlay
)
960 (+ width previous-width
))))
962 ;;; Tag-specific rendering rules.
964 (defun shr-tag-body (cont)
965 (let* ((start (point))
966 (fgcolor (cdr (or (assq :fgcolor cont
)
968 (bgcolor (cdr (assq :bgcolor cont
)))
969 (shr-stylesheet (list (cons 'color fgcolor
)
970 (cons 'background-color bgcolor
))))
972 (shr-colorize-region start
(point) fgcolor bgcolor
)))
974 (defun shr-tag-style (_cont)
977 (defun shr-tag-script (_cont)
980 (defun shr-tag-comment (_cont)
983 (defun shr-dom-to-xml (dom)
984 "Convert DOM into a string containing the xml representation."
988 (dolist (sub (cdr dom
))
991 ;; Ignore external image definitions if required.
992 ;; <image xlink:href="http://TRACKING_URL/"/>
993 (when (or (not (eq (car sub
) 'image
))
994 (not (setq url
(cdr (assq ':xlink
:href
(cdr sub
)))))
995 (not shr-blocked-images
)
996 (not (string-match shr-blocked-images url
)))
997 (setq text
(concat text
(shr-dom-to-xml sub
)))))
998 ((eq (car sub
) 'text
)
999 (setq text
(concat text
(cdr sub
))))
1001 (setq arg
(concat arg
(format "%s=\"%s\" "
1002 (substring (symbol-name (car sub
)) 1)
1004 (format "<%s%s>%s</%s>"
1006 (substring arg
0 (1- (length arg
)))
1010 (defun shr-tag-svg (cont)
1011 (when (and (image-type-available-p 'svg
)
1012 (not shr-inhibit-images
))
1013 (funcall shr-put-image-function
1014 (shr-dom-to-xml (cons 'svg cont
))
1017 (defun shr-tag-sup (cont)
1018 (let ((start (point)))
1020 (put-text-property start
(point) 'display
'(raise 0.5))))
1022 (defun shr-tag-sub (cont)
1023 (let ((start (point)))
1025 (put-text-property start
(point) 'display
'(raise -
0.5))))
1027 (defun shr-tag-label (cont)
1029 (shr-ensure-paragraph))
1031 (defun shr-tag-p (cont)
1032 (shr-ensure-paragraph)
1035 (shr-ensure-paragraph))
1037 (defun shr-tag-div (cont)
1038 (shr-ensure-newline)
1041 (shr-ensure-newline))
1043 (defun shr-tag-s (cont)
1044 (shr-fontize-cont cont
'shr-strike-through
))
1046 (defun shr-tag-del (cont)
1047 (shr-fontize-cont cont
'shr-strike-through
))
1049 (defun shr-tag-b (cont)
1050 (shr-fontize-cont cont
'bold
))
1052 (defun shr-tag-i (cont)
1053 (shr-fontize-cont cont
'italic
))
1055 (defun shr-tag-em (cont)
1056 (shr-fontize-cont cont
'italic
))
1058 (defun shr-tag-strong (cont)
1059 (shr-fontize-cont cont
'bold
))
1061 (defun shr-tag-u (cont)
1062 (shr-fontize-cont cont
'underline
))
1064 (defun shr-parse-style (style)
1067 (when (string-match "\n" style
)
1068 (setq style
(replace-match " " t t style
))))
1070 (dolist (elem (split-string style
";"))
1072 (setq elem
(split-string elem
":"))
1073 (when (and (car elem
)
1075 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
1076 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
1077 (when (string-match " *!important\\'" value
)
1078 (setq value
(substring value
0 (match-beginning 0))))
1079 (push (cons (intern name obarray
)
1084 (defun shr-tag-base (cont)
1085 (let ((base (cdr (assq :href cont
))))
1087 (setq shr-base
(shr-parse-base base
))))
1090 (defun shr-tag-a (cont)
1091 (let ((url (cdr (assq :href cont
)))
1092 (title (cdr (assq :title cont
)))
1096 (when (and shr-target-id
1097 (equal (cdr (assq :name cont
)) shr-target-id
))
1098 ;; We have a zero-length <a name="foo"> element, so just
1099 ;; insert... something.
1100 (when (= start
(point))
1101 (shr-ensure-newline)
1103 (put-text-property start
(1+ start
) 'shr-target-id shr-target-id
))
1105 (not shr-inhibit-decoration
))
1106 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
))))
1108 (defun shr-tag-object (cont)
1109 (let ((start (point))
1112 (when (eq (car elem
) 'embed
)
1113 (setq url
(or url
(cdr (assq :src
(cdr elem
))))))
1114 (when (and (eq (car elem
) 'param
)
1115 (equal (cdr (assq :name
(cdr elem
))) "movie"))
1116 (setq url
(or url
(cdr (assq :value
(cdr elem
)))))))
1118 (shr-insert " [multimedia] ")
1119 (shr-urlify start
(shr-expand-url url
)))
1120 (shr-generic cont
)))
1122 (defcustom shr-prefer-media-type-alist
'(("webm" .
1.0)
1128 "Preferences for media types.
1129 The key element should be a regexp matched against the type of the source or
1130 url if no type is specified. The value should be a float in the range 0.0 to
1131 1.0. Media elements with higher value are preferred."
1134 :type
'(alist :key-type regexp
:value-type float
))
1136 (defun shr--get-media-pref (elem)
1137 "Determine the preference for ELEM.
1138 The preference is a float determined from `shr-prefer-media-type'."
1139 (let ((type (cdr (assq :type elem
)))
1142 (setq type
(cdr (assq :src elem
))))
1144 (dolist (pref shr-prefer-media-type-alist
)
1147 (string-match-p (car pref
) type
))
1148 (setq p
(cdr pref
)))))
1151 (defun shr--extract-best-source (cont &optional url pref
)
1152 "Extract the best `:src' property from <source> blocks in CONT."
1153 (setq pref
(or pref -
1.0))
1156 (when (and (eq (car elem
) 'source
)
1159 (shr--get-media-pref elem
))))
1161 url
(cdr (assq :src elem
)))
1162 ;; libxml's html parser isn't HTML5 compliant and non terminated
1163 ;; source tags might end up as children. So recursion it is...
1164 (dolist (child (cdr elem
))
1165 (when (eq (car child
) 'source
)
1166 (let ((ret (shr--extract-best-source (list child
) url pref
)))
1167 (when (< pref
(cdr ret
))
1169 pref
(cdr ret
)))))))))
1172 (defun shr-tag-video (cont)
1173 (let ((image (cdr (assq :poster cont
)))
1174 (url (cdr (assq :src cont
)))
1177 (setq url
(car (shr--extract-best-source cont
))))
1179 (shr-tag-img nil image
)
1180 (shr-insert " [video] "))
1181 (shr-urlify start
(shr-expand-url url
))))
1183 (defun shr-tag-audio (cont)
1184 (let ((url (cdr (assq :src cont
)))
1187 (setq url
(car (shr--extract-best-source cont
))))
1188 (shr-insert " [audio] ")
1189 (shr-urlify start
(shr-expand-url url
))))
1191 (defun shr-tag-img (cont &optional url
)
1194 (> (length (cdr (assq :src cont
))) 0)))
1195 (when (and (> (current-column) 0)
1196 (not (eq shr-state
'image
)))
1198 (let ((alt (cdr (assq :alt cont
)))
1199 (url (shr-expand-url (or url
(cdr (assq :src cont
))))))
1200 (let ((start (point-marker)))
1201 (when (zerop (length alt
))
1204 ((or (member (cdr (assq :height cont
)) '("0" "1"))
1205 (member (cdr (assq :width cont
)) '("0" "1")))
1206 ;; Ignore zero-sized or single-pixel images.
1208 ((and (not shr-inhibit-images
)
1209 (string-match "\\`data:" url
))
1210 (let ((image (shr-image-from-data (substring url
(match-end 0)))))
1212 (funcall shr-put-image-function image alt
)
1214 ((and (not shr-inhibit-images
)
1215 (string-match "\\`cid:" url
))
1216 (let ((url (substring url
(match-end 0)))
1218 (if (or (not shr-content-function
)
1219 (not (setq image
(funcall shr-content-function url
))))
1221 (funcall shr-put-image-function image alt
))))
1222 ((or shr-inhibit-images
1223 (and shr-blocked-images
1224 (string-match shr-blocked-images url
)))
1225 (setq shr-start
(point))
1226 (let ((shr-state 'space
))
1227 (if (> (string-width alt
) 8)
1228 (shr-insert (truncate-string-to-width alt
8))
1230 ((and (not shr-ignore-cache
)
1231 (url-is-cached (shr-encode-url url
)))
1232 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
1235 (when (and shr-ignore-cache
1236 (url-is-cached (shr-encode-url url
)))
1237 (let ((file (url-cache-create-filename (shr-encode-url url
))))
1238 (when (file-exists-p file
)
1239 (delete-file file
))))
1241 (shr-encode-url url
) 'shr-image-fetched
1242 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
1244 (when (zerop shr-table-depth
) ;; We are not in a table.
1245 (put-text-property start
(point) 'keymap shr-map
)
1246 (put-text-property start
(point) 'shr-alt alt
)
1247 (put-text-property start
(point) 'image-url url
)
1248 (put-text-property start
(point) 'image-displayer
1249 (shr-image-displayer shr-content-function
))
1250 (put-text-property start
(point) 'help-echo
1251 (or (cdr (assq :title cont
))
1253 (setq shr-state
'image
)))))
1255 (defun shr-tag-pre (cont)
1256 (let ((shr-folding-mode 'none
))
1257 (shr-ensure-newline)
1260 (shr-ensure-newline)))
1262 (defun shr-tag-blockquote (cont)
1263 (shr-ensure-paragraph)
1265 (let ((shr-indentation (+ shr-indentation
4)))
1267 (shr-ensure-paragraph))
1269 (defun shr-tag-dl (cont)
1270 (shr-ensure-paragraph)
1272 (shr-ensure-paragraph))
1274 (defun shr-tag-dt (cont)
1275 (shr-ensure-newline)
1277 (shr-ensure-newline))
1279 (defun shr-tag-dd (cont)
1280 (shr-ensure-newline)
1281 (let ((shr-indentation (+ shr-indentation
4)))
1282 (shr-generic cont
)))
1284 (defun shr-tag-ul (cont)
1285 (shr-ensure-paragraph)
1286 (let ((shr-list-mode 'ul
))
1288 (shr-ensure-paragraph))
1290 (defun shr-tag-ol (cont)
1291 (shr-ensure-paragraph)
1292 (let ((shr-list-mode 1))
1294 (shr-ensure-paragraph))
1296 (defun shr-tag-li (cont)
1297 (shr-ensure-newline)
1300 (if (numberp shr-list-mode
)
1302 (format "%d " shr-list-mode
)
1303 (setq shr-list-mode
(1+ shr-list-mode
)))
1305 (shr-indentation (+ shr-indentation
(length bullet
))))
1307 (shr-generic cont
)))
1309 (defun shr-tag-br (cont)
1310 (when (and (not (bobp))
1311 ;; Only add a newline if we break the current line, or
1312 ;; the previous line isn't a blank line.
1314 (and (> (- (point) 2) (point-min))
1315 (not (= (char-after (- (point) 2)) ?
\n)))))
1320 (defun shr-tag-span (cont)
1323 (defun shr-tag-h1 (cont)
1324 (shr-heading cont
'bold
'underline
))
1326 (defun shr-tag-h2 (cont)
1327 (shr-heading cont
'bold
))
1329 (defun shr-tag-h3 (cont)
1330 (shr-heading cont
'italic
))
1332 (defun shr-tag-h4 (cont)
1335 (defun shr-tag-h5 (cont)
1338 (defun shr-tag-h6 (cont)
1341 (defun shr-tag-hr (_cont)
1342 (shr-ensure-newline)
1343 (insert (make-string shr-width shr-hr-line
) "\n"))
1345 (defun shr-tag-title (cont)
1346 (shr-heading cont
'bold
'underline
))
1348 (defun shr-tag-font (cont)
1349 (let* ((start (point))
1350 (color (cdr (assq :color cont
)))
1351 (shr-stylesheet (nconc (list (cons 'color color
))
1355 (shr-colorize-region start
(point) color
1356 (cdr (assq 'background-color shr-stylesheet
))))))
1358 ;;; Table rendering algorithm.
1360 ;; Table rendering is the only complicated thing here. We do this by
1361 ;; first counting how many TDs there are in each TR, and registering
1362 ;; how wide they think they should be ("width=45%", etc). Then we
1363 ;; render each TD separately (this is done in temporary buffers, so
1364 ;; that we can use all the rendering machinery as if we were in the
1365 ;; main buffer). Now we know how much space each TD really takes, so
1366 ;; we then render everything again with the new widths, and finally
1367 ;; insert all these boxes into the main buffer.
1368 (defun shr-tag-table-1 (cont)
1369 (setq cont
(or (cdr (assq 'tbody cont
))
1371 (let* ((shr-inhibit-images t
)
1372 (shr-table-depth (1+ shr-table-depth
))
1373 (shr-kinsoku-shorten t
)
1374 ;; Find all suggested widths.
1375 (columns (shr-column-specs cont
))
1376 ;; Compute how many characters wide each TD should be.
1377 (suggested-widths (shr-pro-rate-columns columns
))
1378 ;; Do a "test rendering" to see how big each TD is (this can
1379 ;; be smaller (if there's little text) or bigger (if there's
1380 ;; unbreakable text).
1381 (sketch (shr-make-table cont suggested-widths
))
1382 ;; Compute the "natural" width by setting each column to 500
1383 ;; characters and see how wide they really render.
1384 (natural (shr-make-table cont
(make-vector (length columns
) 500)))
1385 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1386 ;; This probably won't work very well.
1387 (when (> (+ (loop for width across sketch-widths
1391 (setq truncate-lines t
))
1392 ;; Then render the table again with these new "hard" widths.
1393 (shr-insert-table (shr-make-table cont sketch-widths t
) sketch-widths
)))
1395 (defun shr-tag-table (cont)
1396 (shr-ensure-paragraph)
1397 (let* ((caption (cdr (assq 'caption cont
)))
1398 (header (cdr (assq 'thead cont
)))
1399 (body (or (cdr (assq 'tbody cont
)) cont
))
1400 (footer (cdr (assq 'tfoot cont
)))
1401 (bgcolor (cdr (assq :bgcolor cont
)))
1403 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1405 (nheader (if header
(shr-max-columns header
)))
1406 (nbody (if body
(shr-max-columns body
)))
1407 (nfooter (if footer
(shr-max-columns footer
))))
1408 (if (and (not caption
)
1410 (not (cdr (assq 'tbody cont
)))
1411 (not (cdr (assq 'tr cont
)))
1413 ;; The table is totally invalid and just contains random junk.
1414 ;; Try to output it anyway.
1416 ;; It's a real table, so render it.
1419 (if caption
`((tr (td ,@caption
))))
1422 ;; header + body + footer
1423 (if (= nheader nbody
)
1424 (if (= nbody nfooter
)
1425 `((tr (td (table (tbody ,@header
,@body
,@footer
)))))
1426 (nconc `((tr (td (table (tbody ,@header
,@body
)))))
1429 `((tr (td (table (tbody ,@footer
))))))))
1430 (nconc `((tr (td (table (tbody ,@header
)))))
1431 (if (= nbody nfooter
)
1432 `((tr (td (table (tbody ,@body
,@footer
)))))
1433 (nconc `((tr (td (table (tbody ,@body
)))))
1436 `((tr (td (table (tbody ,@footer
))))))))))
1438 (if (= nheader nbody
)
1439 `((tr (td (table (tbody ,@header
,@body
)))))
1441 `(,@header
(tr (td (table (tbody ,@body
)))))
1442 `((tr (td (table (tbody ,@header
))))
1443 (tr (td (table (tbody ,@body
))))))))
1446 (if (= nbody nfooter
)
1447 `((tr (td (table (tbody ,@body
,@footer
)))))
1448 (nconc `((tr (td (table (tbody ,@body
)))))
1451 `((tr (td (table (tbody ,@footer
))))))))
1453 `((tr (td (table (tbody ,@body
)))))
1456 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1458 ;; Finally, insert all the images after the table. The Emacs buffer
1459 ;; model isn't strong enough to allow us to put the images actually
1461 (when (zerop shr-table-depth
)
1462 (dolist (elem (shr-find-elements cont
'img
))
1463 (shr-tag-img (cdr elem
))))))
1465 (defun shr-find-elements (cont type
)
1468 (cond ((eq (car elem
) type
)
1471 (setq result
(nconc (shr-find-elements (cdr elem
) type
) result
)))))
1474 (defun shr-insert-table (table widths
)
1475 (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet
))
1477 (shr-table-separator-length (if collapse
0 1))
1478 (shr-table-vertical-line (if collapse
"" shr-table-vertical-line
)))
1480 (shr-insert-table-ruler widths
))
1482 (let ((start (point))
1483 (height (let ((max 0))
1484 (dolist (column row
)
1485 (setq max
(max max
(cadr column
))))
1489 (insert shr-table-vertical-line
"\n"))
1490 (dolist (column row
)
1492 (let ((lines (nth 2 column
)))
1493 (dolist (line lines
)
1495 (insert line shr-table-vertical-line
)
1497 ;; Add blank lines at padding at the bottom of the TD,
1499 (dotimes (i (- height
(length lines
)))
1501 (let ((start (point)))
1502 (insert (make-string (string-width (car lines
)) ?
)
1503 shr-table-vertical-line
)
1504 (when (nth 4 column
)
1505 (shr-add-font start
(1- (point))
1506 (list :background
(nth 4 column
)))))
1507 (forward-line 1)))))
1509 (shr-insert-table-ruler widths
)))))
1511 (defun shr-insert-table-ruler (widths)
1512 (when shr-table-horizontal-line
1514 (> shr-indentation
0))
1516 (insert shr-table-corner
)
1517 (dotimes (i (length widths
))
1518 (insert (make-string (aref widths i
) shr-table-horizontal-line
)
1522 (defun shr-table-widths (table natural-table suggested-widths
)
1523 (let* ((length (length suggested-widths
))
1524 (widths (make-vector length
0))
1525 (natural-widths (make-vector length
0)))
1528 (dolist (column row
)
1529 (aset widths i
(max (aref widths i
) column
))
1531 (dolist (row natural-table
)
1533 (dolist (column row
)
1534 (aset natural-widths i
(max (aref natural-widths i
) column
))
1536 (let ((extra (- (apply '+ (append suggested-widths nil
))
1537 (apply '+ (append widths nil
))))
1538 (expanded-columns 0))
1539 ;; We have extra, unused space, so divide this space amongst the
1542 ;; If the natural width is wider than the rendered width, we
1543 ;; want to allow the column to expand.
1545 (when (> (aref natural-widths i
) (aref widths i
))
1546 (setq expanded-columns
(1+ expanded-columns
))))
1548 (when (> (aref natural-widths i
) (aref widths i
))
1550 (aref natural-widths i
)
1551 (+ (/ extra expanded-columns
)
1552 (aref widths i
))))))))
1555 (defun shr-make-table (cont widths
&optional fill
)
1556 (or (cadr (assoc (list cont widths fill
) shr-content-cache
))
1557 (let ((data (shr-make-table-1 cont widths fill
)))
1558 (push (list (list cont widths fill
) data
)
1562 (defun shr-make-table-1 (cont widths
&optional fill
)
1564 (shr-inhibit-decoration (not fill
))
1565 (rowspans (make-vector (length widths
) 0))
1568 (when (eq (car row
) 'tr
)
1574 (while (< i
(length widths
))
1575 ;; If we previously had a rowspan definition, then that
1576 ;; means that we now have a "missing" td/th element here.
1577 ;; So just insert a dummy, empty one to (sort of) emulate
1580 (if (zerop (aref rowspans i
))
1582 (aset rowspans i
(1- (aref rowspans i
)))
1584 (when (or (memq (car column
) '(td th
))
1586 (when (cdr (assq :rowspan
(cdr column
)))
1587 (aset rowspans i
(+ (aref rowspans i
)
1588 (1- (string-to-number
1589 (cdr (assq :rowspan
(cdr column
))))))))
1590 ;; Sanity check for invalid column-spans.
1591 (when (>= width-column
(length widths
))
1592 (setq width-column
0))
1595 (aref widths width-column
)
1598 (setq colspan
(cdr (assq :colspan
(cdr column
)))))
1599 (setq colspan
(min (string-to-number colspan
)
1600 ;; The colspan may be wrong, so
1601 ;; truncate it to the length of the
1602 ;; remaining columns.
1603 (- (length widths
) i
)))
1604 (dotimes (j (1- colspan
))
1605 (if (> (+ i
1 j
) (1- (length widths
)))
1606 (setq width
(aref widths
(1- (length widths
))))
1607 (setq width
(+ width
1608 shr-table-separator-length
1609 (aref widths
(+ i
1 j
))))))
1610 (setq width-column
(+ width-column
(1- colspan
))))
1613 (push (shr-render-td (cdr column
) width fill
)
1616 width-column
(1+ width-column
))))
1617 (push (nreverse tds
) trs
))))
1620 (defun shr-render-td (cont width fill
)
1622 (let ((bgcolor (cdr (assq :bgcolor cont
)))
1623 (fgcolor (cdr (assq :fgcolor cont
)))
1624 (style (cdr (assq :style cont
)))
1625 (shr-stylesheet shr-stylesheet
)
1628 (setq style
(and (string-match "color" style
)
1629 (shr-parse-style style
))))
1631 (setq style
(nconc (list (cons 'background-color bgcolor
)) style
)))
1633 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1635 (setq shr-stylesheet
(append style shr-stylesheet
)))
1636 (let ((shr-width width
)
1637 (shr-indentation 0))
1638 (shr-descend (cons 'td cont
)))
1639 ;; Delete padding at the bottom of the TDs.
1643 (skip-chars-backward " \t\n")
1646 (goto-char (point-min))
1650 (setq max
(max max
(current-column)))
1653 (goto-char (point-min))
1654 ;; If the buffer is totally empty, then put a single blank
1656 (if (zerop (buffer-size))
1657 (insert (make-string width ?
))
1658 ;; Otherwise, fill the buffer.
1659 (let ((align (cdr (assq :align cont
)))
1663 (setq length
(- width
(current-column)))
1666 ((equal align
"right")
1668 (insert (make-string length ?
)))
1669 ((equal align
"center")
1670 (insert (make-string (/ length
2) ?
))
1672 (insert (make-string (- length
(/ length
2)) ?
)))
1674 (insert (make-string length ?
)))))
1678 (shr-colorize-region
1679 (point-min) (point-max)
1680 (cdr (assq 'color shr-stylesheet
))
1681 (cdr (assq 'background-color shr-stylesheet
))))))
1684 (count-lines (point-min) (point-max))
1685 (split-string (buffer-string) "\n")
1687 (car actual-colors
))
1690 (defun shr-buffer-width ()
1691 (goto-char (point-min))
1695 (setq max
(max max
(current-column)))
1699 (defun shr-pro-rate-columns (columns)
1700 (let ((total-percentage 0)
1701 (widths (make-vector (length columns
) 0)))
1702 (dotimes (i (length columns
))
1703 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1704 (setq total-percentage
(/ 1.0 total-percentage
))
1705 (dotimes (i (length columns
))
1706 (aset widths i
(max (truncate (* (aref columns i
)
1708 (- shr-width
(1+ (length columns
)))))
1712 ;; Return a summary of the number and shape of the TDs in the table.
1713 (defun shr-column-specs (cont)
1714 (let ((columns (make-vector (shr-max-columns cont
) 1)))
1716 (when (eq (car row
) 'tr
)
1718 (dolist (column (cdr row
))
1719 (when (memq (car column
) '(td th
))
1720 (let ((width (cdr (assq :width
(cdr column
)))))
1722 (string-match "\\([0-9]+\\)%" width
)
1723 (not (zerop (setq width
(string-to-number
1724 (match-string 1 width
))))))
1725 (aset columns i
(/ width
100.0))))
1726 (setq i
(1+ i
)))))))
1729 (defun shr-count (cont elem
)
1732 (when (eq (car sub
) elem
)
1736 (defun shr-max-columns (cont)
1739 (when (eq (car row
) 'tr
)
1740 (setq max
(max max
(+ (shr-count (cdr row
) 'td
)
1741 (shr-count (cdr row
) 'th
))))))
1750 ;;; shr.el ends here