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