* shr.el (shr-current-column): Remove buggy and unnecessary function.
[gnus] / lisp / shr.el
1 ;;; shr.el --- Simple HTML Renderer
2
3 ;; Copyright (C) 2010 Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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.
30
31 ;;; Code:
32
33 (eval-when-compile (require 'cl))
34 (require 'browse-url)
35
36 (defgroup shr nil
37   "Simple HTML Renderer"
38   :group 'mail)
39
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
45 fit these criteria."
46   :version "24.1"
47   :group 'shr
48   :type 'float)
49
50 (defcustom shr-blocked-images nil
51   "Images that have URLs matching this regexp will be blocked."
52   :version "24.1"
53   :group 'shr
54   :type 'regexp)
55
56 (defcustom shr-table-line ?-
57   "Character used to draw table line."
58   :group 'shr
59   :type 'char)
60
61 (defcustom shr-table-corner ?+
62   "Character used to draw table corner."
63   :group 'shr
64   :type 'char)
65
66 (defcustom shr-hr-line ?-
67   "Character used to draw hr line."
68   :group 'shr
69   :type 'char)
70
71 (defcustom shr-width fill-column
72   "Frame width to use for rendering."
73   :type 'integer
74   :group 'shr)
75
76 (defvar shr-content-function nil
77   "If bound, this should be a function that will return the content.
78 This is used for cid: URLs, and the function is called with the
79 cid: URL as the argument.")
80
81 ;;; Internal variables.
82
83 (defvar shr-folding-mode nil)
84 (defvar shr-state nil)
85 (defvar shr-start nil)
86 (defvar shr-indentation 0)
87 (defvar shr-inhibit-images nil)
88 (defvar shr-list-mode nil)
89 (defvar shr-content-cache nil)
90
91 (defvar shr-map
92   (let ((map (make-sparse-keymap)))
93     (define-key map "a" 'shr-show-alt-text)
94     (define-key map "i" 'shr-browse-image)
95     (define-key map "I" 'shr-insert-image)
96     (define-key map "u" 'shr-copy-url)
97     (define-key map "v" 'shr-browse-url)
98     (define-key map "\r" 'shr-browse-url)
99     map))
100
101 ;; Public functions and commands.
102
103 ;;;###autoload
104 (defun shr-insert-document (dom)
105   (setq shr-content-cache nil)
106   (let ((shr-state nil)
107         (shr-start nil))
108     (shr-descend (shr-transform-dom dom))))
109
110 (defun shr-copy-url ()
111   "Copy the URL under point to the kill ring.
112 If called twice, then try to fetch the URL and see whether it
113 redirects somewhere else."
114   (interactive)
115   (let ((url (get-text-property (point) 'shr-url)))
116     (cond
117      ((not url)
118       (message "No URL under point"))
119      ;; Resolve redirected URLs.
120      ((equal url (car kill-ring))
121       (url-retrieve
122        url
123        (lambda (a)
124          (when (and (consp a)
125                     (eq (car a) :redirect))
126            (with-temp-buffer
127              (insert (cadr a))
128              (goto-char (point-min))
129              ;; Remove common tracking junk from the URL.
130              (when (re-search-forward ".utm_.*" nil t)
131                (replace-match "" t t))
132              (message "Copied %s" (buffer-string))
133              (copy-region-as-kill (point-min) (point-max)))))))
134      ;; Copy the URL to the kill ring.
135      (t
136       (with-temp-buffer
137         (insert url)
138         (copy-region-as-kill (point-min) (point-max))
139         (message "Copied %s" url))))))
140
141 (defun shr-show-alt-text ()
142   "Show the ALT text of the image under point."
143   (interactive)
144   (let ((text (get-text-property (point) 'shr-alt)))
145     (if (not text)
146         (message "No image under point")
147       (message "%s" text))))
148
149 (defun shr-browse-image ()
150   "Browse the image under point."
151   (interactive)
152   (let ((url (get-text-property (point) 'shr-image)))
153     (if (not url)
154         (message "No image under point")
155       (message "Browsing %s..." url)
156       (browse-url url))))
157
158 (defun shr-insert-image ()
159   "Insert the image under point into the buffer."
160   (interactive)
161   (let ((url (get-text-property (point) 'shr-image)))
162     (if (not url)
163         (message "No image under point")
164       (message "Inserting %s..." url)
165       (url-retrieve url 'shr-image-fetched
166                     (list (current-buffer) (1- (point)) (point-marker))
167                     t))))
168
169 ;;; Utility functions.
170
171 (defun shr-transform-dom (dom)
172   (let ((result (list (pop dom))))
173     (dolist (arg (pop dom))
174       (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
175                   (cdr arg))
176             result))
177     (dolist (sub dom)
178       (if (stringp sub)
179           (push (cons :text sub) result)
180         (push (shr-transform-dom sub) result)))
181     (nreverse result)))
182
183 (defun shr-descend (dom)
184   (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
185     (if (fboundp function)
186         (funcall function (cdr dom))
187       (shr-generic (cdr dom)))))
188
189 (defun shr-generic (cont)
190   (dolist (sub cont)
191     (cond
192      ((eq (car sub) :text)
193       (shr-insert (cdr sub)))
194      ((listp (cdr sub))
195       (shr-descend sub)))))
196
197 (defun shr-insert (text)
198   (when (and (eq shr-state 'image)
199              (not (string-match "\\`[ \t\n]+\\'" text)))
200     (insert "\n")
201     (setq shr-state nil))
202   (cond
203    ((eq shr-folding-mode 'none)
204     (insert text))
205    (t
206     (let ((first t)
207           column)
208       (when (and (string-match "\\`[ \t\n]" text)
209                  (not (bolp))
210                  (not (eq (char-after (1- (point))) ? )))
211         (insert " "))
212       (dolist (elem (split-string text))
213         (when (and (bolp)
214                    (> shr-indentation 0))
215           (shr-indent))
216         ;; The shr-start is a special variable that is used to pass
217         ;; upwards the first point in the buffer where the text really
218         ;; starts.
219         (unless shr-start
220           (setq shr-start (point)))
221         (insert elem)
222         (when (> (current-column) shr-width)
223           (if (not (search-backward " " (line-beginning-position) t))
224               (insert "\n")
225             (delete-char 1)
226             (insert "\n")
227             (put-text-property (1- (point)) (point) 'shr-break t)
228             (when (> shr-indentation 0)
229               (shr-indent))
230             (end-of-line)))
231         (insert " "))
232       (unless (string-match "[ \t\n]\\'" text)
233         (delete-char -1))))))
234
235 (defun shr-find-fill-point ()
236   (let ((found nil))
237     (while (and (not found)
238                 (not (bolp)))
239       (when (or (eq (preceding-char) ? )
240                 (aref fill-find-break-point-function-table (preceding-char)))
241         (setq found (point)))
242       (backward-char 1))
243     (or found
244         (end-of-line))))
245
246 (defun shr-ensure-newline ()
247   (unless (zerop (current-column))
248     (insert "\n")))
249
250 (defun shr-ensure-paragraph ()
251   (unless (bobp)
252     (if (<= (current-column) shr-indentation)
253         (unless (save-excursion
254                   (forward-line -1)
255                   (looking-at " *$"))
256           (insert "\n"))
257       (if (save-excursion
258             (beginning-of-line)
259             (looking-at " *$"))
260           (insert "\n")
261         (insert "\n\n")))))
262
263 (defun shr-indent ()
264   (when (> shr-indentation 0)
265     (insert (make-string shr-indentation ? ))))
266
267 (defun shr-fontize-cont (cont &rest types)
268   (let (shr-start)
269     (shr-generic cont)
270     (dolist (type types)
271       (shr-add-font (or shr-start (point)) (point) type))))
272
273 (defun shr-add-font (start end type)
274   (let ((overlay (make-overlay start end)))
275     (overlay-put overlay 'face type)))
276
277 (defun shr-browse-url ()
278   "Browse the URL under point."
279   (interactive)
280   (let ((url (get-text-property (point) 'shr-url)))
281     (if (not url)
282         (message "No link under point")
283       (browse-url url))))
284
285 (defun shr-image-fetched (status buffer start end)
286   (when (and (buffer-name buffer)
287              (not (plist-get status :error)))
288     (url-store-in-cache (current-buffer))
289     (when (or (search-forward "\n\n" nil t)
290               (search-forward "\r\n\r\n" nil t))
291       (let ((data (buffer-substring (point) (point-max))))
292         (with-current-buffer buffer
293           (let ((alt (buffer-substring start end))
294                 (inhibit-read-only t))
295             (delete-region start end)
296             (shr-put-image data start alt))))))
297   (kill-buffer (current-buffer)))
298
299 (defun shr-put-image (data point alt)
300   (if (not (display-graphic-p))
301       (insert alt)
302     (let ((image (ignore-errors
303                    (shr-rescale-image data))))
304       (when image
305         (put-image image point alt)))))
306
307 (defun shr-rescale-image (data)
308   (if (or (not (fboundp 'imagemagick-types))
309           (not (get-buffer-window (current-buffer))))
310       (create-image data nil t)
311     (let* ((image (create-image data nil t))
312            (size (image-size image t))
313            (width (car size))
314            (height (cdr size))
315            (edges (window-inside-pixel-edges
316                    (get-buffer-window (current-buffer))))
317            (window-width (truncate (* shr-max-image-proportion
318                                       (- (nth 2 edges) (nth 0 edges)))))
319            (window-height (truncate (* shr-max-image-proportion
320                                        (- (nth 3 edges) (nth 1 edges)))))
321            scaled-image)
322       (when (> height window-height)
323         (setq image (or (create-image data 'imagemagick t
324                                       :height window-height)
325                         image))
326         (setq size (image-size image t)))
327       (when (> (car size) window-width)
328         (setq image (or
329                      (create-image data 'imagemagick t
330                                    :width window-width)
331                      image)))
332       image)))
333
334 (defun shr-get-image-data (url)
335   "Get image data for URL.
336 Return a string with image data."
337   (with-temp-buffer
338     (mm-disable-multibyte)
339     (when (ignore-errors
340             (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
341             t)
342       (when (or (search-forward "\n\n" nil t)
343                 (search-forward "\r\n\r\n" nil t))
344         (buffer-substring (point) (point-max))))))
345
346 (defun shr-heading (cont &rest types)
347   (shr-ensure-paragraph)
348   (apply #'shr-fontize-cont cont types)
349   (shr-ensure-paragraph))
350
351 ;;; Tag-specific rendering rules.
352
353 (defun shr-tag-p (cont)
354   (shr-ensure-paragraph)
355   (shr-indent)
356   (shr-generic cont)
357   (shr-ensure-paragraph))
358
359 (defun shr-tag-b (cont)
360   (shr-fontize-cont cont 'bold))
361
362 (defun shr-tag-i (cont)
363   (shr-fontize-cont cont 'italic))
364
365 (defun shr-tag-em (cont)
366   (shr-fontize-cont cont 'bold))
367
368 (defun shr-tag-u (cont)
369   (shr-fontize-cont cont 'underline))
370
371 (defun shr-tag-s (cont)
372   (shr-fontize-cont cont 'strike-through))
373
374 (defun shr-tag-a (cont)
375   (let ((url (cdr (assq :href cont)))
376         (start (point))
377         shr-start)
378     (shr-generic cont)
379     (widget-convert-button
380      'link (or shr-start start) (point)
381      :help-echo url)
382     (put-text-property (or shr-start start) (point) 'keymap shr-map)
383     (put-text-property (or shr-start start) (point) 'shr-url url)))
384
385 (defun shr-encode-url (url)
386   "Encode URL."
387   (browse-url-url-encode-chars url "[)$ ]"))
388
389 (defun shr-tag-img (cont)
390   (when (and (> (current-column) 0)
391              (not (eq shr-state 'image)))
392     (insert "\n"))
393   (let ((alt (cdr (assq :alt cont)))
394         (url (cdr (assq :src cont)))
395         (width (cdr (assq :width cont))))
396     ;; Only respect align if width specified.
397     (when width
398       ;; Check that width is not larger than max width, otherwise ignore
399       ;; align
400       (let ((max-width (* shr-width (frame-char-width)))
401             (width (string-to-number width)))
402         (when (< width max-width)
403           (let ((align (cdr (assq :align cont))))
404             (cond ((string= align "right")
405                    (insert (propertize
406                             " " 'display
407                             `(space . (:align-to ,(list (- max-width width)))))))
408                   ((string= align "center")
409                    (insert (propertize
410                             " " 'display
411                             `(space . (:balign-to ,(list (- (/ max-width 2) width))))))))))))
412     (let ((start (point-marker)))
413       (when (zerop (length alt))
414         (setq alt "[img]"))
415       (cond
416        ((and (not shr-inhibit-images)
417              (string-match "\\`cid:" url))
418         (let ((url (substring url (match-end 0)))
419               image)
420           (if (or (not shr-content-function)
421                   (not (setq image (funcall shr-content-function url))))
422               (insert alt)
423             (shr-put-image image (point) alt))))
424        ((or shr-inhibit-images
425             (and shr-blocked-images
426                  (string-match shr-blocked-images url)))
427         (setq shr-start (point))
428         (let ((shr-state 'space))
429           (if (> (length alt) 8)
430               (shr-insert (substring alt 0 8))
431             (shr-insert alt))))
432        ((url-is-cached (shr-encode-url url))
433         (shr-put-image (shr-get-image-data url) (point) alt))
434        (t
435         (insert alt)
436         (ignore-errors
437           (url-retrieve (shr-encode-url url) 'shr-image-fetched
438                         (list (current-buffer) start (point-marker))
439                         t))))
440       (insert " ")
441       (put-text-property start (point) 'keymap shr-map)
442       (put-text-property start (point) 'shr-alt alt)
443       (put-text-property start (point) 'shr-image url)
444       (setq shr-state 'image))))
445
446 (defun shr-tag-pre (cont)
447   (let ((shr-folding-mode 'none))
448     (shr-ensure-newline)
449     (shr-indent)
450     (shr-generic cont)
451     (shr-ensure-newline)))
452
453 (defun shr-tag-blockquote (cont)
454   (shr-ensure-paragraph)
455   (shr-indent)
456   (let ((shr-indentation (+ shr-indentation 4)))
457     (shr-generic cont))
458   (shr-ensure-paragraph))
459
460 (defun shr-tag-ul (cont)
461   (shr-ensure-paragraph)
462   (let ((shr-list-mode 'ul))
463     (shr-generic cont))
464   (shr-ensure-paragraph))
465
466 (defun shr-tag-ol (cont)
467   (shr-ensure-paragraph)
468   (let ((shr-list-mode 1))
469     (shr-generic cont))
470   (shr-ensure-paragraph))
471
472 (defun shr-tag-li (cont)
473   (shr-ensure-paragraph)
474   (shr-indent)
475   (let* ((bullet
476           (if (numberp shr-list-mode)
477               (prog1
478                   (format "%d " shr-list-mode)
479                 (setq shr-list-mode (1+ shr-list-mode)))
480             "* "))
481          (shr-indentation (+ shr-indentation (length bullet))))
482     (insert bullet)
483     (shr-generic cont)))
484
485 (defun shr-tag-br (cont)
486   (unless (bobp)
487     (insert "\n")
488     (shr-indent))
489   (shr-generic cont))
490
491 (defun shr-tag-h1 (cont)
492   (shr-heading cont 'bold 'underline))
493
494 (defun shr-tag-h2 (cont)
495   (shr-heading cont 'bold))
496
497 (defun shr-tag-h3 (cont)
498   (shr-heading cont 'italic))
499
500 (defun shr-tag-h4 (cont)
501   (shr-heading cont))
502
503 (defun shr-tag-h5 (cont)
504   (shr-heading cont))
505
506 (defun shr-tag-h6 (cont)
507   (shr-heading cont))
508
509 (defun shr-tag-hr (cont)
510   (shr-ensure-newline)
511   (insert (make-string shr-width shr-hr-line) "\n"))
512
513 ;;; Table rendering algorithm.
514
515 ;; Table rendering is the only complicated thing here.  We do this by
516 ;; first counting how many TDs there are in each TR, and registering
517 ;; how wide they think they should be ("width=45%", etc).  Then we
518 ;; render each TD separately (this is done in temporary buffers, so
519 ;; that we can use all the rendering machinery as if we were in the
520 ;; main buffer).  Now we know how much space each TD really takes, so
521 ;; we then render everything again with the new widths, and finally
522 ;; insert all these boxes into the main buffer.
523 (defun shr-tag-table (cont)
524   (shr-ensure-paragraph)
525   (setq cont (or (cdr (assq 'tbody cont))
526                  cont))
527   (let* ((shr-inhibit-images t)
528          ;; Find all suggested widths.
529          (columns (shr-column-specs cont))
530          ;; Compute how many characters wide each TD should be.
531          (suggested-widths (shr-pro-rate-columns columns))
532          ;; Do a "test rendering" to see how big each TD is (this can
533          ;; be smaller (if there's little text) or bigger (if there's
534          ;; unbreakable text).
535          (sketch (shr-make-table cont suggested-widths))
536          (sketch-widths (shr-table-widths sketch suggested-widths)))
537     ;; Then render the table again with these new "hard" widths.
538     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
539   ;; Finally, insert all the images after the table.  The Emacs buffer
540   ;; model isn't strong enough to allow us to put the images actually
541   ;; into the tables.
542   (dolist (elem (shr-find-elements cont 'img))
543     (shr-tag-img (cdr elem))))
544
545 (defun shr-find-elements (cont type)
546   (let (result)
547     (dolist (elem cont)
548       (cond ((eq (car elem) type)
549              (push elem result))
550             ((consp (cdr elem))
551              (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
552     (nreverse result)))
553
554 (defun shr-insert-table (table widths)
555   (shr-insert-table-ruler widths)
556   (dolist (row table)
557     (let ((start (point))
558           (height (let ((max 0))
559                     (dolist (column row)
560                       (setq max (max max (cadr column))))
561                     max)))
562       (dotimes (i height)
563         (shr-indent)
564         (insert "|\n"))
565       (dolist (column row)
566         (goto-char start)
567         (let ((lines (nth 2 column))
568               (overlay-lines (nth 3 column))
569               overlay overlay-line)
570           (dolist (line lines)
571             (setq overlay-line (pop overlay-lines))
572             (end-of-line)
573             (insert line "|")
574             (dolist (overlay overlay-line)
575               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
576                                      (- (point) (nth 1 overlay) 1)))
577                     (properties (nth 2 overlay)))
578                 (while properties
579                   (overlay-put o (pop properties) (pop properties)))))
580             (forward-line 1))
581           ;; Add blank lines at padding at the bottom of the TD,
582           ;; possibly.
583           (dotimes (i (- height (length lines)))
584             (end-of-line)
585             (insert (make-string (length (car lines)) ? ) "|")
586             (forward-line 1)))))
587     (shr-insert-table-ruler widths)))
588
589 (defun shr-insert-table-ruler (widths)
590   (shr-indent)
591   (insert shr-table-corner)
592   (dotimes (i (length widths))
593     (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
594   (insert "\n"))
595
596 (defun shr-table-widths (table suggested-widths)
597   (let* ((length (length suggested-widths))
598          (widths (make-vector length 0))
599          (natural-widths (make-vector length 0)))
600     (dolist (row table)
601       (let ((i 0))
602         (dolist (column row)
603           (aset widths i (max (aref widths i)
604                               (car column)))
605           (aset natural-widths i (max (aref natural-widths i)
606                                       (cadr column)))
607           (setq i (1+ i)))))
608     (let ((extra (- (apply '+ (append suggested-widths nil))
609                     (apply '+ (append widths nil))))
610           (expanded-columns 0))
611       (when (> extra 0)
612         (dotimes (i length)
613           ;; If the natural width is wider than the rendered width, we
614           ;; want to allow the column to expand.
615           (when (> (aref natural-widths i) (aref widths i))
616             (setq expanded-columns (1+ expanded-columns))))
617         (dotimes (i length)
618           (when (> (aref natural-widths i) (aref widths i))
619             (aset widths i (min
620                             (1+ (aref natural-widths i))
621                             (+ (/ extra expanded-columns)
622                                (aref widths i))))))))
623     widths))
624
625 (defun shr-make-table (cont widths &optional fill)
626   (let ((trs nil))
627     (dolist (row cont)
628       (when (eq (car row) 'tr)
629         (let ((tds nil)
630               (columns (cdr row))
631               (i 0)
632               column)
633           (while (< i (length widths))
634             (setq column (pop columns))
635             (when (or (memq (car column) '(td th))
636                       (null column))
637               (push (shr-render-td (cdr column) (aref widths i) fill)
638                     tds)
639               (setq i (1+ i))))
640           (push (nreverse tds) trs))))
641     (nreverse trs)))
642
643 (defun shr-render-td (cont width fill)
644   (with-temp-buffer
645     (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
646       (if cache
647           (insert cache)
648         (let ((shr-width width)
649               (shr-indentation 0))
650           (shr-generic cont))
651         (delete-region
652          (point)
653          (+ (point)
654             (skip-chars-backward " \t\n")))
655         (push (cons (cons width cont) (buffer-string))
656               shr-content-cache)))
657     (goto-char (point-min))
658     (let ((max 0))
659       (while (not (eobp))
660         (end-of-line)
661         (setq max (max max (current-column)))
662         (forward-line 1))
663       (when fill
664         (goto-char (point-min))
665         ;; If the buffer is totally empty, then put a single blank
666         ;; line here.
667         (if (zerop (buffer-size))
668             (insert (make-string width ? ))
669           ;; Otherwise, fill the buffer.
670           (while (not (eobp))
671             (end-of-line)
672             (when (> (- width (current-column)) 0)
673               (insert (make-string (- width (current-column)) ? )))
674             (forward-line 1))))
675       (if fill
676           (list max
677                 (count-lines (point-min) (point-max))
678                 (split-string (buffer-string) "\n")
679                 (shr-collect-overlays))
680         (list max
681               (shr-natural-width))))))
682
683 (defun shr-natural-width ()
684   (goto-char (point-min))
685   (let ((current 0)
686         (max 0))
687     (while (not (eobp))
688       (end-of-line)
689       (setq current (+ current (current-column)))
690       (unless (get-text-property (point) 'shr-break)
691         (setq max (max max current)
692               current 0))
693       (forward-line 1))
694     max))
695
696 (defun shr-collect-overlays ()
697   (save-excursion
698     (goto-char (point-min))
699     (let ((overlays nil))
700       (while (not (eobp))
701         (push (shr-overlays-in-region (point) (line-end-position))
702               overlays)
703         (forward-line 1))
704       (nreverse overlays))))
705
706 (defun shr-overlays-in-region (start end)
707   (let (result)
708     (dolist (overlay (overlays-in start end))
709       (push (list (if (> start (overlay-start overlay))
710                       (- end start)
711                     (- end (overlay-start overlay)))
712                   (if (< end (overlay-end overlay))
713                       0
714                     (- end (overlay-end overlay)))
715                   (overlay-properties overlay))
716             result))
717     (nreverse result)))
718
719 (defun shr-pro-rate-columns (columns)
720   (let ((total-percentage 0)
721         (widths (make-vector (length columns) 0)))
722     (dotimes (i (length columns))
723       (setq total-percentage (+ total-percentage (aref columns i))))
724     (setq total-percentage (/ 1.0 total-percentage))
725     (dotimes (i (length columns))
726       (aset widths i (max (truncate (* (aref columns i)
727                                        total-percentage
728                                        (- shr-width (1+ (length columns)))))
729                           10)))
730     widths))
731
732 ;; Return a summary of the number and shape of the TDs in the table.
733 (defun shr-column-specs (cont)
734   (let ((columns (make-vector (shr-max-columns cont) 1)))
735     (dolist (row cont)
736       (when (eq (car row) 'tr)
737         (let ((i 0))
738           (dolist (column (cdr row))
739             (when (memq (car column) '(td th))
740               (let ((width (cdr (assq :width (cdr column)))))
741                 (when (and width
742                            (string-match "\\([0-9]+\\)%" width))
743                   (aset columns i
744                         (/ (string-to-number (match-string 1 width))
745                            100.0))))
746               (setq i (1+ i)))))))
747     columns))
748
749 (defun shr-count (cont elem)
750   (let ((i 0))
751     (dolist (sub cont)
752       (when (eq (car sub) elem)
753         (setq i (1+ i))))
754     i))
755
756 (defun shr-max-columns (cont)
757   (let ((max 0))
758     (dolist (row cont)
759       (when (eq (car row) 'tr)
760         (setq max (max max (+ (shr-count (cdr row) 'td)
761                               (shr-count (cdr row) 'th))))))
762     max))
763
764 (provide 'shr)
765
766 ;;; shr.el ends here