1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2011 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
))
37 "Simple HTML Renderer"
40 (defcustom shr-max-image-proportion
0.9
41 "How big pictures displayed are in relation to the window they're in.
42 A value of 0.7 means that they are allowed to take up 70% of the
43 width and height of the window. If they are larger than this,
44 and Emacs supports it, then the images will be rescaled down to
50 (defcustom shr-blocked-images nil
51 "Images that have URLs matching this regexp will be blocked."
56 (defcustom shr-table-horizontal-line ?
57 "Character used to draw horizontal table lines."
61 (defcustom shr-table-vertical-line ?
62 "Character used to draw vertical table lines."
66 (defcustom shr-table-corner ?
67 "Character used to draw table corners."
71 (defcustom shr-hr-line ?-
72 "Character used to draw hr lines."
76 (defcustom shr-width fill-column
77 "Frame width to use for rendering.
78 May either be an integer specifying a fixed width in characters,
79 or nil, meaning that the full width of the window should be
81 :type
'(choice (integer :tag
"Fixed width in characters")
82 (const :tag
"Use the width of the window" nil
))
85 (defvar shr-content-function nil
86 "If bound, this should be a function that will return the content.
87 This is used for cid: URLs, and the function is called with the
88 cid: URL as the argument.")
90 (defvar shr-put-image-function
'shr-put-image
91 "Function called to put image and alt string.")
93 (defface shr-strike-through
'((t (:strike-through t
)))
94 "Font for <s> elements."
98 '((t (:inherit link
)))
99 "Font for link elements."
102 ;;; Internal variables.
104 (defvar shr-folding-mode nil
)
105 (defvar shr-state nil
)
106 (defvar shr-start nil
)
107 (defvar shr-indentation
0)
108 (defvar shr-inhibit-images nil
)
109 (defvar shr-list-mode nil
)
110 (defvar shr-content-cache nil
)
111 (defvar shr-kinsoku-shorten nil
)
112 (defvar shr-table-depth
0)
113 (defvar shr-stylesheet nil
)
114 (defvar shr-base nil
)
117 (let ((map (make-sparse-keymap)))
118 (define-key map
"a" 'shr-show-alt-text
)
119 (define-key map
"i" 'shr-browse-image
)
120 (define-key map
"I" 'shr-insert-image
)
121 (define-key map
"u" 'shr-copy-url
)
122 (define-key map
"v" 'shr-browse-url
)
123 (define-key map
"o" 'shr-save-contents
)
124 (define-key map
"\r" 'shr-browse-url
)
127 ;; Public functions and commands.
129 (defun shr-visit-file (file)
130 (interactive "fHTML file name: ")
131 (pop-to-buffer "*html*")
135 (insert-file-contents file
)
136 (libxml-parse-html-region (point-min) (point-max)))))
139 (defun shr-insert-document (dom)
140 (setq shr-content-cache nil
)
141 (let ((shr-state nil
)
144 (shr-width (or shr-width
(window-width))))
145 (shr-descend (shr-transform-dom dom
))))
147 (defun shr-copy-url ()
148 "Copy the URL under point to the kill ring.
149 If called twice, then try to fetch the URL and see whether it
150 redirects somewhere else."
152 (let ((url (get-text-property (point) 'shr-url
)))
155 (message "No URL under point"))
156 ;; Resolve redirected URLs.
157 ((equal url
(car kill-ring
))
162 (eq (car a
) :redirect
))
165 (goto-char (point-min))
166 ;; Remove common tracking junk from the URL.
167 (when (re-search-forward ".utm_.*" nil t
)
168 (replace-match "" t t
))
169 (message "Copied %s" (buffer-string))
170 (copy-region-as-kill (point-min) (point-max)))))))
171 ;; Copy the URL to the kill ring.
175 (copy-region-as-kill (point-min) (point-max))
176 (message "Copied %s" url
))))))
178 (defun shr-show-alt-text ()
179 "Show the ALT text of the image under point."
181 (let ((text (get-text-property (point) 'shr-alt
)))
183 (message "No image under point")
184 (message "%s" text
))))
186 (defun shr-browse-image ()
187 "Browse the image under point."
189 (let ((url (get-text-property (point) 'image-url
)))
191 (message "No image under point")
192 (message "Browsing %s..." url
)
195 (defun shr-insert-image ()
196 "Insert the image under point into the buffer."
198 (let ((url (get-text-property (point) 'image-url
)))
200 (message "No image under point")
201 (message "Inserting %s..." url
)
202 (url-retrieve url
'shr-image-fetched
203 (list (current-buffer) (1- (point)) (point-marker))
206 ;;; Utility functions.
208 (defun shr-transform-dom (dom)
209 (let ((result (list (pop dom
))))
210 (dolist (arg (pop dom
))
211 (push (cons (intern (concat ":" (symbol-name (car arg
))) obarray
)
216 (push (cons 'text sub
) result
)
217 (push (shr-transform-dom sub
) result
)))
220 (defun shr-descend (dom)
221 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom
))) obarray
))
222 (style (cdr (assq :style
(cdr dom
))))
223 (shr-stylesheet shr-stylesheet
)
226 (if (string-match "color" style
)
227 (setq shr-stylesheet
(nconc (shr-parse-style style
)
230 (if (fboundp function
)
231 (funcall function
(cdr dom
))
232 (shr-generic (cdr dom
)))
233 ;; If style is set, then this node has set the color.
235 (shr-colorize-region start
(point)
236 (cdr (assq 'color shr-stylesheet
))
237 (cdr (assq 'background-color shr-stylesheet
))))))
239 (defun shr-generic (cont)
242 ((eq (car sub
) 'text
)
243 (shr-insert (cdr sub
)))
245 (shr-descend sub
)))))
247 (defmacro shr-char-breakable-p
(char)
248 "Return non-nil if a line can be broken before and after CHAR."
249 `(aref fill-find-break-point-function-table
,char
))
250 (defmacro shr-char-nospace-p
(char)
251 "Return non-nil if no space is required before and after CHAR."
252 `(aref fill-nospace-between-words-table
,char
))
254 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
255 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
256 ;; parentheses, and so on, that should not be placed in the beginning
257 ;; of a line or the end of a line.
258 (defmacro shr-char-kinsoku-bol-p
(char)
259 "Return non-nil if a line ought not to begin with CHAR."
260 `(aref (char-category-set ,char
) ?
>))
261 (defmacro shr-char-kinsoku-eol-p
(char)
262 "Return non-nil if a line ought not to end with CHAR."
263 `(aref (char-category-set ,char
) ?
<))
264 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
265 (load "kinsoku" nil t
))
267 (defun shr-insert (text)
268 (when (and (eq shr-state
'image
)
269 (not (string-match "\\`[ \t\n]+\\'" text
)))
271 (setq shr-state nil
))
273 ((eq shr-folding-mode
'none
)
276 (when (and (string-match "\\`[ \t\n]" text
)
278 (not (eq (char-after (1- (point))) ?
)))
280 (dolist (elem (split-string text
))
282 (> shr-indentation
0))
284 ;; No space is needed behind a wide character categorized as
285 ;; kinsoku-bol, between characters both categorized as nospace,
286 ;; or at the beginning of a line.
288 (when (and (> (current-column) shr-indentation
)
289 (eq (preceding-char) ?
)
290 (or (= (line-beginning-position) (1- (point)))
291 (and (shr-char-breakable-p
292 (setq prev
(char-after (- (point) 2))))
293 (shr-char-kinsoku-bol-p prev
))
294 (and (shr-char-nospace-p prev
)
295 (shr-char-nospace-p (aref elem
0)))))
297 ;; The shr-start is a special variable that is used to pass
298 ;; upwards the first point in the buffer where the text really
301 (setq shr-start
(point)))
304 (while (and (> (current-column) shr-width
)
306 (setq found
(shr-find-fill-point))
308 (when (eq (preceding-char) ?
)
312 (put-text-property (1- (point)) (point) 'shr-break t
)
313 ;; No space is needed at the beginning of a line.
314 (when (eq (following-char) ?
)
316 (when (> shr-indentation
0)
320 (unless (string-match "[ \t\n]\\'" text
)
323 (defun shr-find-fill-point ()
324 (when (> (move-to-column shr-width
) shr-width
)
328 (while (not (or (setq failed
(= (current-column) shr-indentation
))
329 (eq (preceding-char) ?
)
330 (eq (following-char) ?
)
331 (shr-char-breakable-p (preceding-char))
332 (shr-char-breakable-p (following-char))
333 (if (eq (preceding-char) ?
')
334 (not (memq (char-after (- (point) 2))
336 (and (shr-char-kinsoku-bol-p (preceding-char))
337 (shr-char-breakable-p (following-char))
338 (not (shr-char-kinsoku-bol-p (following-char)))))
339 (shr-char-kinsoku-eol-p (following-char))))
341 (if (and (not (or failed
(eolp)))
342 (eq (preceding-char) ?
'))
343 (while (not (or (setq failed
(eolp))
344 (eq (following-char) ?
)
345 (shr-char-breakable-p (following-char))
346 (shr-char-kinsoku-eol-p (following-char))))
349 ;; There's no breakable point, so we give it up.
352 (unless shr-kinsoku-shorten
353 (while (and (setq found
(re-search-forward
354 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
355 (line-end-position) 'move
))
356 (eq (preceding-char) ?
')))
357 (if (and found
(not (match-beginning 1)))
358 (goto-char (match-beginning 0)))))
361 ;; Don't put kinsoku-bol characters at the beginning of a line,
362 ;; or kinsoku-eol characters at the end of a line.
365 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
366 (shr-char-kinsoku-eol-p (preceding-char)))
368 (when (setq failed
(= (current-column) shr-indentation
))
369 ;; There's no breakable point that doesn't violate kinsoku,
370 ;; so we look for the second best position.
373 (<= (current-column) shr-width
))
376 (shr-char-kinsoku-eol-p (following-char)))))
378 ((shr-char-kinsoku-eol-p (preceding-char))
379 (if (shr-char-kinsoku-eol-p (following-char))
380 ;; There are consecutive kinsoku-eol characters.
386 (and (> (setq count
(1- count
)) 0)
387 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
388 (or (shr-char-kinsoku-eol-p (preceding-char))
389 (shr-char-kinsoku-bol-p (following-char)))))))
390 (if (setq failed
(= (current-column) shr-indentation
))
391 ;; There's no breakable point that doesn't violate kinsoku,
392 ;; so we go to the second best position.
393 (if (looking-at "\\(\\c<+\\)\\c<")
394 (goto-char (match-end 1))
397 (if (shr-char-kinsoku-bol-p (preceding-char))
398 ;; There are consecutive kinsoku-bol characters.
401 (while (and (>= (setq count
(1- count
)) 0)
402 (shr-char-kinsoku-bol-p (following-char))
403 (shr-char-breakable-p (following-char)))
404 (forward-char 1))))))
405 (when (eq (following-char) ?
)
409 (defun shr-expand-url (url)
413 (string-match "\\`[a-z]*:" url
)
416 ((and (not (string-match "/\\'" shr-base
))
417 (not (string-match "\\`/" url
)))
418 (concat shr-base
"/" url
))
420 (concat shr-base url
))))
422 (defun shr-ensure-newline ()
423 (unless (zerop (current-column))
426 (defun shr-ensure-paragraph ()
428 (if (<= (current-column) shr-indentation
)
429 (unless (save-excursion
440 (when (> shr-indentation
0)
441 (insert (make-string shr-indentation ?
))))
443 (defun shr-fontize-cont (cont &rest types
)
447 (shr-add-font (or shr-start
(point)) (point) type
))))
449 ;; Add an overlay in the region, but avoid putting the font properties
450 ;; on blank text at the start of the line, and the newline at the end,
451 ;; to avoid ugliness.
452 (defun shr-add-font (start end type
)
455 (while (< (point) end
)
457 (skip-chars-forward " "))
458 (let ((overlay (make-overlay (point) (min (line-end-position) end
))))
459 (overlay-put overlay
'face type
))
460 (if (< (line-end-position) end
)
464 (defun shr-browse-url ()
465 "Browse the URL under point."
467 (let ((url (get-text-property (point) 'shr-url
)))
470 (message "No link under point"))
471 ((string-match "^mailto:" url
)
472 (browse-url-mailto url
))
476 (defun shr-save-contents (directory)
477 "Save the contents from URL in a file."
478 (interactive "DSave contents of URL to directory: ")
479 (let ((url (get-text-property (point) 'shr-url
)))
481 (message "No link under point")
482 (url-retrieve (shr-encode-url url
)
483 'shr-store-contents
(list url directory
)))))
485 (defun shr-store-contents (status url directory
)
486 (unless (plist-get status
:error
)
487 (when (or (search-forward "\n\n" nil t
)
488 (search-forward "\r\n\r\n" nil t
))
489 (write-region (point) (point-max)
490 (expand-file-name (file-name-nondirectory url
)
493 (defun shr-image-fetched (status buffer start end
)
494 (when (and (buffer-name buffer
)
495 (not (plist-get status
:error
)))
496 (url-store-in-cache (current-buffer))
497 (when (or (search-forward "\n\n" nil t
)
498 (search-forward "\r\n\r\n" nil t
))
499 (let ((data (buffer-substring (point) (point-max))))
500 (with-current-buffer buffer
502 (let ((alt (buffer-substring start end
))
503 (inhibit-read-only t
))
504 (delete-region start end
)
506 (funcall shr-put-image-function data alt
)))))))
507 (kill-buffer (current-buffer)))
509 (defun shr-put-image (data alt
)
510 "Put image DATA with a string ALT. Return image."
511 (if (display-graphic-p)
512 (let ((image (ignore-errors
513 (shr-rescale-image data
))))
515 ;; When inserting big-ish pictures, put them at the
516 ;; beginning of the line.
517 (when (and (> (current-column) 0)
518 (> (car (image-size image t
)) 400))
520 (insert-image image
(or alt
"*")))
524 (defun shr-rescale-image (data)
525 (if (or (not (fboundp 'imagemagick-types
))
526 (not (get-buffer-window (current-buffer))))
527 (create-image data nil t
)
528 (let* ((image (create-image data nil t
))
529 (size (image-size image t
))
532 (edges (window-inside-pixel-edges
533 (get-buffer-window (current-buffer))))
534 (window-width (truncate (* shr-max-image-proportion
535 (- (nth 2 edges
) (nth 0 edges
)))))
536 (window-height (truncate (* shr-max-image-proportion
537 (- (nth 3 edges
) (nth 1 edges
)))))
539 (when (> height window-height
)
540 (setq image
(or (create-image data
'imagemagick t
541 :height window-height
)
543 (setq size
(image-size image t
)))
544 (when (> (car size
) window-width
)
546 (create-image data
'imagemagick t
549 (when (and (fboundp 'create-animated-image
)
550 (eq (image-type data nil t
) 'gif
))
551 (setq image
(create-animated-image data
'gif t
)))
554 ;; url-cache-extract autoloads url-cache.
555 (declare-function url-cache-create-filename
"url-cache" (url))
556 (autoload 'mm-disable-multibyte
"mm-util")
557 (autoload 'browse-url-mailto
"browse-url")
559 (defun shr-get-image-data (url)
560 "Get image data for URL.
561 Return a string with image data."
563 (mm-disable-multibyte)
565 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
567 (when (or (search-forward "\n\n" nil t
)
568 (search-forward "\r\n\r\n" nil t
))
569 (buffer-substring (point) (point-max))))))
571 (defun shr-image-displayer (content-function)
572 "Return a function to display an image.
573 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
574 is an argument. The function to be returned takes three arguments URL,
575 START, and END. Note that START and END should be merkers."
576 `(lambda (url start end
)
578 (if (string-match "\\`cid:" url
)
579 ,(when content-function
580 `(let ((image (funcall ,content-function
581 (substring url
(match-end 0)))))
584 (funcall shr-put-image-function
585 image
(buffer-substring start end
))
586 (delete-region (point) end
))))
587 (url-retrieve url
'shr-image-fetched
588 (list (current-buffer) start end
)
591 (defun shr-heading (cont &rest types
)
592 (shr-ensure-paragraph)
593 (apply #'shr-fontize-cont cont types
)
594 (shr-ensure-paragraph))
596 (autoload 'widget-convert-button
"wid-edit")
598 (defun shr-urlify (start url
&optional title
)
599 (widget-convert-button
600 'url-link start
(point)
601 :help-echo
(if title
(format "%s (%s)" url title
) url
)
604 (put-text-property start
(point) 'face
'shr-link
)
605 (put-text-property start
(point) 'shr-url url
))
607 (defun shr-encode-url (url)
609 (browse-url-url-encode-chars url
"[)$ ]"))
611 (autoload 'shr-color-visible
"shr-color")
612 (autoload 'shr-color-
>hexadecimal
"shr-color")
614 (defun shr-color-check (fg bg
)
615 "Check that FG is visible on BG.
616 Returns (fg bg) with corrected values.
617 Returns nil if the colors that would be used are the default
618 ones, in case fg and bg are nil."
620 (let ((fixed (cond ((null fg
) 'fg
)
622 ;; Convert colors to hexadecimal, or set them to default.
623 (let ((fg (or (shr-color->hexadecimal fg
)
624 (frame-parameter nil
'foreground-color
)))
625 (bg (or (shr-color->hexadecimal bg
)
626 (frame-parameter nil
'background-color
))))
627 (cond ((eq fixed
'bg
)
628 ;; Only return the new fg
629 (list nil
(cadr (shr-color-visible bg fg t
))))
631 ;; Invert args and results and return only the new bg
632 (list (cadr (shr-color-visible fg bg t
)) nil
))
634 (shr-color-visible bg fg
)))))))
636 (defun shr-colorize-region (start end fg
&optional bg
)
638 (let ((new-colors (shr-color-check fg bg
)))
641 (shr-put-color start end
:foreground
(cadr new-colors
)))
643 (shr-put-color start end
:background
(car new-colors
))))
646 ;; Put a color in the region, but avoid putting colors on blank
647 ;; text at the start of the line, and the newline at the end, to avoid
648 ;; ugliness. Also, don't overwrite any existing color information,
649 ;; since this can be called recursively, and we want the "inner" color
651 (defun shr-put-color (start end type color
)
654 (while (< (point) end
)
656 (not (eq type
:background
)))
657 (skip-chars-forward " "))
658 (when (> (line-end-position) (point))
659 (shr-put-color-1 (point) (min (line-end-position) end
) type color
))
660 (if (< (line-end-position) end
)
663 (when (and (eq type
:background
)
664 (= shr-table-depth
0))
665 (shr-expand-newlines start end color
))))
667 (defun shr-expand-newlines (start end color
)
669 ;; Skip past all white space at the start and ends.
671 (skip-chars-forward " \t\n")
675 (skip-chars-backward " \t\n")
678 (narrow-to-region start end
)
679 (let ((width (shr-natural-width))
681 (goto-char (point-min))
684 (when (and (< (setq column
(current-column)) width
)
685 (< (setq column
(shr-previous-newline-padding-width column
))
687 (let ((overlay (make-overlay (point) (1+ (point)))))
688 (overlay-put overlay
'before-string
692 (let ((string (plist-get
693 (overlay-properties overlay
)
697 (overlay-put overlay
'before-string
"")
699 (overlays-at (point))
701 (propertize (make-string (- width column
) ?
)
702 'face
(list :background color
))))))
705 (defun shr-previous-newline-padding-width (width)
706 (let ((overlays (overlays-at (point)))
710 (dolist (overlay overlays
)
713 (length (plist-get (overlay-properties overlay
)
715 (+ width previous-width
))))
717 (defun shr-put-color-1 (start end type color
)
718 (let* ((old-props (get-text-property start
'face
))
719 (do-put (and (listp old-props
)
720 (not (memq type old-props
))))
723 (setq change
(next-single-property-change start
'face nil end
))
725 (put-text-property start change
'face
726 (nconc (list type color
) old-props
)))
727 (setq old-props
(get-text-property change
'face
))
728 (setq do-put
(and (listp old-props
)
729 (not (memq type old-props
))))
733 (put-text-property start end
'face
734 (nconc (list type color old-props
))))))
736 ;;; Tag-specific rendering rules.
738 (defun shr-tag-body (cont)
739 (let* ((start (point))
740 (fgcolor (cdr (or (assq :fgcolor cont
)
742 (bgcolor (cdr (assq :bgcolor cont
)))
743 (shr-stylesheet (list (cons 'color fgcolor
)
744 (cons 'background-color bgcolor
))))
746 (shr-colorize-region start
(point) fgcolor bgcolor
)))
748 (defun shr-tag-style (cont)
751 (defun shr-tag-script (cont)
754 (defun shr-tag-sup (cont)
755 (let ((start (point)))
757 (put-text-property start
(point) 'display
'(raise 0.5))))
759 (defun shr-tag-sub (cont)
760 (let ((start (point)))
762 (put-text-property start
(point) 'display
'(raise -
0.5))))
764 (defun shr-tag-label (cont)
766 (shr-ensure-paragraph))
768 (defun shr-tag-p (cont)
769 (shr-ensure-paragraph)
772 (shr-ensure-paragraph))
774 (defun shr-tag-div (cont)
778 (shr-ensure-newline))
780 (defun shr-tag-s (cont)
781 (shr-fontize-cont cont
'shr-strike-through
))
783 (defun shr-tag-del (cont)
784 (shr-fontize-cont cont
'shr-strike-through
))
786 (defun shr-tag-b (cont)
787 (shr-fontize-cont cont
'bold
))
789 (defun shr-tag-i (cont)
790 (shr-fontize-cont cont
'italic
))
792 (defun shr-tag-em (cont)
793 (shr-fontize-cont cont
'bold
))
795 (defun shr-tag-strong (cont)
796 (shr-fontize-cont cont
'bold
))
798 (defun shr-tag-u (cont)
799 (shr-fontize-cont cont
'underline
))
801 (defun shr-parse-style (style)
804 (when (string-match "\n" style
)
805 (setq style
(replace-match " " t t style
))))
807 (dolist (elem (split-string style
";"))
809 (setq elem
(split-string elem
":"))
810 (when (and (car elem
)
812 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
813 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
814 (when (string-match " *!important\\'" value
)
815 (setq value
(substring value
0 (match-beginning 0))))
816 (push (cons (intern name obarray
)
821 (defun shr-tag-base (cont)
822 (setq shr-base
(cdr (assq :href cont
))))
824 (defun shr-tag-a (cont)
825 (let ((url (cdr (assq :href cont
)))
826 (title (cdr (assq :title cont
)))
830 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
)))
832 (defun shr-tag-object (cont)
833 (let ((start (point))
836 (when (eq (car elem
) 'embed
)
837 (setq url
(or url
(cdr (assq :src
(cdr elem
))))))
838 (when (and (eq (car elem
) 'param
)
839 (equal (cdr (assq :name
(cdr elem
))) "movie"))
840 (setq url
(or url
(cdr (assq :value
(cdr elem
)))))))
842 (shr-insert " [multimedia] ")
843 (shr-urlify start
(shr-expand-url url
)))
846 (defun shr-tag-video (cont)
847 (let ((image (cdr (assq :poster cont
)))
848 (url (cdr (assq :src cont
)))
850 (shr-tag-img nil image
)
851 (shr-urlify start
(shr-expand-url url
))))
853 (defun shr-tag-img (cont &optional url
)
856 (cdr (assq :src cont
))))
857 (when (and (> (current-column) 0)
858 (not (eq shr-state
'image
)))
860 (let ((alt (cdr (assq :alt cont
)))
861 (url (shr-expand-url (or url
(cdr (assq :src cont
))))))
862 (let ((start (point-marker)))
863 (when (zerop (length alt
))
866 ((or (member (cdr (assq :height cont
)) '("0" "1"))
867 (member (cdr (assq :width cont
)) '("0" "1")))
868 ;; Ignore zero-sized or single-pixel images.
870 ((and (not shr-inhibit-images
)
871 (string-match "\\`cid:" url
))
872 (let ((url (substring url
(match-end 0)))
874 (if (or (not shr-content-function
)
875 (not (setq image
(funcall shr-content-function url
))))
877 (funcall shr-put-image-function image alt
))))
878 ((or shr-inhibit-images
879 (and shr-blocked-images
880 (string-match shr-blocked-images url
)))
881 (setq shr-start
(point))
882 (let ((shr-state 'space
))
883 (if (> (string-width alt
) 8)
884 (shr-insert (truncate-string-to-width alt
8))
886 ((url-is-cached (shr-encode-url url
))
887 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
891 (if (fboundp 'url-queue-retrieve
)
894 (shr-encode-url url
) 'shr-image-fetched
895 (list (current-buffer) start
(point-marker))
897 (put-text-property start
(point) 'keymap shr-map
)
898 (put-text-property start
(point) 'shr-alt alt
)
899 (put-text-property start
(point) 'image-url url
)
900 (put-text-property start
(point) 'image-displayer
901 (shr-image-displayer shr-content-function
))
902 (put-text-property start
(point) 'help-echo alt
)
903 (setq shr-state
'image
)))))
905 (defun shr-tag-pre (cont)
906 (let ((shr-folding-mode 'none
))
910 (shr-ensure-newline)))
912 (defun shr-tag-blockquote (cont)
913 (shr-ensure-paragraph)
915 (let ((shr-indentation (+ shr-indentation
4)))
917 (shr-ensure-paragraph))
919 (defun shr-tag-ul (cont)
920 (shr-ensure-paragraph)
921 (let ((shr-list-mode 'ul
))
923 (shr-ensure-paragraph))
925 (defun shr-tag-ol (cont)
926 (shr-ensure-paragraph)
927 (let ((shr-list-mode 1))
929 (shr-ensure-paragraph))
931 (defun shr-tag-li (cont)
932 (shr-ensure-paragraph)
935 (if (numberp shr-list-mode
)
937 (format "%d " shr-list-mode
)
938 (setq shr-list-mode
(1+ shr-list-mode
)))
940 (shr-indentation (+ shr-indentation
(length bullet
))))
944 (defun shr-tag-br (cont)
950 (defun shr-tag-h1 (cont)
951 (shr-heading cont
'bold
'underline
))
953 (defun shr-tag-h2 (cont)
954 (shr-heading cont
'bold
))
956 (defun shr-tag-h3 (cont)
957 (shr-heading cont
'italic
))
959 (defun shr-tag-h4 (cont)
962 (defun shr-tag-h5 (cont)
965 (defun shr-tag-h6 (cont)
968 (defun shr-tag-hr (cont)
970 (insert (make-string shr-width shr-hr-line
) "\n"))
972 (defun shr-tag-title (cont)
973 (shr-heading cont
'bold
'underline
))
975 (defun shr-tag-font (cont)
976 (let* ((start (point))
977 (color (cdr (assq :color cont
)))
978 (shr-stylesheet (nconc (list (cons 'color color
))
982 (shr-colorize-region start
(point) color
983 (cdr (assq 'background-color shr-stylesheet
))))))
985 ;;; Table rendering algorithm.
987 ;; Table rendering is the only complicated thing here. We do this by
988 ;; first counting how many TDs there are in each TR, and registering
989 ;; how wide they think they should be ("width=45%", etc). Then we
990 ;; render each TD separately (this is done in temporary buffers, so
991 ;; that we can use all the rendering machinery as if we were in the
992 ;; main buffer). Now we know how much space each TD really takes, so
993 ;; we then render everything again with the new widths, and finally
994 ;; insert all these boxes into the main buffer.
995 (defun shr-tag-table-1 (cont)
996 (setq cont
(or (cdr (assq 'tbody cont
))
998 (let* ((shr-inhibit-images t
)
999 (shr-table-depth (1+ shr-table-depth
))
1000 (shr-kinsoku-shorten t
)
1001 ;; Find all suggested widths.
1002 (columns (shr-column-specs cont
))
1003 ;; Compute how many characters wide each TD should be.
1004 (suggested-widths (shr-pro-rate-columns columns
))
1005 ;; Do a "test rendering" to see how big each TD is (this can
1006 ;; be smaller (if there's little text) or bigger (if there's
1007 ;; unbreakable text).
1008 (sketch (shr-make-table cont suggested-widths
))
1009 (sketch-widths (shr-table-widths sketch suggested-widths
)))
1010 ;; This probably won't work very well.
1011 (when (> (+ (loop for width across sketch-widths
1015 (setq truncate-lines t
))
1016 ;; Then render the table again with these new "hard" widths.
1017 (shr-insert-table (shr-make-table cont sketch-widths t
) sketch-widths
))
1018 ;; Finally, insert all the images after the table. The Emacs buffer
1019 ;; model isn't strong enough to allow us to put the images actually
1021 (when (zerop shr-table-depth
)
1022 (dolist (elem (shr-find-elements cont
'img
))
1023 (shr-tag-img (cdr elem
)))))
1025 (defun shr-tag-table (cont)
1026 (shr-ensure-paragraph)
1027 (let* ((caption (cdr (assq 'caption cont
)))
1028 (header (cdr (assq 'thead cont
)))
1029 (body (or (cdr (assq 'tbody cont
)) cont
))
1030 (footer (cdr (assq 'tfoot cont
)))
1031 (bgcolor (cdr (assq :bgcolor cont
)))
1033 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1035 (nheader (if header
(shr-max-columns header
)))
1036 (nbody (if body
(shr-max-columns body
)))
1037 (nfooter (if footer
(shr-max-columns footer
))))
1040 (if caption
`((tr (td ,@caption
))))
1043 ;; hader + body + footer
1044 (if (= nheader nbody
)
1045 (if (= nbody nfooter
)
1046 `((tr (td (table (tbody ,@header
,@body
,@footer
)))))
1047 (nconc `((tr (td (table (tbody ,@header
,@body
)))))
1050 `((tr (td (table (tbody ,@footer
))))))))
1051 (nconc `((tr (td (table (tbody ,@header
)))))
1052 (if (= nbody nfooter
)
1053 `((tr (td (table (tbody ,@body
,@footer
)))))
1054 (nconc `((tr (td (table (tbody ,@body
)))))
1057 `((tr (td (table (tbody ,@footer
))))))))))
1059 (if (= nheader nbody
)
1060 `((tr (td (table (tbody ,@header
,@body
)))))
1062 `(,@header
(tr (td (table (tbody ,@body
)))))
1063 `((tr (td (table (tbody ,@header
))))
1064 (tr (td (table (tbody ,@body
))))))))
1067 (if (= nbody nfooter
)
1068 `((tr (td (table (tbody ,@body
,@footer
)))))
1069 (nconc `((tr (td (table (tbody ,@body
)))))
1072 `((tr (td (table (tbody ,@footer
))))))))
1074 `((tr (td (table (tbody ,@body
)))))
1077 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1080 (defun shr-find-elements (cont type
)
1083 (cond ((eq (car elem
) type
)
1086 (setq result
(nconc (shr-find-elements (cdr elem
) type
) result
)))))
1089 (defun shr-insert-table (table widths
)
1090 (shr-insert-table-ruler widths
)
1092 (let ((start (point))
1093 (height (let ((max 0))
1094 (dolist (column row
)
1095 (setq max
(max max
(cadr column
))))
1099 (insert shr-table-vertical-line
"\n"))
1100 (dolist (column row
)
1102 (let ((lines (nth 2 column
))
1103 (overlay-lines (nth 3 column
))
1104 overlay overlay-line
)
1105 (dolist (line lines
)
1106 (setq overlay-line
(pop overlay-lines
))
1108 (insert line shr-table-vertical-line
)
1109 (dolist (overlay overlay-line
)
1110 (let ((o (make-overlay (- (point) (nth 0 overlay
) 1)
1111 (- (point) (nth 1 overlay
) 1)))
1112 (properties (nth 2 overlay
)))
1114 (overlay-put o
(pop properties
) (pop properties
)))))
1116 ;; Add blank lines at padding at the bottom of the TD,
1118 (dotimes (i (- height
(length lines
)))
1120 (let ((start (point)))
1121 (insert (make-string (string-width (car lines
)) ?
)
1122 shr-table-vertical-line
)
1123 (when (nth 4 column
)
1124 (shr-put-color start
(1- (point)) :background
(nth 4 column
))))
1125 (forward-line 1)))))
1126 (shr-insert-table-ruler widths
)))
1128 (defun shr-insert-table-ruler (widths)
1130 (> shr-indentation
0))
1132 (insert shr-table-corner
)
1133 (dotimes (i (length widths
))
1134 (insert (make-string (aref widths i
) shr-table-horizontal-line
)
1138 (defun shr-table-widths (table suggested-widths
)
1139 (let* ((length (length suggested-widths
))
1140 (widths (make-vector length
0))
1141 (natural-widths (make-vector length
0)))
1144 (dolist (column row
)
1145 (aset widths i
(max (aref widths i
)
1147 (aset natural-widths i
(max (aref natural-widths i
)
1150 (let ((extra (- (apply '+ (append suggested-widths nil
))
1151 (apply '+ (append widths nil
))))
1152 (expanded-columns 0))
1155 ;; If the natural width is wider than the rendered width, we
1156 ;; want to allow the column to expand.
1157 (when (> (aref natural-widths i
) (aref widths i
))
1158 (setq expanded-columns
(1+ expanded-columns
))))
1160 (when (> (aref natural-widths i
) (aref widths i
))
1162 (1+ (aref natural-widths i
))
1163 (+ (/ extra expanded-columns
)
1164 (aref widths i
))))))))
1167 (defun shr-make-table (cont widths
&optional fill
)
1170 (when (eq (car row
) 'tr
)
1175 (while (< i
(length widths
))
1176 (setq column
(pop columns
))
1177 (when (or (memq (car column
) '(td th
))
1179 (push (shr-render-td (cdr column
) (aref widths i
) fill
)
1182 (push (nreverse tds
) trs
))))
1185 (defun shr-render-td (cont width fill
)
1187 (let ((bgcolor (cdr (assq :bgcolor cont
)))
1188 (fgcolor (cdr (assq :fgcolor cont
)))
1189 (style (cdr (assq :style cont
)))
1190 (shr-stylesheet shr-stylesheet
)
1191 overlays actual-colors
)
1193 (setq style
(and (string-match "color" style
)
1194 (shr-parse-style style
))))
1196 (setq style
(nconc (list (cons 'background-color bgcolor
)) style
)))
1198 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1200 (setq shr-stylesheet
(append style shr-stylesheet
)))
1201 (let ((cache (cdr (assoc (cons width cont
) shr-content-cache
))))
1204 (insert (car cache
))
1205 (let ((end (length (car cache
))))
1206 (dolist (overlay (cadr cache
))
1208 (make-overlay (1+ (- end
(nth 0 overlay
)))
1209 (1+ (- end
(nth 1 overlay
)))))
1210 (properties (nth 2 overlay
)))
1212 (overlay-put new-overlay
1213 (pop properties
) (pop properties
)))))))
1214 (let ((shr-width width
)
1215 (shr-indentation 0))
1216 (shr-descend (cons 'td cont
)))
1220 (skip-chars-backward " \t\n")))
1221 (push (list (cons width cont
) (buffer-string)
1222 (shr-overlays-in-region (point-min) (point-max)))
1223 shr-content-cache
)))
1224 (goto-char (point-min))
1228 (setq max
(max max
(current-column)))
1231 (goto-char (point-min))
1232 ;; If the buffer is totally empty, then put a single blank
1234 (if (zerop (buffer-size))
1235 (insert (make-string width ?
))
1236 ;; Otherwise, fill the buffer.
1239 (when (> (- width
(current-column)) 0)
1240 (insert (make-string (- width
(current-column)) ?
)))
1244 (shr-colorize-region
1245 (point-min) (point-max)
1246 (cdr (assq 'color shr-stylesheet
))
1247 (cdr (assq 'background-color shr-stylesheet
))))))
1250 (count-lines (point-min) (point-max))
1251 (split-string (buffer-string) "\n")
1252 (shr-collect-overlays)
1253 (car actual-colors
))
1255 (shr-natural-width)))))))
1257 (defun shr-natural-width ()
1258 (goto-char (point-min))
1263 (setq current
(+ current
(current-column)))
1264 (unless (get-text-property (point) 'shr-break
)
1265 (setq max
(max max current
)
1270 (defun shr-collect-overlays ()
1272 (goto-char (point-min))
1273 (let ((overlays nil
))
1275 (push (shr-overlays-in-region (point) (line-end-position))
1278 (nreverse overlays
))))
1280 (defun shr-overlays-in-region (start end
)
1282 (dolist (overlay (overlays-in start end
))
1283 (push (list (if (> start
(overlay-start overlay
))
1285 (- end
(overlay-start overlay
)))
1286 (if (< end
(overlay-end overlay
))
1288 (- end
(overlay-end overlay
)))
1289 (overlay-properties overlay
))
1293 (defun shr-pro-rate-columns (columns)
1294 (let ((total-percentage 0)
1295 (widths (make-vector (length columns
) 0)))
1296 (dotimes (i (length columns
))
1297 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1298 (setq total-percentage
(/ 1.0 total-percentage
))
1299 (dotimes (i (length columns
))
1300 (aset widths i
(max (truncate (* (aref columns i
)
1302 (- shr-width
(1+ (length columns
)))))
1306 ;; Return a summary of the number and shape of the TDs in the table.
1307 (defun shr-column-specs (cont)
1308 (let ((columns (make-vector (shr-max-columns cont
) 1)))
1310 (when (eq (car row
) 'tr
)
1312 (dolist (column (cdr row
))
1313 (when (memq (car column
) '(td th
))
1314 (let ((width (cdr (assq :width
(cdr column
)))))
1316 (string-match "\\([0-9]+\\)%" width
))
1318 (/ (string-to-number (match-string 1 width
))
1320 (setq i
(1+ i
)))))))
1323 (defun shr-count (cont elem
)
1326 (when (eq (car sub
) elem
)
1330 (defun shr-max-columns (cont)
1333 (when (eq (car row
) 'tr
)
1334 (setq max
(max max
(+ (shr-count (cdr row
) 'td
)
1335 (shr-count (cdr row
) 'th
))))))
1340 ;;; shr.el ends here