1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package takes a HTML parse tree (as provided by
26 ;; libxml-parse-html-region) and renders it in the current buffer. It
27 ;; does not do CSS, JavaScript or anything advanced: It's geared
28 ;; towards rendering typical short snippets of HTML, like what you'd
29 ;; find in HTML email and the like.
33 (eval-when-compile (require 'cl
))
37 "Simple HTML Renderer"
40 (defcustom shr-max-image-proportion
0.9
41 "How big pictures displayed are in relation to the window they're in.
42 A value of 0.7 means that they are allowed to take up 70% of the
43 width and height of the window. If they are larger than this,
44 and Emacs supports it, then the images will be rescaled down to
50 (defcustom shr-blocked-images nil
51 "Images that have URLs matching this regexp will be blocked."
56 (defcustom shr-table-horizontal-line ?-
57 "Character used to draw horizontal table lines."
61 (defcustom shr-table-vertical-line ?|
62 "Character used to draw vertical table lines."
66 (defcustom shr-table-corner ?
+
67 "Character used to draw table corners."
71 (defcustom shr-hr-line ?-
72 "Character used to draw hr lines."
76 (defcustom shr-width fill-column
77 "Frame width to use for rendering."
81 (defvar shr-content-function nil
82 "If bound, this should be a function that will return the content.
83 This is used for cid: URLs, and the function is called with the
84 cid: URL as the argument.")
86 ;;; Internal variables.
88 (defvar shr-folding-mode nil
)
89 (defvar shr-state nil
)
90 (defvar shr-start nil
)
91 (defvar shr-indentation
0)
92 (defvar shr-inhibit-images nil
)
93 (defvar shr-list-mode nil
)
94 (defvar shr-content-cache nil
)
95 (defvar shr-kinsoku-shorten nil
)
96 (defvar shr-table-depth
0)
97 (defvar shr-stylesheet nil
)
100 (let ((map (make-sparse-keymap)))
101 (define-key map
"a" 'shr-show-alt-text
)
102 (define-key map
"i" 'shr-browse-image
)
103 (define-key map
"I" 'shr-insert-image
)
104 (define-key map
"u" 'shr-copy-url
)
105 (define-key map
"v" 'shr-browse-url
)
106 (define-key map
"o" 'shr-save-contents
)
107 (define-key map
"\r" 'shr-browse-url
)
110 ;; Public functions and commands.
113 (defun shr-insert-document (dom)
114 (setq shr-content-cache nil
)
115 (let ((shr-state nil
)
117 (shr-descend (shr-transform-dom dom
))))
119 (defun shr-copy-url ()
120 "Copy the URL under point to the kill ring.
121 If called twice, then try to fetch the URL and see whether it
122 redirects somewhere else."
124 (let ((url (get-text-property (point) 'shr-url
)))
127 (message "No URL under point"))
128 ;; Resolve redirected URLs.
129 ((equal url
(car kill-ring
))
134 (eq (car a
) :redirect
))
137 (goto-char (point-min))
138 ;; Remove common tracking junk from the URL.
139 (when (re-search-forward ".utm_.*" nil t
)
140 (replace-match "" t t
))
141 (message "Copied %s" (buffer-string))
142 (copy-region-as-kill (point-min) (point-max)))))))
143 ;; Copy the URL to the kill ring.
147 (copy-region-as-kill (point-min) (point-max))
148 (message "Copied %s" url
))))))
150 (defun shr-show-alt-text ()
151 "Show the ALT text of the image under point."
153 (let ((text (get-text-property (point) 'shr-alt
)))
155 (message "No image under point")
156 (message "%s" text
))))
158 (defun shr-browse-image ()
159 "Browse the image under point."
161 (let ((url (get-text-property (point) 'image-url
)))
163 (message "No image under point")
164 (message "Browsing %s..." url
)
167 (defun shr-insert-image ()
168 "Insert the image under point into the buffer."
170 (let ((url (get-text-property (point) 'image-url
)))
172 (message "No image under point")
173 (message "Inserting %s..." url
)
174 (url-retrieve url
'shr-image-fetched
175 (list (current-buffer) (1- (point)) (point-marker))
178 ;;; Utility functions.
180 (defun shr-transform-dom (dom)
181 (let ((result (list (pop dom
))))
182 (dolist (arg (pop dom
))
183 (push (cons (intern (concat ":" (symbol-name (car arg
))) obarray
)
188 (push (cons 'text sub
) result
)
189 (push (shr-transform-dom sub
) result
)))
192 (defun shr-descend (dom)
193 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom
))) obarray
))
194 (style (cdr (assq :style
(cdr dom
))))
195 (shr-stylesheet shr-stylesheet
)
198 (string-match "color" style
))
199 (setq shr-stylesheet
(nconc (shr-parse-style style
)
201 (if (fboundp function
)
202 (funcall function
(cdr dom
))
203 (shr-generic (cdr dom
)))
204 (let ((color (cdr (assq 'color shr-stylesheet
)))
205 (background (cdr (assq 'background-color
207 (when (and shr-stylesheet
208 (or color background
))
209 (shr-colorize-region start
(point) color background
)))))
211 (defun shr-generic (cont)
214 ((eq (car sub
) 'text
)
215 (shr-insert (cdr sub
)))
217 (shr-descend sub
)))))
219 (defmacro shr-char-breakable-p
(char)
220 "Return non-nil if a line can be broken before and after CHAR."
221 `(aref fill-find-break-point-function-table
,char
))
222 (defmacro shr-char-nospace-p
(char)
223 "Return non-nil if no space is required before and after CHAR."
224 `(aref fill-nospace-between-words-table
,char
))
226 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
227 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
228 ;; parentheses, and so on, that should not be placed in the beginning
229 ;; of a line or the end of a line.
230 (defmacro shr-char-kinsoku-bol-p
(char)
231 "Return non-nil if a line ought not to begin with CHAR."
232 `(aref (char-category-set ,char
) ?
>))
233 (defmacro shr-char-kinsoku-eol-p
(char)
234 "Return non-nil if a line ought not to end with CHAR."
235 `(aref (char-category-set ,char
) ?
<))
236 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208
33 35))
237 (load "kinsoku" nil t
))
239 (defun shr-insert (text)
240 (when (and (eq shr-state
'image
)
241 (not (string-match "\\`[ \t\n]+\\'" text
)))
243 (setq shr-state nil
))
245 ((eq shr-folding-mode
'none
)
248 (when (and (string-match "\\`[ \t\n]" text
)
250 (not (eq (char-after (1- (point))) ?
)))
252 (dolist (elem (split-string text
))
254 (> shr-indentation
0))
256 ;; The shr-start is a special variable that is used to pass
257 ;; upwards the first point in the buffer where the text really
260 (setq shr-start
(point)))
261 ;; No space is needed behind a wide character categorized as
262 ;; kinsoku-bol, between characters both categorized as nospace,
263 ;; or at the beginning of a line.
265 (when (and (eq (preceding-char) ?
)
266 (or (= (line-beginning-position) (1- (point)))
267 (and (shr-char-breakable-p
268 (setq prev
(char-after (- (point) 2))))
269 (shr-char-kinsoku-bol-p prev
))
270 (and (shr-char-nospace-p prev
)
271 (shr-char-nospace-p (aref elem
0)))))
275 (while (and (> (current-column) shr-width
)
277 (setq found
(shr-find-fill-point))
279 (when (eq (preceding-char) ?
)
283 (put-text-property (1- (point)) (point) 'shr-break t
)
284 ;; No space is needed at the beginning of a line.
285 (when (eq (following-char) ?
)
287 (when (> shr-indentation
0)
291 (unless (string-match "[ \t\n]\\'" text
)
294 (defun shr-find-fill-point ()
295 (when (> (move-to-column shr-width
) shr-width
)
299 (while (not (or (setq failed
(= (current-column) shr-indentation
))
300 (eq (preceding-char) ?
)
301 (eq (following-char) ?
)
302 (shr-char-breakable-p (preceding-char))
303 (shr-char-breakable-p (following-char))
304 (and (eq (preceding-char) ?
')
305 (not (memq (char-after (- (point) 2))
307 ;; There're some kinsoku CJK chars that aren't breakable.
308 (and (shr-char-kinsoku-bol-p (preceding-char))
309 (not (shr-char-kinsoku-bol-p (following-char))))
310 (shr-char-kinsoku-eol-p (following-char))))
312 (if (and (not (or failed
(eolp)))
313 (eq (preceding-char) ?
'))
314 (while (not (or (setq failed
(eolp))
315 (eq (following-char) ?
)
316 (shr-char-breakable-p (following-char))
317 (shr-char-kinsoku-eol-p (following-char))))
320 ;; There's no breakable point, so we give it up.
323 (unless shr-kinsoku-shorten
324 (while (and (setq found
(re-search-forward
325 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
326 (line-end-position) 'move
))
327 (eq (preceding-char) ?
')))
328 (if (and found
(not (match-beginning 1)))
329 (goto-char (match-beginning 0)))))
332 ;; Don't put kinsoku-bol characters at the beginning of a line,
333 ;; or kinsoku-eol characters at the end of a line.
336 (while (and (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
337 (shr-char-kinsoku-eol-p (preceding-char)))
339 (when (setq failed
(= (current-column) shr-indentation
))
340 ;; There's no breakable point that doesn't violate kinsoku,
341 ;; so we look for the second best position.
344 (<= (current-column) shr-width
))
347 (shr-char-kinsoku-eol-p (following-char)))))
349 ((shr-char-kinsoku-eol-p (preceding-char))
350 (if (shr-char-kinsoku-eol-p (following-char))
351 ;; There are consecutive kinsoku-eol characters.
357 (and (> (setq count
(1- count
)) 0)
358 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
359 (or (shr-char-kinsoku-eol-p (preceding-char))
360 (shr-char-kinsoku-bol-p (following-char)))))))
361 (if (setq failed
(= (current-column) shr-indentation
))
362 ;; There's no breakable point that doesn't violate kinsoku,
363 ;; so we go to the second best position.
364 (if (looking-at "\\(\\c<+\\)\\c<")
365 (goto-char (match-end 1))
368 (if (shr-char-kinsoku-bol-p (preceding-char))
369 ;; There are consecutive kinsoku-bol characters.
372 (while (and (>= (setq count
(1- count
)) 0)
373 (shr-char-kinsoku-bol-p (following-char))
374 (shr-char-breakable-p (following-char)))
375 (forward-char 1))))))
376 (when (eq (following-char) ?
)
380 (defun shr-ensure-newline ()
381 (unless (zerop (current-column))
384 (defun shr-ensure-paragraph ()
386 (if (<= (current-column) shr-indentation
)
387 (unless (save-excursion
398 (when (> shr-indentation
0)
399 (insert (make-string shr-indentation ?
))))
401 (defun shr-fontize-cont (cont &rest types
)
405 (shr-add-font (or shr-start
(point)) (point) type
))))
407 ;; Add an overlay in the region, but avoid putting the font properties
408 ;; on blank text at the start of the line, and the newline at the end,
409 ;; to avoid ugliness.
410 (defun shr-add-font (start end type
)
413 (while (< (point) end
)
415 (skip-chars-forward " "))
416 (let ((overlay (make-overlay (point) (min (line-end-position) end
))))
417 (overlay-put overlay
'face type
))
418 (if (< (line-end-position) end
)
422 (defun shr-browse-url ()
423 "Browse the URL under point."
425 (let ((url (get-text-property (point) 'shr-url
)))
428 (message "No link under point"))
429 ((string-match "^mailto:" url
)
430 (browse-url-mailto url
))
434 (defun shr-save-contents (directory)
435 "Save the contents from URL in a file."
436 (interactive "DSave contents of URL to directory: ")
437 (let ((url (get-text-property (point) 'shr-url
)))
439 (message "No link under point")
440 (url-retrieve (shr-encode-url url
)
441 'shr-store-contents
(list url directory
)))))
443 (defun shr-store-contents (status url directory
)
444 (unless (plist-get status
:error
)
445 (when (or (search-forward "\n\n" nil t
)
446 (search-forward "\r\n\r\n" nil t
))
447 (write-region (point) (point-max)
448 (expand-file-name (file-name-nondirectory url
)
451 (defun shr-image-fetched (status buffer start end
)
452 (when (and (buffer-name buffer
)
453 (not (plist-get status
:error
)))
454 (url-store-in-cache (current-buffer))
455 (when (or (search-forward "\n\n" nil t
)
456 (search-forward "\r\n\r\n" nil t
))
457 (let ((data (buffer-substring (point) (point-max))))
458 (with-current-buffer buffer
459 (let ((alt (buffer-substring start end
))
460 (inhibit-read-only t
))
461 (delete-region start end
)
463 (shr-put-image data alt
))))))
464 (kill-buffer (current-buffer)))
466 (defun shr-put-image (data alt
)
467 (if (display-graphic-p)
468 (let ((image (ignore-errors
469 (shr-rescale-image data
))))
471 ;; When inserting big-ish pictures, put them at the
472 ;; beginning of the line.
473 (when (and (> (current-column) 0)
474 (> (car (image-size image t
)) 400))
476 (insert-image image
(or alt
"*"))))
479 (defun shr-rescale-image (data)
480 (if (or (not (fboundp 'imagemagick-types
))
481 (not (get-buffer-window (current-buffer))))
482 (create-image data nil t
)
483 (let* ((image (create-image data nil t
))
484 (size (image-size image t
))
487 (edges (window-inside-pixel-edges
488 (get-buffer-window (current-buffer))))
489 (window-width (truncate (* shr-max-image-proportion
490 (- (nth 2 edges
) (nth 0 edges
)))))
491 (window-height (truncate (* shr-max-image-proportion
492 (- (nth 3 edges
) (nth 1 edges
)))))
494 (when (> height window-height
)
495 (setq image
(or (create-image data
'imagemagick t
496 :height window-height
)
498 (setq size
(image-size image t
)))
499 (when (> (car size
) window-width
)
501 (create-image data
'imagemagick t
506 ;; url-cache-extract autoloads url-cache.
507 (declare-function url-cache-create-filename
"url-cache" (url))
508 (autoload 'mm-disable-multibyte
"mm-util")
509 (autoload 'browse-url-mailto
"browse-url")
511 (defun shr-get-image-data (url)
512 "Get image data for URL.
513 Return a string with image data."
515 (mm-disable-multibyte)
517 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
519 (when (or (search-forward "\n\n" nil t
)
520 (search-forward "\r\n\r\n" nil t
))
521 (buffer-substring (point) (point-max))))))
523 (defun shr-image-displayer (content-function)
524 "Return a function to display an image.
525 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
526 is an argument. The function to be returned takes three arguments URL,
528 `(lambda (url start end
)
530 (if (string-match "\\`cid:" url
)
531 ,(when content-function
532 `(let ((image (funcall ,content-function
533 (substring url
(match-end 0)))))
538 (buffer-substring-no-properties start end
)
539 (delete-region start end
))))))
540 (url-retrieve url
'shr-image-fetched
541 (list (current-buffer) start end
)
544 (defun shr-heading (cont &rest types
)
545 (shr-ensure-paragraph)
546 (apply #'shr-fontize-cont cont types
)
547 (shr-ensure-paragraph))
549 (autoload 'widget-convert-button
"wid-edit")
551 (defun shr-urlify (start url
&optional title
)
552 (widget-convert-button
553 'url-link start
(point)
554 :help-echo
(if title
(format "%s (%s)" url title
) url
)
557 (put-text-property start
(point) 'shr-url url
))
559 (defun shr-encode-url (url)
561 (browse-url-url-encode-chars url
"[)$ ]"))
563 (autoload 'shr-color-visible
"shr-color")
564 (autoload 'shr-color-
>hexadecimal
"shr-color")
566 (defun shr-color-check (fg bg
)
567 "Check that FG is visible on BG.
568 Returns (fg bg) with corrected values.
569 Returns nil if the colors that would be used are the default
570 ones, in case fg and bg are nil."
572 (let ((fixed (cond ((null fg
) 'fg
)
574 ;; Convert colors to hexadecimal, or set them to default.
575 (let ((fg (or (shr-color->hexadecimal fg
)
576 (frame-parameter nil
'foreground-color
)))
577 (bg (or (shr-color->hexadecimal bg
)
578 (frame-parameter nil
'background-color
))))
579 (cond ((eq fixed
'bg
)
580 ;; Only return the new fg
581 (list nil
(cadr (shr-color-visible bg fg t
))))
583 ;; Invert args and results and return only the new bg
584 (list (cadr (shr-color-visible fg bg t
)) nil
))
586 (shr-color-visible bg fg
)))))))
588 (defun shr-colorize-region (start end fg
&optional bg
)
590 (let ((new-colors (shr-color-check fg bg
)))
592 (shr-put-color start end
:foreground
(cadr new-colors
))
594 (shr-put-color start end
:background
(car new-colors
)))))))
596 ;; Put a color in the region, but avoid putting colors on on blank
597 ;; text at the start of the line, and the newline at the end, to avoid
598 ;; ugliness. Also, don't overwrite any existing color information,
599 ;; since this can be called recursively, and we want the "inner" color
601 (defun shr-put-color (start end type color
)
604 (while (< (point) end
)
606 (skip-chars-forward " "))
607 (when (> (line-end-position) (point))
608 (shr-put-color-1 (point) (min (line-end-position) end
) type color
))
609 (if (< (line-end-position) end
)
613 (defun shr-put-color-1 (start end type color
)
614 (let* ((old-props (get-text-property start
'face
))
615 (do-put (not (memq type old-props
)))
618 (setq change
(next-single-property-change start
'face nil end
))
620 (put-text-property start change
'face
621 (nconc (list type color
) old-props
)))
622 (setq old-props
(get-text-property change
'face
))
623 (setq do-put
(not (memq type old-props
)))
627 (put-text-property start end
'face
628 (nconc (list type color old-props
))))))
630 ;;; Tag-specific rendering rules.
632 (defun shr-tag-body (cont)
633 (let* ((start (point))
634 (fgcolor (cdr (assq :fgcolor cont
)))
635 (bgcolor (cdr (assq :bgcolor cont
)))
636 (shr-stylesheet (list (cons :color fgcolor
)
637 (cons :background-color bgcolor
))))
639 (shr-colorize-region start
(point) fgcolor bgcolor
)))
641 (defun shr-tag-p (cont)
642 (shr-ensure-paragraph)
645 (shr-ensure-paragraph))
647 (defun shr-tag-div (cont)
651 (shr-ensure-newline))
653 (defun shr-tag-b (cont)
654 (shr-fontize-cont cont
'bold
))
656 (defun shr-tag-i (cont)
657 (shr-fontize-cont cont
'italic
))
659 (defun shr-tag-em (cont)
660 (shr-fontize-cont cont
'bold
))
662 (defun shr-tag-strong (cont)
663 (shr-fontize-cont cont
'bold
))
665 (defun shr-tag-u (cont)
666 (shr-fontize-cont cont
'underline
))
668 (defun shr-tag-s (cont)
669 (shr-fontize-cont cont
'strike-through
))
671 (defun shr-parse-style (style)
674 (when (string-match "\n" style
)
675 (setq style
(replace-match " " t t style
))))
677 (dolist (elem (split-string style
";"))
679 (setq elem
(split-string elem
":"))
680 (when (and (car elem
)
682 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
683 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
684 (when (string-match " *!important\\'" value
)
685 (setq value
(substring value
0 (match-beginning 0))))
686 (push (cons (intern name obarray
)
691 (defun shr-tag-a (cont)
692 (let ((url (cdr (assq :href cont
)))
693 (title (cdr (assq :title cont
)))
697 (shr-urlify (or shr-start start
) url title
)))
699 (defun shr-tag-object (cont)
700 (let ((start (point))
703 (when (eq (car elem
) 'embed
)
704 (setq url
(or url
(cdr (assq :src
(cdr elem
))))))
705 (when (and (eq (car elem
) 'param
)
706 (equal (cdr (assq :name
(cdr elem
))) "movie"))
707 (setq url
(or url
(cdr (assq :value
(cdr elem
)))))))
709 (shr-insert " [multimedia] ")
710 (shr-urlify start url
))
713 (defun shr-tag-video (cont)
714 (let ((image (cdr (assq :poster cont
)))
715 (url (cdr (assq :src cont
)))
717 (shr-tag-img nil image
)
718 (shr-urlify start url
)))
720 (defun shr-tag-img (cont &optional url
)
723 (cdr (assq :src cont
))))
724 (when (and (> (current-column) 0)
725 (not (eq shr-state
'image
)))
727 (let ((alt (cdr (assq :alt cont
)))
728 (url (or url
(cdr (assq :src cont
)))))
729 (let ((start (point-marker)))
730 (when (zerop (length alt
))
733 ((or (member (cdr (assq :height cont
)) '("0" "1"))
734 (member (cdr (assq :width cont
)) '("0" "1")))
735 ;; Ignore zero-sized or single-pixel images.
737 ((and (not shr-inhibit-images
)
738 (string-match "\\`cid:" url
))
739 (let ((url (substring url
(match-end 0)))
741 (if (or (not shr-content-function
)
742 (not (setq image
(funcall shr-content-function url
))))
744 (shr-put-image image alt
))))
745 ((or shr-inhibit-images
746 (and shr-blocked-images
747 (string-match shr-blocked-images url
)))
748 (setq shr-start
(point))
749 (let ((shr-state 'space
))
750 (if (> (string-width alt
) 8)
751 (shr-insert (truncate-string-to-width alt
8))
753 ((url-is-cached (shr-encode-url url
))
754 (shr-put-image (shr-get-image-data url
) alt
))
758 (url-retrieve (shr-encode-url url
) 'shr-image-fetched
759 (list (current-buffer) start
(point-marker))
761 (put-text-property start
(point) 'keymap shr-map
)
762 (put-text-property start
(point) 'shr-alt alt
)
763 (put-text-property start
(point) 'image-url url
)
764 (put-text-property start
(point) 'image-displayer
765 (shr-image-displayer shr-content-function
))
766 (put-text-property start
(point) 'help-echo alt
)
767 (setq shr-state
'image
)))))
769 (defun shr-tag-pre (cont)
770 (let ((shr-folding-mode 'none
))
774 (shr-ensure-newline)))
776 (defun shr-tag-blockquote (cont)
777 (shr-ensure-paragraph)
779 (let ((shr-indentation (+ shr-indentation
4)))
781 (shr-ensure-paragraph))
783 (defun shr-tag-ul (cont)
784 (shr-ensure-paragraph)
785 (let ((shr-list-mode 'ul
))
787 (shr-ensure-paragraph))
789 (defun shr-tag-ol (cont)
790 (shr-ensure-paragraph)
791 (let ((shr-list-mode 1))
793 (shr-ensure-paragraph))
795 (defun shr-tag-li (cont)
796 (shr-ensure-paragraph)
799 (if (numberp shr-list-mode
)
801 (format "%d " shr-list-mode
)
802 (setq shr-list-mode
(1+ shr-list-mode
)))
804 (shr-indentation (+ shr-indentation
(length bullet
))))
808 (defun shr-tag-br (cont)
814 (defun shr-tag-h1 (cont)
815 (shr-heading cont
'bold
'underline
))
817 (defun shr-tag-h2 (cont)
818 (shr-heading cont
'bold
))
820 (defun shr-tag-h3 (cont)
821 (shr-heading cont
'italic
))
823 (defun shr-tag-h4 (cont)
826 (defun shr-tag-h5 (cont)
829 (defun shr-tag-h6 (cont)
832 (defun shr-tag-hr (cont)
834 (insert (make-string shr-width shr-hr-line
) "\n"))
836 (defun shr-tag-title (cont)
837 (shr-heading cont
'bold
'underline
))
839 (defun shr-tag-font (cont)
840 (let ((start (point))
841 (color (cdr (assq :color cont
))))
843 (shr-colorize-region start
(point) color
)))
845 ;;; Table rendering algorithm.
847 ;; Table rendering is the only complicated thing here. We do this by
848 ;; first counting how many TDs there are in each TR, and registering
849 ;; how wide they think they should be ("width=45%", etc). Then we
850 ;; render each TD separately (this is done in temporary buffers, so
851 ;; that we can use all the rendering machinery as if we were in the
852 ;; main buffer). Now we know how much space each TD really takes, so
853 ;; we then render everything again with the new widths, and finally
854 ;; insert all these boxes into the main buffer.
855 (defun shr-tag-table-1 (cont)
856 (setq cont
(or (cdr (assq 'tbody cont
))
858 (let* ((shr-inhibit-images t
)
859 (shr-table-depth (1+ shr-table-depth
))
860 (shr-kinsoku-shorten t
)
861 ;; Find all suggested widths.
862 (columns (shr-column-specs cont
))
863 ;; Compute how many characters wide each TD should be.
864 (suggested-widths (shr-pro-rate-columns columns
))
865 ;; Do a "test rendering" to see how big each TD is (this can
866 ;; be smaller (if there's little text) or bigger (if there's
867 ;; unbreakable text).
868 (sketch (shr-make-table cont suggested-widths
))
869 (sketch-widths (shr-table-widths sketch suggested-widths
)))
870 ;; This probably won't work very well.
871 (when (> (+ (loop for width across sketch-widths
875 (setq truncate-lines t
))
876 ;; Then render the table again with these new "hard" widths.
877 (shr-insert-table (shr-make-table cont sketch-widths t
) sketch-widths
))
878 ;; Finally, insert all the images after the table. The Emacs buffer
879 ;; model isn't strong enough to allow us to put the images actually
881 (when (zerop shr-table-depth
)
882 (dolist (elem (shr-find-elements cont
'img
))
883 (shr-tag-img (cdr elem
)))))
885 (defun shr-tag-table (cont)
886 (shr-ensure-paragraph)
887 (let* ((caption (cdr (assq 'caption cont
)))
888 (header (cdr (assq 'thead cont
)))
889 (body (or (cdr (assq 'tbody cont
)) cont
))
890 (footer (cdr (assq 'tfoot cont
)))
891 (bgcolor (cdr (assq :bgcolor cont
)))
892 (nheader (if header
(shr-max-columns header
)))
893 (nbody (if body
(shr-max-columns body
)))
894 (nfooter (if footer
(shr-max-columns footer
))))
897 (if caption
`((tr (td ,@caption
))))
900 ;; hader + body + footer
901 (if (= nheader nbody
)
902 (if (= nbody nfooter
)
903 `((tr (td (table (tbody ,@header
,@body
,@footer
)))))
904 (nconc `((tr (td (table (tbody ,@header
,@body
)))))
907 `((tr (td (table (tbody ,@footer
))))))))
908 (nconc `((tr (td (table (tbody ,@header
)))))
909 (if (= nbody nfooter
)
910 `((tr (td (table (tbody ,@body
,@footer
)))))
911 (nconc `((tr (td (table (tbody ,@body
)))))
914 `((tr (td (table (tbody ,@footer
))))))))))
916 (if (= nheader nbody
)
917 `((tr (td (table (tbody ,@header
,@body
)))))
919 `(,@header
(tr (td (table (tbody ,@body
)))))
920 `((tr (td (table (tbody ,@header
))))
921 (tr (td (table (tbody ,@body
))))))))
924 (if (= nbody nfooter
)
925 `((tr (td (table (tbody ,@body
,@footer
)))))
926 (nconc `((tr (td (table (tbody ,@body
)))))
929 `((tr (td (table (tbody ,@footer
))))))))
931 `((tr (td (table (tbody ,@body
)))))
934 (defun shr-find-elements (cont type
)
937 (cond ((eq (car elem
) type
)
940 (setq result
(nconc (shr-find-elements (cdr elem
) type
) result
)))))
943 (defun shr-insert-table (table widths
)
944 (shr-insert-table-ruler widths
)
946 (let ((start (point))
947 (height (let ((max 0))
949 (setq max
(max max
(cadr column
))))
953 (insert shr-table-vertical-line
"\n"))
956 (let ((lines (nth 2 column
))
957 (overlay-lines (nth 3 column
))
958 overlay overlay-line
)
960 (setq overlay-line
(pop overlay-lines
))
962 (insert line shr-table-vertical-line
)
963 (dolist (overlay overlay-line
)
964 (let ((o (make-overlay (- (point) (nth 0 overlay
) 1)
965 (- (point) (nth 1 overlay
) 1)))
966 (properties (nth 2 overlay
)))
968 (overlay-put o
(pop properties
) (pop properties
)))))
970 ;; Add blank lines at padding at the bottom of the TD,
972 (dotimes (i (- height
(length lines
)))
974 (insert (make-string (string-width (car lines
)) ?
)
975 shr-table-vertical-line
)
977 (shr-insert-table-ruler widths
)))
979 (defun shr-insert-table-ruler (widths)
981 (> shr-indentation
0))
983 (insert shr-table-corner
)
984 (dotimes (i (length widths
))
985 (insert (make-string (aref widths i
) shr-table-horizontal-line
)
989 (defun shr-table-widths (table suggested-widths
)
990 (let* ((length (length suggested-widths
))
991 (widths (make-vector length
0))
992 (natural-widths (make-vector length
0)))
996 (aset widths i
(max (aref widths i
)
998 (aset natural-widths i
(max (aref natural-widths i
)
1001 (let ((extra (- (apply '+ (append suggested-widths nil
))
1002 (apply '+ (append widths nil
))))
1003 (expanded-columns 0))
1006 ;; If the natural width is wider than the rendered width, we
1007 ;; want to allow the column to expand.
1008 (when (> (aref natural-widths i
) (aref widths i
))
1009 (setq expanded-columns
(1+ expanded-columns
))))
1011 (when (> (aref natural-widths i
) (aref widths i
))
1013 (1+ (aref natural-widths i
))
1014 (+ (/ extra expanded-columns
)
1015 (aref widths i
))))))))
1018 (defun shr-make-table (cont widths
&optional fill
)
1021 (when (eq (car row
) 'tr
)
1026 (while (< i
(length widths
))
1027 (setq column
(pop columns
))
1028 (when (or (memq (car column
) '(td th
))
1030 (push (shr-render-td (cdr column
) (aref widths i
) fill
)
1033 (push (nreverse tds
) trs
))))
1036 (defun shr-render-td (cont width fill
)
1038 (let ((cache (cdr (assoc (cons width cont
) shr-content-cache
))))
1041 (let ((shr-width width
)
1042 (shr-indentation 0))
1047 (skip-chars-backward " \t\n")))
1048 (push (cons (cons width cont
) (buffer-string))
1049 shr-content-cache
)))
1050 (goto-char (point-min))
1054 (setq max
(max max
(current-column)))
1057 (goto-char (point-min))
1058 ;; If the buffer is totally empty, then put a single blank
1060 (if (zerop (buffer-size))
1061 (insert (make-string width ?
))
1062 ;; Otherwise, fill the buffer.
1065 (when (> (- width
(current-column)) 0)
1066 (insert (make-string (- width
(current-column)) ?
)))
1070 (count-lines (point-min) (point-max))
1071 (split-string (buffer-string) "\n")
1072 (shr-collect-overlays))
1074 (shr-natural-width))))))
1076 (defun shr-natural-width ()
1077 (goto-char (point-min))
1082 (setq current
(+ current
(current-column)))
1083 (unless (get-text-property (point) 'shr-break
)
1084 (setq max
(max max current
)
1089 (defun shr-collect-overlays ()
1091 (goto-char (point-min))
1092 (let ((overlays nil
))
1094 (push (shr-overlays-in-region (point) (line-end-position))
1097 (nreverse overlays
))))
1099 (defun shr-overlays-in-region (start end
)
1101 (dolist (overlay (overlays-in start end
))
1102 (push (list (if (> start
(overlay-start overlay
))
1104 (- end
(overlay-start overlay
)))
1105 (if (< end
(overlay-end overlay
))
1107 (- end
(overlay-end overlay
)))
1108 (overlay-properties overlay
))
1112 (defun shr-pro-rate-columns (columns)
1113 (let ((total-percentage 0)
1114 (widths (make-vector (length columns
) 0)))
1115 (dotimes (i (length columns
))
1116 (setq total-percentage
(+ total-percentage
(aref columns i
))))
1117 (setq total-percentage
(/ 1.0 total-percentage
))
1118 (dotimes (i (length columns
))
1119 (aset widths i
(max (truncate (* (aref columns i
)
1121 (- shr-width
(1+ (length columns
)))))
1125 ;; Return a summary of the number and shape of the TDs in the table.
1126 (defun shr-column-specs (cont)
1127 (let ((columns (make-vector (shr-max-columns cont
) 1)))
1129 (when (eq (car row
) 'tr
)
1131 (dolist (column (cdr row
))
1132 (when (memq (car column
) '(td th
))
1133 (let ((width (cdr (assq :width
(cdr column
)))))
1135 (string-match "\\([0-9]+\\)%" width
))
1137 (/ (string-to-number (match-string 1 width
))
1139 (setq i
(1+ i
)))))))
1142 (defun shr-count (cont elem
)
1145 (when (eq (car sub
) elem
)
1149 (defun shr-max-columns (cont)
1152 (when (eq (car row
) 'tr
)
1153 (setq max
(max max
(+ (shr-count (cdr row
) 'td
)
1154 (shr-count (cdr row
) 'th
))))))
1159 ;;; shr.el ends here