Indent.
[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 'character)
60
61 (defcustom shr-table-corner ?+
62   "Character used to draw table corner."
63   :group 'shr
64   :type 'character)
65
66 (defcustom shr-hr-line ?-
67   "Character used to draw hr line."
68   :group 'shr
69   :type 'character)
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         (while (> (current-column) shr-width)
223           (if (not (shr-find-fill-point))
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                 (> (current-column) shr-indentation))
239       (when (and (or (eq (preceding-char) ? )
240                      (aref fill-find-break-point-function-table
241                            (preceding-char)))
242                  (<= (current-column) shr-width))
243         (setq found (point)))
244       (backward-char 1))
245     (or found
246         (end-of-line))))
247
248 (defun shr-ensure-newline ()
249   (unless (zerop (current-column))
250     (insert "\n")))
251
252 (defun shr-ensure-paragraph ()
253   (unless (bobp)
254     (if (<= (current-column) shr-indentation)
255         (unless (save-excursion
256                   (forward-line -1)
257                   (looking-at " *$"))
258           (insert "\n"))
259       (if (save-excursion
260             (beginning-of-line)
261             (looking-at " *$"))
262           (insert "\n")
263         (insert "\n\n")))))
264
265 (defun shr-indent ()
266   (when (> shr-indentation 0)
267     (insert (make-string shr-indentation ? ))))
268
269 (defun shr-fontize-cont (cont &rest types)
270   (let (shr-start)
271     (shr-generic cont)
272     (dolist (type types)
273       (shr-add-font (or shr-start (point)) (point) type))))
274
275 (defun shr-add-font (start end type)
276   (let ((overlay (make-overlay start end)))
277     (overlay-put overlay 'face type)))
278
279 (defun shr-browse-url ()
280   "Browse the URL under point."
281   (interactive)
282   (let ((url (get-text-property (point) 'shr-url)))
283     (if (not url)
284         (message "No link under point")
285       (browse-url url))))
286
287 (defun shr-image-fetched (status buffer start end)
288   (when (and (buffer-name buffer)
289              (not (plist-get status :error)))
290     (url-store-in-cache (current-buffer))
291     (when (or (search-forward "\n\n" nil t)
292               (search-forward "\r\n\r\n" nil t))
293       (let ((data (buffer-substring (point) (point-max))))
294         (with-current-buffer buffer
295           (let ((alt (buffer-substring start end))
296                 (inhibit-read-only t))
297             (delete-region start end)
298             (shr-put-image data start alt))))))
299   (kill-buffer (current-buffer)))
300
301 (defun shr-put-image (data point alt)
302   (if (not (display-graphic-p))
303       (insert alt)
304     (let ((image (ignore-errors
305                    (shr-rescale-image data))))
306       (when image
307         (put-image image point alt)))))
308
309 (defun shr-rescale-image (data)
310   (if (or (not (fboundp 'imagemagick-types))
311           (not (get-buffer-window (current-buffer))))
312       (create-image data nil t)
313     (let* ((image (create-image data nil t))
314            (size (image-size image t))
315            (width (car size))
316            (height (cdr size))
317            (edges (window-inside-pixel-edges
318                    (get-buffer-window (current-buffer))))
319            (window-width (truncate (* shr-max-image-proportion
320                                       (- (nth 2 edges) (nth 0 edges)))))
321            (window-height (truncate (* shr-max-image-proportion
322                                        (- (nth 3 edges) (nth 1 edges)))))
323            scaled-image)
324       (when (> height window-height)
325         (setq image (or (create-image data 'imagemagick t
326                                       :height window-height)
327                         image))
328         (setq size (image-size image t)))
329       (when (> (car size) window-width)
330         (setq image (or
331                      (create-image data 'imagemagick t
332                                    :width window-width)
333                      image)))
334       image)))
335
336 (defun shr-get-image-data (url)
337   "Get image data for URL.
338 Return a string with image data."
339   (with-temp-buffer
340     (mm-disable-multibyte)
341     (when (ignore-errors
342             (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
343             t)
344       (when (or (search-forward "\n\n" nil t)
345                 (search-forward "\r\n\r\n" nil t))
346         (buffer-substring (point) (point-max))))))
347
348 (defun shr-heading (cont &rest types)
349   (shr-ensure-paragraph)
350   (apply #'shr-fontize-cont cont types)
351   (shr-ensure-paragraph))
352
353 ;;; Tag-specific rendering rules.
354
355 (defun shr-tag-p (cont)
356   (shr-ensure-paragraph)
357   (shr-indent)
358   (shr-generic cont)
359   (shr-ensure-paragraph))
360
361 (defun shr-tag-b (cont)
362   (shr-fontize-cont cont 'bold))
363
364 (defun shr-tag-i (cont)
365   (shr-fontize-cont cont 'italic))
366
367 (defun shr-tag-em (cont)
368   (shr-fontize-cont cont 'bold))
369
370 (defun shr-tag-u (cont)
371   (shr-fontize-cont cont 'underline))
372
373 (defun shr-tag-s (cont)
374   (shr-fontize-cont cont 'strike-through))
375
376 (defun shr-tag-a (cont)
377   (let ((url (cdr (assq :href cont)))
378         (start (point))
379         shr-start)
380     (shr-generic cont)
381     (widget-convert-button
382      'url-link (or shr-start start) (point)
383      :help-echo url
384      :keymap shr-map
385      url)
386     (put-text-property (or shr-start start) (point) 'shr-url url)))
387
388 (defun shr-encode-url (url)
389   "Encode URL."
390   (browse-url-url-encode-chars url "[)$ ]"))
391
392 (defun shr-tag-img (cont)
393   (when (and (> (current-column) 0)
394              (not (eq shr-state 'image)))
395     (insert "\n"))
396   (let ((alt (cdr (assq :alt cont)))
397         (url (cdr (assq :src cont)))
398         (width (cdr (assq :width cont))))
399     ;; Only respect align if width specified.
400     (when width
401       ;; Check that width is not larger than max width, otherwise ignore
402       ;; align
403       (let ((max-width (* shr-width (frame-char-width)))
404             (width (string-to-number width)))
405         (when (< width max-width)
406           (let ((align (cdr (assq :align cont))))
407             (cond
408              ((string= align "right")
409               (insert (propertize
410                        " " 'display
411                        `(space . (:align-to
412                                   ,(list (- max-width width)))))))
413              ((string= align "center")
414               (insert (propertize
415                        " " 'display
416                        `(space . (:balign-to
417                                   ,(list (- (/ max-width 2) width))))))))))))
418     (let ((start (point-marker)))
419       (when (zerop (length alt))
420         (setq alt "[img]"))
421       (cond
422        ((and (not shr-inhibit-images)
423              (string-match "\\`cid:" url))
424         (let ((url (substring url (match-end 0)))
425               image)
426           (if (or (not shr-content-function)
427                   (not (setq image (funcall shr-content-function url))))
428               (insert alt)
429             (shr-put-image image (point) alt))))
430        ((or shr-inhibit-images
431             (and shr-blocked-images
432                  (string-match shr-blocked-images url)))
433         (setq shr-start (point))
434         (let ((shr-state 'space))
435           (if (> (length alt) 8)
436               (shr-insert (substring alt 0 8))
437             (shr-insert alt))))
438        ((url-is-cached (shr-encode-url url))
439         (shr-put-image (shr-get-image-data url) (point) alt))
440        (t
441         (insert alt)
442         (ignore-errors
443           (url-retrieve (shr-encode-url url) 'shr-image-fetched
444                         (list (current-buffer) start (point-marker))
445                         t))))
446       (insert " ")
447       (put-text-property start (point) 'keymap shr-map)
448       (put-text-property start (point) 'shr-alt alt)
449       (put-text-property start (point) 'shr-image url)
450       (setq shr-state 'image))))
451
452 (defun shr-tag-pre (cont)
453   (let ((shr-folding-mode 'none))
454     (shr-ensure-newline)
455     (shr-indent)
456     (shr-generic cont)
457     (shr-ensure-newline)))
458
459 (defun shr-tag-blockquote (cont)
460   (shr-ensure-paragraph)
461   (shr-indent)
462   (let ((shr-indentation (+ shr-indentation 4)))
463     (shr-generic cont))
464   (shr-ensure-paragraph))
465
466 (defun shr-tag-ul (cont)
467   (shr-ensure-paragraph)
468   (let ((shr-list-mode 'ul))
469     (shr-generic cont))
470   (shr-ensure-paragraph))
471
472 (defun shr-tag-ol (cont)
473   (shr-ensure-paragraph)
474   (let ((shr-list-mode 1))
475     (shr-generic cont))
476   (shr-ensure-paragraph))
477
478 (defun shr-tag-li (cont)
479   (shr-ensure-paragraph)
480   (shr-indent)
481   (let* ((bullet
482           (if (numberp shr-list-mode)
483               (prog1
484                   (format "%d " shr-list-mode)
485                 (setq shr-list-mode (1+ shr-list-mode)))
486             "* "))
487          (shr-indentation (+ shr-indentation (length bullet))))
488     (insert bullet)
489     (shr-generic cont)))
490
491 (defun shr-tag-br (cont)
492   (unless (bobp)
493     (insert "\n")
494     (shr-indent))
495   (shr-generic cont))
496
497 (defun shr-tag-h1 (cont)
498   (shr-heading cont 'bold 'underline))
499
500 (defun shr-tag-h2 (cont)
501   (shr-heading cont 'bold))
502
503 (defun shr-tag-h3 (cont)
504   (shr-heading cont 'italic))
505
506 (defun shr-tag-h4 (cont)
507   (shr-heading cont))
508
509 (defun shr-tag-h5 (cont)
510   (shr-heading cont))
511
512 (defun shr-tag-h6 (cont)
513   (shr-heading cont))
514
515 (defun shr-tag-hr (cont)
516   (shr-ensure-newline)
517   (insert (make-string shr-width shr-hr-line) "\n"))
518
519 ;;; Table rendering algorithm.
520
521 ;; Table rendering is the only complicated thing here.  We do this by
522 ;; first counting how many TDs there are in each TR, and registering
523 ;; how wide they think they should be ("width=45%", etc).  Then we
524 ;; render each TD separately (this is done in temporary buffers, so
525 ;; that we can use all the rendering machinery as if we were in the
526 ;; main buffer).  Now we know how much space each TD really takes, so
527 ;; we then render everything again with the new widths, and finally
528 ;; insert all these boxes into the main buffer.
529 (defun shr-tag-table (cont)
530   (shr-ensure-paragraph)
531   (setq cont (or (cdr (assq 'tbody cont))
532                  cont))
533   (let* ((shr-inhibit-images t)
534          ;; Find all suggested widths.
535          (columns (shr-column-specs cont))
536          ;; Compute how many characters wide each TD should be.
537          (suggested-widths (shr-pro-rate-columns columns))
538          ;; Do a "test rendering" to see how big each TD is (this can
539          ;; be smaller (if there's little text) or bigger (if there's
540          ;; unbreakable text).
541          (sketch (shr-make-table cont suggested-widths))
542          (sketch-widths (shr-table-widths sketch suggested-widths)))
543     ;; Then render the table again with these new "hard" widths.
544     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
545   ;; Finally, insert all the images after the table.  The Emacs buffer
546   ;; model isn't strong enough to allow us to put the images actually
547   ;; into the tables.
548   (dolist (elem (shr-find-elements cont 'img))
549     (shr-tag-img (cdr elem))))
550
551 (defun shr-find-elements (cont type)
552   (let (result)
553     (dolist (elem cont)
554       (cond ((eq (car elem) type)
555              (push elem result))
556             ((consp (cdr elem))
557              (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
558     (nreverse result)))
559
560 (defun shr-insert-table (table widths)
561   (shr-insert-table-ruler widths)
562   (dolist (row table)
563     (let ((start (point))
564           (height (let ((max 0))
565                     (dolist (column row)
566                       (setq max (max max (cadr column))))
567                     max)))
568       (dotimes (i height)
569         (shr-indent)
570         (insert "|\n"))
571       (dolist (column row)
572         (goto-char start)
573         (let ((lines (nth 2 column))
574               (overlay-lines (nth 3 column))
575               overlay overlay-line)
576           (dolist (line lines)
577             (setq overlay-line (pop overlay-lines))
578             (end-of-line)
579             (insert line "|")
580             (dolist (overlay overlay-line)
581               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
582                                      (- (point) (nth 1 overlay) 1)))
583                     (properties (nth 2 overlay)))
584                 (while properties
585                   (overlay-put o (pop properties) (pop properties)))))
586             (forward-line 1))
587           ;; Add blank lines at padding at the bottom of the TD,
588           ;; possibly.
589           (dotimes (i (- height (length lines)))
590             (end-of-line)
591             (insert (make-string (length (car lines)) ? ) "|")
592             (forward-line 1)))))
593     (shr-insert-table-ruler widths)))
594
595 (defun shr-insert-table-ruler (widths)
596   (shr-indent)
597   (insert shr-table-corner)
598   (dotimes (i (length widths))
599     (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
600   (insert "\n"))
601
602 (defun shr-table-widths (table suggested-widths)
603   (let* ((length (length suggested-widths))
604          (widths (make-vector length 0))
605          (natural-widths (make-vector length 0)))
606     (dolist (row table)
607       (let ((i 0))
608         (dolist (column row)
609           (aset widths i (max (aref widths i)
610                               (car column)))
611           (aset natural-widths i (max (aref natural-widths i)
612                                       (cadr column)))
613           (setq i (1+ i)))))
614     (let ((extra (- (apply '+ (append suggested-widths nil))
615                     (apply '+ (append widths nil))))
616           (expanded-columns 0))
617       (when (> extra 0)
618         (dotimes (i length)
619           ;; If the natural width is wider than the rendered width, we
620           ;; want to allow the column to expand.
621           (when (> (aref natural-widths i) (aref widths i))
622             (setq expanded-columns (1+ expanded-columns))))
623         (dotimes (i length)
624           (when (> (aref natural-widths i) (aref widths i))
625             (aset widths i (min
626                             (1+ (aref natural-widths i))
627                             (+ (/ extra expanded-columns)
628                                (aref widths i))))))))
629     widths))
630
631 (defun shr-make-table (cont widths &optional fill)
632   (let ((trs nil))
633     (dolist (row cont)
634       (when (eq (car row) 'tr)
635         (let ((tds nil)
636               (columns (cdr row))
637               (i 0)
638               column)
639           (while (< i (length widths))
640             (setq column (pop columns))
641             (when (or (memq (car column) '(td th))
642                       (null column))
643               (push (shr-render-td (cdr column) (aref widths i) fill)
644                     tds)
645               (setq i (1+ i))))
646           (push (nreverse tds) trs))))
647     (nreverse trs)))
648
649 (defun shr-render-td (cont width fill)
650   (with-temp-buffer
651     (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
652       (if cache
653           (insert cache)
654         (let ((shr-width width)
655               (shr-indentation 0))
656           (shr-generic cont))
657         (delete-region
658          (point)
659          (+ (point)
660             (skip-chars-backward " \t\n")))
661         (push (cons (cons width cont) (buffer-string))
662               shr-content-cache)))
663     (goto-char (point-min))
664     (let ((max 0))
665       (while (not (eobp))
666         (end-of-line)
667         (setq max (max max (current-column)))
668         (forward-line 1))
669       (when fill
670         (goto-char (point-min))
671         ;; If the buffer is totally empty, then put a single blank
672         ;; line here.
673         (if (zerop (buffer-size))
674             (insert (make-string width ? ))
675           ;; Otherwise, fill the buffer.
676           (while (not (eobp))
677             (end-of-line)
678             (when (> (- width (current-column)) 0)
679               (insert (make-string (- width (current-column)) ? )))
680             (forward-line 1))))
681       (if fill
682           (list max
683                 (count-lines (point-min) (point-max))
684                 (split-string (buffer-string) "\n")
685                 (shr-collect-overlays))
686         (list max
687               (shr-natural-width))))))
688
689 (defun shr-natural-width ()
690   (goto-char (point-min))
691   (let ((current 0)
692         (max 0))
693     (while (not (eobp))
694       (end-of-line)
695       (setq current (+ current (current-column)))
696       (unless (get-text-property (point) 'shr-break)
697         (setq max (max max current)
698               current 0))
699       (forward-line 1))
700     max))
701
702 (defun shr-collect-overlays ()
703   (save-excursion
704     (goto-char (point-min))
705     (let ((overlays nil))
706       (while (not (eobp))
707         (push (shr-overlays-in-region (point) (line-end-position))
708               overlays)
709         (forward-line 1))
710       (nreverse overlays))))
711
712 (defun shr-overlays-in-region (start end)
713   (let (result)
714     (dolist (overlay (overlays-in start end))
715       (push (list (if (> start (overlay-start overlay))
716                       (- end start)
717                     (- end (overlay-start overlay)))
718                   (if (< end (overlay-end overlay))
719                       0
720                     (- end (overlay-end overlay)))
721                   (overlay-properties overlay))
722             result))
723     (nreverse result)))
724
725 (defun shr-pro-rate-columns (columns)
726   (let ((total-percentage 0)
727         (widths (make-vector (length columns) 0)))
728     (dotimes (i (length columns))
729       (setq total-percentage (+ total-percentage (aref columns i))))
730     (setq total-percentage (/ 1.0 total-percentage))
731     (dotimes (i (length columns))
732       (aset widths i (max (truncate (* (aref columns i)
733                                        total-percentage
734                                        (- shr-width (1+ (length columns)))))
735                           10)))
736     widths))
737
738 ;; Return a summary of the number and shape of the TDs in the table.
739 (defun shr-column-specs (cont)
740   (let ((columns (make-vector (shr-max-columns cont) 1)))
741     (dolist (row cont)
742       (when (eq (car row) 'tr)
743         (let ((i 0))
744           (dolist (column (cdr row))
745             (when (memq (car column) '(td th))
746               (let ((width (cdr (assq :width (cdr column)))))
747                 (when (and width
748                            (string-match "\\([0-9]+\\)%" width))
749                   (aset columns i
750                         (/ (string-to-number (match-string 1 width))
751                            100.0))))
752               (setq i (1+ i)))))))
753     columns))
754
755 (defun shr-count (cont elem)
756   (let ((i 0))
757     (dolist (sub cont)
758       (when (eq (car sub) elem)
759         (setq i (1+ i))))
760     i))
761
762 (defun shr-max-columns (cont)
763   (let ((max 0))
764     (dolist (row cont)
765       (when (eq (car row) 'tr)
766         (setq max (max max (+ (shr-count (cdr row) 'td)
767                               (shr-count (cdr row) 'th))))))
768     max))
769
770 (provide 'shr)
771
772 ;;; shr.el ends here