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
"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-visit-file (file)
133 "Parse FILE as an HTML document, and render it in a new buffer."
134 (interactive "fHTML file name: ")
135 (pop-to-buffer "*html*")
139 (insert-file-contents file
)
140 (libxml-parse-html-region (point-min) (point-max))))
141 (goto-char (point-min)))
144 (defun shr-insert-document (dom)
145 "Render the parsed document DOM into the current buffer.
146 DOM should be a parse tree as generated by
147 `libxml-parse-html-region' or similar."
148 (setq shr-content-cache nil
)
149 (let ((start (point))
153 (shr-width (or shr-width
(window-width))))
154 (shr-descend (shr-transform-dom dom
))
155 (shr-remove-trailing-whitespace start
(point))))
157 (defun shr-remove-trailing-whitespace (start end
)
158 (let ((width (window-width)))
160 (narrow-to-region start end
)
164 (when (> (shr-previous-newline-padding-width (current-column)) width
)
165 (dolist (overlay (overlays-at (point)))
166 (when (overlay-get overlay
'before-string
)
167 (overlay-put overlay
'before-string nil
))))
170 (defun shr-copy-url ()
171 "Copy the URL under point to the kill ring.
172 If called twice, then try to fetch the URL and see whether it
173 redirects somewhere else."
175 (let ((url (get-text-property (point) 'shr-url
)))
178 (message "No URL under point"))
179 ;; Resolve redirected URLs.
180 ((equal url
(car kill-ring
))
185 (eq (car a
) :redirect
))
188 (goto-char (point-min))
189 ;; Remove common tracking junk from the URL.
190 (when (re-search-forward ".utm_.*" nil t
)
191 (replace-match "" t t
))
192 (message "Copied %s" (buffer-string))
193 (copy-region-as-kill (point-min) (point-max)))))
195 ;; Copy the URL to the kill ring.
199 (copy-region-as-kill (point-min) (point-max))
200 (message "Copied %s" url
))))))
202 (defun shr-show-alt-text ()
203 "Show the ALT text of the image under point."
205 (let ((text (get-text-property (point) 'shr-alt
)))
207 (message "No image under point")
208 (message "%s" text
))))
210 (defun shr-browse-image (&optional copy-url
)
211 "Browse the image under point.
212 If COPY-URL (the prefix if called interactively) is non-nil, copy
213 the URL of the image to the kill buffer instead."
215 (let ((url (get-text-property (point) 'image-url
)))
218 (message "No image under point"))
222 (copy-region-as-kill (point-min) (point-max))
223 (message "Copied %s" url
)))
225 (message "Browsing %s..." url
)
228 (defun shr-insert-image ()
229 "Insert the image under point into the buffer."
231 (let ((url (get-text-property (point) 'image-url
)))
233 (message "No image under point")
234 (message "Inserting %s..." url
)
235 (url-retrieve url
'shr-image-fetched
236 (list (current-buffer) (1- (point)) (point-marker))
239 (defun shr-zoom-image ()
240 "Toggle the image size.
241 The size will be rotated between the default size, the original
242 size, and full-buffer size."
244 (let ((url (get-text-property (point) 'image-url
))
245 (size (get-text-property (point) 'image-size
))
246 (buffer-read-only nil
))
248 (message "No image under point")
249 ;; Delete the old picture.
250 (while (get-text-property (point) 'image-url
)
253 (let ((start (point)))
254 (while (get-text-property (point) 'image-url
)
257 (put-text-property start
(point) 'display nil
)
258 (when (> (- (point) start
) 2)
259 (delete-region start
(1- (point)))))
260 (message "Inserting %s..." url
)
261 (url-retrieve url
'shr-image-fetched
262 (list (current-buffer) (1- (point)) (point-marker)
264 (cond ((or (eq size
'default
)
273 ;;; Utility functions.
275 (defun shr-transform-dom (dom)
276 (let ((result (list (pop dom
))))
277 (dolist (arg (pop dom
))
278 (push (cons (intern (concat ":" (symbol-name (car arg
))) obarray
)
283 (push (cons 'text sub
) result
)
284 (push (shr-transform-dom sub
) result
)))
287 (defun shr-descend (dom)
288 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom
))) obarray
))
289 (style (cdr (assq :style
(cdr dom
))))
290 (shr-stylesheet shr-stylesheet
)
293 (if (string-match "color" style
)
294 (setq shr-stylesheet
(nconc (shr-parse-style style
)
297 (if (fboundp function
)
298 (funcall function
(cdr dom
))
299 (shr-generic (cdr dom
)))
300 ;; If style is set, then this node has set the color.
302 (shr-colorize-region start
(point)
303 (cdr (assq 'color shr-stylesheet
))
304 (cdr (assq 'background-color shr-stylesheet
))))))
306 (defun shr-generic (cont)
309 ((eq (car sub
) 'text
)
310 (shr-insert (cdr sub
)))
312 (shr-descend sub
)))))
314 (defmacro shr-char-breakable-p
(char)
315 "Return non-nil if a line can be broken before and after CHAR."
316 `(aref fill-find-break-point-function-table
,char
))
317 (defmacro shr-char-nospace-p
(char)
318 "Return non-nil if no space is required before and after CHAR."
319 `(aref fill-nospace-between-words-table
,char
))
321 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
322 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
323 ;; parentheses, and so on, that should not be placed in the beginning
324 ;; of a line or the end of a line.
325 (defmacro shr-char-kinsoku-bol-p
(char)
326 "Return non-nil if a line ought not to begin with CHAR."
327 `(aref (char-category-set ,char
) ?
>))
328 (defmacro shr-char-kinsoku-eol-p
(char)
329 "Return non-nil if a line ought not to end with CHAR."
330 `(aref (char-category-set ,char
) ?
<))
331 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
332 (load "kinsoku" nil t
))
334 (defun shr-insert (text)
335 (when (and (eq shr-state
'image
)
337 (not (string-match "\\`[ \t\n]+\\'" text
)))
339 (setq shr-state nil
))
341 ((eq shr-folding-mode
'none
)
344 (when (and (string-match "\\`[ \t\n ]" text
)
346 (not (eq (char-after (1- (point))) ?
)))
348 (dolist (elem (split-string text
"[ \f\t\n\r\v ]+" t
))
350 (> shr-indentation
0))
352 ;; No space is needed behind a wide character categorized as
353 ;; kinsoku-bol, between characters both categorized as nospace,
354 ;; or at the beginning of a line.
356 (when (and (> (current-column) shr-indentation
)
357 (eq (preceding-char) ?
)
358 (or (= (line-beginning-position) (1- (point)))
359 (and (shr-char-breakable-p
360 (setq prev
(char-after (- (point) 2))))
361 (shr-char-kinsoku-bol-p prev
))
362 (and (shr-char-nospace-p prev
)
363 (shr-char-nospace-p (aref elem
0)))))
365 ;; The shr-start is a special variable that is used to pass
366 ;; upwards the first point in the buffer where the text really
369 (setq shr-start
(point)))
373 (while (and (> (current-column) shr-width
)
375 (setq found
(shr-find-fill-point))
377 (when (eq (preceding-char) ?
)
381 ;; No space is needed at the beginning of a line.
382 (when (eq (following-char) ?
)
384 (when (> shr-indentation
0)
388 (unless (string-match "[ \t\n ]\\'" text
)
391 (defun shr-find-fill-point ()
392 (when (> (move-to-column shr-width
) shr-width
)
396 (while (not (or (setq failed
(= (current-column) shr-indentation
))
397 (eq (preceding-char) ?
)
398 (eq (following-char) ?
)
399 (shr-char-breakable-p (preceding-char))
400 (shr-char-breakable-p (following-char))
401 (if (eq (preceding-char) ?
')
402 (not (memq (char-after (- (point) 2))
404 (and (shr-char-kinsoku-bol-p (preceding-char))
405 (shr-char-breakable-p (following-char))
406 (not (shr-char-kinsoku-bol-p (following-char)))))
407 (shr-char-kinsoku-eol-p (following-char))))
409 (if (and (not (or failed
(eolp)))
410 (eq (preceding-char) ?
'))
411 (while (not (or (setq failed
(eolp))
412 (eq (following-char) ?
)
413 (shr-char-breakable-p (following-char))
414 (shr-char-kinsoku-eol-p (following-char))))
417 ;; There's no breakable point, so we give it up.
420 (unless shr-kinsoku-shorten
421 (while (and (setq found
(re-search-forward
422 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
423 (line-end-position) 'move
))
424 (eq (preceding-char) ?
')))
425 (if (and found
(not (match-beginning 1)))
426 (goto-char (match-beginning 0)))))
429 ;; Don't put kinsoku-bol characters at the beginning of a line,
430 ;; or kinsoku-eol characters at the end of a line.
433 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
434 (shr-char-kinsoku-eol-p (preceding-char)))
436 (when (setq failed
(= (current-column) shr-indentation
))
437 ;; There's no breakable point that doesn't violate kinsoku,
438 ;; so we look for the second best position.
441 (<= (current-column) shr-width
))
444 (shr-char-kinsoku-eol-p (following-char)))))
446 ((shr-char-kinsoku-eol-p (preceding-char))
447 ;; Find backward the point where kinsoku-eol characters begin.
452 (and (> (setq count
(1- count
)) 0)
453 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
454 (or (shr-char-kinsoku-eol-p (preceding-char))
455 (shr-char-kinsoku-bol-p (following-char)))))))
456 (if (setq failed
(= (current-column) shr-indentation
))
457 ;; There's no breakable point that doesn't violate kinsoku,
458 ;; so we go to the second best position.
459 (if (looking-at "\\(\\c<+\\)\\c<")
460 (goto-char (match-end 1))
462 ((shr-char-kinsoku-bol-p (following-char))
463 ;; Find forward the point where kinsoku-bol characters end.
467 (and (>= (setq count
(1- count
)) 0)
468 (shr-char-kinsoku-bol-p (following-char))
469 (shr-char-breakable-p (following-char))))))))
470 (when (eq (following-char) ?
)
474 (defun shr-expand-url (url)
478 (string-match "\\`[a-z]*:" url
)
481 ((and (not (string-match "/\\'" shr-base
))
482 (not (string-match "\\`/" url
)))
483 (concat shr-base
"/" url
))
485 (concat shr-base url
))))
487 (defun shr-ensure-newline ()
488 (unless (zerop (current-column))
491 (defun shr-ensure-paragraph ()
493 (if (<= (current-column) shr-indentation
)
494 (unless (save-excursion
501 (delete-region (match-beginning 0) (match-end 0))
505 (when (> shr-indentation
0)
506 (insert (make-string shr-indentation ?
))))
508 (defun shr-fontize-cont (cont &rest types
)
512 (shr-add-font (or shr-start
(point)) (point) type
))))
514 ;; Add an overlay in the region, but avoid putting the font properties
515 ;; on blank text at the start of the line, and the newline at the end,
516 ;; to avoid ugliness.
517 (defun shr-add-font (start end type
)
520 (while (< (point) end
)
522 (skip-chars-forward " "))
523 (let ((overlay (make-overlay (point) (min (line-end-position) end
))))
524 (overlay-put overlay
'face type
))
525 (if (< (line-end-position) end
)
529 (defun shr-browse-url ()
530 "Browse the URL under point."
532 (let ((url (get-text-property (point) 'shr-url
)))
535 (message "No link under point"))
536 ((string-match "^mailto:" url
)
537 (browse-url-mail url
))
541 (defun shr-save-contents (directory)
542 "Save the contents from URL in a file."
543 (interactive "DSave contents of URL to directory: ")
544 (let ((url (get-text-property (point) 'shr-url
)))
546 (message "No link under point")
547 (url-retrieve (shr-encode-url url
)
548 'shr-store-contents
(list url directory
)
551 (defun shr-store-contents (status url directory
)
552 (unless (plist-get status
:error
)
553 (when (or (search-forward "\n\n" nil t
)
554 (search-forward "\r\n\r\n" nil t
))
555 (write-region (point) (point-max)
556 (expand-file-name (file-name-nondirectory url
)
559 (defun shr-image-fetched (status buffer start end
&optional flags
)
560 (let ((image-buffer (current-buffer)))
561 (when (and (buffer-name buffer
)
562 (not (plist-get status
:error
)))
563 (url-store-in-cache image-buffer
)
564 (when (or (search-forward "\n\n" nil t
)
565 (search-forward "\r\n\r\n" nil t
))
566 (let ((data (buffer-substring (point) (point-max))))
567 (with-current-buffer buffer
569 (let ((alt (buffer-substring start end
))
570 (properties (text-properties-at start
))
571 (inhibit-read-only t
))
572 (delete-region start end
)
574 (funcall shr-put-image-function data alt flags
)
576 (let ((type (pop properties
))
577 (value (pop properties
)))
578 (unless (memq type
'(display image-size
))
579 (put-text-property start
(point) type value
))))))))))
580 (kill-buffer image-buffer
)))
582 (defun shr-put-image (data alt
&optional flags
)
583 "Put image DATA with a string ALT. Return image."
584 (if (display-graphic-p)
585 (let* ((size (cdr (assq 'size flags
)))
589 (create-image data nil t
:ascent
100))
592 (shr-rescale-image data t
)))
595 (shr-rescale-image data
))))))
597 ;; When inserting big-ish pictures, put them at the
598 ;; beginning of the line.
599 (when (and (> (current-column) 0)
600 (> (car (image-size image t
)) 400))
602 (if (eq size
'original
)
603 (let ((overlays (overlays-at (point))))
604 (insert-sliced-image image
(or alt
"*") nil
20 1)
605 (dolist (overlay overlays
)
606 (overlay-put overlay
'face
'default
)))
607 (insert-image image
(or alt
"*")))
608 (put-text-property start
(point) 'image-size size
)
609 (when (image-animated-p image
)
610 (image-animate image nil
60)))
614 (defun shr-rescale-image (data &optional force
)
615 "Rescale DATA, if too big, to fit the current buffer.
616 If FORCE, rescale the image anyway."
617 (let ((image (create-image data nil t
:ascent
100)))
618 (if (or (not (fboundp 'imagemagick-types
))
619 (not (get-buffer-window (current-buffer))))
621 (let* ((size (image-size image t
))
624 (edges (window-inside-pixel-edges
625 (get-buffer-window (current-buffer))))
626 (window-width (truncate (* shr-max-image-proportion
627 (- (nth 2 edges
) (nth 0 edges
)))))
628 (window-height (truncate (* shr-max-image-proportion
629 (- (nth 3 edges
) (nth 1 edges
)))))
632 (> height window-height
))
633 (setq image
(or (create-image data
'imagemagick t
634 :height window-height
637 (setq size
(image-size image t
)))
638 (when (> (car size
) window-width
)
640 (create-image data
'imagemagick t
646 ;; url-cache-extract autoloads url-cache.
647 (declare-function url-cache-create-filename
"url-cache" (url))
648 (autoload 'mm-disable-multibyte
"mm-util")
649 (autoload 'browse-url-mail
"browse-url")
651 (defun shr-get-image-data (url)
652 "Get image data for URL.
653 Return a string with image data."
655 (mm-disable-multibyte)
657 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
659 (when (or (search-forward "\n\n" nil t
)
660 (search-forward "\r\n\r\n" nil t
))
661 (buffer-substring (point) (point-max))))))
663 (defun shr-image-displayer (content-function)
664 "Return a function to display an image.
665 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
666 is an argument. The function to be returned takes three arguments URL,
667 START, and END. Note that START and END should be markers."
668 `(lambda (url start end
)
670 (if (string-match "\\`cid:" url
)
671 ,(when content-function
672 `(let ((image (funcall ,content-function
673 (substring url
(match-end 0)))))
676 (funcall shr-put-image-function
677 image
(buffer-substring start end
))
678 (delete-region (point) end
))))
679 (url-retrieve url
'shr-image-fetched
680 (list (current-buffer) start end
)
683 (defun shr-heading (cont &rest types
)
684 (shr-ensure-paragraph)
685 (apply #'shr-fontize-cont cont types
)
686 (shr-ensure-paragraph))
688 (autoload 'widget-convert-button
"wid-edit")
690 (defun shr-urlify (start url
&optional title
)
691 (widget-convert-button
692 'url-link start
(point)
693 :help-echo
(if title
(format "%s (%s)" url title
) url
)
696 (shr-add-font start
(point) 'shr-link
)
697 (put-text-property start
(point) 'shr-url url
))
699 (defun shr-encode-url (url)
701 (browse-url-url-encode-chars url
"[)$ ]"))
703 (autoload 'shr-color-visible
"shr-color")
704 (autoload 'shr-color-
>hexadecimal
"shr-color")
706 (defun shr-color-check (fg bg
)
707 "Check that FG is visible on BG.
708 Returns (fg bg) with corrected values.
709 Returns nil if the colors that would be used are the default
710 ones, in case fg and bg are nil."
712 (let ((fixed (cond ((null fg
) 'fg
)
714 ;; Convert colors to hexadecimal, or set them to default.
715 (let ((fg (or (shr-color->hexadecimal fg
)
716 (frame-parameter nil
'foreground-color
)))
717 (bg (or (shr-color->hexadecimal bg
)
718 (frame-parameter nil
'background-color
))))
719 (cond ((eq fixed
'bg
)
720 ;; Only return the new fg
721 (list nil
(cadr (shr-color-visible bg fg t
))))
723 ;; Invert args and results and return only the new bg
724 (list (cadr (shr-color-visible fg bg t
)) nil
))
726 (shr-color-visible bg fg
)))))))
728 (defun shr-colorize-region (start end fg
&optional bg
)
730 (let ((new-colors (shr-color-check fg bg
)))
733 (shr-put-color start end
:foreground
(cadr new-colors
)))
735 (shr-put-color start end
:background
(car new-colors
))))
738 ;; Put a color in the region, but avoid putting colors on blank
739 ;; text at the start of the line, and the newline at the end, to avoid
740 ;; ugliness. Also, don't overwrite any existing color information,
741 ;; since this can be called recursively, and we want the "inner" color
743 (defun shr-put-color (start end type color
)
746 (while (< (point) end
)
748 (not (eq type
:background
)))
749 (skip-chars-forward " "))
750 (when (> (line-end-position) (point))
751 (shr-put-color-1 (point) (min (line-end-position) end
) type color
))
752 (if (< (line-end-position) end
)
755 (when (and (eq type
:background
)
756 (= shr-table-depth
0))
757 (shr-expand-newlines start end color
))))
759 (defun shr-expand-newlines (start end color
)
761 ;; Skip past all white space at the start and ends.
763 (skip-chars-forward " \t\n")
767 (skip-chars-backward " \t\n")
770 (narrow-to-region start end
)
771 (let ((width (shr-buffer-width))
773 (goto-char (point-min))
776 (when (and (< (setq column
(current-column)) width
)
777 (< (setq column
(shr-previous-newline-padding-width column
))
779 (let ((overlay (make-overlay (point) (1+ (point)))))
780 (overlay-put overlay
'before-string
784 (let ((string (plist-get
785 (overlay-properties overlay
)
789 (overlay-put overlay
'before-string
"")
791 (overlays-at (point))
793 (propertize (make-string (- width column
) ?
)
794 'face
(list :background color
))))))
797 (defun shr-previous-newline-padding-width (width)
798 (let ((overlays (overlays-at (point)))
802 (dolist (overlay overlays
)
805 (length (plist-get (overlay-properties overlay
)
807 (+ width previous-width
))))
809 (defun shr-put-color-1 (start end type color
)
810 (let* ((old-props (get-text-property start
'face
))
811 (do-put (and (listp old-props
)
812 (not (memq type old-props
))))
815 (setq change
(next-single-property-change start
'face nil end
))
817 (put-text-property start change
'face
818 (nconc (list type color
) old-props
)))
819 (setq old-props
(get-text-property change
'face
))
820 (setq do-put
(and (listp old-props
)
821 (not (memq type old-props
))))
825 (put-text-property start end
'face
826 (nconc (list type color old-props
))))))
828 ;;; Tag-specific rendering rules.
830 (defun shr-tag-body (cont)
831 (let* ((start (point))
832 (fgcolor (cdr (or (assq :fgcolor cont
)
834 (bgcolor (cdr (assq :bgcolor cont
)))
835 (shr-stylesheet (list (cons 'color fgcolor
)
836 (cons 'background-color bgcolor
))))
838 (shr-colorize-region start
(point) fgcolor bgcolor
)))
840 (defun shr-tag-style (cont)
843 (defun shr-tag-script (cont)
846 (defun shr-tag-comment (cont)
849 (defun shr-tag-sup (cont)
850 (let ((start (point)))
852 (put-text-property start
(point) 'display
'(raise 0.5))))
854 (defun shr-tag-sub (cont)
855 (let ((start (point)))
857 (put-text-property start
(point) 'display
'(raise -
0.5))))
859 (defun shr-tag-label (cont)
861 (shr-ensure-paragraph))
863 (defun shr-tag-p (cont)
864 (shr-ensure-paragraph)
867 (shr-ensure-paragraph))
869 (defun shr-tag-div (cont)
873 (shr-ensure-newline))
875 (defun shr-tag-s (cont)
876 (shr-fontize-cont cont
'shr-strike-through
))
878 (defun shr-tag-del (cont)
879 (shr-fontize-cont cont
'shr-strike-through
))
881 (defun shr-tag-b (cont)
882 (shr-fontize-cont cont
'bold
))
884 (defun shr-tag-i (cont)
885 (shr-fontize-cont cont
'italic
))
887 (defun shr-tag-em (cont)
888 (shr-fontize-cont cont
'bold
))
890 (defun shr-tag-strong (cont)
891 (shr-fontize-cont cont
'bold
))
893 (defun shr-tag-u (cont)
894 (shr-fontize-cont cont
'underline
))
896 (defun shr-parse-style (style)
899 (when (string-match "\n" style
)
900 (setq style
(replace-match " " t t style
))))
902 (dolist (elem (split-string style
";"))
904 (setq elem
(split-string elem
":"))
905 (when (and (car elem
)
907 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
908 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
909 (when (string-match " *!important\\'" value
)
910 (setq value
(substring value
0 (match-beginning 0))))
911 (push (cons (intern name obarray
)
916 (defun shr-tag-base (cont)
917 (setq shr-base
(cdr (assq :href cont
))))
919 (defun shr-tag-a (cont)
920 (let ((url (cdr (assq :href cont
)))
921 (title (cdr (assq :title cont
)))
925 (shr-urlify (or shr-start start
) (shr-expand-url url
) title
)))
927 (defun shr-tag-object (cont)
928 (let ((start (point))
931 (when (eq (car elem
) 'embed
)
932 (setq url
(or url
(cdr (assq :src
(cdr elem
))))))
933 (when (and (eq (car elem
) 'param
)
934 (equal (cdr (assq :name
(cdr elem
))) "movie"))
935 (setq url
(or url
(cdr (assq :value
(cdr elem
)))))))
937 (shr-insert " [multimedia] ")
938 (shr-urlify start
(shr-expand-url url
)))
941 (defun shr-tag-video (cont)
942 (let ((image (cdr (assq :poster cont
)))
943 (url (cdr (assq :src cont
)))
945 (shr-tag-img nil image
)
946 (shr-urlify start
(shr-expand-url url
))))
948 (defun shr-tag-img (cont &optional url
)
951 (cdr (assq :src cont
))))
952 (when (and (> (current-column) 0)
953 (not (eq shr-state
'image
)))
955 (let ((alt (cdr (assq :alt cont
)))
956 (url (shr-expand-url (or url
(cdr (assq :src cont
))))))
957 (let ((start (point-marker)))
958 (when (zerop (length alt
))
961 ((or (member (cdr (assq :height cont
)) '("0" "1"))
962 (member (cdr (assq :width cont
)) '("0" "1")))
963 ;; Ignore zero-sized or single-pixel images.
965 ((and (not shr-inhibit-images
)
966 (string-match "\\`cid:" url
))
967 (let ((url (substring url
(match-end 0)))
969 (if (or (not shr-content-function
)
970 (not (setq image
(funcall shr-content-function url
))))
972 (funcall shr-put-image-function image alt
))))
973 ((or shr-inhibit-images
974 (and shr-blocked-images
975 (string-match shr-blocked-images url
)))
976 (setq shr-start
(point))
977 (let ((shr-state 'space
))
978 (if (> (string-width alt
) 8)
979 (shr-insert (truncate-string-to-width alt
8))
981 ((and (not shr-ignore-cache
)
982 (url-is-cached (shr-encode-url url
)))
983 (funcall shr-put-image-function
(shr-get-image-data url
) alt
))
986 (when (and shr-ignore-cache
987 (url-is-cached (shr-encode-url url
)))
988 (let ((file (url-cache-create-filename (shr-encode-url url
))))
989 (when (file-exists-p file
)
990 (delete-file file
))))
992 (shr-encode-url url
) 'shr-image-fetched
993 (list (current-buffer) start
(set-marker (make-marker) (1- (point))))
995 (when (zerop shr-table-depth
) ;; We are not in a table.
996 (put-text-property start
(point) 'keymap shr-map
)
997 (put-text-property start
(point) 'shr-alt alt
)
998 (put-text-property start
(point) 'image-url url
)
999 (put-text-property start
(point) 'image-displayer
1000 (shr-image-displayer shr-content-function
))
1001 (put-text-property start
(point) 'help-echo alt
))
1002 (setq shr-state
'image
)))))
1004 (defun shr-tag-pre (cont)
1005 (let ((shr-folding-mode 'none
))
1006 (shr-ensure-newline)
1009 (shr-ensure-newline)))
1011 (defun shr-tag-blockquote (cont)
1012 (shr-ensure-paragraph)
1014 (let ((shr-indentation (+ shr-indentation
4)))
1016 (shr-ensure-paragraph))
1018 (defun shr-tag-ul (cont)
1019 (shr-ensure-paragraph)
1020 (let ((shr-list-mode 'ul
))
1022 (shr-ensure-paragraph))
1024 (defun shr-tag-ol (cont)
1025 (shr-ensure-paragraph)
1026 (let ((shr-list-mode 1))
1028 (shr-ensure-paragraph))
1030 (defun shr-tag-li (cont)
1031 (shr-ensure-paragraph)
1034 (if (numberp shr-list-mode
)
1036 (format "%d " shr-list-mode
)
1037 (setq shr-list-mode
(1+ shr-list-mode
)))
1039 (shr-indentation (+ shr-indentation
(length bullet
))))
1041 (shr-generic cont
)))
1043 (defun shr-tag-br (cont)
1044 (when (and (not (bobp))
1045 ;; Only add a newline if we break the current line, or
1046 ;; the previous line isn't a blank line.
1048 (and (> (- (point) 2) (point-min))
1049 (not (= (char-after (- (point) 2)) ?
\n)))))
1054 (defun shr-tag-h1 (cont)
1055 (shr-heading cont
'bold
'underline
))
1057 (defun shr-tag-h2 (cont)
1058 (shr-heading cont
'bold
))
1060 (defun shr-tag-h3 (cont)
1061 (shr-heading cont
'italic
))
1063 (defun shr-tag-h4 (cont)
1066 (defun shr-tag-h5 (cont)
1069 (defun shr-tag-h6 (cont)
1072 (defun shr-tag-hr (cont)
1073 (shr-ensure-newline)
1074 (insert (make-string shr-width shr-hr-line
) "\n"))
1076 (defun shr-tag-title (cont)
1077 (shr-heading cont
'bold
'underline
))
1079 (defun shr-tag-font (cont)
1080 (let* ((start (point))
1081 (color (cdr (assq :color cont
)))
1082 (shr-stylesheet (nconc (list (cons 'color color
))
1086 (shr-colorize-region start
(point) color
1087 (cdr (assq 'background-color shr-stylesheet
))))))
1089 ;;; Table rendering algorithm.
1091 ;; Table rendering is the only complicated thing here. We do this by
1092 ;; first counting how many TDs there are in each TR, and registering
1093 ;; how wide they think they should be ("width=45%", etc). Then we
1094 ;; render each TD separately (this is done in temporary buffers, so
1095 ;; that we can use all the rendering machinery as if we were in the
1096 ;; main buffer). Now we know how much space each TD really takes, so
1097 ;; we then render everything again with the new widths, and finally
1098 ;; insert all these boxes into the main buffer.
1099 (defun shr-tag-table-1 (cont)
1100 (setq cont
(or (cdr (assq 'tbody cont
))
1102 (let* ((shr-inhibit-images t
)
1103 (shr-table-depth (1+ shr-table-depth
))
1104 (shr-kinsoku-shorten t
)
1105 ;; Find all suggested widths.
1106 (columns (shr-column-specs cont
))
1107 ;; Compute how many characters wide each TD should be.
1108 (suggested-widths (shr-pro-rate-columns columns
))
1109 ;; Do a "test rendering" to see how big each TD is (this can
1110 ;; be smaller (if there's little text) or bigger (if there's
1111 ;; unbreakable text).
1112 (sketch (shr-make-table cont suggested-widths
))
1113 ;; Compute the "natural" width by setting each column to 500
1114 ;; characters and see how wide they really render.
1115 (natural (shr-make-table cont
(make-vector (length columns
) 500)))
1116 (sketch-widths (shr-table-widths sketch natural suggested-widths
)))
1117 ;; This probably won't work very well.
1118 (when (> (+ (loop for width across sketch-widths
1122 (setq truncate-lines t
))
1123 ;; Then render the table again with these new "hard" widths.
1124 (shr-insert-table (shr-make-table cont sketch-widths t
) sketch-widths
))
1125 ;; Finally, insert all the images after the table. The Emacs buffer
1126 ;; model isn't strong enough to allow us to put the images actually
1128 (when (zerop shr-table-depth
)
1129 (dolist (elem (shr-find-elements cont
'img
))
1130 (shr-tag-img (cdr elem
)))))
1132 (defun shr-tag-table (cont)
1133 (shr-ensure-paragraph)
1134 (let* ((caption (cdr (assq 'caption cont
)))
1135 (header (cdr (assq 'thead cont
)))
1136 (body (or (cdr (assq 'tbody cont
)) cont
))
1137 (footer (cdr (assq 'tfoot cont
)))
1138 (bgcolor (cdr (assq :bgcolor cont
)))
1140 (shr-stylesheet (nconc (list (cons 'background-color bgcolor
))
1142 (nheader (if header
(shr-max-columns header
)))
1143 (nbody (if body
(shr-max-columns body
)))
1144 (nfooter (if footer
(shr-max-columns footer
))))
1145 (if (and (not caption
)
1147 (not (cdr (assq 'tbody cont
)))
1148 (not (cdr (assq 'tr cont
)))
1150 ;; The table is totally invalid and just contains random junk.
1151 ;; Try to output it anyway.
1153 ;; It's a real table, so render it.
1156 (if caption
`((tr (td ,@caption
))))
1159 ;; hader + body + footer
1160 (if (= nheader nbody
)
1161 (if (= nbody nfooter
)
1162 `((tr (td (table (tbody ,@header
,@body
,@footer
)))))
1163 (nconc `((tr (td (table (tbody ,@header
,@body
)))))
1166 `((tr (td (table (tbody ,@footer
))))))))
1167 (nconc `((tr (td (table (tbody ,@header
)))))
1168 (if (= nbody nfooter
)
1169 `((tr (td (table (tbody ,@body
,@footer
)))))
1170 (nconc `((tr (td (table (tbody ,@body
)))))
1173 `((tr (td (table (tbody ,@footer
))))))))))
1175 (if (= nheader nbody
)
1176 `((tr (td (table (tbody ,@header
,@body
)))))
1178 `(,@header
(tr (td (table (tbody ,@body
)))))
1179 `((tr (td (table (tbody ,@header
))))
1180 (tr (td (table (tbody ,@body
))))))))
1183 (if (= nbody nfooter
)
1184 `((tr (td (table (tbody ,@body
,@footer
)))))
1185 (nconc `((tr (td (table (tbody ,@body
)))))
1188 `((tr (td (table (tbody ,@footer
))))))))
1190 `((tr (td (table (tbody ,@body
)))))
1193 (shr-colorize-region start
(point) (cdr (assq 'color shr-stylesheet
))
1196 (defun shr-find-elements (cont type
)
1199 (cond ((eq (car elem
) type
)
1202 (setq result
(nconc (shr-find-elements (cdr elem
) type
) result
)))))
1205 (defun shr-insert-table (table widths
)
1206 (shr-insert-table-ruler widths
)
1208 (let ((start (point))
1209 (height (let ((max 0))
1210 (dolist (column row
)
1211 (setq max
(max max
(cadr column
))))
1215 (insert shr-table-vertical-line
"\n"))
1216 (dolist (column row
)
1218 (let ((lines (nth 2 column
))
1219 (overlay-lines (nth 3 column
))
1220 overlay overlay-line
)
1221 (dolist (line lines
)
1222 (setq overlay-line
(pop overlay-lines
))
1224 (insert line shr-table-vertical-line
)
1225 (dolist (overlay overlay-line
)
1226 (let ((o (make-overlay (- (point) (nth 0 overlay
) 1)
1227 (- (point) (nth 1 overlay
) 1)))
1228 (properties (nth 2 overlay
)))
1230 (overlay-put o
(pop properties
) (pop properties
)))))
1232 ;; Add blank lines at padding at the bottom of the TD,
1234 (dotimes (i (- height
(length lines
)))
1236 (let ((start (point)))
1237 (insert (make-string (string-width (car lines
)) ?
)
1238 shr-table-vertical-line
)
1239 (when (nth 4 column
)
1240 (shr-put-color start
(1- (point)) :background
(nth 4 column
))))
1241 (forward-line 1)))))
1242 (shr-insert-table-ruler widths
)))
1244 (defun shr-insert-table-ruler (widths)
1246 (> shr-indentation
0))
1248 (insert shr-table-corner
)
1249 (dotimes (i (length widths
))
1250 (insert (make-string (aref widths i
) shr-table-horizontal-line
)
1254 (defun shr-table-widths (table natural-table suggested-widths
)
1255 (let* ((length (length suggested-widths
))
1256 (widths (make-vector length
0))
1257 (natural-widths (make-vector length
0)))
1260 (dolist (column row
)
1261 (aset widths i
(max (aref widths i
) column
))
1263 (dolist (row natural-table
)
1265 (dolist (column row
)
1266 (aset natural-widths i
(max (aref natural-widths i
) column
))
1268 (let ((extra (- (apply '+ (append suggested-widths nil
))
1269 (apply '+ (append widths nil
))))
1270 (expanded-columns 0))
1271 ;; We have extra, unused space, so divide this space amongst the
1274 ;; If the natural width is wider than the rendered width, we
1275 ;; want to allow the column to expand.
1277 (when (> (aref natural-widths i
) (aref widths i
))
1278 (setq expanded-columns
(1+ expanded-columns
))))
1280 (when (> (aref natural-widths i
) (aref widths i
))
1282 (aref natural-widths i
)
1283 (+ (/ extra expanded-columns
)
1284 (aref widths i
))))))))
1287 (defun shr-make-table (cont widths
&optional fill
)
1290 (when (eq (car row
) 'tr
)
1295 (while (< i
(length widths
))
1296 (setq column
(pop columns
))
1297 (when (or (memq (car column
) '(td th
))
1299 (push (shr-render-td (cdr column
) (aref widths i
) fill
)
1302 (push (nreverse tds
) trs
))))
1305 (defun shr-render-td (cont width fill
)
1307 (let ((bgcolor (cdr (assq :bgcolor cont
)))
1308 (fgcolor (cdr (assq :fgcolor cont
)))
1309 (style (cdr (assq :style cont
)))
1310 (shr-stylesheet shr-stylesheet
)
1311 overlays actual-colors
)
1313 (setq style
(and (string-match "color" style
)
1314 (shr-parse-style style
))))
1316 (setq style
(nconc (list (cons 'background-color bgcolor
)) style
)))
1318 (setq style
(nconc (list (cons 'color fgcolor
)) style
)))
1320 (setq shr-stylesheet
(append style shr-stylesheet
)))
1321 (let ((cache (cdr (assoc (cons width cont
) shr-content-cache
))))
1324 (insert (car cache
))
1325 (let ((end (length (car cache
))))
1326 (dolist (overlay (cadr cache
))
1328 (make-overlay (1+ (- end
(nth 0 overlay
)))
1329 (1+ (- end
(nth 1 overlay
)))))
1330 (properties (nth 2 overlay
)))
1332 (overlay-put new-overlay
1333 (pop properties
) (pop properties
)))))))
1334 (let ((shr-width width
)
1335 (shr-indentation 0))
1336 (shr-descend (cons 'td cont
)))
1337 ;; Delete padding at the bottom of the TDs.
1341 (skip-chars-backward " \t\n")
1344 (push (list (cons width cont
) (buffer-string)
1345 (shr-overlays-in-region (point-min) (point-max)))
1346 shr-content-cache
)))
1347 (goto-char (point-min))
1351 (setq max
(max max
(current-column)))
1354 (goto-char (point-min))
1355 ;; If the buffer is totally empty, then put a single blank
1357 (if (zerop (buffer-size))
1358 (insert (make-string width ?
))
1359 ;; Otherwise, fill the buffer.
1362 (when (> (- width
(current-column)) 0)
1363 (insert (make-string (- width
(current-column)) ?
)))
1367 (shr-colorize-region
1368 (point-min) (point-max)
1369 (cdr (assq 'color shr-stylesheet
))
1370 (cdr (assq 'background-color shr-stylesheet
))))))
1373 (count-lines (point-min) (point-max))
1374 (split-string (buffer-string) "\n")
1375 (shr-collect-overlays)
1376 (car actual-colors
))
1379 (defun shr-buffer-width ()
1380 (goto-char (point-min))
1384 (setq max
(max max
(current-column)))
1388 (defun shr-collect-overlays ()
1390 (goto-char (point-min))
1391 (let ((overlays nil
))
1393 (push (shr-overlays-in-region (point) (line-end-position))
1396 (nreverse overlays
))))
1398 (defun shr-overlays-in-region (start end
)
1400 (dolist (overlay (overlays-in start end
))
1401 (push (list (if (> start
(overlay-start overlay
))
1403 (- end
(overlay-start overlay
)))
1404 (if (< end
(overlay-end overlay
))
1406 (- end
(overlay-end overlay
)))
1407 (overlay-properties overlay
))
1411 (defun shr-pro-rate-columns (columns)
1412 (let ((total-percentage 0)
1413 (widths (make-vector (length columns
) 0)))
1414 (dotimes (i (length columns
))
1415 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1416 (setq total-percentage
(/ 1.0 total-percentage
))
1417 (dotimes (i (length columns
))
1418 (aset widths i
(max (truncate (* (aref columns i
)
1420 (- shr-width
(1+ (length columns
)))))
1424 ;; Return a summary of the number and shape of the TDs in the table.
1425 (defun shr-column-specs (cont)
1426 (let ((columns (make-vector (shr-max-columns cont
) 1)))
1428 (when (eq (car row
) 'tr
)
1430 (dolist (column (cdr row
))
1431 (when (memq (car column
) '(td th
))
1432 (let ((width (cdr (assq :width
(cdr column
)))))
1434 (string-match "\\([0-9]+\\)%" width
)
1435 (not (zerop (setq width
(string-to-number
1436 (match-string 1 width
))))))
1437 (aset columns i
(/ width
100.0))))
1438 (setq i
(1+ i
)))))))
1441 (defun shr-count (cont elem
)
1444 (when (eq (car sub
) elem
)
1448 (defun shr-max-columns (cont)
1451 (when (eq (car row
) 'tr
)
1452 (setq max
(max max
(+ (shr-count (cdr row
) 'td
)
1453 (shr-count (cdr row
) 'th
))))))
1458 ;;; shr.el ends here