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