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
)
115 (defvar shr-ignore-cache nil
)
118 (let ((map (make-sparse-keymap)))
119 (define-key map
"a" 'shr-show-alt-text
)
120 (define-key map
"i" 'shr-browse-image
)
121 (define-key map
"I" 'shr-insert-image
)
122 (define-key map
"u" 'shr-copy-url
)
123 (define-key map
"v" 'shr-browse-url
)
124 (define-key map
"o" 'shr-save-contents
)
125 (define-key map
"\r" 'shr-browse-url
)
128 ;; Public functions and commands.
130 (defun shr-visit-file (file)
131 (interactive "fHTML file name: ")
132 (pop-to-buffer "*html*")
136 (insert-file-contents file
)
137 (libxml-parse-html-region (point-min) (point-max)))))
140 (defun shr-insert-document (dom)
141 (setq shr-content-cache nil
)
142 (let ((shr-state nil
)
145 (shr-width (or shr-width
(window-width))))
146 (shr-descend (shr-transform-dom dom
))))
148 (defun shr-copy-url ()
149 "Copy the URL under point to the kill ring.
150 If called twice, then try to fetch the URL and see whether it
151 redirects somewhere else."
153 (let ((url (get-text-property (point) 'shr-url
)))
156 (message "No URL under point"))
157 ;; Resolve redirected URLs.
158 ((equal url
(car kill-ring
))
163 (eq (car a
) :redirect
))
166 (goto-char (point-min))
167 ;; Remove common tracking junk from the URL.
168 (when (re-search-forward ".utm_.*" nil t
)
169 (replace-match "" t t
))
170 (message "Copied %s" (buffer-string))
171 (copy-region-as-kill (point-min) (point-max)))))))
172 ;; Copy the URL to the kill ring.
176 (copy-region-as-kill (point-min) (point-max))
177 (message "Copied %s" url
))))))
179 (defun shr-show-alt-text ()
180 "Show the ALT text of the image under point."
182 (let ((text (get-text-property (point) 'shr-alt
)))
184 (message "No image under point")
185 (message "%s" text
))))
187 (defun shr-browse-image (&optional copy-url
)
188 "Browse the image under point.
189 If COPY-URL (the prefix if called interactively) is non-nil, copy
190 the URL of the image to the kill buffer instead."
192 (let ((url (get-text-property (point) 'image-url
)))
195 (message "No image under point"))
199 (copy-region-as-kill (point-min) (point-max))
200 (message "Copied %s" url
)))
202 (message "Browsing %s..." url
)
205 (defun shr-insert-image ()
206 "Insert the image under point into the buffer."
208 (let ((url (get-text-property (point) 'image-url
)))
210 (message "No image under point")
211 (message "Inserting %s..." url
)
212 (url-retrieve url
'shr-image-fetched
213 (list (current-buffer) (1- (point)) (point-marker))
216 ;;; Utility functions.
218 (defun shr-transform-dom (dom)
219 (let ((result (list (pop dom
))))
220 (dolist (arg (pop dom
))
221 (push (cons (intern (concat ":" (symbol-name (car arg
))) obarray
)
226 (push (cons 'text sub
) result
)
227 (push (shr-transform-dom sub
) result
)))
230 (defun shr-descend (dom)
231 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom
))) obarray
))
232 (style (cdr (assq :style
(cdr dom
))))
233 (shr-stylesheet shr-stylesheet
)
236 (if (string-match "color" style
)
237 (setq shr-stylesheet
(nconc (shr-parse-style style
)
240 (if (fboundp function
)
241 (funcall function
(cdr dom
))
242 (shr-generic (cdr dom
)))
243 ;; If style is set, then this node has set the color.
245 (shr-colorize-region start
(point)
246 (cdr (assq 'color shr-stylesheet
))
247 (cdr (assq 'background-color shr-stylesheet
))))))
249 (defun shr-generic (cont)
252 ((eq (car sub
) 'text
)
253 (shr-insert (cdr sub
)))
255 (shr-descend sub
)))))
257 (defmacro shr-char-breakable-p
(char)
258 "Return non-nil if a line can be broken before and after CHAR."
259 `(aref fill-find-break-point-function-table
,char
))
260 (defmacro shr-char-nospace-p
(char)
261 "Return non-nil if no space is required before and after CHAR."
262 `(aref fill-nospace-between-words-table
,char
))
264 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
265 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
266 ;; parentheses, and so on, that should not be placed in the beginning
267 ;; of a line or the end of a line.
268 (defmacro shr-char-kinsoku-bol-p
(char)
269 "Return non-nil if a line ought not to begin with CHAR."
270 `(aref (char-category-set ,char
) ?
>))
271 (defmacro shr-char-kinsoku-eol-p
(char)
272 "Return non-nil if a line ought not to end with CHAR."
273 `(aref (char-category-set ,char
) ?
<))
274 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
275 (load "kinsoku" nil t
))
277 (defun shr-insert (text)
278 (when (and (eq shr-state
'image
)
279 (not (string-match "\\`[ \t\n]+\\'" text
)))
281 (setq shr-state nil
))
283 ((eq shr-folding-mode
'none
)
286 (when (and (string-match "\\`[ \t\n]" text
)
288 (not (eq (char-after (1- (point))) ?
)))
290 (dolist (elem (split-string text
))
292 (> shr-indentation
0))
294 ;; No space is needed behind a wide character categorized as
295 ;; kinsoku-bol, between characters both categorized as nospace,
296 ;; or at the beginning of a line.
298 (when (and (> (current-column) shr-indentation
)
299 (eq (preceding-char) ?
)
300 (or (= (line-beginning-position) (1- (point)))
301 (and (shr-char-breakable-p
302 (setq prev
(char-after (- (point) 2))))
303 (shr-char-kinsoku-bol-p prev
))
304 (and (shr-char-nospace-p prev
)
305 (shr-char-nospace-p (aref elem
0)))))
307 ;; The shr-start is a special variable that is used to pass
308 ;; upwards the first point in the buffer where the text really
311 (setq shr-start
(point)))
314 (while (and (> (current-column) shr-width
)
316 (setq found
(shr-find-fill-point))
318 (when (eq (preceding-char) ?
)
322 (put-text-property (1- (point)) (point) 'shr-break t
)
323 ;; No space is needed at the beginning of a line.
324 (when (eq (following-char) ?
)
326 (when (> shr-indentation
0)
330 (unless (string-match "[ \t\n]\\'" text
)
333 (defun shr-find-fill-point ()
334 (when (> (move-to-column shr-width
) shr-width
)
338 (while (not (or (setq failed
(= (current-column) shr-indentation
))
339 (eq (preceding-char) ?
)
340 (eq (following-char) ?
)
341 (shr-char-breakable-p (preceding-char))
342 (shr-char-breakable-p (following-char))
343 (if (eq (preceding-char) ?
')
344 (not (memq (char-after (- (point) 2))
346 (and (shr-char-kinsoku-bol-p (preceding-char))
347 (shr-char-breakable-p (following-char))
348 (not (shr-char-kinsoku-bol-p (following-char)))))
349 (shr-char-kinsoku-eol-p (following-char))))
351 (if (and (not (or failed
(eolp)))
352 (eq (preceding-char) ?
'))
353 (while (not (or (setq failed
(eolp))
354 (eq (following-char) ?
)
355 (shr-char-breakable-p (following-char))
356 (shr-char-kinsoku-eol-p (following-char))))
359 ;; There's no breakable point, so we give it up.
362 (unless shr-kinsoku-shorten
363 (while (and (setq found
(re-search-forward
364 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
365 (line-end-position) 'move
))
366 (eq (preceding-char) ?
')))
367 (if (and found
(not (match-beginning 1)))
368 (goto-char (match-beginning 0)))))
371 ;; Don't put kinsoku-bol characters at the beginning of a line,
372 ;; or kinsoku-eol characters at the end of a line.
375 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
376 (shr-char-kinsoku-eol-p (preceding-char)))
378 (when (setq failed
(= (current-column) shr-indentation
))
379 ;; There's no breakable point that doesn't violate kinsoku,
380 ;; so we look for the second best position.
383 (<= (current-column) shr-width
))
386 (shr-char-kinsoku-eol-p (following-char)))))
388 ((shr-char-kinsoku-eol-p (preceding-char))
389 (if (shr-char-kinsoku-eol-p (following-char))
390 ;; There are consecutive kinsoku-eol characters.
396 (and (> (setq count
(1- count
)) 0)
397 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
398 (or (shr-char-kinsoku-eol-p (preceding-char))
399 (shr-char-kinsoku-bol-p (following-char)))))))
400 (if (setq failed
(= (current-column) shr-indentation
))
401 ;; There's no breakable point that doesn't violate kinsoku,
402 ;; so we go to the second best position.
403 (if (looking-at "\\(\\c<+\\)\\c<")
404 (goto-char (match-end 1))
407 (if (shr-char-kinsoku-bol-p (preceding-char))
408 ;; There are consecutive kinsoku-bol characters.
411 (while (and (>= (setq count
(1- count
)) 0)
412 (shr-char-kinsoku-bol-p (following-char))
413 (shr-char-breakable-p (following-char)))
414 (forward-char 1))))))
415 (when (eq (following-char) ?
)
419 (defun shr-expand-url (url)
423 (string-match "\\`[a-z]*:" url
)
426 ((and (not (string-match "/\\'" shr-base
))
427 (not (string-match "\\`/" url
)))
428 (concat shr-base
"/" url
))
430 (concat shr-base url
))))
432 (defun shr-ensure-newline ()
433 (unless (zerop (current-column))
436 (defun shr-ensure-paragraph ()
438 (if (<= (current-column) shr-indentation
)
439 (unless (save-excursion
450 (when (> shr-indentation
0)
451 (insert (make-string shr-indentation ?
))))
453 (defun shr-fontize-cont (cont &rest types
)
457 (shr-add-font (or shr-start
(point)) (point) type
))))
459 ;; Add an overlay in the region, but avoid putting the font properties
460 ;; on blank text at the start of the line, and the newline at the end,
461 ;; to avoid ugliness.
462 (defun shr-add-font (start end type
)
465 (while (< (point) end
)
467 (skip-chars-forward " "))
468 (let ((overlay (make-overlay (point) (min (line-end-position) end
))))
469 (overlay-put overlay
'face type
))
470 (if (< (line-end-position) end
)
474 (defun shr-browse-url ()
475 "Browse the URL under point."
477 (let ((url (get-text-property (point) 'shr-url
)))
480 (message "No link under point"))
481 ((string-match "^mailto:" url
)
482 (browse-url-mailto url
))
486 (defun shr-save-contents (directory)
487 "Save the contents from URL in a file."
488 (interactive "DSave contents of URL to directory: ")
489 (let ((url (get-text-property (point) 'shr-url
)))
491 (message "No link under point")
492 (url-retrieve (shr-encode-url url
)
493 'shr-store-contents
(list url directory
)))))
495 (defun shr-store-contents (status url directory
)
496 (unless (plist-get status
:error
)
497 (when (or (search-forward "\n\n" nil t
)
498 (search-forward "\r\n\r\n" nil t
))
499 (write-region (point) (point-max)
500 (expand-file-name (file-name-nondirectory url
)
503 (defun shr-image-fetched (status buffer start end
)
504 (when (and (buffer-name buffer
)
505 (not (plist-get status
:error
)))
506 (url-store-in-cache (current-buffer))
507 (when (or (search-forward "\n\n" nil t
)
508 (search-forward "\r\n\r\n" nil t
))
509 (let ((data (buffer-substring (point) (point-max))))
510 (with-current-buffer buffer
512 (let ((alt (buffer-substring start end
))
513 (inhibit-read-only t
))
514 (delete-region start end
)
516 (funcall shr-put-image-function data alt
)))))))
517 (kill-buffer (current-buffer)))
519 (defun shr-put-image (data alt
)
520 "Put image DATA with a string ALT. Return image."
521 (if (display-graphic-p)
522 (let ((image (ignore-errors
523 (shr-rescale-image data
))))
525 ;; When inserting big-ish pictures, put them at the
526 ;; beginning of the line.
527 (when (and (> (current-column) 0)
528 (> (car (image-size image t
)) 400))
530 (insert-image image
(or alt
"*"))
531 (when (image-animated-p image
)
532 (image-animate image nil
60)))
536 (defun shr-rescale-image (data)
537 (if (or (not (fboundp 'imagemagick-types
))
538 (not (get-buffer-window (current-buffer))))
539 (create-image data nil t
541 (let* ((image (create-image data nil t
:ascent
100))
542 (size (image-size image t
))
545 (edges (window-inside-pixel-edges
546 (get-buffer-window (current-buffer))))
547 (window-width (truncate (* shr-max-image-proportion
548 (- (nth 2 edges
) (nth 0 edges
)))))
549 (window-height (truncate (* shr-max-image-proportion
550 (- (nth 3 edges
) (nth 1 edges
)))))
552 (when (> height window-height
)
553 (setq image
(or (create-image data
'imagemagick t
554 :height window-height
)
556 (setq size
(image-size image t
)))
557 (when (> (car size
) window-width
)
559 (create-image data
'imagemagick t
565 ;; url-cache-extract autoloads url-cache.
566 (declare-function url-cache-create-filename
"url-cache" (url))
567 (autoload 'mm-disable-multibyte
"mm-util")
568 (autoload 'browse-url-mailto
"browse-url")
570 (defun shr-get-image-data (url)
571 "Get image data for URL.
572 Return a string with image data."
574 (mm-disable-multibyte)
576 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
578 (when (or (search-forward "\n\n" nil t
)
579 (search-forward "\r\n\r\n" nil t
))
580 (buffer-substring (point) (point-max))))))
582 (defun shr-image-displayer (content-function)
583 "Return a function to display an image.
584 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
585 is an argument. The function to be returned takes three arguments URL,
586 START, and END. Note that START and END should be merkers."
587 `(lambda (url start end
)
589 (if (string-match "\\`cid:" url
)
590 ,(when content-function
591 `(let ((image (funcall ,content-function
592 (substring url
(match-end 0)))))
595 (funcall shr-put-image-function
596 image
(buffer-substring start end
))
597 (delete-region (point) end
))))
598 (url-retrieve url
'shr-image-fetched
599 (list (current-buffer) start end
)
602 (defun shr-heading (cont &rest types
)
603 (shr-ensure-paragraph)
604 (apply #'shr-fontize-cont cont types
)
605 (shr-ensure-paragraph))
607 (autoload 'widget-convert-button
"wid-edit")
609 (defun shr-urlify (start url
&optional title
)
610 (widget-convert-button
611 'url-link start
(point)
612 :help-echo
(if title
(format "%s (%s)" url title
) url
)
615 (shr-add-font start
(point) 'shr-link
)
616 (put-text-property start
(point) 'shr-url url
))
618 (defun shr-encode-url (url)
620 (browse-url-url-encode-chars url
"[)$ ]"))
622 (autoload 'shr-color-visible
"shr-color")
623 (autoload 'shr-color-
>hexadecimal
"shr-color")
625 (defun shr-color-check (fg bg
)
626 "Check that FG is visible on BG.
627 Returns (fg bg) with corrected values.
628 Returns nil if the colors that would be used are the default
629 ones, in case fg and bg are nil."
631 (let ((fixed (cond ((null fg
) 'fg
)
633 ;; Convert colors to hexadecimal, or set them to default.
634 (let ((fg (or (shr-color->hexadecimal fg
)
635 (frame-parameter nil
'foreground-color
)))
636 (bg (or (shr-color->hexadecimal bg
)
637 (frame-parameter nil
'background-color
))))
638 (cond ((eq fixed
'bg
)
639 ;; Only return the new fg
640 (list nil
(cadr (shr-color-visible bg fg t
))))
642 ;; Invert args and results and return only the new bg
643 (list (cadr (shr-color-visible fg bg t
)) nil
))
645 (shr-color-visible bg fg
)))))))
647 (defun shr-colorize-region (start end fg
&optional bg
)
649 (let ((new-colors (shr-color-check fg bg
)))
652 (shr-put-color start end
:foreground
(cadr new-colors
)))
654 (shr-put-color start end
:background
(car new-colors
))))
657 ;; Put a color in the region, but avoid putting colors on blank
658 ;; text at the start of the line, and the newline at the end, to avoid
659 ;; ugliness. Also, don't overwrite any existing color information,
660 ;; since this can be called recursively, and we want the "inner" color
662 (defun shr-put-color (start end type color
)
665 (while (< (point) end
)
667 (not (eq type
:background
)))
668 (skip-chars-forward " "))
669 (when (> (line-end-position) (point))
670 (shr-put-color-1 (point) (min (line-end-position) end
) type color
))
671 (if (< (line-end-position) end
)
674 (when (and (eq type
:background
)
675 (= shr-table-depth
0))
676 (shr-expand-newlines start end color
))))
678 (defun shr-expand-newlines (start end color
)
680 ;; Skip past all white space at the start and ends.
682 (skip-chars-forward " \t\n")
686 (skip-chars-backward " \t\n")
689 (narrow-to-region start end
)
690 (let ((width (shr-natural-width))
692 (goto-char (point-min))
695 (when (and (< (setq column
(current-column)) width
)
696 (< (setq column
(shr-previous-newline-padding-width column
))
698 (let ((overlay (make-overlay (point) (1+ (point)))))
699 (overlay-put overlay
'before-string
703 (let ((string (plist-get
704 (overlay-properties overlay
)
708 (overlay-put overlay
'before-string
"")
710 (overlays-at (point))
712 (propertize (make-string (- width column
) ?
)
713 'face
(list :background color
))))))
716 (defun shr-previous-newline-padding-width (width)
717 (let ((overlays (overlays-at (point)))
721 (dolist (overlay overlays
)
724 (length (plist-get (overlay-properties overlay
)
726 (+ width previous-width
))))
728 (defun shr-put-color-1 (start end type color
)
729 (let* ((old-props (get-text-property start
'face
))
730 (do-put (and (listp old-props
)
731 (not (memq type old-props
))))
734 (setq change
(next-single-property-change start
'face nil end
))
736 (put-text-property start change
'face
737 (nconc (list type color
) old-props
)))
738 (setq old-props
(get-text-property change
'face
))
739 (setq do-put
(and (listp old-props
)
740 (not (memq type old-props
))))
744 (put-text-property start end
'face
745 (nconc (list type color old-props
))))))
747 ;;; Tag-specific rendering rules.
749 (defun shr-tag-body (cont)
750 (let* ((start (point))
751 (fgcolor (cdr (or (assq :fgcolor cont
)
753 (bgcolor (cdr (assq :bgcolor cont
)))
754 (shr-stylesheet (list (cons 'color fgcolor
)
755 (cons 'background-color bgcolor
))))
757 (shr-colorize-region start
(point) fgcolor bgcolor
)))
759 (defun shr-tag-style (cont)
762 (defun shr-tag-script (cont)
765 (defun shr-tag-comment (cont)
768 (defun shr-tag-sup (cont)
769 (let ((start (point)))
771 (put-text-property start
(point) 'display
'(raise 0.5))))
773 (defun shr-tag-sub (cont)
774 (let ((start (point)))
776 (put-text-property start
(point) 'display
'(raise -
0.5))))
778 (defun shr-tag-label (cont)
780 (shr-ensure-paragraph))
782 (defun shr-tag-p (cont)
783 (shr-ensure-paragraph)
786 (shr-ensure-paragraph))
788 (defun shr-tag-div (cont)
792 (shr-ensure-newline))
794 (defun shr-tag-s (cont)
795 (shr-fontize-cont cont
'shr-strike-through
))
797 (defun shr-tag-del (cont)
798 (shr-fontize-cont cont
'shr-strike-through
))
800 (defun shr-tag-b (cont)
801 (shr-fontize-cont cont
'bold
))
803 (defun shr-tag-i (cont)
804 (shr-fontize-cont cont
'italic
))
806 (defun shr-tag-em (cont)
807 (shr-fontize-cont cont
'bold
))
809 (defun shr-tag-strong (cont)
810 (shr-fontize-cont cont
'bold
))
812 (defun shr-tag-u (cont)
813 (shr-fontize-cont cont
'underline
))
815 (defun shr-parse-style (style)
818 (when (string-match "\n" style
)
819 (setq style
(replace-match " " t t style
))))
821 (dolist (elem (split-string style
";"))
823 (setq elem
(split-string elem
":"))
824 (when (and (car elem
)
826 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
827 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
828 (when (string-match " *!important\\'" value
)
829 (setq value
(substring value
0 (match-beginning 0))))
830 (push (cons (intern name obarray
)
835 (defun shr-tag-base (cont)
836 (setq shr-base
(cdr (assq :href cont
))))
838 (defun shr-tag-a (cont)
839 (let ((url (cdr (assq :href cont
)))
840 (title (cdr (assq :title cont
)))
844 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
)))
846 (defun shr-tag-object (cont)
847 (let ((start (point))
850 (when (eq (car elem
) 'embed
)
851 (setq url
(or url
(cdr (assq :src
(cdr elem
))))))
852 (when (and (eq (car elem
) 'param
)
853 (equal (cdr (assq :name
(cdr elem
))) "movie"))
854 (setq url
(or url
(cdr (assq :value
(cdr elem
)))))))
856 (shr-insert " [multimedia] ")
857 (shr-urlify start
(shr-expand-url url
)))
860 (defun shr-tag-video (cont)
861 (let ((image (cdr (assq :poster cont
)))
862 (url (cdr (assq :src cont
)))
864 (shr-tag-img nil image
)
865 (shr-urlify start
(shr-expand-url url
))))
867 (defun shr-tag-img (cont &optional url
)
870 (cdr (assq :src cont
))))
871 (when (and (> (current-column) 0)
872 (not (eq shr-state
'image
)))
874 (let ((alt (cdr (assq :alt cont
)))
875 (url (shr-expand-url (or url
(cdr (assq :src cont
))))))
876 (let ((start (point-marker)))
877 (when (zerop (length alt
))
880 ((or (member (cdr (assq :height cont
)) '("0" "1"))
881 (member (cdr (assq :width cont
)) '("0" "1")))
882 ;; Ignore zero-sized or single-pixel images.
884 ((and (not shr-inhibit-images
)
885 (string-match "\\`cid:" url
))
886 (let ((url (substring url
(match-end 0)))
888 (if (or (not shr-content-function
)
889 (not (setq image
(funcall shr-content-function url
))))
891 (funcall shr-put-image-function image alt
))))
892 ((or shr-inhibit-images
893 (and shr-blocked-images
894 (string-match shr-blocked-images url
)))
895 (setq shr-start
(point))
896 (let ((shr-state 'space
))
897 (if (> (string-width alt
) 8)
898 (shr-insert (truncate-string-to-width alt
8))
900 ((and (not shr-ignore-cache
)
901 (url-is-cached (shr-encode-url url
)))
902 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
905 (when (and shr-ignore-cache
906 (url-is-cached (shr-encode-url url
)))
907 (let ((file (url-cache-create-filename (shr-encode-url url
))))
908 (when (file-exists-p file
)
909 (delete-file file
))))
911 (if (fboundp 'url-queue-retrieve
)
914 (shr-encode-url url
) 'shr-image-fetched
915 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
917 (when (zerop shr-table-depth
) ;; We are not in a table.
918 (put-text-property start
(point) 'keymap shr-map
)
919 (put-text-property start
(point) 'shr-alt alt
)
920 (put-text-property start
(point) 'image-url url
)
921 (put-text-property start
(point) 'image-displayer
922 (shr-image-displayer shr-content-function
))
923 (put-text-property start
(point) 'help-echo alt
))
924 (setq shr-state
'image
)))))
926 (defun shr-tag-pre (cont)
927 (let ((shr-folding-mode 'none
))
931 (shr-ensure-newline)))
933 (defun shr-tag-blockquote (cont)
934 (shr-ensure-paragraph)
936 (let ((shr-indentation (+ shr-indentation
4)))
938 (shr-ensure-paragraph))
940 (defun shr-tag-ul (cont)
941 (shr-ensure-paragraph)
942 (let ((shr-list-mode 'ul
))
944 (shr-ensure-paragraph))
946 (defun shr-tag-ol (cont)
947 (shr-ensure-paragraph)
948 (let ((shr-list-mode 1))
950 (shr-ensure-paragraph))
952 (defun shr-tag-li (cont)
953 (shr-ensure-paragraph)
956 (if (numberp shr-list-mode
)
958 (format "%d " shr-list-mode
)
959 (setq shr-list-mode
(1+ shr-list-mode
)))
961 (shr-indentation (+ shr-indentation
(length bullet
))))
965 (defun shr-tag-br (cont)
971 (defun shr-tag-h1 (cont)
972 (shr-heading cont
'bold
'underline
))
974 (defun shr-tag-h2 (cont)
975 (shr-heading cont
'bold
))
977 (defun shr-tag-h3 (cont)
978 (shr-heading cont
'italic
))
980 (defun shr-tag-h4 (cont)
983 (defun shr-tag-h5 (cont)
986 (defun shr-tag-h6 (cont)
989 (defun shr-tag-hr (cont)
991 (insert (make-string shr-width shr-hr-line
) "\n"))
993 (defun shr-tag-title (cont)
994 (shr-heading cont
'bold
'underline
))
996 (defun shr-tag-font (cont)
997 (let* ((start (point))
998 (color (cdr (assq :color cont
)))
999 (shr-stylesheet (nconc (list (cons 'color color
))
1003 (shr-colorize-region start
(point) color
1004 (cdr (assq 'background-color shr-stylesheet
))))))
1006 ;;; Table rendering algorithm.
1008 ;; Table rendering is the only complicated thing here. We do this by
1009 ;; first counting how many TDs there are in each TR, and registering
1010 ;; how wide they think they should be ("width=45%", etc). Then we
1011 ;; render each TD separately (this is done in temporary buffers, so
1012 ;; that we can use all the rendering machinery as if we were in the
1013 ;; main buffer). Now we know how much space each TD really takes, so
1014 ;; we then render everything again with the new widths, and finally
1015 ;; insert all these boxes into the main buffer.
1016 (defun shr-tag-table-1 (cont)
1017 (setq cont
(or (cdr (assq 'tbody cont
))
1019 (let* ((shr-inhibit-images t
)
1020 (shr-table-depth (1+ shr-table-depth
))
1021 (shr-kinsoku-shorten t
)
1022 ;; Find all suggested widths.
1023 (columns (shr-column-specs cont
))
1024 ;; Compute how many characters wide each TD should be.
1025 (suggested-widths (shr-pro-rate-columns columns
))
1026 ;; Do a "test rendering" to see how big each TD is (this can
1027 ;; be smaller (if there's little text) or bigger (if there's
1028 ;; unbreakable text).
1029 (sketch (shr-make-table cont suggested-widths
))
1030 (sketch-widths (shr-table-widths sketch suggested-widths
)))
1031 ;; This probably won't work very well.
1032 (when (> (+ (loop for width across sketch-widths
1036 (setq truncate-lines t
))
1037 ;; Then render the table again with these new "hard" widths.
1038 (shr-insert-table (shr-make-table cont sketch-widths t
) sketch-widths
))
1039 ;; Finally, insert all the images after the table. The Emacs buffer
1040 ;; model isn't strong enough to allow us to put the images actually
1042 (when (zerop shr-table-depth
)
1043 (dolist (elem (shr-find-elements cont
'img
))
1044 (shr-tag-img (cdr elem
)))))
1046 (defun shr-tag-table (cont)
1047 (shr-ensure-paragraph)
1048 (let* ((caption (cdr (assq 'caption cont
)))
1049 (header (cdr (assq 'thead cont
)))
1050 (body (or (cdr (assq 'tbody cont
)) cont
))
1051 (footer (cdr (assq 'tfoot cont
)))
1052 (bgcolor (cdr (assq :bgcolor cont
)))
1054 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1056 (nheader (if header
(shr-max-columns header
)))
1057 (nbody (if body
(shr-max-columns body
)))
1058 (nfooter (if footer
(shr-max-columns footer
))))
1059 (if (and (not caption
)
1061 (not (cdr (assq 'tbody cont
)))
1062 (not (cdr (assq 'tr cont
)))
1064 ;; The table is totally invalid and just contains random junk.
1065 ;; Try to output it anyway.
1067 ;; It's a real table, so render it.
1070 (if caption
`((tr (td ,@caption
))))
1073 ;; hader + body + footer
1074 (if (= nheader nbody
)
1075 (if (= nbody nfooter
)
1076 `((tr (td (table (tbody ,@header
,@body
,@footer
)))))
1077 (nconc `((tr (td (table (tbody ,@header
,@body
)))))
1080 `((tr (td (table (tbody ,@footer
))))))))
1081 (nconc `((tr (td (table (tbody ,@header
)))))
1082 (if (= nbody nfooter
)
1083 `((tr (td (table (tbody ,@body
,@footer
)))))
1084 (nconc `((tr (td (table (tbody ,@body
)))))
1087 `((tr (td (table (tbody ,@footer
))))))))))
1089 (if (= nheader nbody
)
1090 `((tr (td (table (tbody ,@header
,@body
)))))
1092 `(,@header
(tr (td (table (tbody ,@body
)))))
1093 `((tr (td (table (tbody ,@header
))))
1094 (tr (td (table (tbody ,@body
))))))))
1097 (if (= nbody nfooter
)
1098 `((tr (td (table (tbody ,@body
,@footer
)))))
1099 (nconc `((tr (td (table (tbody ,@body
)))))
1102 `((tr (td (table (tbody ,@footer
))))))))
1104 `((tr (td (table (tbody ,@body
)))))
1107 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1110 (defun shr-find-elements (cont type
)
1113 (cond ((eq (car elem
) type
)
1116 (setq result
(nconc (shr-find-elements (cdr elem
) type
) result
)))))
1119 (defun shr-insert-table (table widths
)
1120 (shr-insert-table-ruler widths
)
1122 (let ((start (point))
1123 (height (let ((max 0))
1124 (dolist (column row
)
1125 (setq max
(max max
(cadr column
))))
1129 (insert shr-table-vertical-line
"\n"))
1130 (dolist (column row
)
1132 (let ((lines (nth 2 column
))
1133 (overlay-lines (nth 3 column
))
1134 overlay overlay-line
)
1135 (dolist (line lines
)
1136 (setq overlay-line
(pop overlay-lines
))
1138 (insert line shr-table-vertical-line
)
1139 (dolist (overlay overlay-line
)
1140 (let ((o (make-overlay (- (point) (nth 0 overlay
) 1)
1141 (- (point) (nth 1 overlay
) 1)))
1142 (properties (nth 2 overlay
)))
1144 (overlay-put o
(pop properties
) (pop properties
)))))
1146 ;; Add blank lines at padding at the bottom of the TD,
1148 (dotimes (i (- height
(length lines
)))
1150 (let ((start (point)))
1151 (insert (make-string (string-width (car lines
)) ?
)
1152 shr-table-vertical-line
)
1153 (when (nth 4 column
)
1154 (shr-put-color start
(1- (point)) :background
(nth 4 column
))))
1155 (forward-line 1)))))
1156 (shr-insert-table-ruler widths
)))
1158 (defun shr-insert-table-ruler (widths)
1160 (> shr-indentation
0))
1162 (insert shr-table-corner
)
1163 (dotimes (i (length widths
))
1164 (insert (make-string (aref widths i
) shr-table-horizontal-line
)
1168 (defun shr-table-widths (table suggested-widths
)
1169 (let* ((length (length suggested-widths
))
1170 (widths (make-vector length
0))
1171 (natural-widths (make-vector length
0)))
1174 (dolist (column row
)
1175 (aset widths i
(max (aref widths i
)
1177 (aset natural-widths i
(max (aref natural-widths i
)
1180 (let ((extra (- (apply '+ (append suggested-widths nil
))
1181 (apply '+ (append widths nil
))))
1182 (expanded-columns 0))
1185 ;; If the natural width is wider than the rendered width, we
1186 ;; want to allow the column to expand.
1187 (when (> (aref natural-widths i
) (aref widths i
))
1188 (setq expanded-columns
(1+ expanded-columns
))))
1190 (when (> (aref natural-widths i
) (aref widths i
))
1192 (1+ (aref natural-widths i
))
1193 (+ (/ extra expanded-columns
)
1194 (aref widths i
))))))))
1197 (defun shr-make-table (cont widths
&optional fill
)
1200 (when (eq (car row
) 'tr
)
1205 (while (< i
(length widths
))
1206 (setq column
(pop columns
))
1207 (when (or (memq (car column
) '(td th
))
1209 (push (shr-render-td (cdr column
) (aref widths i
) fill
)
1212 (push (nreverse tds
) trs
))))
1215 (defun shr-render-td (cont width fill
)
1217 (let ((bgcolor (cdr (assq :bgcolor cont
)))
1218 (fgcolor (cdr (assq :fgcolor cont
)))
1219 (style (cdr (assq :style cont
)))
1220 (shr-stylesheet shr-stylesheet
)
1221 overlays actual-colors
)
1223 (setq style
(and (string-match "color" style
)
1224 (shr-parse-style style
))))
1226 (setq style
(nconc (list (cons 'background-color bgcolor
)) style
)))
1228 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1230 (setq shr-stylesheet
(append style shr-stylesheet
)))
1231 (let ((cache (cdr (assoc (cons width cont
) shr-content-cache
))))
1234 (insert (car cache
))
1235 (let ((end (length (car cache
))))
1236 (dolist (overlay (cadr cache
))
1238 (make-overlay (1+ (- end
(nth 0 overlay
)))
1239 (1+ (- end
(nth 1 overlay
)))))
1240 (properties (nth 2 overlay
)))
1242 (overlay-put new-overlay
1243 (pop properties
) (pop properties
)))))))
1244 (let ((shr-width width
)
1245 (shr-indentation 0))
1246 (shr-descend (cons 'td cont
)))
1250 (skip-chars-backward " \t\n")))
1251 (push (list (cons width cont
) (buffer-string)
1252 (shr-overlays-in-region (point-min) (point-max)))
1253 shr-content-cache
)))
1254 (goto-char (point-min))
1258 (setq max
(max max
(current-column)))
1261 (goto-char (point-min))
1262 ;; If the buffer is totally empty, then put a single blank
1264 (if (zerop (buffer-size))
1265 (insert (make-string width ?
))
1266 ;; Otherwise, fill the buffer.
1269 (when (> (- width
(current-column)) 0)
1270 (insert (make-string (- width
(current-column)) ?
)))
1274 (shr-colorize-region
1275 (point-min) (point-max)
1276 (cdr (assq 'color shr-stylesheet
))
1277 (cdr (assq 'background-color shr-stylesheet
))))))
1280 (count-lines (point-min) (point-max))
1281 (split-string (buffer-string) "\n")
1282 (shr-collect-overlays)
1283 (car actual-colors
))
1285 (shr-natural-width)))))))
1287 (defun shr-natural-width ()
1288 (goto-char (point-min))
1293 (setq current
(+ current
(current-column)))
1294 (unless (get-text-property (point) 'shr-break
)
1295 (setq max
(max max current
)
1300 (defun shr-collect-overlays ()
1302 (goto-char (point-min))
1303 (let ((overlays nil
))
1305 (push (shr-overlays-in-region (point) (line-end-position))
1308 (nreverse overlays
))))
1310 (defun shr-overlays-in-region (start end
)
1312 (dolist (overlay (overlays-in start end
))
1313 (push (list (if (> start
(overlay-start overlay
))
1315 (- end
(overlay-start overlay
)))
1316 (if (< end
(overlay-end overlay
))
1318 (- end
(overlay-end overlay
)))
1319 (overlay-properties overlay
))
1323 (defun shr-pro-rate-columns (columns)
1324 (let ((total-percentage 0)
1325 (widths (make-vector (length columns
) 0)))
1326 (dotimes (i (length columns
))
1327 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1328 (setq total-percentage
(/ 1.0 total-percentage
))
1329 (dotimes (i (length columns
))
1330 (aset widths i
(max (truncate (* (aref columns i
)
1332 (- shr-width
(1+ (length columns
)))))
1336 ;; Return a summary of the number and shape of the TDs in the table.
1337 (defun shr-column-specs (cont)
1338 (let ((columns (make-vector (shr-max-columns cont
) 1)))
1340 (when (eq (car row
) 'tr
)
1342 (dolist (column (cdr row
))
1343 (when (memq (car column
) '(td th
))
1344 (let ((width (cdr (assq :width
(cdr column
)))))
1346 (string-match "\\([0-9]+\\)%" width
))
1348 (/ (string-to-number (match-string 1 width
))
1350 (setq i
(1+ i
)))))))
1353 (defun shr-count (cont elem
)
1356 (when (eq (car sub
) elem
)
1360 (defun shr-max-columns (cont)
1363 (when (eq (car row
) 'tr
)
1364 (setq max
(max max
(+ (shr-count (cdr row
) 'td
)
1365 (shr-count (cdr row
) 'th
))))))
1370 ;;; shr.el ends here