1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2012 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"
41 (defcustom shr-max-image-proportion
0.9
42 "How big pictures displayed are in relation to the window they're in.
43 A value of 0.7 means that they are allowed to take up 70% of the
44 width and height of the window. If they are larger than this,
45 and Emacs supports it, then the images will be rescaled down to
51 (defcustom shr-blocked-images nil
52 "Images that have URLs matching this regexp will be blocked."
57 (defcustom shr-table-horizontal-line ?\s
58 "Character used to draw horizontal table lines."
62 (defcustom shr-table-vertical-line ?\s
63 "Character used to draw vertical table lines."
67 (defcustom shr-table-corner ?\s
68 "Character used to draw table corners."
72 (defcustom shr-hr-line ?-
73 "Character used to draw hr lines."
77 (defcustom shr-width fill-column
78 "Frame width to use for rendering.
79 May either be an integer specifying a fixed width in characters,
80 or nil, meaning that the full width of the window should be
82 :type
'(choice (integer :tag
"Fixed width in characters")
83 (const :tag
"Use the width of the window" nil
))
86 (defvar shr-content-function nil
87 "If bound, this should be a function that will return the content.
88 This is used for cid: URLs, and the function is called with the
89 cid: URL as the argument.")
91 (defvar shr-put-image-function
'shr-put-image
92 "Function called to put image and alt string.")
94 (defface shr-strike-through
'((t (:strike-through t
)))
95 "Font for <s> elements."
99 '((t (:inherit link
)))
100 "Font for link elements."
103 ;;; Internal variables.
105 (defvar shr-folding-mode nil
)
106 (defvar shr-state nil
)
107 (defvar shr-start nil
)
108 (defvar shr-indentation
0)
109 (defvar shr-inhibit-images nil
)
110 (defvar shr-list-mode nil
)
111 (defvar shr-content-cache nil
)
112 (defvar shr-kinsoku-shorten nil
)
113 (defvar shr-table-depth
0)
114 (defvar shr-stylesheet nil
)
115 (defvar shr-base nil
)
116 (defvar shr-ignore-cache nil
)
119 (let ((map (make-sparse-keymap)))
120 (define-key map
"a" 'shr-show-alt-text
)
121 (define-key map
"i" 'shr-browse-image
)
122 (define-key map
"I" 'shr-insert-image
)
123 (define-key map
"u" 'shr-copy-url
)
124 (define-key map
"v" 'shr-browse-url
)
125 (define-key map
"o" 'shr-save-contents
)
126 (define-key map
"\r" 'shr-browse-url
)
129 ;; Public functions and commands.
131 (defun shr-visit-file (file)
132 "Parse FILE as an HTML document, and render it in a new buffer."
133 (interactive "fHTML file name: ")
134 (pop-to-buffer "*html*")
138 (insert-file-contents file
)
139 (libxml-parse-html-region (point-min) (point-max))))
140 (goto-char (point-min)))
143 (defun shr-insert-document (dom)
144 "Render the parsed document DOM into the current buffer.
145 DOM should be a parse tree as generated by
146 `libxml-parse-html-region' or similar."
147 (setq shr-content-cache nil
)
148 (let ((start (point))
152 (shr-width (or shr-width
(window-width))))
153 (shr-descend (shr-transform-dom dom
))
154 (shr-remove-trailing-whitespace start
(point))))
156 (defun shr-remove-trailing-whitespace (start end
)
157 (let ((width (window-width)))
159 (narrow-to-region start end
)
163 (when (> (shr-previous-newline-padding-width (current-column)) width
)
164 (dolist (overlay (overlays-at (point)))
165 (when (overlay-get overlay
'before-string
)
166 (overlay-put overlay
'before-string nil
))))
169 (defun shr-copy-url ()
170 "Copy the URL under point to the kill ring.
171 If called twice, then try to fetch the URL and see whether it
172 redirects somewhere else."
174 (let ((url (get-text-property (point) 'shr-url
)))
177 (message "No URL under point"))
178 ;; Resolve redirected URLs.
179 ((equal url
(car kill-ring
))
184 (eq (car a
) :redirect
))
187 (goto-char (point-min))
188 ;; Remove common tracking junk from the URL.
189 (when (re-search-forward ".utm_.*" nil t
)
190 (replace-match "" t t
))
191 (message "Copied %s" (buffer-string))
192 (copy-region-as-kill (point-min) (point-max)))))
194 ;; Copy the URL to the kill ring.
198 (copy-region-as-kill (point-min) (point-max))
199 (message "Copied %s" url
))))))
201 (defun shr-show-alt-text ()
202 "Show the ALT text of the image under point."
204 (let ((text (get-text-property (point) 'shr-alt
)))
206 (message "No image under point")
207 (message "%s" text
))))
209 (defun shr-browse-image (&optional copy-url
)
210 "Browse the image under point.
211 If COPY-URL (the prefix if called interactively) is non-nil, copy
212 the URL of the image to the kill buffer instead."
214 (let ((url (get-text-property (point) 'image-url
)))
217 (message "No image under point"))
221 (copy-region-as-kill (point-min) (point-max))
222 (message "Copied %s" url
)))
224 (message "Browsing %s..." url
)
227 (defun shr-insert-image ()
228 "Insert the image under point into the buffer."
230 (let ((url (get-text-property (point) 'image-url
)))
232 (message "No image under point")
233 (message "Inserting %s..." url
)
234 (url-retrieve url
'shr-image-fetched
235 (list (current-buffer) (1- (point)) (point-marker))
238 ;;; Utility functions.
240 (defun shr-transform-dom (dom)
241 (let ((result (list (pop dom
))))
242 (dolist (arg (pop dom
))
243 (push (cons (intern (concat ":" (symbol-name (car arg
))) obarray
)
248 (push (cons 'text sub
) result
)
249 (push (shr-transform-dom sub
) result
)))
252 (defun shr-descend (dom)
253 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom
))) obarray
))
254 (style (cdr (assq :style
(cdr dom
))))
255 (shr-stylesheet shr-stylesheet
)
258 (if (string-match "color" style
)
259 (setq shr-stylesheet
(nconc (shr-parse-style style
)
262 (if (fboundp function
)
263 (funcall function
(cdr dom
))
264 (shr-generic (cdr dom
)))
265 ;; If style is set, then this node has set the color.
267 (shr-colorize-region start
(point)
268 (cdr (assq 'color shr-stylesheet
))
269 (cdr (assq 'background-color shr-stylesheet
))))))
271 (defun shr-generic (cont)
274 ((eq (car sub
) 'text
)
275 (shr-insert (cdr sub
)))
277 (shr-descend sub
)))))
279 (defmacro shr-char-breakable-p
(char)
280 "Return non-nil if a line can be broken before and after CHAR."
281 `(aref fill-find-break-point-function-table
,char
))
282 (defmacro shr-char-nospace-p
(char)
283 "Return non-nil if no space is required before and after CHAR."
284 `(aref fill-nospace-between-words-table
,char
))
286 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
287 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
288 ;; parentheses, and so on, that should not be placed in the beginning
289 ;; of a line or the end of a line.
290 (defmacro shr-char-kinsoku-bol-p
(char)
291 "Return non-nil if a line ought not to begin with CHAR."
292 `(aref (char-category-set ,char
) ?
>))
293 (defmacro shr-char-kinsoku-eol-p
(char)
294 "Return non-nil if a line ought not to end with CHAR."
295 `(aref (char-category-set ,char
) ?
<))
296 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
297 (load "kinsoku" nil t
))
299 (defun shr-insert (text)
300 (when (and (eq shr-state
'image
)
301 (not (string-match "\\`[ \t\n]+\\'" text
)))
303 (setq shr-state nil
))
305 ((eq shr-folding-mode
'none
)
308 (when (and (string-match "\\`[ \t\n]" text
)
310 (not (eq (char-after (1- (point))) ?
)))
312 (dolist (elem (split-string text
))
314 (> shr-indentation
0))
316 ;; No space is needed behind a wide character categorized as
317 ;; kinsoku-bol, between characters both categorized as nospace,
318 ;; or at the beginning of a line.
320 (when (and (> (current-column) shr-indentation
)
321 (eq (preceding-char) ?
)
322 (or (= (line-beginning-position) (1- (point)))
323 (and (shr-char-breakable-p
324 (setq prev
(char-after (- (point) 2))))
325 (shr-char-kinsoku-bol-p prev
))
326 (and (shr-char-nospace-p prev
)
327 (shr-char-nospace-p (aref elem
0)))))
329 ;; The shr-start is a special variable that is used to pass
330 ;; upwards the first point in the buffer where the text really
333 (setq shr-start
(point)))
337 (while (and (> (current-column) shr-width
)
339 (setq found
(shr-find-fill-point))
341 (when (eq (preceding-char) ?
)
345 ;; No space is needed at the beginning of a line.
346 (when (eq (following-char) ?
)
348 (when (> shr-indentation
0)
352 (unless (string-match "[ \t\n]\\'" text
)
355 (defun shr-find-fill-point ()
356 (when (> (move-to-column shr-width
) shr-width
)
360 (while (not (or (setq failed
(= (current-column) shr-indentation
))
361 (eq (preceding-char) ?
)
362 (eq (following-char) ?
)
363 (shr-char-breakable-p (preceding-char))
364 (shr-char-breakable-p (following-char))
365 (if (eq (preceding-char) ?
')
366 (not (memq (char-after (- (point) 2))
368 (and (shr-char-kinsoku-bol-p (preceding-char))
369 (shr-char-breakable-p (following-char))
370 (not (shr-char-kinsoku-bol-p (following-char)))))
371 (shr-char-kinsoku-eol-p (following-char))))
373 (if (and (not (or failed
(eolp)))
374 (eq (preceding-char) ?
'))
375 (while (not (or (setq failed
(eolp))
376 (eq (following-char) ?
)
377 (shr-char-breakable-p (following-char))
378 (shr-char-kinsoku-eol-p (following-char))))
381 ;; There's no breakable point, so we give it up.
384 (unless shr-kinsoku-shorten
385 (while (and (setq found
(re-search-forward
386 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
387 (line-end-position) 'move
))
388 (eq (preceding-char) ?
')))
389 (if (and found
(not (match-beginning 1)))
390 (goto-char (match-beginning 0)))))
393 ;; Don't put kinsoku-bol characters at the beginning of a line,
394 ;; or kinsoku-eol characters at the end of a line.
397 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
398 (shr-char-kinsoku-eol-p (preceding-char)))
400 (when (setq failed
(= (current-column) shr-indentation
))
401 ;; There's no breakable point that doesn't violate kinsoku,
402 ;; so we look for the second best position.
405 (<= (current-column) shr-width
))
408 (shr-char-kinsoku-eol-p (following-char)))))
410 ((shr-char-kinsoku-eol-p (preceding-char))
411 (if (shr-char-kinsoku-eol-p (following-char))
412 ;; There are consecutive kinsoku-eol characters.
418 (and (> (setq count
(1- count
)) 0)
419 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
420 (or (shr-char-kinsoku-eol-p (preceding-char))
421 (shr-char-kinsoku-bol-p (following-char)))))))
422 (if (setq failed
(= (current-column) shr-indentation
))
423 ;; There's no breakable point that doesn't violate kinsoku,
424 ;; so we go to the second best position.
425 (if (looking-at "\\(\\c<+\\)\\c<")
426 (goto-char (match-end 1))
429 (if (shr-char-kinsoku-bol-p (preceding-char))
430 ;; There are consecutive kinsoku-bol characters.
433 (while (and (>= (setq count
(1- count
)) 0)
434 (shr-char-kinsoku-bol-p (following-char))
435 (shr-char-breakable-p (following-char)))
436 (forward-char 1))))))
437 (when (eq (following-char) ?
)
441 (defun shr-expand-url (url)
445 (string-match "\\`[a-z]*:" url
)
448 ((and (not (string-match "/\\'" shr-base
))
449 (not (string-match "\\`/" url
)))
450 (concat shr-base
"/" url
))
452 (concat shr-base url
))))
454 (defun shr-ensure-newline ()
455 (unless (zerop (current-column))
458 (defun shr-ensure-paragraph ()
460 (if (<= (current-column) shr-indentation
)
461 (unless (save-excursion
472 (when (> shr-indentation
0)
473 (insert (make-string shr-indentation ?
))))
475 (defun shr-fontize-cont (cont &rest types
)
479 (shr-add-font (or shr-start
(point)) (point) type
))))
481 ;; Add an overlay in the region, but avoid putting the font properties
482 ;; on blank text at the start of the line, and the newline at the end,
483 ;; to avoid ugliness.
484 (defun shr-add-font (start end type
)
487 (while (< (point) end
)
489 (skip-chars-forward " "))
490 (let ((overlay (make-overlay (point) (min (line-end-position) end
))))
491 (overlay-put overlay
'face type
))
492 (if (< (line-end-position) end
)
496 (defun shr-browse-url ()
497 "Browse the URL under point."
499 (let ((url (get-text-property (point) 'shr-url
)))
502 (message "No link under point"))
503 ((string-match "^mailto:" url
)
504 (browse-url-mail url
))
508 (defun shr-save-contents (directory)
509 "Save the contents from URL in a file."
510 (interactive "DSave contents of URL to directory: ")
511 (let ((url (get-text-property (point) 'shr-url
)))
513 (message "No link under point")
514 (url-retrieve (shr-encode-url url
)
515 'shr-store-contents
(list url directory
)
518 (defun shr-store-contents (status url directory
)
519 (unless (plist-get status
:error
)
520 (when (or (search-forward "\n\n" nil t
)
521 (search-forward "\r\n\r\n" nil t
))
522 (write-region (point) (point-max)
523 (expand-file-name (file-name-nondirectory url
)
526 (defun shr-image-fetched (status buffer start end
)
527 (let ((image-buffer (current-buffer)))
528 (when (and (buffer-name buffer
)
529 (not (plist-get status
:error
)))
530 (url-store-in-cache image-buffer
)
531 (when (or (search-forward "\n\n" nil t
)
532 (search-forward "\r\n\r\n" nil t
))
533 (let ((data (buffer-substring (point) (point-max))))
534 (with-current-buffer buffer
536 (let ((alt (buffer-substring start end
))
537 (inhibit-read-only t
))
538 (delete-region start end
)
540 (funcall shr-put-image-function data alt
)))))))
541 (kill-buffer image-buffer
)))
543 (defun shr-put-image (data alt
)
544 "Put image DATA with a string ALT. Return image."
545 (if (display-graphic-p)
546 (let ((image (ignore-errors
547 (shr-rescale-image data
))))
549 ;; When inserting big-ish pictures, put them at the
550 ;; beginning of the line.
551 (when (and (> (current-column) 0)
552 (> (car (image-size image t
)) 400))
554 (insert-image image
(or alt
"*"))
555 (when (image-animated-p image
)
556 (image-animate image nil
60)))
560 (defun shr-rescale-image (data)
561 (let ((image (create-image data nil t
:ascent
100)))
562 (if (or (not (fboundp 'imagemagick-types
))
563 (not (get-buffer-window (current-buffer))))
565 (let* ((size (image-size image t
))
568 (edges (window-inside-pixel-edges
569 (get-buffer-window (current-buffer))))
570 (window-width (truncate (* shr-max-image-proportion
571 (- (nth 2 edges
) (nth 0 edges
)))))
572 (window-height (truncate (* shr-max-image-proportion
573 (- (nth 3 edges
) (nth 1 edges
)))))
575 (when (> height window-height
)
576 (setq image
(or (create-image data
'imagemagick t
577 :height window-height
580 (setq size
(image-size image t
)))
581 (when (> (car size
) window-width
)
583 (create-image data
'imagemagick t
589 ;; url-cache-extract autoloads url-cache.
590 (declare-function url-cache-create-filename
"url-cache" (url))
591 (autoload 'mm-disable-multibyte
"mm-util")
592 (autoload 'browse-url-mail
"browse-url")
594 (defun shr-get-image-data (url)
595 "Get image data for URL.
596 Return a string with image data."
598 (mm-disable-multibyte)
600 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
602 (when (or (search-forward "\n\n" nil t
)
603 (search-forward "\r\n\r\n" nil t
))
604 (buffer-substring (point) (point-max))))))
606 (defun shr-image-displayer (content-function)
607 "Return a function to display an image.
608 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
609 is an argument. The function to be returned takes three arguments URL,
610 START, and END. Note that START and END should be markers."
611 `(lambda (url start end
)
613 (if (string-match "\\`cid:" url
)
614 ,(when content-function
615 `(let ((image (funcall ,content-function
616 (substring url
(match-end 0)))))
619 (funcall shr-put-image-function
620 image
(buffer-substring start end
))
621 (delete-region (point) end
))))
622 (url-retrieve url
'shr-image-fetched
623 (list (current-buffer) start end
)
626 (defun shr-heading (cont &rest types
)
627 (shr-ensure-paragraph)
628 (apply #'shr-fontize-cont cont types
)
629 (shr-ensure-paragraph))
631 (autoload 'widget-convert-button
"wid-edit")
633 (defun shr-urlify (start url
&optional title
)
634 (widget-convert-button
635 'url-link start
(point)
636 :help-echo
(if title
(format "%s (%s)" url title
) url
)
639 (shr-add-font start
(point) 'shr-link
)
640 (put-text-property start
(point) 'shr-url url
))
642 (defun shr-encode-url (url)
644 (browse-url-url-encode-chars url
"[)$ ]"))
646 (autoload 'shr-color-visible
"shr-color")
647 (autoload 'shr-color-
>hexadecimal
"shr-color")
649 (defun shr-color-check (fg bg
)
650 "Check that FG is visible on BG.
651 Returns (fg bg) with corrected values.
652 Returns nil if the colors that would be used are the default
653 ones, in case fg and bg are nil."
655 (let ((fixed (cond ((null fg
) 'fg
)
657 ;; Convert colors to hexadecimal, or set them to default.
658 (let ((fg (or (shr-color->hexadecimal fg
)
659 (frame-parameter nil
'foreground-color
)))
660 (bg (or (shr-color->hexadecimal bg
)
661 (frame-parameter nil
'background-color
))))
662 (cond ((eq fixed
'bg
)
663 ;; Only return the new fg
664 (list nil
(cadr (shr-color-visible bg fg t
))))
666 ;; Invert args and results and return only the new bg
667 (list (cadr (shr-color-visible fg bg t
)) nil
))
669 (shr-color-visible bg fg
)))))))
671 (defun shr-colorize-region (start end fg
&optional bg
)
673 (let ((new-colors (shr-color-check fg bg
)))
676 (shr-put-color start end
:foreground
(cadr new-colors
)))
678 (shr-put-color start end
:background
(car new-colors
))))
681 ;; Put a color in the region, but avoid putting colors on blank
682 ;; text at the start of the line, and the newline at the end, to avoid
683 ;; ugliness. Also, don't overwrite any existing color information,
684 ;; since this can be called recursively, and we want the "inner" color
686 (defun shr-put-color (start end type color
)
689 (while (< (point) end
)
691 (not (eq type
:background
)))
692 (skip-chars-forward " "))
693 (when (> (line-end-position) (point))
694 (shr-put-color-1 (point) (min (line-end-position) end
) type color
))
695 (if (< (line-end-position) end
)
698 (when (and (eq type
:background
)
699 (= shr-table-depth
0))
700 (shr-expand-newlines start end color
))))
702 (defun shr-expand-newlines (start end color
)
704 ;; Skip past all white space at the start and ends.
706 (skip-chars-forward " \t\n")
710 (skip-chars-backward " \t\n")
713 (narrow-to-region start end
)
714 (let ((width (shr-buffer-width))
716 (goto-char (point-min))
719 (when (and (< (setq column
(current-column)) width
)
720 (< (setq column
(shr-previous-newline-padding-width column
))
722 (let ((overlay (make-overlay (point) (1+ (point)))))
723 (overlay-put overlay
'before-string
727 (let ((string (plist-get
728 (overlay-properties overlay
)
732 (overlay-put overlay
'before-string
"")
734 (overlays-at (point))
736 (propertize (make-string (- width column
) ?
)
737 'face
(list :background color
))))))
740 (defun shr-previous-newline-padding-width (width)
741 (let ((overlays (overlays-at (point)))
745 (dolist (overlay overlays
)
748 (length (plist-get (overlay-properties overlay
)
750 (+ width previous-width
))))
752 (defun shr-put-color-1 (start end type color
)
753 (let* ((old-props (get-text-property start
'face
))
754 (do-put (and (listp old-props
)
755 (not (memq type old-props
))))
758 (setq change
(next-single-property-change start
'face nil end
))
760 (put-text-property start change
'face
761 (nconc (list type color
) old-props
)))
762 (setq old-props
(get-text-property change
'face
))
763 (setq do-put
(and (listp old-props
)
764 (not (memq type old-props
))))
768 (put-text-property start end
'face
769 (nconc (list type color old-props
))))))
771 ;;; Tag-specific rendering rules.
773 (defun shr-tag-body (cont)
774 (let* ((start (point))
775 (fgcolor (cdr (or (assq :fgcolor cont
)
777 (bgcolor (cdr (assq :bgcolor cont
)))
778 (shr-stylesheet (list (cons 'color fgcolor
)
779 (cons 'background-color bgcolor
))))
781 (shr-colorize-region start
(point) fgcolor bgcolor
)))
783 (defun shr-tag-style (cont)
786 (defun shr-tag-script (cont)
789 (defun shr-tag-comment (cont)
792 (defun shr-tag-sup (cont)
793 (let ((start (point)))
795 (put-text-property start
(point) 'display
'(raise 0.5))))
797 (defun shr-tag-sub (cont)
798 (let ((start (point)))
800 (put-text-property start
(point) 'display
'(raise -
0.5))))
802 (defun shr-tag-label (cont)
804 (shr-ensure-paragraph))
806 (defun shr-tag-p (cont)
807 (shr-ensure-paragraph)
810 (shr-ensure-paragraph))
812 (defun shr-tag-div (cont)
816 (shr-ensure-newline))
818 (defun shr-tag-s (cont)
819 (shr-fontize-cont cont
'shr-strike-through
))
821 (defun shr-tag-del (cont)
822 (shr-fontize-cont cont
'shr-strike-through
))
824 (defun shr-tag-b (cont)
825 (shr-fontize-cont cont
'bold
))
827 (defun shr-tag-i (cont)
828 (shr-fontize-cont cont
'italic
))
830 (defun shr-tag-em (cont)
831 (shr-fontize-cont cont
'bold
))
833 (defun shr-tag-strong (cont)
834 (shr-fontize-cont cont
'bold
))
836 (defun shr-tag-u (cont)
837 (shr-fontize-cont cont
'underline
))
839 (defun shr-parse-style (style)
842 (when (string-match "\n" style
)
843 (setq style
(replace-match " " t t style
))))
845 (dolist (elem (split-string style
";"))
847 (setq elem
(split-string elem
":"))
848 (when (and (car elem
)
850 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
851 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
852 (when (string-match " *!important\\'" value
)
853 (setq value
(substring value
0 (match-beginning 0))))
854 (push (cons (intern name obarray
)
859 (defun shr-tag-base (cont)
860 (setq shr-base
(cdr (assq :href cont
))))
862 (defun shr-tag-a (cont)
863 (let ((url (cdr (assq :href cont
)))
864 (title (cdr (assq :title cont
)))
868 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
)))
870 (defun shr-tag-object (cont)
871 (let ((start (point))
874 (when (eq (car elem
) 'embed
)
875 (setq url
(or url
(cdr (assq :src
(cdr elem
))))))
876 (when (and (eq (car elem
) 'param
)
877 (equal (cdr (assq :name
(cdr elem
))) "movie"))
878 (setq url
(or url
(cdr (assq :value
(cdr elem
)))))))
880 (shr-insert " [multimedia] ")
881 (shr-urlify start
(shr-expand-url url
)))
884 (defun shr-tag-video (cont)
885 (let ((image (cdr (assq :poster cont
)))
886 (url (cdr (assq :src cont
)))
888 (shr-tag-img nil image
)
889 (shr-urlify start
(shr-expand-url url
))))
891 (defun shr-tag-img (cont &optional url
)
894 (cdr (assq :src cont
))))
895 (when (and (> (current-column) 0)
896 (not (eq shr-state
'image
)))
898 (let ((alt (cdr (assq :alt cont
)))
899 (url (shr-expand-url (or url
(cdr (assq :src cont
))))))
900 (let ((start (point-marker)))
901 (when (zerop (length alt
))
904 ((or (member (cdr (assq :height cont
)) '("0" "1"))
905 (member (cdr (assq :width cont
)) '("0" "1")))
906 ;; Ignore zero-sized or single-pixel images.
908 ((and (not shr-inhibit-images
)
909 (string-match "\\`cid:" url
))
910 (let ((url (substring url
(match-end 0)))
912 (if (or (not shr-content-function
)
913 (not (setq image
(funcall shr-content-function url
))))
915 (funcall shr-put-image-function image alt
))))
916 ((or shr-inhibit-images
917 (and shr-blocked-images
918 (string-match shr-blocked-images url
)))
919 (setq shr-start
(point))
920 (let ((shr-state 'space
))
921 (if (> (string-width alt
) 8)
922 (shr-insert (truncate-string-to-width alt
8))
924 ((and (not shr-ignore-cache
)
925 (url-is-cached (shr-encode-url url
)))
926 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
929 (when (and shr-ignore-cache
930 (url-is-cached (shr-encode-url url
)))
931 (let ((file (url-cache-create-filename (shr-encode-url url
))))
932 (when (file-exists-p file
)
933 (delete-file file
))))
935 (shr-encode-url url
) 'shr-image-fetched
936 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
938 (when (zerop shr-table-depth
) ;; We are not in a table.
939 (put-text-property start
(point) 'keymap shr-map
)
940 (put-text-property start
(point) 'shr-alt alt
)
941 (put-text-property start
(point) 'image-url url
)
942 (put-text-property start
(point) 'image-displayer
943 (shr-image-displayer shr-content-function
))
944 (put-text-property start
(point) 'help-echo alt
))
945 (setq shr-state
'image
)))))
947 (defun shr-tag-pre (cont)
948 (let ((shr-folding-mode 'none
))
952 (shr-ensure-newline)))
954 (defun shr-tag-blockquote (cont)
955 (shr-ensure-paragraph)
957 (let ((shr-indentation (+ shr-indentation
4)))
959 (shr-ensure-paragraph))
961 (defun shr-tag-ul (cont)
962 (shr-ensure-paragraph)
963 (let ((shr-list-mode 'ul
))
965 (shr-ensure-paragraph))
967 (defun shr-tag-ol (cont)
968 (shr-ensure-paragraph)
969 (let ((shr-list-mode 1))
971 (shr-ensure-paragraph))
973 (defun shr-tag-li (cont)
974 (shr-ensure-paragraph)
977 (if (numberp shr-list-mode
)
979 (format "%d " shr-list-mode
)
980 (setq shr-list-mode
(1+ shr-list-mode
)))
982 (shr-indentation (+ shr-indentation
(length bullet
))))
986 (defun shr-tag-br (cont)
992 (defun shr-tag-h1 (cont)
993 (shr-heading cont
'bold
'underline
))
995 (defun shr-tag-h2 (cont)
996 (shr-heading cont
'bold
))
998 (defun shr-tag-h3 (cont)
999 (shr-heading cont
'italic
))
1001 (defun shr-tag-h4 (cont)
1004 (defun shr-tag-h5 (cont)
1007 (defun shr-tag-h6 (cont)
1010 (defun shr-tag-hr (cont)
1011 (shr-ensure-newline)
1012 (insert (make-string shr-width shr-hr-line
) "\n"))
1014 (defun shr-tag-title (cont)
1015 (shr-heading cont
'bold
'underline
))
1017 (defun shr-tag-font (cont)
1018 (let* ((start (point))
1019 (color (cdr (assq :color cont
)))
1020 (shr-stylesheet (nconc (list (cons 'color color
))
1024 (shr-colorize-region start
(point) color
1025 (cdr (assq 'background-color shr-stylesheet
))))))
1027 ;;; Table rendering algorithm.
1029 ;; Table rendering is the only complicated thing here. We do this by
1030 ;; first counting how many TDs there are in each TR, and registering
1031 ;; how wide they think they should be ("width=45%", etc). Then we
1032 ;; render each TD separately (this is done in temporary buffers, so
1033 ;; that we can use all the rendering machinery as if we were in the
1034 ;; main buffer). Now we know how much space each TD really takes, so
1035 ;; we then render everything again with the new widths, and finally
1036 ;; insert all these boxes into the main buffer.
1037 (defun shr-tag-table-1 (cont)
1038 (setq cont
(or (cdr (assq 'tbody cont
))
1040 (let* ((shr-inhibit-images t
)
1041 (shr-table-depth (1+ shr-table-depth
))
1042 (shr-kinsoku-shorten t
)
1043 ;; Find all suggested widths.
1044 (columns (shr-column-specs cont
))
1045 ;; Compute how many characters wide each TD should be.
1046 (suggested-widths (shr-pro-rate-columns columns
))
1047 ;; Do a "test rendering" to see how big each TD is (this can
1048 ;; be smaller (if there's little text) or bigger (if there's
1049 ;; unbreakable text).
1050 (sketch (shr-make-table cont suggested-widths
))
1051 ;; Compute the "natural" width by setting each column to 500
1052 ;; characters and see how wide they really render.
1053 (natural (shr-make-table cont
(make-vector (length columns
) 500)))
1054 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1055 ;; This probably won't work very well.
1056 (when (> (+ (loop for width across sketch-widths
1060 (setq truncate-lines t
))
1061 ;; Then render the table again with these new "hard" widths.
1062 (shr-insert-table (shr-make-table cont sketch-widths t
) sketch-widths
))
1063 ;; Finally, insert all the images after the table. The Emacs buffer
1064 ;; model isn't strong enough to allow us to put the images actually
1066 (when (zerop shr-table-depth
)
1067 (dolist (elem (shr-find-elements cont
'img
))
1068 (shr-tag-img (cdr elem
)))))
1070 (defun shr-tag-table (cont)
1071 (shr-ensure-paragraph)
1072 (let* ((caption (cdr (assq 'caption cont
)))
1073 (header (cdr (assq 'thead cont
)))
1074 (body (or (cdr (assq 'tbody cont
)) cont
))
1075 (footer (cdr (assq 'tfoot cont
)))
1076 (bgcolor (cdr (assq :bgcolor cont
)))
1078 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1080 (nheader (if header
(shr-max-columns header
)))
1081 (nbody (if body
(shr-max-columns body
)))
1082 (nfooter (if footer
(shr-max-columns footer
))))
1083 (if (and (not caption
)
1085 (not (cdr (assq 'tbody cont
)))
1086 (not (cdr (assq 'tr cont
)))
1088 ;; The table is totally invalid and just contains random junk.
1089 ;; Try to output it anyway.
1091 ;; It's a real table, so render it.
1094 (if caption
`((tr (td ,@caption
))))
1097 ;; hader + body + footer
1098 (if (= nheader nbody
)
1099 (if (= nbody nfooter
)
1100 `((tr (td (table (tbody ,@header
,@body
,@footer
)))))
1101 (nconc `((tr (td (table (tbody ,@header
,@body
)))))
1104 `((tr (td (table (tbody ,@footer
))))))))
1105 (nconc `((tr (td (table (tbody ,@header
)))))
1106 (if (= nbody nfooter
)
1107 `((tr (td (table (tbody ,@body
,@footer
)))))
1108 (nconc `((tr (td (table (tbody ,@body
)))))
1111 `((tr (td (table (tbody ,@footer
))))))))))
1113 (if (= nheader nbody
)
1114 `((tr (td (table (tbody ,@header
,@body
)))))
1116 `(,@header
(tr (td (table (tbody ,@body
)))))
1117 `((tr (td (table (tbody ,@header
))))
1118 (tr (td (table (tbody ,@body
))))))))
1121 (if (= nbody nfooter
)
1122 `((tr (td (table (tbody ,@body
,@footer
)))))
1123 (nconc `((tr (td (table (tbody ,@body
)))))
1126 `((tr (td (table (tbody ,@footer
))))))))
1128 `((tr (td (table (tbody ,@body
)))))
1131 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1134 (defun shr-find-elements (cont type
)
1137 (cond ((eq (car elem
) type
)
1140 (setq result
(nconc (shr-find-elements (cdr elem
) type
) result
)))))
1143 (defun shr-insert-table (table widths
)
1144 (shr-insert-table-ruler widths
)
1146 (let ((start (point))
1147 (height (let ((max 0))
1148 (dolist (column row
)
1149 (setq max
(max max
(cadr column
))))
1153 (insert shr-table-vertical-line
"\n"))
1154 (dolist (column row
)
1156 (let ((lines (nth 2 column
))
1157 (overlay-lines (nth 3 column
))
1158 overlay overlay-line
)
1159 (dolist (line lines
)
1160 (setq overlay-line
(pop overlay-lines
))
1162 (insert line shr-table-vertical-line
)
1163 (dolist (overlay overlay-line
)
1164 (let ((o (make-overlay (- (point) (nth 0 overlay
) 1)
1165 (- (point) (nth 1 overlay
) 1)))
1166 (properties (nth 2 overlay
)))
1168 (overlay-put o
(pop properties
) (pop properties
)))))
1170 ;; Add blank lines at padding at the bottom of the TD,
1172 (dotimes (i (- height
(length lines
)))
1174 (let ((start (point)))
1175 (insert (make-string (string-width (car lines
)) ?
)
1176 shr-table-vertical-line
)
1177 (when (nth 4 column
)
1178 (shr-put-color start
(1- (point)) :background
(nth 4 column
))))
1179 (forward-line 1)))))
1180 (shr-insert-table-ruler widths
)))
1182 (defun shr-insert-table-ruler (widths)
1184 (> shr-indentation
0))
1186 (insert shr-table-corner
)
1187 (dotimes (i (length widths
))
1188 (insert (make-string (aref widths i
) shr-table-horizontal-line
)
1192 (defun shr-table-widths (table natural-table suggested-widths
)
1193 (let* ((length (length suggested-widths
))
1194 (widths (make-vector length
0))
1195 (natural-widths (make-vector length
0)))
1198 (dolist (column row
)
1199 (aset widths i
(max (aref widths i
) column
))
1201 (dolist (row natural-table
)
1203 (dolist (column row
)
1204 (aset natural-widths i
(max (aref natural-widths i
) column
))
1206 (let ((extra (- (apply '+ (append suggested-widths nil
))
1207 (apply '+ (append widths nil
))))
1208 (expanded-columns 0))
1209 ;; We have extra, unused space, so divide this space amongst the
1212 ;; If the natural width is wider than the rendered width, we
1213 ;; want to allow the column to expand.
1215 (when (> (aref natural-widths i
) (aref widths i
))
1216 (setq expanded-columns
(1+ expanded-columns
))))
1218 (when (> (aref natural-widths i
) (aref widths i
))
1220 (aref natural-widths i
)
1221 (+ (/ extra expanded-columns
)
1222 (aref widths i
))))))))
1225 (defun shr-make-table (cont widths
&optional fill
)
1228 (when (eq (car row
) 'tr
)
1233 (while (< i
(length widths
))
1234 (setq column
(pop columns
))
1235 (when (or (memq (car column
) '(td th
))
1237 (push (shr-render-td (cdr column
) (aref widths i
) fill
)
1240 (push (nreverse tds
) trs
))))
1243 (defun shr-render-td (cont width fill
)
1245 (let ((bgcolor (cdr (assq :bgcolor cont
)))
1246 (fgcolor (cdr (assq :fgcolor cont
)))
1247 (style (cdr (assq :style cont
)))
1248 (shr-stylesheet shr-stylesheet
)
1249 overlays actual-colors
)
1251 (setq style
(and (string-match "color" style
)
1252 (shr-parse-style style
))))
1254 (setq style
(nconc (list (cons 'background-color bgcolor
)) style
)))
1256 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1258 (setq shr-stylesheet
(append style shr-stylesheet
)))
1259 (let ((cache (cdr (assoc (cons width cont
) shr-content-cache
))))
1262 (insert (car cache
))
1263 (let ((end (length (car cache
))))
1264 (dolist (overlay (cadr cache
))
1266 (make-overlay (1+ (- end
(nth 0 overlay
)))
1267 (1+ (- end
(nth 1 overlay
)))))
1268 (properties (nth 2 overlay
)))
1270 (overlay-put new-overlay
1271 (pop properties
) (pop properties
)))))))
1272 (let ((shr-width width
)
1273 (shr-indentation 0))
1274 (shr-descend (cons 'td cont
)))
1275 ;; Delete padding at the bottom of the TDs.
1279 (skip-chars-backward " \t\n")
1282 (push (list (cons width cont
) (buffer-string)
1283 (shr-overlays-in-region (point-min) (point-max)))
1284 shr-content-cache
)))
1285 (goto-char (point-min))
1289 (setq max
(max max
(current-column)))
1292 (goto-char (point-min))
1293 ;; If the buffer is totally empty, then put a single blank
1295 (if (zerop (buffer-size))
1296 (insert (make-string width ?
))
1297 ;; Otherwise, fill the buffer.
1300 (when (> (- width
(current-column)) 0)
1301 (insert (make-string (- width
(current-column)) ?
)))
1305 (shr-colorize-region
1306 (point-min) (point-max)
1307 (cdr (assq 'color shr-stylesheet
))
1308 (cdr (assq 'background-color shr-stylesheet
))))))
1311 (count-lines (point-min) (point-max))
1312 (split-string (buffer-string) "\n")
1313 (shr-collect-overlays)
1314 (car actual-colors
))
1317 (defun shr-buffer-width ()
1318 (goto-char (point-min))
1322 (setq max
(max max
(current-column)))
1326 (defun shr-collect-overlays ()
1328 (goto-char (point-min))
1329 (let ((overlays nil
))
1331 (push (shr-overlays-in-region (point) (line-end-position))
1334 (nreverse overlays
))))
1336 (defun shr-overlays-in-region (start end
)
1338 (dolist (overlay (overlays-in start end
))
1339 (push (list (if (> start
(overlay-start overlay
))
1341 (- end
(overlay-start overlay
)))
1342 (if (< end
(overlay-end overlay
))
1344 (- end
(overlay-end overlay
)))
1345 (overlay-properties overlay
))
1349 (defun shr-pro-rate-columns (columns)
1350 (let ((total-percentage 0)
1351 (widths (make-vector (length columns
) 0)))
1352 (dotimes (i (length columns
))
1353 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1354 (setq total-percentage
(/ 1.0 total-percentage
))
1355 (dotimes (i (length columns
))
1356 (aset widths i
(max (truncate (* (aref columns i
)
1358 (- shr-width
(1+ (length columns
)))))
1362 ;; Return a summary of the number and shape of the TDs in the table.
1363 (defun shr-column-specs (cont)
1364 (let ((columns (make-vector (shr-max-columns cont
) 1)))
1366 (when (eq (car row
) 'tr
)
1368 (dolist (column (cdr row
))
1369 (when (memq (car column
) '(td th
))
1370 (let ((width (cdr (assq :width
(cdr column
)))))
1372 (string-match "\\([0-9]+\\)%" width
)
1373 (not (zerop (setq width
(string-to-number
1374 (match-string 1 width
))))))
1375 (aset columns i
(/ width
100.0))))
1376 (setq i
(1+ i
)))))))
1379 (defun shr-count (cont elem
)
1382 (when (eq (car sub
) elem
)
1386 (defun shr-max-columns (cont)
1389 (when (eq (car row
) 'tr
)
1390 (setq max
(max max
(+ (shr-count (cdr row
) 'td
)
1391 (shr-count (cdr row
) 'th
))))))
1396 ;;; shr.el ends here