shr.el (shr-image-displayer): Protect function against non-existent image source.
[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 (unless (aref (char-category-set (make-char 'japanese-jisx0208 33 35)) ?>)
36   (load "kinsoku" nil t))
37
38 (defgroup shr nil
39   "Simple HTML Renderer"
40   :group 'mail)
41
42 (defcustom shr-max-image-proportion 0.9
43   "How big pictures displayed are in relation to the window they're in.
44 A value of 0.7 means that they are allowed to take up 70% of the
45 width and height of the window.  If they are larger than this,
46 and Emacs supports it, then the images will be rescaled down to
47 fit these criteria."
48   :version "24.1"
49   :group 'shr
50   :type 'float)
51
52 (defcustom shr-blocked-images nil
53   "Images that have URLs matching this regexp will be blocked."
54   :version "24.1"
55   :group 'shr
56   :type 'regexp)
57
58 (defcustom shr-table-line ?-
59   "Character used to draw table line."
60   :group 'shr
61   :type 'character)
62
63 (defcustom shr-table-corner ?+
64   "Character used to draw table corner."
65   :group 'shr
66   :type 'character)
67
68 (defcustom shr-hr-line ?-
69   "Character used to draw hr line."
70   :group 'shr
71   :type 'character)
72
73 (defcustom shr-width fill-column
74   "Frame width to use for rendering."
75   :type 'integer
76   :group 'shr)
77
78 (defvar shr-content-function nil
79   "If bound, this should be a function that will return the content.
80 This is used for cid: URLs, and the function is called with the
81 cid: URL as the argument.")
82
83 ;;; Internal variables.
84
85 (defvar shr-folding-mode nil)
86 (defvar shr-state nil)
87 (defvar shr-start nil)
88 (defvar shr-indentation 0)
89 (defvar shr-inhibit-images nil)
90 (defvar shr-list-mode nil)
91 (defvar shr-content-cache nil)
92 (defvar shr-kinsoku-shorten nil)
93 (defvar shr-table-depth 0)
94
95 (defvar shr-map
96   (let ((map (make-sparse-keymap)))
97     (define-key map "a" 'shr-show-alt-text)
98     (define-key map "i" 'shr-browse-image)
99     (define-key map "I" 'shr-insert-image)
100     (define-key map "u" 'shr-copy-url)
101     (define-key map "v" 'shr-browse-url)
102     (define-key map "o" 'shr-save-contents)
103     (define-key map "\r" 'shr-browse-url)
104     map))
105
106 ;; Public functions and commands.
107
108 ;;;###autoload
109 (defun shr-insert-document (dom)
110   (setq shr-content-cache nil)
111   (let ((shr-state nil)
112         (shr-start nil))
113     (shr-descend (shr-transform-dom dom))))
114
115 (defun shr-copy-url ()
116   "Copy the URL under point to the kill ring.
117 If called twice, then try to fetch the URL and see whether it
118 redirects somewhere else."
119   (interactive)
120   (let ((url (get-text-property (point) 'shr-url)))
121     (cond
122      ((not url)
123       (message "No URL under point"))
124      ;; Resolve redirected URLs.
125      ((equal url (car kill-ring))
126       (url-retrieve
127        url
128        (lambda (a)
129          (when (and (consp a)
130                     (eq (car a) :redirect))
131            (with-temp-buffer
132              (insert (cadr a))
133              (goto-char (point-min))
134              ;; Remove common tracking junk from the URL.
135              (when (re-search-forward ".utm_.*" nil t)
136                (replace-match "" t t))
137              (message "Copied %s" (buffer-string))
138              (copy-region-as-kill (point-min) (point-max)))))))
139      ;; Copy the URL to the kill ring.
140      (t
141       (with-temp-buffer
142         (insert url)
143         (copy-region-as-kill (point-min) (point-max))
144         (message "Copied %s" url))))))
145
146 (defun shr-show-alt-text ()
147   "Show the ALT text of the image under point."
148   (interactive)
149   (let ((text (get-text-property (point) 'shr-alt)))
150     (if (not text)
151         (message "No image under point")
152       (message "%s" text))))
153
154 (defun shr-browse-image ()
155   "Browse the image under point."
156   (interactive)
157   (let ((url (get-text-property (point) 'image-url)))
158     (if (not url)
159         (message "No image under point")
160       (message "Browsing %s..." url)
161       (browse-url url))))
162
163 (defun shr-insert-image ()
164   "Insert the image under point into the buffer."
165   (interactive)
166   (let ((url (get-text-property (point) 'image-url)))
167     (if (not url)
168         (message "No image under point")
169       (message "Inserting %s..." url)
170       (url-retrieve url 'shr-image-fetched
171                     (list (current-buffer) (1- (point)) (point-marker))
172                     t))))
173
174 ;;; Utility functions.
175
176 (defun shr-transform-dom (dom)
177   (let ((result (list (pop dom))))
178     (dolist (arg (pop dom))
179       (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
180                   (cdr arg))
181             result))
182     (dolist (sub dom)
183       (if (stringp sub)
184           (push (cons 'text sub) result)
185         (push (shr-transform-dom sub) result)))
186     (nreverse result)))
187
188 (defun shr-descend (dom)
189   (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)))
190     (if (fboundp function)
191         (funcall function (cdr dom))
192       (shr-generic (cdr dom)))))
193
194 (defun shr-generic (cont)
195   (dolist (sub cont)
196     (cond
197      ((eq (car sub) 'text)
198       (shr-insert (cdr sub)))
199      ((listp (cdr sub))
200       (shr-descend sub)))))
201
202 (defun shr-insert (text)
203   (when (and (eq shr-state 'image)
204              (not (string-match "\\`[ \t\n]+\\'" text)))
205     (insert "\n")
206     (setq shr-state nil))
207   (cond
208    ((eq shr-folding-mode 'none)
209     (insert text))
210    (t
211     (when (and (string-match "\\`[ \t\n]" text)
212                (not (bolp))
213                (not (eq (char-after (1- (point))) ? )))
214       (insert " "))
215     (dolist (elem (split-string text))
216       (when (and (bolp)
217                  (> shr-indentation 0))
218         (shr-indent))
219       ;; The shr-start is a special variable that is used to pass
220       ;; upwards the first point in the buffer where the text really
221       ;; starts.
222       (unless shr-start
223         (setq shr-start (point)))
224       ;; No space is needed behind a wide character categorized as
225       ;; kinsoku-bol, between characters both categorized as nospace,
226       ;; or at the beginning of a line.
227       (let (prev)
228         (when (and (eq (preceding-char) ? )
229                    (or (= (line-beginning-position) (1- (point)))
230                        (and (aref fill-find-break-point-function-table
231                                   (setq prev (char-after (- (point) 2))))
232                             (aref (char-category-set prev) ?>))
233                        (and (aref fill-nospace-between-words-table prev)
234                             (aref fill-nospace-between-words-table
235                                   (aref elem 0)))))
236           (delete-char -1)))
237       (insert elem)
238       (let (found)
239         (while (and (> (current-column) shr-width)
240                     (progn
241                       (setq found (shr-find-fill-point))
242                       (not (eolp))))
243           (when (eq (preceding-char) ? )
244             (delete-char -1))
245           (insert "\n")
246           (unless found
247             (put-text-property (1- (point)) (point) 'shr-break t)
248             ;; No space is needed at the beginning of a line.
249             (when (eq (following-char) ? )
250               (delete-char 1)))
251           (when (> shr-indentation 0)
252             (shr-indent))
253           (end-of-line))
254         (insert " ")))
255     (unless (string-match "[ \t\n]\\'" text)
256       (delete-char -1)))))
257
258 (defun shr-find-fill-point ()
259   (when (> (move-to-column shr-width) shr-width)
260     (backward-char 1))
261   (let (failed)
262     (while (not
263             (or (setq failed (= (current-column) shr-indentation))
264                 (eq (preceding-char) ? )
265                 (eq (following-char) ? )
266                 (aref fill-find-break-point-function-table (preceding-char))))
267       (backward-char 1))
268     (if failed
269         ;; There's no breakable point, so we give it up.
270         (progn
271           (end-of-line)
272           (while (aref fill-find-break-point-function-table (preceding-char))
273             (backward-char 1))
274           nil)
275       (or (eolp)
276           ;; Don't put kinsoku-bol characters at the beginning of a line,
277           ;; or kinsoku-eol characters at the end of a line,
278           (let ((count 4))
279             (if (or shr-kinsoku-shorten
280                     (and (aref (char-category-set (preceding-char)) ?<)
281                          (progn
282                            (setq count (1- count))
283                            (backward-char 1)
284                            t)))
285                 (while (and
286                         (>= (setq count (1- count)) 0)
287                         (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
288                         (or (aref (char-category-set (preceding-char)) ?<)
289                             (aref (char-category-set (following-char)) ?>)))
290                   (backward-char 1))
291               (while (and (>= (setq count (1- count)) 0)
292                           (aref (char-category-set (following-char)) ?>)
293                           (aref fill-find-break-point-function-table
294                                 (following-char)))
295                 (forward-char 1)))
296             (when (eq (following-char) ? )
297               (forward-char 1))
298             t)))))
299
300 (defun shr-ensure-newline ()
301   (unless (zerop (current-column))
302     (insert "\n")))
303
304 (defun shr-ensure-paragraph ()
305   (unless (bobp)
306     (if (<= (current-column) shr-indentation)
307         (unless (save-excursion
308                   (forward-line -1)
309                   (looking-at " *$"))
310           (insert "\n"))
311       (if (save-excursion
312             (beginning-of-line)
313             (looking-at " *$"))
314           (insert "\n")
315         (insert "\n\n")))))
316
317 (defun shr-indent ()
318   (when (> shr-indentation 0)
319     (insert (make-string shr-indentation ? ))))
320
321 (defun shr-fontize-cont (cont &rest types)
322   (let (shr-start)
323     (shr-generic cont)
324     (dolist (type types)
325       (shr-add-font (or shr-start (point)) (point) type))))
326
327 ;; Add an overlay in the region, but avoid putting the font properties
328 ;; on blank text at the start of the line, and the newline at the end,
329 ;; to avoid ugliness.
330 (defun shr-add-font (start end type)
331   (save-excursion
332     (goto-char start)
333     (while (< (point) end)
334       (when (bolp)
335         (skip-chars-forward " "))
336       (let ((overlay (make-overlay (point) (min (line-end-position) end))))
337         (overlay-put overlay 'face type))
338       (if (< (line-end-position) end)
339           (forward-line 1)
340         (goto-char end)))))
341
342 (defun shr-browse-url ()
343   "Browse the URL under point."
344   (interactive)
345   (let ((url (get-text-property (point) 'shr-url)))
346     (cond
347      ((not url)
348       (message "No link under point"))
349      ((string-match "^mailto:" url)
350       (browse-url-mailto url))
351      (t
352       (browse-url url)))))
353
354 (defun shr-save-contents (directory)
355   "Save the contents from URL in a file."
356   (interactive "DSave contents of URL to directory: ")
357   (let ((url (get-text-property (point) 'shr-url)))
358     (if (not url)
359         (message "No link under point")
360       (url-retrieve (shr-encode-url url)
361                     'shr-store-contents (list url directory)))))
362
363 (defun shr-store-contents (status url directory)
364   (unless (plist-get status :error)
365     (when (or (search-forward "\n\n" nil t)
366               (search-forward "\r\n\r\n" nil t))
367       (write-region (point) (point-max)
368                     (expand-file-name (file-name-nondirectory url)
369                                       directory)))))
370
371 (defun shr-image-fetched (status buffer start end)
372   (when (and (buffer-name buffer)
373              (not (plist-get status :error)))
374     (url-store-in-cache (current-buffer))
375     (when (or (search-forward "\n\n" nil t)
376               (search-forward "\r\n\r\n" nil t))
377       (let ((data (buffer-substring (point) (point-max))))
378         (with-current-buffer buffer
379           (let ((alt (buffer-substring start end))
380                 (inhibit-read-only t))
381             (delete-region start end)
382             (goto-char start)
383             (shr-put-image data alt))))))
384   (kill-buffer (current-buffer)))
385
386 (defun shr-put-image (data alt)
387   (if (display-graphic-p)
388       (let ((image (ignore-errors
389                      (shr-rescale-image data))))
390         (when image
391           (insert-image image (or alt "*"))))
392     (insert alt)))
393
394 (defun shr-rescale-image (data)
395   (if (or (not (fboundp 'imagemagick-types))
396           (not (get-buffer-window (current-buffer))))
397       (create-image data nil t)
398     (let* ((image (create-image data nil t))
399            (size (image-size image t))
400            (width (car size))
401            (height (cdr size))
402            (edges (window-inside-pixel-edges
403                    (get-buffer-window (current-buffer))))
404            (window-width (truncate (* shr-max-image-proportion
405                                       (- (nth 2 edges) (nth 0 edges)))))
406            (window-height (truncate (* shr-max-image-proportion
407                                        (- (nth 3 edges) (nth 1 edges)))))
408            scaled-image)
409       (when (> height window-height)
410         (setq image (or (create-image data 'imagemagick t
411                                       :height window-height)
412                         image))
413         (setq size (image-size image t)))
414       (when (> (car size) window-width)
415         (setq image (or
416                      (create-image data 'imagemagick t
417                                    :width window-width)
418                      image)))
419       image)))
420
421 ;; url-cache-extract autoloads url-cache.
422 (declare-function url-cache-create-filename "url-cache" (url))
423 (autoload 'mm-disable-multibyte "mm-util")
424 (autoload 'browse-url-mailto "browse-url")
425
426 (defun shr-get-image-data (url)
427   "Get image data for URL.
428 Return a string with image data."
429   (with-temp-buffer
430     (mm-disable-multibyte)
431     (when (ignore-errors
432             (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
433             t)
434       (when (or (search-forward "\n\n" nil t)
435                 (search-forward "\r\n\r\n" nil t))
436         (buffer-substring (point) (point-max))))))
437
438 (defun shr-image-displayer (content-function)
439   "Return a function to display an image.
440 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
441 is an argument.  The function to be returned takes three arguments URL,
442 START, and END."
443   `(lambda (url start end)
444      (when url
445        (if (string-match "\\`cid:" url)
446            ,(when content-function
447               `(let ((image (funcall ,content-function
448                                      (substring url (match-end 0)))))
449                  (when image
450                    (goto-char start)
451                    (shr-put-image image
452                                   (prog1
453                                       (buffer-substring-no-properties start end)
454                                     (delete-region start end))))))
455          (url-retrieve url 'shr-image-fetched
456                        (list (current-buffer) start end)
457                        t)))))
458
459 (defun shr-heading (cont &rest types)
460   (shr-ensure-paragraph)
461   (apply #'shr-fontize-cont cont types)
462   (shr-ensure-paragraph))
463
464 (autoload 'widget-convert-button "wid-edit")
465
466 (defun shr-urlify (start url)
467   (widget-convert-button
468    'url-link start (point)
469    :help-echo url
470    :keymap shr-map
471    url)
472   (put-text-property start (point) 'shr-url url))
473
474 (defun shr-encode-url (url)
475   "Encode URL."
476   (browse-url-url-encode-chars url "[)$ ]"))
477
478 ;;; Tag-specific rendering rules.
479
480 (defun shr-tag-p (cont)
481   (shr-ensure-paragraph)
482   (shr-indent)
483   (shr-generic cont)
484   (shr-ensure-paragraph))
485
486 (defun shr-tag-div (cont)
487   (shr-ensure-newline)
488   (shr-indent)
489   (shr-generic cont)
490   (shr-ensure-newline))
491
492 (defun shr-tag-b (cont)
493   (shr-fontize-cont cont 'bold))
494
495 (defun shr-tag-i (cont)
496   (shr-fontize-cont cont 'italic))
497
498 (defun shr-tag-em (cont)
499   (shr-fontize-cont cont 'bold))
500
501 (defun shr-tag-strong (cont)
502   (shr-fontize-cont cont 'bold))
503
504 (defun shr-tag-u (cont)
505   (shr-fontize-cont cont 'underline))
506
507 (defun shr-tag-s (cont)
508   (shr-fontize-cont cont 'strike-through))
509
510 (defun shr-parse-style (style)
511   (when style
512     (let ((plist nil))
513       (dolist (elem (split-string style ";"))
514         (when elem
515           (setq elem (split-string elem ":"))
516           (when (and (car elem)
517                      (cadr elem))
518             (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
519                   (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
520               (push (cons (intern name obarray)
521                           value)
522                     plist)))))
523       plist)))
524
525 (defun shr-tag-a (cont)
526   (let ((url (cdr (assq :href cont)))
527         (start (point))
528         shr-start)
529     (shr-generic cont)
530     (shr-urlify (or shr-start start) url)))
531
532 (defun shr-tag-object (cont)
533   (let ((start (point))
534         url)
535     (dolist (elem cont)
536       (when (eq (car elem) 'embed)
537         (setq url (or url (cdr (assq :src (cdr elem))))))
538       (when (and (eq (car elem) 'param)
539                  (equal (cdr (assq :name (cdr elem))) "movie"))
540         (setq url (or url (cdr (assq :value (cdr elem)))))))
541     (when url
542       (shr-insert " [multimedia] ")
543       (shr-urlify start url))
544     (shr-generic cont)))
545
546 (defun shr-tag-video (cont)
547   (let ((image (cdr (assq :poster cont)))
548         (url (cdr (assq :src cont)))
549         (start (point)))
550     (shr-tag-img nil image)
551     (shr-urlify start url)))
552
553 (defun shr-tag-img (cont &optional url)
554   (when (or url
555             (and cont
556                  (cdr (assq :src cont))))
557     (when (and (> (current-column) 0)
558                (not (eq shr-state 'image)))
559       (insert "\n"))
560     (let ((alt (cdr (assq :alt cont)))
561           (url (or url (cdr (assq :src cont)))))
562       (let ((start (point-marker)))
563         (when (zerop (length alt))
564           (setq alt "*"))
565         (cond
566          ((or (member (cdr (assq :height cont)) '("0" "1"))
567               (member (cdr (assq :width cont)) '("0" "1")))
568           ;; Ignore zero-sized or single-pixel images.
569           )
570          ((and (not shr-inhibit-images)
571                (string-match "\\`cid:" url))
572           (let ((url (substring url (match-end 0)))
573                 image)
574             (if (or (not shr-content-function)
575                     (not (setq image (funcall shr-content-function url))))
576                 (insert alt)
577               (shr-put-image image alt))))
578          ((or shr-inhibit-images
579               (and shr-blocked-images
580                    (string-match shr-blocked-images url)))
581           (setq shr-start (point))
582           (let ((shr-state 'space))
583             (if (> (string-width alt) 8)
584                 (shr-insert (truncate-string-to-width alt 8))
585               (shr-insert alt))))
586          ((url-is-cached (shr-encode-url url))
587           (shr-put-image (shr-get-image-data url) alt))
588          (t
589           (insert alt)
590           (ignore-errors
591             (url-retrieve (shr-encode-url url) 'shr-image-fetched
592                           (list (current-buffer) start (point-marker))
593                           t))))
594         (put-text-property start (point) 'keymap shr-map)
595         (put-text-property start (point) 'shr-alt alt)
596         (put-text-property start (point) 'image-url url)
597         (put-text-property start (point) 'image-displayer
598                            (shr-image-displayer shr-content-function))
599         (put-text-property start (point) 'help-echo alt)
600         (setq shr-state 'image)))))
601
602 (defun shr-tag-pre (cont)
603   (let ((shr-folding-mode 'none))
604     (shr-ensure-newline)
605     (shr-indent)
606     (shr-generic cont)
607     (shr-ensure-newline)))
608
609 (defun shr-tag-blockquote (cont)
610   (shr-ensure-paragraph)
611   (shr-indent)
612   (let ((shr-indentation (+ shr-indentation 4)))
613     (shr-generic cont))
614   (shr-ensure-paragraph))
615
616 (defun shr-tag-ul (cont)
617   (shr-ensure-paragraph)
618   (let ((shr-list-mode 'ul))
619     (shr-generic cont))
620   (shr-ensure-paragraph))
621
622 (defun shr-tag-ol (cont)
623   (shr-ensure-paragraph)
624   (let ((shr-list-mode 1))
625     (shr-generic cont))
626   (shr-ensure-paragraph))
627
628 (defun shr-tag-li (cont)
629   (shr-ensure-paragraph)
630   (shr-indent)
631   (let* ((bullet
632           (if (numberp shr-list-mode)
633               (prog1
634                   (format "%d " shr-list-mode)
635                 (setq shr-list-mode (1+ shr-list-mode)))
636             "* "))
637          (shr-indentation (+ shr-indentation (length bullet))))
638     (insert bullet)
639     (shr-generic cont)))
640
641 (defun shr-tag-br (cont)
642   (unless (bobp)
643     (insert "\n")
644     (shr-indent))
645   (shr-generic cont))
646
647 (defun shr-tag-h1 (cont)
648   (shr-heading cont 'bold 'underline))
649
650 (defun shr-tag-h2 (cont)
651   (shr-heading cont 'bold))
652
653 (defun shr-tag-h3 (cont)
654   (shr-heading cont 'italic))
655
656 (defun shr-tag-h4 (cont)
657   (shr-heading cont))
658
659 (defun shr-tag-h5 (cont)
660   (shr-heading cont))
661
662 (defun shr-tag-h6 (cont)
663   (shr-heading cont))
664
665 (defun shr-tag-hr (cont)
666   (shr-ensure-newline)
667   (insert (make-string shr-width shr-hr-line) "\n"))
668
669 ;;; Table rendering algorithm.
670
671 ;; Table rendering is the only complicated thing here.  We do this by
672 ;; first counting how many TDs there are in each TR, and registering
673 ;; how wide they think they should be ("width=45%", etc).  Then we
674 ;; render each TD separately (this is done in temporary buffers, so
675 ;; that we can use all the rendering machinery as if we were in the
676 ;; main buffer).  Now we know how much space each TD really takes, so
677 ;; we then render everything again with the new widths, and finally
678 ;; insert all these boxes into the main buffer.
679 (defun shr-tag-table-1 (cont)
680   (setq cont (or (cdr (assq 'tbody cont))
681                  cont))
682   (let* ((shr-inhibit-images t)
683          (shr-table-depth (1+ shr-table-depth))
684          (shr-kinsoku-shorten t)
685          ;; Find all suggested widths.
686          (columns (shr-column-specs cont))
687          ;; Compute how many characters wide each TD should be.
688          (suggested-widths (shr-pro-rate-columns columns))
689          ;; Do a "test rendering" to see how big each TD is (this can
690          ;; be smaller (if there's little text) or bigger (if there's
691          ;; unbreakable text).
692          (sketch (shr-make-table cont suggested-widths))
693          (sketch-widths (shr-table-widths sketch suggested-widths)))
694     ;; This probably won't work very well.
695     (when (> (+ (loop for width across sketch-widths
696                       summing (1+ width))
697                 shr-indentation 1)
698              (frame-width))
699       (setq truncate-lines t))
700     ;; Then render the table again with these new "hard" widths.
701     (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
702   ;; Finally, insert all the images after the table.  The Emacs buffer
703   ;; model isn't strong enough to allow us to put the images actually
704   ;; into the tables.
705   (when (zerop shr-table-depth)
706     (dolist (elem (shr-find-elements cont 'img))
707       (shr-tag-img (cdr elem)))))
708
709 (defun shr-tag-table (cont)
710   (shr-ensure-paragraph)
711   (let* ((caption (cdr (assq 'caption cont)))
712          (header (cdr (assq 'thead cont)))
713          (body (or (cdr (assq 'tbody cont)) cont))
714          (footer (cdr (assq 'tfoot cont)))
715          (nheader (if header (shr-max-columns header)))
716          (nbody (if body (shr-max-columns body)))
717          (nfooter (if footer (shr-max-columns footer))))
718     (shr-tag-table-1
719      (nconc
720       (if caption `((tr (td ,@caption))))
721       (if header
722           (if footer
723               ;; hader + body + footer
724               (if (= nheader nbody)
725                   (if (= nbody nfooter)
726                       `((tr (td (table (tbody ,@header ,@body ,@footer)))))
727                     (nconc `((tr (td (table (tbody ,@header ,@body)))))
728                            (if (= nfooter 1)
729                                footer
730                              `((tr (td (table (tbody ,@footer))))))))
731                 (nconc `((tr (td (table (tbody ,@header)))))
732                        (if (= nbody nfooter)
733                            `((tr (td (table (tbody ,@body ,@footer)))))
734                          (nconc `((tr (td (table (tbody ,@body)))))
735                                 (if (= nfooter 1)
736                                     footer
737                                   `((tr (td (table (tbody ,@footer))))))))))
738             ;; header + body
739             (if (= nheader nbody)
740                 `((tr (td (table (tbody ,@header ,@body)))))
741               (if (= nheader 1)
742                   `(,@header (tr (td (table (tbody ,@body)))))
743                 `((tr (td (table (tbody ,@header))))
744                   (tr (td (table (tbody ,@body))))))))
745         (if footer
746             ;; body + footer
747             (if (= nbody nfooter)
748                 `((tr (td (table (tbody ,@body ,@footer)))))
749               (nconc `((tr (td (table (tbody ,@body)))))
750                      (if (= nfooter 1)
751                          footer
752                        `((tr (td (table (tbody ,@footer))))))))
753           (if caption
754               `((tr (td (table (tbody ,@body)))))
755             body)))))))
756
757 (defun shr-find-elements (cont type)
758   (let (result)
759     (dolist (elem cont)
760       (cond ((eq (car elem) type)
761              (push elem result))
762             ((consp (cdr elem))
763              (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
764     (nreverse result)))
765
766 (defun shr-insert-table (table widths)
767   (shr-insert-table-ruler widths)
768   (dolist (row table)
769     (let ((start (point))
770           (height (let ((max 0))
771                     (dolist (column row)
772                       (setq max (max max (cadr column))))
773                     max)))
774       (dotimes (i height)
775         (shr-indent)
776         (insert "|\n"))
777       (dolist (column row)
778         (goto-char start)
779         (let ((lines (nth 2 column))
780               (overlay-lines (nth 3 column))
781               overlay overlay-line)
782           (dolist (line lines)
783             (setq overlay-line (pop overlay-lines))
784             (end-of-line)
785             (insert line "|")
786             (dolist (overlay overlay-line)
787               (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
788                                      (- (point) (nth 1 overlay) 1)))
789                     (properties (nth 2 overlay)))
790                 (while properties
791                   (overlay-put o (pop properties) (pop properties)))))
792             (forward-line 1))
793           ;; Add blank lines at padding at the bottom of the TD,
794           ;; possibly.
795           (dotimes (i (- height (length lines)))
796             (end-of-line)
797             (insert (make-string (string-width (car lines)) ? ) "|")
798             (forward-line 1)))))
799     (shr-insert-table-ruler widths)))
800
801 (defun shr-insert-table-ruler (widths)
802   (when (and (bolp)
803              (> shr-indentation 0))
804     (shr-indent))
805   (insert shr-table-corner)
806   (dotimes (i (length widths))
807     (insert (make-string (aref widths i) shr-table-line) shr-table-corner))
808   (insert "\n"))
809
810 (defun shr-table-widths (table suggested-widths)
811   (let* ((length (length suggested-widths))
812          (widths (make-vector length 0))
813          (natural-widths (make-vector length 0)))
814     (dolist (row table)
815       (let ((i 0))
816         (dolist (column row)
817           (aset widths i (max (aref widths i)
818                               (car column)))
819           (aset natural-widths i (max (aref natural-widths i)
820                                       (cadr column)))
821           (setq i (1+ i)))))
822     (let ((extra (- (apply '+ (append suggested-widths nil))
823                     (apply '+ (append widths nil))))
824           (expanded-columns 0))
825       (when (> extra 0)
826         (dotimes (i length)
827           ;; If the natural width is wider than the rendered width, we
828           ;; want to allow the column to expand.
829           (when (> (aref natural-widths i) (aref widths i))
830             (setq expanded-columns (1+ expanded-columns))))
831         (dotimes (i length)
832           (when (> (aref natural-widths i) (aref widths i))
833             (aset widths i (min
834                             (1+ (aref natural-widths i))
835                             (+ (/ extra expanded-columns)
836                                (aref widths i))))))))
837     widths))
838
839 (defun shr-make-table (cont widths &optional fill)
840   (let ((trs nil))
841     (dolist (row cont)
842       (when (eq (car row) 'tr)
843         (let ((tds nil)
844               (columns (cdr row))
845               (i 0)
846               column)
847           (while (< i (length widths))
848             (setq column (pop columns))
849             (when (or (memq (car column) '(td th))
850                       (null column))
851               (push (shr-render-td (cdr column) (aref widths i) fill)
852                     tds)
853               (setq i (1+ i))))
854           (push (nreverse tds) trs))))
855     (nreverse trs)))
856
857 (defun shr-render-td (cont width fill)
858   (with-temp-buffer
859     (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
860       (if cache
861           (insert cache)
862         (let ((shr-width width)
863               (shr-indentation 0))
864           (shr-generic cont))
865         (delete-region
866          (point)
867          (+ (point)
868             (skip-chars-backward " \t\n")))
869         (push (cons (cons width cont) (buffer-string))
870               shr-content-cache)))
871     (goto-char (point-min))
872     (let ((max 0))
873       (while (not (eobp))
874         (end-of-line)
875         (setq max (max max (current-column)))
876         (forward-line 1))
877       (when fill
878         (goto-char (point-min))
879         ;; If the buffer is totally empty, then put a single blank
880         ;; line here.
881         (if (zerop (buffer-size))
882             (insert (make-string width ? ))
883           ;; Otherwise, fill the buffer.
884           (while (not (eobp))
885             (end-of-line)
886             (when (> (- width (current-column)) 0)
887               (insert (make-string (- width (current-column)) ? )))
888             (forward-line 1))))
889       (if fill
890           (list max
891                 (count-lines (point-min) (point-max))
892                 (split-string (buffer-string) "\n")
893                 (shr-collect-overlays))
894         (list max
895               (shr-natural-width))))))
896
897 (defun shr-natural-width ()
898   (goto-char (point-min))
899   (let ((current 0)
900         (max 0))
901     (while (not (eobp))
902       (end-of-line)
903       (setq current (+ current (current-column)))
904       (unless (get-text-property (point) 'shr-break)
905         (setq max (max max current)
906               current 0))
907       (forward-line 1))
908     max))
909
910 (defun shr-collect-overlays ()
911   (save-excursion
912     (goto-char (point-min))
913     (let ((overlays nil))
914       (while (not (eobp))
915         (push (shr-overlays-in-region (point) (line-end-position))
916               overlays)
917         (forward-line 1))
918       (nreverse overlays))))
919
920 (defun shr-overlays-in-region (start end)
921   (let (result)
922     (dolist (overlay (overlays-in start end))
923       (push (list (if (> start (overlay-start overlay))
924                       (- end start)
925                     (- end (overlay-start overlay)))
926                   (if (< end (overlay-end overlay))
927                       0
928                     (- end (overlay-end overlay)))
929                   (overlay-properties overlay))
930             result))
931     (nreverse result)))
932
933 (defun shr-pro-rate-columns (columns)
934   (let ((total-percentage 0)
935         (widths (make-vector (length columns) 0)))
936     (dotimes (i (length columns))
937       (setq total-percentage (+ total-percentage (aref columns i))))
938     (setq total-percentage (/ 1.0 total-percentage))
939     (dotimes (i (length columns))
940       (aset widths i (max (truncate (* (aref columns i)
941                                        total-percentage
942                                        (- shr-width (1+ (length columns)))))
943                           10)))
944     widths))
945
946 ;; Return a summary of the number and shape of the TDs in the table.
947 (defun shr-column-specs (cont)
948   (let ((columns (make-vector (shr-max-columns cont) 1)))
949     (dolist (row cont)
950       (when (eq (car row) 'tr)
951         (let ((i 0))
952           (dolist (column (cdr row))
953             (when (memq (car column) '(td th))
954               (let ((width (cdr (assq :width (cdr column)))))
955                 (when (and width
956                            (string-match "\\([0-9]+\\)%" width))
957                   (aset columns i
958                         (/ (string-to-number (match-string 1 width))
959                            100.0))))
960               (setq i (1+ i)))))))
961     columns))
962
963 (defun shr-count (cont elem)
964   (let ((i 0))
965     (dolist (sub cont)
966       (when (eq (car sub) elem)
967         (setq i (1+ i))))
968     i))
969
970 (defun shr-max-columns (cont)
971   (let ((max 0))
972     (dolist (row cont)
973       (when (eq (car row) 'tr)
974         (setq max (max max (+ (shr-count (cdr row) 'td)
975                               (shr-count (cdr row) 'th))))))
976     max))
977
978 (provide 'shr)
979
980 ;;; shr.el ends here