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