Start thinking about breaking CJVK text.
[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 "^\\s-*$" 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 ((start (point-marker)))
397     (let ((alt (cdr (assq :alt cont)))
398           (url (cdr (assq :src cont))))
399       (when (zerop (length alt))
400         (setq alt "[img]"))
401       (cond
402        ((and (not shr-inhibit-images)
403              (string-match "\\`cid:" url))
404         (let ((url (substring url (match-end 0)))
405               image)
406           (if (or (not shr-content-function)
407                   (not (setq image (funcall shr-content-function url))))
408               (insert alt)
409             (shr-put-image image (point) alt))))
410        ((or shr-inhibit-images
411             (and shr-blocked-images
412                  (string-match shr-blocked-images url)))
413         (setq shr-start (point))
414         (let ((shr-state 'space))
415           (if (> (length alt) 8)
416               (shr-insert (substring alt 0 8))
417             (shr-insert alt))))
418        ((url-is-cached (browse-url-url-encode-chars url "[&)$ ]"))
419         (shr-put-image (shr-get-image-data url) (point) alt))
420        (t
421         (insert alt)
422         (ignore-errors
423           (url-retrieve url 'shr-image-fetched
424                         (list (current-buffer) start (point-marker))
425                         t))))
426       (insert " ")
427       (put-text-property start (point) 'keymap shr-map)
428       (put-text-property start (point) 'shr-alt alt)
429       (put-text-property start (point) 'shr-image url)
430       (setq shr-state 'image))))
431
432 (defun shr-tag-pre (cont)
433   (let ((shr-folding-mode 'none))
434     (shr-ensure-newline)
435     (shr-indent)
436     (shr-generic cont)
437     (shr-ensure-newline)))
438
439 (defun shr-tag-blockquote (cont)
440   (shr-ensure-paragraph)
441   (shr-indent)
442   (let ((shr-indentation (+ shr-indentation 4)))
443     (shr-generic cont))
444   (shr-ensure-paragraph))
445
446 (defun shr-tag-ul (cont)
447   (shr-ensure-paragraph)
448   (let ((shr-list-mode 'ul))
449     (shr-generic cont))
450   (shr-ensure-paragraph))
451
452 (defun shr-tag-ol (cont)
453   (shr-ensure-paragraph)
454   (let ((shr-list-mode 1))
455     (shr-generic cont))
456   (shr-ensure-paragraph))
457
458 (defun shr-tag-li (cont)
459   (shr-ensure-paragraph)
460   (shr-indent)
461   (let* ((bullet
462           (if (numberp shr-list-mode)
463               (prog1
464                   (format "%d " shr-list-mode)
465                 (setq shr-list-mode (1+ shr-list-mode)))
466             "* "))
467          (shr-indentation (+ shr-indentation (length bullet))))
468     (insert bullet)
469     (shr-generic cont)))
470
471 (defun shr-tag-br (cont)
472   (unless (bobp)
473     (insert "\n")
474     (shr-indent))
475   (shr-generic cont))
476
477 (defun shr-tag-h1 (cont)
478   (shr-heading cont 'bold 'underline))
479
480 (defun shr-tag-h2 (cont)
481   (shr-heading cont 'bold))
482
483 (defun shr-tag-h3 (cont)
484   (shr-heading cont 'italic))
485
486 (defun shr-tag-h4 (cont)
487   (shr-heading cont))
488
489 (defun shr-tag-h5 (cont)
490   (shr-heading cont))
491
492 (defun shr-tag-h6 (cont)
493   (shr-heading cont))
494
495 (defun shr-tag-hr (cont)
496   (shr-ensure-newline)
497   (insert (make-string shr-width shr-hr-line) "\n"))
498
499 ;;; Table rendering algorithm.
500
501 ;; Table rendering is the only complicated thing here.  We do this by
502 ;; first counting how many TDs there are in each TR, and registering
503 ;; how wide they think they should be ("width=45%", etc).  Then we
504 ;; render each TD separately (this is done in temporary buffers, so
505 ;; that we can use all the rendering machinery as if we were in the
506 ;; main buffer).  Now we know how much space each TD really takes, so
507 ;; we then render everything again with the new widths, and finally
508 ;; insert all these boxes into the main buffer.
509 (defun shr-tag-table (cont)
510   (shr-ensure-paragraph)
511   (setq cont (or (cdr (assq 'tbody cont))
512                  cont))
513   (let* ((shr-inhibit-images t)
514          ;; Find all suggested widths.
515          (columns (shr-column-specs cont))
516          ;; Compute how many characters wide each TD should be.
517          (suggested-widths (shr-pro-rate-columns columns))
518          ;; Do a "test rendering" to see how big each TD is (this can
519          ;; be smaller (if there's little text) or bigger (if there's
520          ;; unbreakable text).
521          (sketch (shr-make-table cont suggested-widths))
522          (sketch-widths (shr-table-widths sketch suggested-widths)))
523     ;; Then render the table again with these new "hard" widths.
524     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
525   ;; Finally, insert all the images after the table.  The Emacs buffer
526   ;; model isn't strong enough to allow us to put the images actually
527   ;; into the tables.
528   (dolist (elem (shr-find-elements cont 'img))
529     (shr-tag-img (cdr elem))))
530
531 (defun shr-find-elements (cont type)
532   (let (result)
533     (dolist (elem cont)
534       (cond ((eq (car elem) type)
535              (push elem result))
536             ((consp (cdr elem))
537              (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
538     (nreverse result)))
539
540 (defun shr-insert-table (table widths)
541   (shr-insert-table-ruler widths)
542   (dolist (row table)
543     (let ((start (point))
544           (height (let ((max 0))
545                     (dolist (column row)
546                       (setq max (max max (cadr column))))
547                     max)))
548       (dotimes (i height)
549         (shr-indent)
550         (insert "|\n"))
551       (dolist (column row)
552         (goto-char start)
553         (let ((lines (nth 2 column))
554               (overlay-lines (nth 3 column))
555               overlay overlay-line)
556           (dolist (line lines)
557             (setq overlay-line (pop overlay-lines))
558             (end-of-line)
559             (insert line "|")
560             (dolist (overlay overlay-line)
561               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
562                                      (- (point) (nth 1 overlay) 1)))
563                     (properties (nth 2 overlay)))
564                 (while properties
565                   (overlay-put o (pop properties) (pop properties)))))
566             (forward-line 1))
567           ;; Add blank lines at padding at the bottom of the TD,
568           ;; possibly.
569           (dotimes (i (- height (length lines)))
570             (end-of-line)
571             (insert (make-string (length (car lines)) ? ) "|")
572             (forward-line 1)))))
573     (shr-insert-table-ruler widths)))
574
575 (defun shr-insert-table-ruler (widths)
576   (shr-indent)
577   (insert shr-table-corner)
578   (dotimes (i (length widths))
579     (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
580   (insert "\n"))
581
582 (defun shr-table-widths (table suggested-widths)
583   (let* ((length (length suggested-widths))
584          (widths (make-vector length 0))
585          (natural-widths (make-vector length 0)))
586     (dolist (row table)
587       (let ((i 0))
588         (dolist (column row)
589           (aset widths i (max (aref widths i)
590                               (car column)))
591           (aset natural-widths i (max (aref natural-widths i)
592                                       (cadr column)))
593           (setq i (1+ i)))))
594     (let ((extra (- (apply '+ (append suggested-widths nil))
595                     (apply '+ (append widths nil))))
596           (expanded-columns 0))
597       (when (> extra 0)
598         (dotimes (i length)
599           ;; If the natural width is wider than the rendered width, we
600           ;; want to allow the column to expand.
601           (when (> (aref natural-widths i) (aref widths i))
602             (setq expanded-columns (1+ expanded-columns))))
603         (dotimes (i length)
604           (when (> (aref natural-widths i) (aref widths i))
605             (aset widths i (min
606                             (1+ (aref natural-widths i))
607                             (+ (/ extra expanded-columns)
608                                (aref widths i))))))))
609     widths))
610
611 (defun shr-make-table (cont widths &optional fill)
612   (let ((trs nil))
613     (dolist (row cont)
614       (when (eq (car row) 'tr)
615         (let ((tds nil)
616               (columns (cdr row))
617               (i 0)
618               column)
619           (while (< i (length widths))
620             (setq column (pop columns))
621             (when (or (memq (car column) '(td th))
622                       (null column))
623               (push (shr-render-td (cdr column) (aref widths i) fill)
624                     tds)
625               (setq i (1+ i))))
626           (push (nreverse tds) trs))))
627     (nreverse trs)))
628
629 (defun shr-render-td (cont width fill)
630   (with-temp-buffer
631     (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
632       (if cache
633           (insert cache)
634         (let ((shr-width width)
635               (shr-indentation 0))
636           (shr-generic cont))
637         (delete-region
638          (point)
639          (+ (point)
640             (skip-chars-backward " \t\n")))
641         (push (cons (cons width cont) (buffer-string))
642               shr-content-cache)))
643     (goto-char (point-min))
644     (let ((max 0))
645       (while (not (eobp))
646         (end-of-line)
647         (setq max (max max (current-column)))
648         (forward-line 1))
649       (when fill
650         (goto-char (point-min))
651         ;; If the buffer is totally empty, then put a single blank
652         ;; line here.
653         (if (zerop (buffer-size))
654             (insert (make-string width ? ))
655           ;; Otherwise, fill the buffer.
656           (while (not (eobp))
657             (end-of-line)
658             (when (> (- width (current-column)) 0)
659               (insert (make-string (- width (current-column)) ? )))
660             (forward-line 1))))
661       (if fill
662           (list max
663                 (count-lines (point-min) (point-max))
664                 (split-string (buffer-string) "\n")
665                 (shr-collect-overlays))
666         (list max
667               (shr-natural-width))))))
668
669 (defun shr-natural-width ()
670   (goto-char (point-min))
671   (let ((current 0)
672         (max 0))
673     (while (not (eobp))
674       (end-of-line)
675       (setq current (+ current (current-column)))
676       (unless (get-text-property (point) 'shr-break)
677         (setq max (max max current)
678               current 0))
679       (forward-line 1))
680     max))
681
682 (defun shr-collect-overlays ()
683   (save-excursion
684     (goto-char (point-min))
685     (let ((overlays nil))
686       (while (not (eobp))
687         (push (shr-overlays-in-region (point) (line-end-position))
688               overlays)
689         (forward-line 1))
690       (nreverse overlays))))
691
692 (defun shr-overlays-in-region (start end)
693   (let (result)
694     (dolist (overlay (overlays-in start end))
695       (push (list (if (> start (overlay-start overlay))
696                       (- end start)
697                     (- end (overlay-start overlay)))
698                   (if (< end (overlay-end overlay))
699                       0
700                     (- end (overlay-end overlay)))
701                   (overlay-properties overlay))
702             result))
703     (nreverse result)))
704
705 (defun shr-pro-rate-columns (columns)
706   (let ((total-percentage 0)
707         (widths (make-vector (length columns) 0)))
708     (dotimes (i (length columns))
709       (setq total-percentage (+ total-percentage (aref columns i))))
710     (setq total-percentage (/ 1.0 total-percentage))
711     (dotimes (i (length columns))
712       (aset widths i (max (truncate (* (aref columns i)
713                                        total-percentage
714                                        (- shr-width (1+ (length columns)))))
715                           10)))
716     widths))
717
718 ;; Return a summary of the number and shape of the TDs in the table.
719 (defun shr-column-specs (cont)
720   (let ((columns (make-vector (shr-max-columns cont) 1)))
721     (dolist (row cont)
722       (when (eq (car row) 'tr)
723         (let ((i 0))
724           (dolist (column (cdr row))
725             (when (memq (car column) '(td th))
726               (let ((width (cdr (assq :width (cdr column)))))
727                 (when (and width
728                            (string-match "\\([0-9]+\\)%" width))
729                   (aset columns i
730                         (/ (string-to-number (match-string 1 width))
731                            100.0))))
732               (setq i (1+ i)))))))
733     columns))
734
735 (defun shr-count (cont elem)
736   (let ((i 0))
737     (dolist (sub cont)
738       (when (eq (car sub) elem)
739         (setq i (1+ i))))
740     i))
741
742 (defun shr-max-columns (cont)
743   (let ((max 0))
744     (dolist (row cont)
745       (when (eq (car row) 'tr)
746         (setq max (max max (+ (shr-count (cdr row) 'td)
747                               (shr-count (cdr row) 'th))))))
748     max))
749
750 (provide 'shr)
751
752 ;;; shr.el ends here