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
))
35 (unless (aref (char-category-set (make-char 'japanese-jisx0208
33 35)) ?
>)
36 (load "kinsoku" nil t
))
39 "Simple HTML Renderer"
42 (defcustom shr-max-image-proportion
0.9
43 "How big pictures displayed are in relation to the window they're in.
44 A value of 0.7 means that they are allowed to take up 70% of the
45 width and height of the window. If they are larger than this,
46 and Emacs supports it, then the images will be rescaled down to
52 (defcustom shr-blocked-images nil
53 "Images that have URLs matching this regexp will be blocked."
58 (defcustom shr-table-line ?-
59 "Character used to draw table line."
63 (defcustom shr-table-corner ?
+
64 "Character used to draw table corner."
68 (defcustom shr-hr-line ?-
69 "Character used to draw hr line."
73 (defcustom shr-width fill-column
74 "Frame width to use for rendering."
78 (defvar shr-content-function nil
79 "If bound, this should be a function that will return the content.
80 This is used for cid: URLs, and the function is called with the
81 cid: URL as the argument.")
83 ;;; Internal variables.
85 (defvar shr-folding-mode nil
)
86 (defvar shr-state nil
)
87 (defvar shr-start nil
)
88 (defvar shr-indentation
0)
89 (defvar shr-inhibit-images nil
)
90 (defvar shr-list-mode nil
)
91 (defvar shr-content-cache nil
)
92 (defvar shr-kinsoku-shorten nil
)
93 (defvar shr-table-depth
0)
96 (let ((map (make-sparse-keymap)))
97 (define-key map
"a" 'shr-show-alt-text
)
98 (define-key map
"i" 'shr-browse-image
)
99 (define-key map
"I" 'shr-insert-image
)
100 (define-key map
"u" 'shr-copy-url
)
101 (define-key map
"v" 'shr-browse-url
)
102 (define-key map
"o" 'shr-save-contents
)
103 (define-key map
"\r" 'shr-browse-url
)
106 ;; Public functions and commands.
109 (defun shr-insert-document (dom)
110 (setq shr-content-cache nil
)
111 (let ((shr-state nil
)
113 (shr-descend (shr-transform-dom dom
))))
115 (defun shr-copy-url ()
116 "Copy the URL under point to the kill ring.
117 If called twice, then try to fetch the URL and see whether it
118 redirects somewhere else."
120 (let ((url (get-text-property (point) 'shr-url
)))
123 (message "No URL under point"))
124 ;; Resolve redirected URLs.
125 ((equal url
(car kill-ring
))
130 (eq (car a
) :redirect
))
133 (goto-char (point-min))
134 ;; Remove common tracking junk from the URL.
135 (when (re-search-forward ".utm_.*" nil t
)
136 (replace-match "" t t
))
137 (message "Copied %s" (buffer-string))
138 (copy-region-as-kill (point-min) (point-max)))))))
139 ;; Copy the URL to the kill ring.
143 (copy-region-as-kill (point-min) (point-max))
144 (message "Copied %s" url
))))))
146 (defun shr-show-alt-text ()
147 "Show the ALT text of the image under point."
149 (let ((text (get-text-property (point) 'shr-alt
)))
151 (message "No image under point")
152 (message "%s" text
))))
154 (defun shr-browse-image ()
155 "Browse the image under point."
157 (let ((url (get-text-property (point) 'image-url
)))
159 (message "No image under point")
160 (message "Browsing %s..." url
)
163 (defun shr-insert-image ()
164 "Insert the image under point into the buffer."
166 (let ((url (get-text-property (point) 'image-url
)))
168 (message "No image under point")
169 (message "Inserting %s..." url
)
170 (url-retrieve url
'shr-image-fetched
171 (list (current-buffer) (1- (point)) (point-marker))
174 ;;; Utility functions.
176 (defun shr-transform-dom (dom)
177 (let ((result (list (pop dom
))))
178 (dolist (arg (pop dom
))
179 (push (cons (intern (concat ":" (symbol-name (car arg
))) obarray
)
184 (push (cons 'text sub
) result
)
185 (push (shr-transform-dom sub
) result
)))
188 (defun shr-descend (dom)
189 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom
))) obarray
)))
190 (if (fboundp function
)
191 (funcall function
(cdr dom
))
192 (shr-generic (cdr dom
)))))
194 (defun shr-generic (cont)
197 ((eq (car sub
) 'text
)
198 (shr-insert (cdr sub
)))
200 (shr-descend sub
)))))
202 (defun shr-insert (text)
203 (when (and (eq shr-state
'image
)
204 (not (string-match "\\`[ \t\n]+\\'" text
)))
206 (setq shr-state nil
))
208 ((eq shr-folding-mode
'none
)
211 (when (and (string-match "\\`[ \t\n]" text
)
213 (not (eq (char-after (1- (point))) ?
)))
215 (dolist (elem (split-string text
))
217 (> shr-indentation
0))
219 ;; The shr-start is a special variable that is used to pass
220 ;; upwards the first point in the buffer where the text really
223 (setq shr-start
(point)))
224 ;; No space is needed behind a wide character categorized as
225 ;; kinsoku-bol, between characters both categorized as nospace,
226 ;; or at the beginning of a line.
228 (when (and (eq (preceding-char) ?
)
229 (or (= (line-beginning-position) (1- (point)))
230 (and (aref fill-find-break-point-function-table
231 (setq prev
(char-after (- (point) 2))))
232 (aref (char-category-set prev
) ?
>))
233 (and (aref fill-nospace-between-words-table prev
)
234 (aref fill-nospace-between-words-table
239 (while (and (> (current-column) shr-width
)
241 (setq found
(shr-find-fill-point))
243 (when (eq (preceding-char) ?
)
247 (put-text-property (1- (point)) (point) 'shr-break t
)
248 ;; No space is needed at the beginning of a line.
249 (when (eq (following-char) ?
)
251 (when (> shr-indentation
0)
255 (unless (string-match "[ \t\n]\\'" text
)
258 (defun shr-find-fill-point ()
259 (when (> (move-to-column shr-width
) shr-width
)
263 (or (setq failed
(= (current-column) shr-indentation
))
264 (eq (preceding-char) ?
)
265 (eq (following-char) ?
)
266 (aref fill-find-break-point-function-table
(preceding-char))))
269 ;; There's no breakable point, so we give it up.
272 (while (aref fill-find-break-point-function-table
(preceding-char))
276 ;; Don't put kinsoku-bol characters at the beginning of a line,
277 ;; or kinsoku-eol characters at the end of a line,
279 (if (or shr-kinsoku-shorten
280 (and (aref (char-category-set (preceding-char)) ?
<)
282 (setq count
(1- count
))
286 (>= (setq count
(1- count
)) 0)
287 (not (memq (preceding-char) (list ?\C-
@ ?
\n ?
)))
288 (or (aref (char-category-set (preceding-char)) ?
<)
289 (aref (char-category-set (following-char)) ?
>)))
291 (while (and (>= (setq count
(1- count
)) 0)
292 (aref (char-category-set (following-char)) ?
>)
293 (aref fill-find-break-point-function-table
296 (when (eq (following-char) ?
)
300 (defun shr-ensure-newline ()
301 (unless (zerop (current-column))
304 (defun shr-ensure-paragraph ()
306 (if (<= (current-column) shr-indentation
)
307 (unless (save-excursion
318 (when (> shr-indentation
0)
319 (insert (make-string shr-indentation ?
))))
321 (defun shr-fontize-cont (cont &rest types
)
325 (shr-add-font (or shr-start
(point)) (point) type
))))
327 ;; Add an overlay in the region, but avoid putting the font properties
328 ;; on blank text at the start of the line, and the newline at the end,
329 ;; to avoid ugliness.
330 (defun shr-add-font (start end type
)
333 (while (< (point) end
)
335 (skip-chars-forward " "))
336 (let ((overlay (make-overlay (point) (min (line-end-position) end
))))
337 (overlay-put overlay
'face type
))
338 (if (< (line-end-position) end
)
342 (defun shr-browse-url ()
343 "Browse the URL under point."
345 (let ((url (get-text-property (point) 'shr-url
)))
348 (message "No link under point"))
349 ((string-match "^mailto:" url
)
350 (browse-url-mailto url
))
354 (defun shr-save-contents (directory)
355 "Save the contents from URL in a file."
356 (interactive "DSave contents of URL to directory: ")
357 (let ((url (get-text-property (point) 'shr-url
)))
359 (message "No link under point")
360 (url-retrieve (shr-encode-url url
)
361 'shr-store-contents
(list url directory
)))))
363 (defun shr-store-contents (status url directory
)
364 (unless (plist-get status
:error
)
365 (when (or (search-forward "\n\n" nil t
)
366 (search-forward "\r\n\r\n" nil t
))
367 (write-region (point) (point-max)
368 (expand-file-name (file-name-nondirectory url
)
371 (defun shr-image-fetched (status buffer start end
)
372 (when (and (buffer-name buffer
)
373 (not (plist-get status
:error
)))
374 (url-store-in-cache (current-buffer))
375 (when (or (search-forward "\n\n" nil t
)
376 (search-forward "\r\n\r\n" nil t
))
377 (let ((data (buffer-substring (point) (point-max))))
378 (with-current-buffer buffer
379 (let ((alt (buffer-substring start end
))
380 (inhibit-read-only t
))
381 (delete-region start end
)
383 (shr-put-image data alt
))))))
384 (kill-buffer (current-buffer)))
386 (defun shr-put-image (data alt
)
387 (if (display-graphic-p)
388 (let ((image (ignore-errors
389 (shr-rescale-image data
))))
391 (insert-image image
(or alt
"*"))))
394 (defun shr-rescale-image (data)
395 (if (or (not (fboundp 'imagemagick-types
))
396 (not (get-buffer-window (current-buffer))))
397 (create-image data nil t
)
398 (let* ((image (create-image data nil t
))
399 (size (image-size image t
))
402 (edges (window-inside-pixel-edges
403 (get-buffer-window (current-buffer))))
404 (window-width (truncate (* shr-max-image-proportion
405 (- (nth 2 edges
) (nth 0 edges
)))))
406 (window-height (truncate (* shr-max-image-proportion
407 (- (nth 3 edges
) (nth 1 edges
)))))
409 (when (> height window-height
)
410 (setq image
(or (create-image data
'imagemagick t
411 :height window-height
)
413 (setq size
(image-size image t
)))
414 (when (> (car size
) window-width
)
416 (create-image data
'imagemagick t
421 ;; url-cache-extract autoloads url-cache.
422 (declare-function url-cache-create-filename
"url-cache" (url))
423 (autoload 'mm-disable-multibyte
"mm-util")
424 (autoload 'browse-url-mailto
"browse-url")
426 (defun shr-get-image-data (url)
427 "Get image data for URL.
428 Return a string with image data."
430 (mm-disable-multibyte)
432 (url-cache-extract (url-cache-create-filename (shr-encode-url url
)))
434 (when (or (search-forward "\n\n" nil t
)
435 (search-forward "\r\n\r\n" nil t
))
436 (buffer-substring (point) (point-max))))))
438 (defun shr-heading (cont &rest types
)
439 (shr-ensure-paragraph)
440 (apply #'shr-fontize-cont cont types
)
441 (shr-ensure-paragraph))
443 (autoload 'widget-convert-button
"wid-edit")
445 (defun shr-urlify (start url
)
446 (widget-convert-button
447 'url-link start
(point)
451 (put-text-property start
(point) 'shr-url url
))
453 (defun shr-encode-url (url)
455 (browse-url-url-encode-chars url
"[)$ ]"))
457 ;;; Tag-specific rendering rules.
459 (defun shr-tag-p (cont)
460 (shr-ensure-paragraph)
463 (shr-ensure-paragraph))
465 (defun shr-tag-div (cont)
469 (shr-ensure-newline))
471 (defun shr-tag-b (cont)
472 (shr-fontize-cont cont
'bold
))
474 (defun shr-tag-i (cont)
475 (shr-fontize-cont cont
'italic
))
477 (defun shr-tag-em (cont)
478 (shr-fontize-cont cont
'bold
))
480 (defun shr-tag-strong (cont)
481 (shr-fontize-cont cont
'bold
))
483 (defun shr-tag-u (cont)
484 (shr-fontize-cont cont
'underline
))
486 (defun shr-tag-s (cont)
487 (shr-fontize-cont cont
'strike-through
))
489 (defun shr-parse-style (style)
492 (dolist (elem (split-string style
";"))
494 (setq elem
(split-string elem
":"))
495 (when (and (car elem
)
497 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem
)))
498 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem
))))
499 (push (cons (intern name obarray
)
504 (defun shr-tag-a (cont)
505 (let ((url (cdr (assq :href cont
)))
509 (shr-urlify (or shr-start start
) url
)))
511 (defun shr-tag-object (cont)
512 (let ((start (point))
515 (when (eq (car elem
) 'embed
)
516 (setq url
(or url
(cdr (assq :src
(cdr elem
))))))
517 (when (and (eq (car elem
) 'param
)
518 (equal (cdr (assq :name
(cdr elem
))) "movie"))
519 (setq url
(or url
(cdr (assq :value
(cdr elem
)))))))
521 (shr-insert " [multimedia] ")
522 (shr-urlify start url
))
525 (defun shr-tag-video (cont)
526 (let ((image (cdr (assq :poster cont
)))
527 (url (cdr (assq :src cont
)))
529 (shr-tag-img nil image
)
530 (shr-urlify start url
)))
532 (defun shr-tag-img (cont &optional url
)
535 (cdr (assq :src cont
))))
536 (when (and (> (current-column) 0)
537 (not (eq shr-state
'image
)))
539 (let ((alt (cdr (assq :alt cont
)))
540 (url (or url
(cdr (assq :src cont
)))))
541 (let ((start (point-marker)))
542 (when (zerop (length alt
))
545 ((or (member (cdr (assq :height cont
)) '("0" "1"))
546 (member (cdr (assq :width cont
)) '("0" "1")))
547 ;; Ignore zero-sized or single-pixel images.
549 ((and (not shr-inhibit-images
)
550 (string-match "\\`cid:" url
))
551 (let ((url (substring url
(match-end 0)))
553 (if (or (not shr-content-function
)
554 (not (setq image
(funcall shr-content-function url
))))
556 (shr-put-image image alt
))))
557 ((or shr-inhibit-images
558 (and shr-blocked-images
559 (string-match shr-blocked-images url
)))
560 (setq shr-start
(point))
561 (let ((shr-state 'space
))
562 (if (> (string-width alt
) 8)
563 (shr-insert (truncate-string-to-width alt
8))
565 ((url-is-cached (shr-encode-url url
))
566 (shr-put-image (shr-get-image-data url
) alt
))
570 (url-retrieve (shr-encode-url url
) 'shr-image-fetched
571 (list (current-buffer) start
(point-marker))
573 (put-text-property start
(point) 'keymap shr-map
)
574 (put-text-property start
(point) 'shr-alt alt
)
575 (put-text-property start
(point) 'image-url url
)
576 (put-text-property start
(point) 'help-echo alt
)
577 (setq shr-state
'image
)))))
579 (defun shr-tag-pre (cont)
580 (let ((shr-folding-mode 'none
))
584 (shr-ensure-newline)))
586 (defun shr-tag-blockquote (cont)
587 (shr-ensure-paragraph)
589 (let ((shr-indentation (+ shr-indentation
4)))
591 (shr-ensure-paragraph))
593 (defun shr-tag-ul (cont)
594 (shr-ensure-paragraph)
595 (let ((shr-list-mode 'ul
))
597 (shr-ensure-paragraph))
599 (defun shr-tag-ol (cont)
600 (shr-ensure-paragraph)
601 (let ((shr-list-mode 1))
603 (shr-ensure-paragraph))
605 (defun shr-tag-li (cont)
606 (shr-ensure-paragraph)
609 (if (numberp shr-list-mode
)
611 (format "%d " shr-list-mode
)
612 (setq shr-list-mode
(1+ shr-list-mode
)))
614 (shr-indentation (+ shr-indentation
(length bullet
))))
618 (defun shr-tag-br (cont)
624 (defun shr-tag-h1 (cont)
625 (shr-heading cont
'bold
'underline
))
627 (defun shr-tag-h2 (cont)
628 (shr-heading cont
'bold
))
630 (defun shr-tag-h3 (cont)
631 (shr-heading cont
'italic
))
633 (defun shr-tag-h4 (cont)
636 (defun shr-tag-h5 (cont)
639 (defun shr-tag-h6 (cont)
642 (defun shr-tag-hr (cont)
644 (insert (make-string shr-width shr-hr-line
) "\n"))
646 ;;; Table rendering algorithm.
648 ;; Table rendering is the only complicated thing here. We do this by
649 ;; first counting how many TDs there are in each TR, and registering
650 ;; how wide they think they should be ("width=45%", etc). Then we
651 ;; render each TD separately (this is done in temporary buffers, so
652 ;; that we can use all the rendering machinery as if we were in the
653 ;; main buffer). Now we know how much space each TD really takes, so
654 ;; we then render everything again with the new widths, and finally
655 ;; insert all these boxes into the main buffer.
656 (defun shr-tag-table-1 (cont)
657 (setq cont
(or (cdr (assq 'tbody cont
))
659 (let* ((shr-inhibit-images t
)
660 (shr-table-depth (1+ shr-table-depth
))
661 (shr-kinsoku-shorten t
)
662 ;; Find all suggested widths.
663 (columns (shr-column-specs cont
))
664 ;; Compute how many characters wide each TD should be.
665 (suggested-widths (shr-pro-rate-columns columns
))
666 ;; Do a "test rendering" to see how big each TD is (this can
667 ;; be smaller (if there's little text) or bigger (if there's
668 ;; unbreakable text).
669 (sketch (shr-make-table cont suggested-widths
))
670 (sketch-widths (shr-table-widths sketch suggested-widths
)))
671 ;; This probably won't work very well.
672 (when (> (+ (loop for width across sketch-widths
676 (setq truncate-lines t
))
677 ;; Then render the table again with these new "hard" widths.
678 (shr-insert-table (shr-make-table cont sketch-widths t
) sketch-widths
))
679 ;; Finally, insert all the images after the table. The Emacs buffer
680 ;; model isn't strong enough to allow us to put the images actually
682 (when (zerop shr-table-depth
)
683 (dolist (elem (shr-find-elements cont
'img
))
684 (shr-tag-img (cdr elem
)))))
686 (defun shr-tag-table (cont)
687 (shr-ensure-paragraph)
688 (let* ((caption (cdr (assq 'caption cont
)))
689 (header (cdr (assq 'thead cont
)))
690 (body (or (cdr (assq 'tbody cont
)) cont
))
691 (footer (cdr (assq 'tfoot cont
)))
692 (nheader (if header
(shr-max-columns header
)))
693 (nbody (if body
(shr-max-columns body
)))
694 (nfooter (if footer
(shr-max-columns footer
))))
697 (if caption
`((tr (td ,@caption
))))
700 ;; hader + body + footer
701 (if (= nheader nbody
)
702 (if (= nbody nfooter
)
703 `((tr (td (table (tbody ,@header
,@body
,@footer
)))))
704 (nconc `((tr (td (table (tbody ,@header
,@body
)))))
707 `((tr (td (table (tbody ,@footer
))))))))
708 (nconc `((tr (td (table (tbody ,@header
)))))
709 (if (= nbody nfooter
)
710 `((tr (td (table (tbody ,@body
,@footer
)))))
711 (nconc `((tr (td (table (tbody ,@body
)))))
714 `((tr (td (table (tbody ,@footer
))))))))))
716 (if (= nheader nbody
)
717 `((tr (td (table (tbody ,@header
,@body
)))))
719 `(,@header
(tr (td (table (tbody ,@body
)))))
720 `((tr (td (table (tbody ,@header
))))
721 (tr (td (table (tbody ,@body
))))))))
724 (if (= nbody nfooter
)
725 `((tr (td (table (tbody ,@body
,@footer
)))))
726 (nconc `((tr (td (table (tbody ,@body
)))))
729 `((tr (td (table (tbody ,@footer
))))))))
731 `((tr (td (table (tbody ,@body
)))))
734 (defun shr-find-elements (cont type
)
737 (cond ((eq (car elem
) type
)
740 (setq result
(nconc (shr-find-elements (cdr elem
) type
) result
)))))
743 (defun shr-insert-table (table widths
)
744 (shr-insert-table-ruler widths
)
746 (let ((start (point))
747 (height (let ((max 0))
749 (setq max
(max max
(cadr column
))))
756 (let ((lines (nth 2 column
))
757 (overlay-lines (nth 3 column
))
758 overlay overlay-line
)
760 (setq overlay-line
(pop overlay-lines
))
763 (dolist (overlay overlay-line
)
764 (let ((o (make-overlay (- (point) (nth 0 overlay
) 1)
765 (- (point) (nth 1 overlay
) 1)))
766 (properties (nth 2 overlay
)))
768 (overlay-put o
(pop properties
) (pop properties
)))))
770 ;; Add blank lines at padding at the bottom of the TD,
772 (dotimes (i (- height
(length lines
)))
774 (insert (make-string (string-width (car lines
)) ?
) "|")
776 (shr-insert-table-ruler widths
)))
778 (defun shr-insert-table-ruler (widths)
780 (> shr-indentation
0))
782 (insert shr-table-corner
)
783 (dotimes (i (length widths
))
784 (insert (make-string (aref widths i
) shr-table-line
) shr-table-corner
))
787 (defun shr-table-widths (table suggested-widths
)
788 (let* ((length (length suggested-widths
))
789 (widths (make-vector length
0))
790 (natural-widths (make-vector length
0)))
794 (aset widths i
(max (aref widths i
)
796 (aset natural-widths i
(max (aref natural-widths i
)
799 (let ((extra (- (apply '+ (append suggested-widths nil
))
800 (apply '+ (append widths nil
))))
801 (expanded-columns 0))
804 ;; If the natural width is wider than the rendered width, we
805 ;; want to allow the column to expand.
806 (when (> (aref natural-widths i
) (aref widths i
))
807 (setq expanded-columns
(1+ expanded-columns
))))
809 (when (> (aref natural-widths i
) (aref widths i
))
811 (1+ (aref natural-widths i
))
812 (+ (/ extra expanded-columns
)
813 (aref widths i
))))))))
816 (defun shr-make-table (cont widths
&optional fill
)
819 (when (eq (car row
) 'tr
)
824 (while (< i
(length widths
))
825 (setq column
(pop columns
))
826 (when (or (memq (car column
) '(td th
))
828 (push (shr-render-td (cdr column
) (aref widths i
) fill
)
831 (push (nreverse tds
) trs
))))
834 (defun shr-render-td (cont width fill
)
836 (let ((cache (cdr (assoc (cons width cont
) shr-content-cache
))))
839 (let ((shr-width width
)
845 (skip-chars-backward " \t\n")))
846 (push (cons (cons width cont
) (buffer-string))
848 (goto-char (point-min))
852 (setq max
(max max
(current-column)))
855 (goto-char (point-min))
856 ;; If the buffer is totally empty, then put a single blank
858 (if (zerop (buffer-size))
859 (insert (make-string width ?
))
860 ;; Otherwise, fill the buffer.
863 (when (> (- width
(current-column)) 0)
864 (insert (make-string (- width
(current-column)) ?
)))
868 (count-lines (point-min) (point-max))
869 (split-string (buffer-string) "\n")
870 (shr-collect-overlays))
872 (shr-natural-width))))))
874 (defun shr-natural-width ()
875 (goto-char (point-min))
880 (setq current
(+ current
(current-column)))
881 (unless (get-text-property (point) 'shr-break
)
882 (setq max
(max max current
)
887 (defun shr-collect-overlays ()
889 (goto-char (point-min))
890 (let ((overlays nil
))
892 (push (shr-overlays-in-region (point) (line-end-position))
895 (nreverse overlays
))))
897 (defun shr-overlays-in-region (start end
)
899 (dolist (overlay (overlays-in start end
))
900 (push (list (if (> start
(overlay-start overlay
))
902 (- end
(overlay-start overlay
)))
903 (if (< end
(overlay-end overlay
))
905 (- end
(overlay-end overlay
)))
906 (overlay-properties overlay
))
910 (defun shr-pro-rate-columns (columns)
911 (let ((total-percentage 0)
912 (widths (make-vector (length columns
) 0)))
913 (dotimes (i (length columns
))
914 (setq total-percentage
(+ total-percentage
(aref columns i
))))
915 (setq total-percentage
(/ 1.0 total-percentage
))
916 (dotimes (i (length columns
))
917 (aset widths i
(max (truncate (* (aref columns i
)
919 (- shr-width
(1+ (length columns
)))))
923 ;; Return a summary of the number and shape of the TDs in the table.
924 (defun shr-column-specs (cont)
925 (let ((columns (make-vector (shr-max-columns cont
) 1)))
927 (when (eq (car row
) 'tr
)
929 (dolist (column (cdr row
))
930 (when (memq (car column
) '(td th
))
931 (let ((width (cdr (assq :width
(cdr column
)))))
933 (string-match "\\([0-9]+\\)%" width
))
935 (/ (string-to-number (match-string 1 width
))
940 (defun shr-count (cont elem
)
943 (when (eq (car sub
) elem
)
947 (defun shr-max-columns (cont)
950 (when (eq (car row
) 'tr
)
951 (setq max
(max max
(+ (shr-count (cdr row
) 'td
)
952 (shr-count (cdr row
) 'th
))))))