1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2013 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 "z" 'shr-zoom-image)
123 (define-key map "I" 'shr-insert-image)
124 (define-key map "u" 'shr-copy-url)
125 (define-key map "v" 'shr-browse-url)
126 (define-key map "o" 'shr-save-contents)
127 (define-key map "\r" 'shr-browse-url)
130 ;; Public functions and commands.
132 (defun shr-render-buffer (buffer)
133 "Display the HTML rendering of the current buffer."
134 (interactive (list (current-buffer)))
135 (pop-to-buffer "*html*")
138 (with-current-buffer buffer
139 (libxml-parse-html-region (point-min) (point-max))))
140 (goto-char (point-min)))
142 (defun shr-visit-file (file)
143 "Parse FILE as an HTML document, and render it in a new buffer."
144 (interactive "fHTML file name: ")
146 (insert-file-contents file)
147 (shr-render-buffer (current-buffer))))
150 (defun shr-insert-document (dom)
151 "Render the parsed document DOM into the current buffer.
152 DOM should be a parse tree as generated by
153 `libxml-parse-html-region' or similar."
154 (setq shr-content-cache nil)
155 (let ((start (point))
159 (shr-width (or shr-width (window-width))))
160 (shr-descend (shr-transform-dom dom))
161 (shr-remove-trailing-whitespace start (point))))
163 (defun shr-remove-trailing-whitespace (start end)
164 (let ((width (window-width)))
166 (narrow-to-region start end)
170 (when (> (shr-previous-newline-padding-width (current-column)) width)
171 (dolist (overlay (overlays-at (point)))
172 (when (overlay-get overlay 'before-string)
173 (overlay-put overlay 'before-string nil))))
176 (defun shr-copy-url ()
177 "Copy the URL under point to the kill ring.
178 If called twice, then try to fetch the URL and see whether it
179 redirects somewhere else."
181 (let ((url (get-text-property (point) 'shr-url)))
184 (message "No URL under point"))
185 ;; Resolve redirected URLs.
186 ((equal url (car kill-ring))
191 (eq (car a) :redirect))
194 (goto-char (point-min))
195 ;; Remove common tracking junk from the URL.
196 (when (re-search-forward ".utm_.*" nil t)
197 (replace-match "" t t))
198 (message "Copied %s" (buffer-string))
199 (copy-region-as-kill (point-min) (point-max)))))
201 ;; Copy the URL to the kill ring.
205 (copy-region-as-kill (point-min) (point-max))
206 (message "Copied %s" url))))))
208 (defun shr-show-alt-text ()
209 "Show the ALT text of the image under point."
211 (let ((text (get-text-property (point) 'shr-alt)))
213 (message "No image under point")
214 (message "%s" text))))
216 (defun shr-browse-image (&optional copy-url)
217 "Browse the image under point.
218 If COPY-URL (the prefix if called interactively) is non-nil, copy
219 the URL of the image to the kill buffer instead."
221 (let ((url (get-text-property (point) 'image-url)))
224 (message "No image under point"))
228 (copy-region-as-kill (point-min) (point-max))
229 (message "Copied %s" url)))
231 (message "Browsing %s..." url)
234 (defun shr-insert-image ()
235 "Insert the image under point into the buffer."
237 (let ((url (get-text-property (point) 'image-url)))
239 (message "No image under point")
240 (message "Inserting %s..." url)
241 (url-retrieve url 'shr-image-fetched
242 (list (current-buffer) (1- (point)) (point-marker))
245 (defun shr-zoom-image ()
246 "Toggle the image size.
247 The size will be rotated between the default size, the original
248 size, and full-buffer size."
250 (let ((url (get-text-property (point) 'image-url))
251 (size (get-text-property (point) 'image-size))
252 (buffer-read-only nil))
254 (message "No image under point")
255 ;; Delete the old picture.
256 (while (get-text-property (point) 'image-url)
259 (let ((start (point)))
260 (while (get-text-property (point) 'image-url)
263 (put-text-property start (point) 'display nil)
264 (when (> (- (point) start) 2)
265 (delete-region start (1- (point)))))
266 (message "Inserting %s..." url)
267 (url-retrieve url 'shr-image-fetched
268 (list (current-buffer) (1- (point)) (point-marker)
270 (cond ((or (eq size 'default)
279 ;;; Utility functions.
281 (defun shr-transform-dom (dom)
282 (let ((result (list (pop dom))))
283 (dolist (arg (pop dom))
284 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
289 (push (cons 'text sub) result)
290 (push (shr-transform-dom sub) result)))
293 (defun shr-descend (dom)
294 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray))
295 (style (cdr (assq :style (cdr dom))))
296 (shr-stylesheet shr-stylesheet)
299 (if (string-match "color" style)
300 (setq shr-stylesheet (nconc (shr-parse-style style)
303 (if (fboundp function)
304 (funcall function (cdr dom))
305 (shr-generic (cdr dom)))
306 ;; If style is set, then this node has set the color.
308 (shr-colorize-region start (point)
309 (cdr (assq 'color shr-stylesheet))
310 (cdr (assq 'background-color shr-stylesheet))))))
312 (defun shr-generic (cont)
315 ((eq (car sub) 'text)
316 (shr-insert (cdr sub)))
318 (shr-descend sub)))))
320 (defmacro shr-char-breakable-p (char)
321 "Return non-nil if a line can be broken before and after CHAR."
322 `(aref fill-find-break-point-function-table ,char))
323 (defmacro shr-char-nospace-p (char)
324 "Return non-nil if no space is required before and after CHAR."
325 `(aref fill-nospace-between-words-table ,char))
327 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
328 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
329 ;; parentheses, and so on, that should not be placed in the beginning
330 ;; of a line or the end of a line.
331 (defmacro shr-char-kinsoku-bol-p (char)
332 "Return non-nil if a line ought not to begin with CHAR."
333 `(aref (char-category-set ,char) ?>))
334 (defmacro shr-char-kinsoku-eol-p (char)
335 "Return non-nil if a line ought not to end with CHAR."
336 `(aref (char-category-set ,char) ?<))
337 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
338 (load "kinsoku" nil t))
340 (defun shr-insert (text)
341 (when (and (eq shr-state 'image)
343 (not (string-match "\\`[ \t\n]+\\'" text)))
345 (setq shr-state nil))
347 ((eq shr-folding-mode 'none)
350 (when (and (string-match "\\`[ \t\n ]" text)
352 (not (eq (char-after (1- (point))) ? )))
354 (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t))
356 (> shr-indentation 0))
358 ;; No space is needed behind a wide character categorized as
359 ;; kinsoku-bol, between characters both categorized as nospace,
360 ;; or at the beginning of a line.
362 (when (and (> (current-column) shr-indentation)
363 (eq (preceding-char) ? )
364 (or (= (line-beginning-position) (1- (point)))
365 (and (shr-char-breakable-p
366 (setq prev (char-after (- (point) 2))))
367 (shr-char-kinsoku-bol-p prev))
368 (and (shr-char-nospace-p prev)
369 (shr-char-nospace-p (aref elem 0)))))
371 ;; The shr-start is a special variable that is used to pass
372 ;; upwards the first point in the buffer where the text really
375 (setq shr-start (point)))
379 (while (and (> (current-column) shr-width)
381 (setq found (shr-find-fill-point))
383 (when (eq (preceding-char) ? )
387 ;; No space is needed at the beginning of a line.
388 (when (eq (following-char) ? )
390 (when (> shr-indentation 0)
394 (unless (string-match "[ \t\r\n ]\\'" text)
397 (defun shr-find-fill-point ()
398 (when (> (move-to-column shr-width) shr-width)
402 (while (not (or (setq failed (= (current-column) shr-indentation))
403 (eq (preceding-char) ? )
404 (eq (following-char) ? )
405 (shr-char-breakable-p (preceding-char))
406 (shr-char-breakable-p (following-char))
407 (if (eq (preceding-char) ?')
408 (not (memq (char-after (- (point) 2))
410 (and (shr-char-kinsoku-bol-p (preceding-char))
411 (shr-char-breakable-p (following-char))
412 (not (shr-char-kinsoku-bol-p (following-char)))))
413 (shr-char-kinsoku-eol-p (following-char))))
415 (if (and (not (or failed (eolp)))
416 (eq (preceding-char) ?'))
417 (while (not (or (setq failed (eolp))
418 (eq (following-char) ? )
419 (shr-char-breakable-p (following-char))
420 (shr-char-kinsoku-eol-p (following-char))))
423 ;; There's no breakable point, so we give it up.
426 (unless shr-kinsoku-shorten
427 (while (and (setq found (re-search-forward
428 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
429 (line-end-position) 'move))
430 (eq (preceding-char) ?')))
431 (if (and found (not (match-beginning 1)))
432 (goto-char (match-beginning 0)))))
435 ;; Don't put kinsoku-bol characters at the beginning of a line,
436 ;; or kinsoku-eol characters at the end of a line.
439 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
440 (shr-char-kinsoku-eol-p (preceding-char)))
442 (when (setq failed (= (current-column) shr-indentation))
443 ;; There's no breakable point that doesn't violate kinsoku,
444 ;; so we look for the second best position.
447 (<= (current-column) shr-width))
450 (shr-char-kinsoku-eol-p (following-char)))))
452 ((shr-char-kinsoku-eol-p (preceding-char))
453 ;; Find backward the point where kinsoku-eol characters begin.
458 (and (> (setq count (1- count)) 0)
459 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
460 (or (shr-char-kinsoku-eol-p (preceding-char))
461 (shr-char-kinsoku-bol-p (following-char)))))))
462 (if (setq failed (= (current-column) shr-indentation))
463 ;; There's no breakable point that doesn't violate kinsoku,
464 ;; so we go to the second best position.
465 (if (looking-at "\\(\\c<+\\)\\c<")
466 (goto-char (match-end 1))
468 ((shr-char-kinsoku-bol-p (following-char))
469 ;; Find forward the point where kinsoku-bol characters end.
473 (and (>= (setq count (1- count)) 0)
474 (shr-char-kinsoku-bol-p (following-char))
475 (shr-char-breakable-p (following-char))))))))
476 (when (eq (following-char) ? )
480 (defun shr-expand-url (url)
484 (string-match "\\`[a-z]*:" url)
487 ((and (string-match "\\`//" url)
488 (string-match "\\`[a-z]*:" shr-base))
489 (concat (match-string 0 shr-base) url))
490 ((and (not (string-match "/\\'" shr-base))
491 (not (string-match "\\`/" url)))
492 (concat shr-base "/" url))
494 (concat shr-base url))))
496 (defun shr-ensure-newline ()
497 (unless (zerop (current-column))
500 (defun shr-ensure-paragraph ()
502 (if (<= (current-column) shr-indentation)
503 (unless (save-excursion
510 (delete-region (match-beginning 0) (match-end 0))
514 (when (> shr-indentation 0)
515 (insert (make-string shr-indentation ? ))))
517 (defun shr-fontize-cont (cont &rest types)
521 (shr-add-font (or shr-start (point)) (point) type))))
523 ;; Add an overlay in the region, but avoid putting the font properties
524 ;; on blank text at the start of the line, and the newline at the end,
525 ;; to avoid ugliness.
526 (defun shr-add-font (start end type)
529 (while (< (point) end)
531 (skip-chars-forward " "))
532 (let ((overlay (make-overlay (point) (min (line-end-position) end))))
533 (overlay-put overlay 'face type))
534 (if (< (line-end-position) end)
538 (defun shr-browse-url ()
539 "Browse the URL under point."
541 (let ((url (get-text-property (point) 'shr-url)))
544 (message "No link under point"))
545 ((string-match "^mailto:" url)
546 (browse-url-mail url))
550 (defun shr-save-contents (directory)
551 "Save the contents from URL in a file."
552 (interactive "DSave contents of URL to directory: ")
553 (let ((url (get-text-property (point) 'shr-url)))
555 (message "No link under point")
556 (url-retrieve (shr-encode-url url)
557 'shr-store-contents (list url directory)
560 (defun shr-store-contents (status url directory)
561 (unless (plist-get status :error)
562 (when (or (search-forward "\n\n" nil t)
563 (search-forward "\r\n\r\n" nil t))
564 (write-region (point) (point-max)
565 (expand-file-name (file-name-nondirectory url)
568 (defun shr-image-fetched (status buffer start end &optional flags)
569 (let ((image-buffer (current-buffer)))
570 (when (and (buffer-name buffer)
571 (not (plist-get status :error)))
572 (url-store-in-cache image-buffer)
573 (when (or (search-forward "\n\n" nil t)
574 (search-forward "\r\n\r\n" nil t))
575 (let ((data (buffer-substring (point) (point-max))))
576 (with-current-buffer buffer
578 (let ((alt (buffer-substring start end))
579 (properties (text-properties-at start))
580 (inhibit-read-only t))
581 (delete-region start end)
583 (funcall shr-put-image-function data alt flags)
585 (let ((type (pop properties))
586 (value (pop properties)))
587 (unless (memq type '(display image-size))
588 (put-text-property start (point) type value))))))))))
589 (kill-buffer image-buffer)))
591 (defun shr-put-image (data alt &optional flags)
592 "Put image DATA with a string ALT. Return image."
593 (if (display-graphic-p)
594 (let* ((size (cdr (assq 'size flags)))
598 (create-image data nil t :ascent 100))
601 (shr-rescale-image data t)))
604 (shr-rescale-image data))))))
606 ;; When inserting big-ish pictures, put them at the
607 ;; beginning of the line.
608 (when (and (> (current-column) 0)
609 (> (car (image-size image t)) 400))
611 (if (eq size 'original)
612 (let ((overlays (overlays-at (point))))
613 (insert-sliced-image image (or alt "*") nil 20 1)
614 (dolist (overlay overlays)
615 (overlay-put overlay 'face 'default)))
616 (insert-image image (or alt "*")))
617 (put-text-property start (point) 'image-size size)
618 (when (image-animated-p image)
619 (image-animate image nil 60)))
623 (defun shr-rescale-image (data &optional force)
624 "Rescale DATA, if too big, to fit the current buffer.
625 If FORCE, rescale the image anyway."
626 (let ((image (create-image data nil t :ascent 100)))
627 (if (or (not (fboundp 'imagemagick-types))
628 (not (get-buffer-window (current-buffer))))
630 (let* ((size (image-size image t))
633 (edges (window-inside-pixel-edges
634 (get-buffer-window (current-buffer))))
635 (window-width (truncate (* shr-max-image-proportion
636 (- (nth 2 edges) (nth 0 edges)))))
637 (window-height (truncate (* shr-max-image-proportion
638 (- (nth 3 edges) (nth 1 edges)))))
641 (> height window-height))
642 (setq image (or (create-image data 'imagemagick t
643 :height window-height
646 (setq size (image-size image t)))
647 (when (> (car size) window-width)
649 (create-image data 'imagemagick t
655 ;; url-cache-extract autoloads url-cache.
656 (declare-function url-cache-create-filename "url-cache" (url))
657 (autoload 'mm-disable-multibyte "mm-util")
658 (autoload 'browse-url-mail "browse-url")
660 (defun shr-get-image-data (url)
661 "Get image data for URL.
662 Return a string with image data."
664 (mm-disable-multibyte)
666 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
668 (when (or (search-forward "\n\n" nil t)
669 (search-forward "\r\n\r\n" nil t))
670 (buffer-substring (point) (point-max))))))
672 (defun shr-image-displayer (content-function)
673 "Return a function to display an image.
674 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
675 is an argument. The function to be returned takes three arguments URL,
676 START, and END. Note that START and END should be markers."
677 `(lambda (url start end)
679 (if (string-match "\\`cid:" url)
680 ,(when content-function
681 `(let ((image (funcall ,content-function
682 (substring url (match-end 0)))))
685 (funcall shr-put-image-function
686 image (buffer-substring start end))
687 (delete-region (point) end))))
688 (url-retrieve url 'shr-image-fetched
689 (list (current-buffer) start end)
692 (defun shr-heading (cont &rest types)
693 (shr-ensure-paragraph)
694 (apply #'shr-fontize-cont cont types)
695 (shr-ensure-paragraph))
697 (autoload 'widget-convert-button "wid-edit")
699 (defun shr-urlify (start url &optional title)
700 (widget-convert-button
701 'url-link start (point)
702 :help-echo (if title (format "%s (%s)" url title) url)
705 (shr-add-font start (point) 'shr-link)
706 (put-text-property start (point) 'shr-url url))
708 (defun shr-encode-url (url)
710 (browse-url-url-encode-chars url "[)$ ]"))
712 (autoload 'shr-color-visible "shr-color")
713 (autoload 'shr-color->hexadecimal "shr-color")
715 (defun shr-color-check (fg bg)
716 "Check that FG is visible on BG.
717 Returns (fg bg) with corrected values.
718 Returns nil if the colors that would be used are the default
719 ones, in case fg and bg are nil."
721 (let ((fixed (cond ((null fg) 'fg)
723 ;; Convert colors to hexadecimal, or set them to default.
724 (let ((fg (or (shr-color->hexadecimal fg)
725 (frame-parameter nil 'foreground-color)))
726 (bg (or (shr-color->hexadecimal bg)
727 (frame-parameter nil 'background-color))))
728 (cond ((eq fixed 'bg)
729 ;; Only return the new fg
730 (list nil (cadr (shr-color-visible bg fg t))))
732 ;; Invert args and results and return only the new bg
733 (list (cadr (shr-color-visible fg bg t)) nil))
735 (shr-color-visible bg fg)))))))
737 (defun shr-colorize-region (start end fg &optional bg)
739 (let ((new-colors (shr-color-check fg bg)))
742 (shr-put-color start end :foreground (cadr new-colors)))
744 (shr-put-color start end :background (car new-colors))))
747 ;; Put a color in the region, but avoid putting colors on blank
748 ;; text at the start of the line, and the newline at the end, to avoid
749 ;; ugliness. Also, don't overwrite any existing color information,
750 ;; since this can be called recursively, and we want the "inner" color
752 (defun shr-put-color (start end type color)
755 (while (< (point) end)
757 (not (eq type :background)))
758 (skip-chars-forward " "))
759 (when (> (line-end-position) (point))
760 (shr-put-color-1 (point) (min (line-end-position) end) type color))
761 (if (< (line-end-position) end)
764 (when (and (eq type :background)
765 (= shr-table-depth 0))
766 (shr-expand-newlines start end color))))
768 (defun shr-expand-newlines (start end color)
770 ;; Skip past all white space at the start and ends.
772 (skip-chars-forward " \t\n")
776 (skip-chars-backward " \t\n")
779 (narrow-to-region start end)
780 (let ((width (shr-buffer-width))
782 (goto-char (point-min))
785 (when (and (< (setq column (current-column)) width)
786 (< (setq column (shr-previous-newline-padding-width column))
788 (let ((overlay (make-overlay (point) (1+ (point)))))
789 (overlay-put overlay 'before-string
793 (let ((string (plist-get
794 (overlay-properties overlay)
798 (overlay-put overlay 'before-string "")
800 (overlays-at (point))
802 (propertize (make-string (- width column) ? )
803 'face (list :background color))))))
806 (defun shr-previous-newline-padding-width (width)
807 (let ((overlays (overlays-at (point)))
811 (dolist (overlay overlays)
814 (length (plist-get (overlay-properties overlay)
816 (+ width previous-width))))
818 (defun shr-put-color-1 (start end type color)
819 (let* ((old-props (get-text-property start 'face))
820 (do-put (and (listp old-props)
821 (not (memq type old-props))))
824 (setq change (next-single-property-change start 'face nil end))
826 (put-text-property start change 'face
827 (nconc (list type color) old-props)))
828 (setq old-props (get-text-property change 'face))
829 (setq do-put (and (listp old-props)
830 (not (memq type old-props))))
834 (put-text-property start end 'face
835 (nconc (list type color old-props))))))
837 ;;; Tag-specific rendering rules.
839 (defun shr-tag-body (cont)
840 (let* ((start (point))
841 (fgcolor (cdr (or (assq :fgcolor cont)
843 (bgcolor (cdr (assq :bgcolor cont)))
844 (shr-stylesheet (list (cons 'color fgcolor)
845 (cons 'background-color bgcolor))))
847 (shr-colorize-region start (point) fgcolor bgcolor)))
849 (defun shr-tag-style (cont)
852 (defun shr-tag-script (cont)
855 (defun shr-tag-comment (cont)
858 (defun shr-tag-sup (cont)
859 (let ((start (point)))
861 (put-text-property start (point) 'display '(raise 0.5))))
863 (defun shr-tag-sub (cont)
864 (let ((start (point)))
866 (put-text-property start (point) 'display '(raise -0.5))))
868 (defun shr-tag-label (cont)
870 (shr-ensure-paragraph))
872 (defun shr-tag-p (cont)
873 (shr-ensure-paragraph)
876 (shr-ensure-paragraph))
878 (defun shr-tag-div (cont)
882 (shr-ensure-newline))
884 (defun shr-tag-s (cont)
885 (shr-fontize-cont cont 'shr-strike-through))
887 (defun shr-tag-del (cont)
888 (shr-fontize-cont cont 'shr-strike-through))
890 (defun shr-tag-b (cont)
891 (shr-fontize-cont cont 'bold))
893 (defun shr-tag-i (cont)
894 (shr-fontize-cont cont 'italic))
896 (defun shr-tag-em (cont)
897 (shr-fontize-cont cont 'italic))
899 (defun shr-tag-strong (cont)
900 (shr-fontize-cont cont 'bold))
902 (defun shr-tag-u (cont)
903 (shr-fontize-cont cont 'underline))
905 (defun shr-parse-style (style)
908 (when (string-match "\n" style)
909 (setq style (replace-match " " t t style))))
911 (dolist (elem (split-string style ";"))
913 (setq elem (split-string elem ":"))
914 (when (and (car elem)
916 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
917 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
918 (when (string-match " *!important\\'" value)
919 (setq value (substring value 0 (match-beginning 0))))
920 (push (cons (intern name obarray)
925 (defun shr-tag-base (cont)
926 (setq shr-base (cdr (assq :href cont))))
928 (defun shr-tag-a (cont)
929 (let ((url (cdr (assq :href cont)))
930 (title (cdr (assq :title cont)))
935 (shr-urlify (or shr-start start) (shr-expand-url url) title))))
937 (defun shr-tag-object (cont)
938 (let ((start (point))
941 (when (eq (car elem) 'embed)
942 (setq url (or url (cdr (assq :src (cdr elem))))))
943 (when (and (eq (car elem) 'param)
944 (equal (cdr (assq :name (cdr elem))) "movie"))
945 (setq url (or url (cdr (assq :value (cdr elem)))))))
947 (shr-insert " [multimedia] ")
948 (shr-urlify start (shr-expand-url url)))
951 (defun shr-tag-video (cont)
952 (let ((image (cdr (assq :poster cont)))
953 (url (cdr (assq :src cont)))
955 (shr-tag-img nil image)
956 (shr-urlify start (shr-expand-url url))))
958 (defun shr-tag-img (cont &optional url)
961 (cdr (assq :src cont))))
962 (when (and (> (current-column) 0)
963 (not (eq shr-state 'image)))
965 (let ((alt (cdr (assq :alt cont)))
966 (url (shr-expand-url (or url (cdr (assq :src cont))))))
967 (let ((start (point-marker)))
968 (when (zerop (length alt))
971 ((or (member (cdr (assq :height cont)) '("0" "1"))
972 (member (cdr (assq :width cont)) '("0" "1")))
973 ;; Ignore zero-sized or single-pixel images.
975 ((and (not shr-inhibit-images)
976 (string-match "\\`cid:" url))
977 (let ((url (substring url (match-end 0)))
979 (if (or (not shr-content-function)
980 (not (setq image (funcall shr-content-function url))))
982 (funcall shr-put-image-function image alt))))
983 ((or shr-inhibit-images
984 (and shr-blocked-images
985 (string-match shr-blocked-images url)))
986 (setq shr-start (point))
987 (let ((shr-state 'space))
988 (if (> (string-width alt) 8)
989 (shr-insert (truncate-string-to-width alt 8))
991 ((and (not shr-ignore-cache)
992 (url-is-cached (shr-encode-url url)))
993 (funcall shr-put-image-function (shr-get-image-data url) alt))
996 (when (and shr-ignore-cache
997 (url-is-cached (shr-encode-url url)))
998 (let ((file (url-cache-create-filename (shr-encode-url url))))
999 (when (file-exists-p file)
1000 (delete-file file))))
1002 (shr-encode-url url) 'shr-image-fetched
1003 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
1005 (when (zerop shr-table-depth) ;; We are not in a table.
1006 (put-text-property start (point) 'keymap shr-map)
1007 (put-text-property start (point) 'shr-alt alt)
1008 (put-text-property start (point) 'image-url url)
1009 (put-text-property start (point) 'image-displayer
1010 (shr-image-displayer shr-content-function))
1011 (put-text-property start (point) 'help-echo alt))
1012 (setq shr-state 'image)))))
1014 (defun shr-tag-pre (cont)
1015 (let ((shr-folding-mode 'none))
1016 (shr-ensure-newline)
1019 (shr-ensure-newline)))
1021 (defun shr-tag-blockquote (cont)
1022 (shr-ensure-paragraph)
1024 (let ((shr-indentation (+ shr-indentation 4)))
1026 (shr-ensure-paragraph))
1028 (defun shr-tag-ul (cont)
1029 (shr-ensure-paragraph)
1030 (let ((shr-list-mode 'ul))
1032 (shr-ensure-paragraph))
1034 (defun shr-tag-ol (cont)
1035 (shr-ensure-paragraph)
1036 (let ((shr-list-mode 1))
1038 (shr-ensure-paragraph))
1040 (defun shr-tag-li (cont)
1041 (shr-ensure-paragraph)
1044 (if (numberp shr-list-mode)
1046 (format "%d " shr-list-mode)
1047 (setq shr-list-mode (1+ shr-list-mode)))
1049 (shr-indentation (+ shr-indentation (length bullet))))
1051 (shr-generic cont)))
1053 (defun shr-tag-br (cont)
1054 (when (and (not (bobp))
1055 ;; Only add a newline if we break the current line, or
1056 ;; the previous line isn't a blank line.
1058 (and (> (- (point) 2) (point-min))
1059 (not (= (char-after (- (point) 2)) ?\n)))))
1064 (defun shr-tag-h1 (cont)
1065 (shr-heading cont 'bold 'underline))
1067 (defun shr-tag-h2 (cont)
1068 (shr-heading cont 'bold))
1070 (defun shr-tag-h3 (cont)
1071 (shr-heading cont 'italic))
1073 (defun shr-tag-h4 (cont)
1076 (defun shr-tag-h5 (cont)
1079 (defun shr-tag-h6 (cont)
1082 (defun shr-tag-hr (cont)
1083 (shr-ensure-newline)
1084 (insert (make-string shr-width shr-hr-line) "\n"))
1086 (defun shr-tag-title (cont)
1087 (shr-heading cont 'bold 'underline))
1089 (defun shr-tag-font (cont)
1090 (let* ((start (point))
1091 (color (cdr (assq :color cont)))
1092 (shr-stylesheet (nconc (list (cons 'color color))
1096 (shr-colorize-region start (point) color
1097 (cdr (assq 'background-color shr-stylesheet))))))
1099 ;;; Table rendering algorithm.
1101 ;; Table rendering is the only complicated thing here. We do this by
1102 ;; first counting how many TDs there are in each TR, and registering
1103 ;; how wide they think they should be ("width=45%", etc). Then we
1104 ;; render each TD separately (this is done in temporary buffers, so
1105 ;; that we can use all the rendering machinery as if we were in the
1106 ;; main buffer). Now we know how much space each TD really takes, so
1107 ;; we then render everything again with the new widths, and finally
1108 ;; insert all these boxes into the main buffer.
1109 (defun shr-tag-table-1 (cont)
1110 (setq cont (or (cdr (assq 'tbody cont))
1112 (let* ((shr-inhibit-images t)
1113 (shr-table-depth (1+ shr-table-depth))
1114 (shr-kinsoku-shorten t)
1115 ;; Find all suggested widths.
1116 (columns (shr-column-specs cont))
1117 ;; Compute how many characters wide each TD should be.
1118 (suggested-widths (shr-pro-rate-columns columns))
1119 ;; Do a "test rendering" to see how big each TD is (this can
1120 ;; be smaller (if there's little text) or bigger (if there's
1121 ;; unbreakable text).
1122 (sketch (shr-make-table cont suggested-widths))
1123 ;; Compute the "natural" width by setting each column to 500
1124 ;; characters and see how wide they really render.
1125 (natural (shr-make-table cont (make-vector (length columns) 500)))
1126 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1127 ;; This probably won't work very well.
1128 (when (> (+ (loop for width across sketch-widths
1132 (setq truncate-lines t))
1133 ;; Then render the table again with these new "hard" widths.
1134 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
1135 ;; Finally, insert all the images after the table. The Emacs buffer
1136 ;; model isn't strong enough to allow us to put the images actually
1138 (when (zerop shr-table-depth)
1139 (dolist (elem (shr-find-elements cont 'img))
1140 (shr-tag-img (cdr elem)))))
1142 (defun shr-tag-table (cont)
1143 (shr-ensure-paragraph)
1144 (let* ((caption (cdr (assq 'caption cont)))
1145 (header (cdr (assq 'thead cont)))
1146 (body (or (cdr (assq 'tbody cont)) cont))
1147 (footer (cdr (assq 'tfoot cont)))
1148 (bgcolor (cdr (assq :bgcolor cont)))
1150 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1152 (nheader (if header (shr-max-columns header)))
1153 (nbody (if body (shr-max-columns body)))
1154 (nfooter (if footer (shr-max-columns footer))))
1155 (if (and (not caption)
1157 (not (cdr (assq 'tbody cont)))
1158 (not (cdr (assq 'tr cont)))
1160 ;; The table is totally invalid and just contains random junk.
1161 ;; Try to output it anyway.
1163 ;; It's a real table, so render it.
1166 (if caption `((tr (td ,@caption))))
1169 ;; hader + body + footer
1170 (if (= nheader nbody)
1171 (if (= nbody nfooter)
1172 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
1173 (nconc `((tr (td (table (tbody ,@header ,@body)))))
1176 `((tr (td (table (tbody ,@footer))))))))
1177 (nconc `((tr (td (table (tbody ,@header)))))
1178 (if (= nbody nfooter)
1179 `((tr (td (table (tbody ,@body ,@footer)))))
1180 (nconc `((tr (td (table (tbody ,@body)))))
1183 `((tr (td (table (tbody ,@footer))))))))))
1185 (if (= nheader nbody)
1186 `((tr (td (table (tbody ,@header ,@body)))))
1188 `(,@header (tr (td (table (tbody ,@body)))))
1189 `((tr (td (table (tbody ,@header))))
1190 (tr (td (table (tbody ,@body))))))))
1193 (if (= nbody nfooter)
1194 `((tr (td (table (tbody ,@body ,@footer)))))
1195 (nconc `((tr (td (table (tbody ,@body)))))
1198 `((tr (td (table (tbody ,@footer))))))))
1200 `((tr (td (table (tbody ,@body)))))
1203 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1206 (defun shr-find-elements (cont type)
1209 (cond ((eq (car elem) type)
1212 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1215 (defun shr-insert-table (table widths)
1216 (shr-insert-table-ruler widths)
1218 (let ((start (point))
1219 (height (let ((max 0))
1220 (dolist (column row)
1221 (setq max (max max (cadr column))))
1225 (insert shr-table-vertical-line "\n"))
1226 (dolist (column row)
1228 (let ((lines (nth 2 column))
1229 (overlay-lines (nth 3 column))
1230 overlay overlay-line)
1231 (dolist (line lines)
1232 (setq overlay-line (pop overlay-lines))
1234 (insert line shr-table-vertical-line)
1235 (dolist (overlay overlay-line)
1236 (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
1237 (- (point) (nth 1 overlay) 1)))
1238 (properties (nth 2 overlay)))
1240 (overlay-put o (pop properties) (pop properties)))))
1242 ;; Add blank lines at padding at the bottom of the TD,
1244 (dotimes (i (- height (length lines)))
1246 (let ((start (point)))
1247 (insert (make-string (string-width (car lines)) ? )
1248 shr-table-vertical-line)
1249 (when (nth 4 column)
1250 (shr-put-color start (1- (point)) :background (nth 4 column))))
1251 (forward-line 1)))))
1252 (shr-insert-table-ruler widths)))
1254 (defun shr-insert-table-ruler (widths)
1256 (> shr-indentation 0))
1258 (insert shr-table-corner)
1259 (dotimes (i (length widths))
1260 (insert (make-string (aref widths i) shr-table-horizontal-line)
1264 (defun shr-table-widths (table natural-table suggested-widths)
1265 (let* ((length (length suggested-widths))
1266 (widths (make-vector length 0))
1267 (natural-widths (make-vector length 0)))
1270 (dolist (column row)
1271 (aset widths i (max (aref widths i) column))
1273 (dolist (row natural-table)
1275 (dolist (column row)
1276 (aset natural-widths i (max (aref natural-widths i) column))
1278 (let ((extra (- (apply '+ (append suggested-widths nil))
1279 (apply '+ (append widths nil))))
1280 (expanded-columns 0))
1281 ;; We have extra, unused space, so divide this space amongst the
1284 ;; If the natural width is wider than the rendered width, we
1285 ;; want to allow the column to expand.
1287 (when (> (aref natural-widths i) (aref widths i))
1288 (setq expanded-columns (1+ expanded-columns))))
1290 (when (> (aref natural-widths i) (aref widths i))
1292 (aref natural-widths i)
1293 (+ (/ extra expanded-columns)
1294 (aref widths i))))))))
1297 (defun shr-make-table (cont widths &optional fill)
1300 (when (eq (car row) 'tr)
1305 (while (< i (length widths))
1306 (setq column (pop columns))
1307 (when (or (memq (car column) '(td th))
1309 (push (shr-render-td (cdr column) (aref widths i) fill)
1312 (push (nreverse tds) trs))))
1315 (defun shr-render-td (cont width fill)
1317 (let ((bgcolor (cdr (assq :bgcolor cont)))
1318 (fgcolor (cdr (assq :fgcolor cont)))
1319 (style (cdr (assq :style cont)))
1320 (shr-stylesheet shr-stylesheet)
1321 overlays actual-colors)
1323 (setq style (and (string-match "color" style)
1324 (shr-parse-style style))))
1326 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1328 (setq style (nconc (list (cons 'color fgcolor)) style)))
1330 (setq shr-stylesheet (append style shr-stylesheet)))
1331 (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
1334 (insert (car cache))
1335 (let ((end (length (car cache))))
1336 (dolist (overlay (cadr cache))
1338 (make-overlay (1+ (- end (nth 0 overlay)))
1339 (1+ (- end (nth 1 overlay)))))
1340 (properties (nth 2 overlay)))
1342 (overlay-put new-overlay
1343 (pop properties) (pop properties)))))))
1344 (let ((shr-width width)
1345 (shr-indentation 0))
1346 (shr-descend (cons 'td cont)))
1347 ;; Delete padding at the bottom of the TDs.
1351 (skip-chars-backward " \t\n")
1354 (push (list (cons width cont) (buffer-string)
1355 (shr-overlays-in-region (point-min) (point-max)))
1356 shr-content-cache)))
1357 (goto-char (point-min))
1361 (setq max (max max (current-column)))
1364 (goto-char (point-min))
1365 ;; If the buffer is totally empty, then put a single blank
1367 (if (zerop (buffer-size))
1368 (insert (make-string width ? ))
1369 ;; Otherwise, fill the buffer.
1372 (when (> (- width (current-column)) 0)
1373 (insert (make-string (- width (current-column)) ? )))
1377 (shr-colorize-region
1378 (point-min) (point-max)
1379 (cdr (assq 'color shr-stylesheet))
1380 (cdr (assq 'background-color shr-stylesheet))))))
1383 (count-lines (point-min) (point-max))
1384 (split-string (buffer-string) "\n")
1385 (shr-collect-overlays)
1386 (car actual-colors))
1389 (defun shr-buffer-width ()
1390 (goto-char (point-min))
1394 (setq max (max max (current-column)))
1398 (defun shr-collect-overlays ()
1400 (goto-char (point-min))
1401 (let ((overlays nil))
1403 (push (shr-overlays-in-region (point) (line-end-position))
1406 (nreverse overlays))))
1408 (defun shr-overlays-in-region (start end)
1410 (dolist (overlay (overlays-in start end))
1411 (push (list (if (> start (overlay-start overlay))
1413 (- end (overlay-start overlay)))
1414 (if (< end (overlay-end overlay))
1416 (- end (overlay-end overlay)))
1417 (overlay-properties overlay))
1421 (defun shr-pro-rate-columns (columns)
1422 (let ((total-percentage 0)
1423 (widths (make-vector (length columns) 0)))
1424 (dotimes (i (length columns))
1425 (setq total-percentage (+ total-percentage (aref columns i))))
1426 (setq total-percentage (/ 1.0 total-percentage))
1427 (dotimes (i (length columns))
1428 (aset widths i (max (truncate (* (aref columns i)
1430 (- shr-width (1+ (length columns)))))
1434 ;; Return a summary of the number and shape of the TDs in the table.
1435 (defun shr-column-specs (cont)
1436 (let ((columns (make-vector (shr-max-columns cont) 1)))
1438 (when (eq (car row) 'tr)
1440 (dolist (column (cdr row))
1441 (when (memq (car column) '(td th))
1442 (let ((width (cdr (assq :width (cdr column)))))
1444 (string-match "\\([0-9]+\\)%" width)
1445 (not (zerop (setq width (string-to-number
1446 (match-string 1 width))))))
1447 (aset columns i (/ width 100.0))))
1448 (setq i (1+ i)))))))
1451 (defun shr-count (cont elem)
1454 (when (eq (car sub) elem)
1458 (defun shr-max-columns (cont)
1461 (when (eq (car row) 'tr)
1462 (setq max (max max (+ (shr-count (cdr row) 'td)
1463 (shr-count (cdr row) 'th))))))
1469 ;; coding: iso-8859-1
1472 ;;; shr.el ends here