* src/eval.c (Fcalled_interactively_p): Doc fix. (Bug#11747)
[emacs.git] / lisp / gnus / shr.el
blob0d9b052f42514a174224f3fb3b17fb561bdbf820
1 ;;; shr.el --- Simple HTML Renderer
3 ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: html
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
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.
31 ;;; Code:
33 (eval-when-compile (require 'cl))
34 (require 'browse-url)
36 (defgroup shr nil
37 "Simple HTML Renderer"
38 :version "24.1"
39 :group 'mail)
41 (defcustom shr-max-image-proportion 0.9
42 "How big pictures displayed are in relation to the window they're in.
43 A value of 0.7 means that they are allowed to take up 70% of the
44 width and height of the window. If they are larger than this,
45 and Emacs supports it, then the images will be rescaled down to
46 fit these criteria."
47 :version "24.1"
48 :group 'shr
49 :type 'float)
51 (defcustom shr-blocked-images nil
52 "Images that have URLs matching this regexp will be blocked."
53 :version "24.1"
54 :group 'shr
55 :type 'regexp)
57 (defcustom shr-table-horizontal-line ?\s
58 "Character used to draw horizontal table lines."
59 :group 'shr
60 :type 'character)
62 (defcustom shr-table-vertical-line ?\s
63 "Character used to draw vertical table lines."
64 :group 'shr
65 :type 'character)
67 (defcustom shr-table-corner ?\s
68 "Character used to draw table corners."
69 :group 'shr
70 :type 'character)
72 (defcustom shr-hr-line ?-
73 "Character used to draw hr lines."
74 :group 'shr
75 :type 'character)
77 (defcustom shr-width fill-column
78 "Frame width to use for rendering.
79 May either be an integer specifying a fixed width in characters,
80 or nil, meaning that the full width of the window should be
81 used."
82 :type '(choice (integer :tag "Fixed width in characters")
83 (const :tag "Use the width of the window" nil))
84 :group 'shr)
86 (defvar shr-content-function nil
87 "If bound, this should be a function that will return the content.
88 This is used for cid: URLs, and the function is called with the
89 cid: URL as the argument.")
91 (defvar shr-put-image-function 'shr-put-image
92 "Function called to put image and alt string.")
94 (defface shr-strike-through '((t (:strike-through t)))
95 "Font for <s> elements."
96 :group 'shr)
98 (defface shr-link
99 '((t (:inherit link)))
100 "Font for link elements."
101 :group 'shr)
103 ;;; Internal variables.
105 (defvar shr-folding-mode nil)
106 (defvar shr-state nil)
107 (defvar shr-start nil)
108 (defvar shr-indentation 0)
109 (defvar shr-inhibit-images nil)
110 (defvar shr-list-mode nil)
111 (defvar shr-content-cache nil)
112 (defvar shr-kinsoku-shorten nil)
113 (defvar shr-table-depth 0)
114 (defvar shr-stylesheet nil)
115 (defvar shr-base nil)
116 (defvar shr-ignore-cache nil)
118 (defvar shr-map
119 (let ((map (make-sparse-keymap)))
120 (define-key map "a" 'shr-show-alt-text)
121 (define-key map "i" 'shr-browse-image)
122 (define-key map "I" 'shr-insert-image)
123 (define-key map "u" 'shr-copy-url)
124 (define-key map "v" 'shr-browse-url)
125 (define-key map "o" 'shr-save-contents)
126 (define-key map "\r" 'shr-browse-url)
127 map))
129 ;; Public functions and commands.
131 (defun shr-visit-file (file)
132 "Parse FILE as an HTML document, and render it in a new buffer."
133 (interactive "fHTML file name: ")
134 (pop-to-buffer "*html*")
135 (erase-buffer)
136 (shr-insert-document
137 (with-temp-buffer
138 (insert-file-contents file)
139 (libxml-parse-html-region (point-min) (point-max))))
140 (goto-char (point-min)))
142 ;;;###autoload
143 (defun shr-insert-document (dom)
144 "Render the parsed document DOM into the current buffer.
145 DOM should be a parse tree as generated by
146 `libxml-parse-html-region' or similar."
147 (setq shr-content-cache nil)
148 (let ((start (point))
149 (shr-state nil)
150 (shr-start nil)
151 (shr-base nil)
152 (shr-width (or shr-width (window-width))))
153 (shr-descend (shr-transform-dom dom))
154 (shr-remove-trailing-whitespace start (point))))
156 (defun shr-remove-trailing-whitespace (start end)
157 (let ((width (window-width)))
158 (save-restriction
159 (narrow-to-region start end)
160 (goto-char start)
161 (while (not (eobp))
162 (end-of-line)
163 (when (> (shr-previous-newline-padding-width (current-column)) width)
164 (dolist (overlay (overlays-at (point)))
165 (when (overlay-get overlay 'before-string)
166 (overlay-put overlay 'before-string nil))))
167 (forward-line 1)))))
169 (defun shr-copy-url ()
170 "Copy the URL under point to the kill ring.
171 If called twice, then try to fetch the URL and see whether it
172 redirects somewhere else."
173 (interactive)
174 (let ((url (get-text-property (point) 'shr-url)))
175 (cond
176 ((not url)
177 (message "No URL under point"))
178 ;; Resolve redirected URLs.
179 ((equal url (car kill-ring))
180 (url-retrieve
182 (lambda (a)
183 (when (and (consp a)
184 (eq (car a) :redirect))
185 (with-temp-buffer
186 (insert (cadr a))
187 (goto-char (point-min))
188 ;; Remove common tracking junk from the URL.
189 (when (re-search-forward ".utm_.*" nil t)
190 (replace-match "" t t))
191 (message "Copied %s" (buffer-string))
192 (copy-region-as-kill (point-min) (point-max)))))
193 nil t))
194 ;; Copy the URL to the kill ring.
196 (with-temp-buffer
197 (insert url)
198 (copy-region-as-kill (point-min) (point-max))
199 (message "Copied %s" url))))))
201 (defun shr-show-alt-text ()
202 "Show the ALT text of the image under point."
203 (interactive)
204 (let ((text (get-text-property (point) 'shr-alt)))
205 (if (not text)
206 (message "No image under point")
207 (message "%s" text))))
209 (defun shr-browse-image (&optional copy-url)
210 "Browse the image under point.
211 If COPY-URL (the prefix if called interactively) is non-nil, copy
212 the URL of the image to the kill buffer instead."
213 (interactive "P")
214 (let ((url (get-text-property (point) 'image-url)))
215 (cond
216 ((not url)
217 (message "No image under point"))
218 (copy-url
219 (with-temp-buffer
220 (insert url)
221 (copy-region-as-kill (point-min) (point-max))
222 (message "Copied %s" url)))
224 (message "Browsing %s..." url)
225 (browse-url url)))))
227 (defun shr-insert-image ()
228 "Insert the image under point into the buffer."
229 (interactive)
230 (let ((url (get-text-property (point) 'image-url)))
231 (if (not url)
232 (message "No image under point")
233 (message "Inserting %s..." url)
234 (url-retrieve url 'shr-image-fetched
235 (list (current-buffer) (1- (point)) (point-marker))
236 t t))))
238 ;;; Utility functions.
240 (defun shr-transform-dom (dom)
241 (let ((result (list (pop dom))))
242 (dolist (arg (pop dom))
243 (push (cons (intern (concat ":" (symbol-name (car arg))) obarray)
244 (cdr arg))
245 result))
246 (dolist (sub dom)
247 (if (stringp sub)
248 (push (cons 'text sub) result)
249 (push (shr-transform-dom sub) result)))
250 (nreverse result)))
252 (defun shr-descend (dom)
253 (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray))
254 (style (cdr (assq :style (cdr dom))))
255 (shr-stylesheet shr-stylesheet)
256 (start (point)))
257 (when style
258 (if (string-match "color" style)
259 (setq shr-stylesheet (nconc (shr-parse-style style)
260 shr-stylesheet))
261 (setq style nil)))
262 (if (fboundp function)
263 (funcall function (cdr dom))
264 (shr-generic (cdr dom)))
265 ;; If style is set, then this node has set the color.
266 (when style
267 (shr-colorize-region start (point)
268 (cdr (assq 'color shr-stylesheet))
269 (cdr (assq 'background-color shr-stylesheet))))))
271 (defun shr-generic (cont)
272 (dolist (sub cont)
273 (cond
274 ((eq (car sub) 'text)
275 (shr-insert (cdr sub)))
276 ((listp (cdr sub))
277 (shr-descend sub)))))
279 (defmacro shr-char-breakable-p (char)
280 "Return non-nil if a line can be broken before and after CHAR."
281 `(aref fill-find-break-point-function-table ,char))
282 (defmacro shr-char-nospace-p (char)
283 "Return non-nil if no space is required before and after CHAR."
284 `(aref fill-nospace-between-words-table ,char))
286 ;; KINSOKU is a Japanese word meaning a rule that should not be violated.
287 ;; In Emacs, it is a term used for characters, e.g. punctuation marks,
288 ;; parentheses, and so on, that should not be placed in the beginning
289 ;; of a line or the end of a line.
290 (defmacro shr-char-kinsoku-bol-p (char)
291 "Return non-nil if a line ought not to begin with CHAR."
292 `(aref (char-category-set ,char) ?>))
293 (defmacro shr-char-kinsoku-eol-p (char)
294 "Return non-nil if a line ought not to end with CHAR."
295 `(aref (char-category-set ,char) ?<))
296 (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35))
297 (load "kinsoku" nil t))
299 (defun shr-insert (text)
300 (when (and (eq shr-state 'image)
301 (not (string-match "\\`[ \t\n]+\\'" text)))
302 (insert "\n")
303 (setq shr-state nil))
304 (cond
305 ((eq shr-folding-mode 'none)
306 (insert text))
308 (when (and (string-match "\\`[ \t\n]" text)
309 (not (bolp))
310 (not (eq (char-after (1- (point))) ? )))
311 (insert " "))
312 (dolist (elem (split-string text))
313 (when (and (bolp)
314 (> shr-indentation 0))
315 (shr-indent))
316 ;; No space is needed behind a wide character categorized as
317 ;; kinsoku-bol, between characters both categorized as nospace,
318 ;; or at the beginning of a line.
319 (let (prev)
320 (when (and (> (current-column) shr-indentation)
321 (eq (preceding-char) ? )
322 (or (= (line-beginning-position) (1- (point)))
323 (and (shr-char-breakable-p
324 (setq prev (char-after (- (point) 2))))
325 (shr-char-kinsoku-bol-p prev))
326 (and (shr-char-nospace-p prev)
327 (shr-char-nospace-p (aref elem 0)))))
328 (delete-char -1)))
329 ;; The shr-start is a special variable that is used to pass
330 ;; upwards the first point in the buffer where the text really
331 ;; starts.
332 (unless shr-start
333 (setq shr-start (point)))
334 (insert elem)
335 (setq shr-state nil)
336 (let (found)
337 (while (and (> (current-column) shr-width)
338 (progn
339 (setq found (shr-find-fill-point))
340 (not (eolp))))
341 (when (eq (preceding-char) ? )
342 (delete-char -1))
343 (insert "\n")
344 (unless found
345 ;; No space is needed at the beginning of a line.
346 (when (eq (following-char) ? )
347 (delete-char 1)))
348 (when (> shr-indentation 0)
349 (shr-indent))
350 (end-of-line))
351 (insert " ")))
352 (unless (string-match "[ \t\n]\\'" text)
353 (delete-char -1)))))
355 (defun shr-find-fill-point ()
356 (when (> (move-to-column shr-width) shr-width)
357 (backward-char 1))
358 (let ((bp (point))
359 failed)
360 (while (not (or (setq failed (= (current-column) shr-indentation))
361 (eq (preceding-char) ? )
362 (eq (following-char) ? )
363 (shr-char-breakable-p (preceding-char))
364 (shr-char-breakable-p (following-char))
365 (if (eq (preceding-char) ?')
366 (not (memq (char-after (- (point) 2))
367 (list nil ?\n ? )))
368 (and (shr-char-kinsoku-bol-p (preceding-char))
369 (shr-char-breakable-p (following-char))
370 (not (shr-char-kinsoku-bol-p (following-char)))))
371 (shr-char-kinsoku-eol-p (following-char))))
372 (backward-char 1))
373 (if (and (not (or failed (eolp)))
374 (eq (preceding-char) ?'))
375 (while (not (or (setq failed (eolp))
376 (eq (following-char) ? )
377 (shr-char-breakable-p (following-char))
378 (shr-char-kinsoku-eol-p (following-char))))
379 (forward-char 1)))
380 (if failed
381 ;; There's no breakable point, so we give it up.
382 (let (found)
383 (goto-char bp)
384 (unless shr-kinsoku-shorten
385 (while (and (setq found (re-search-forward
386 "\\(\\c>\\)\\| \\|\\c<\\|\\c|"
387 (line-end-position) 'move))
388 (eq (preceding-char) ?')))
389 (if (and found (not (match-beginning 1)))
390 (goto-char (match-beginning 0)))))
392 (eolp)
393 ;; Don't put kinsoku-bol characters at the beginning of a line,
394 ;; or kinsoku-eol characters at the end of a line.
395 (cond
396 (shr-kinsoku-shorten
397 (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
398 (shr-char-kinsoku-eol-p (preceding-char)))
399 (backward-char 1))
400 (when (setq failed (= (current-column) shr-indentation))
401 ;; There's no breakable point that doesn't violate kinsoku,
402 ;; so we look for the second best position.
403 (while (and (progn
404 (forward-char 1)
405 (<= (current-column) shr-width))
406 (progn
407 (setq bp (point))
408 (shr-char-kinsoku-eol-p (following-char)))))
409 (goto-char bp)))
410 ((shr-char-kinsoku-eol-p (preceding-char))
411 (if (shr-char-kinsoku-eol-p (following-char))
412 ;; There are consecutive kinsoku-eol characters.
413 (setq failed t)
414 (let ((count 4))
415 (while
416 (progn
417 (backward-char 1)
418 (and (> (setq count (1- count)) 0)
419 (not (memq (preceding-char) (list ?\C-@ ?\n ? )))
420 (or (shr-char-kinsoku-eol-p (preceding-char))
421 (shr-char-kinsoku-bol-p (following-char)))))))
422 (if (setq failed (= (current-column) shr-indentation))
423 ;; There's no breakable point that doesn't violate kinsoku,
424 ;; so we go to the second best position.
425 (if (looking-at "\\(\\c<+\\)\\c<")
426 (goto-char (match-end 1))
427 (forward-char 1)))))
429 (if (shr-char-kinsoku-bol-p (preceding-char))
430 ;; There are consecutive kinsoku-bol characters.
431 (setq failed t)
432 (let ((count 4))
433 (while (and (>= (setq count (1- count)) 0)
434 (shr-char-kinsoku-bol-p (following-char))
435 (shr-char-breakable-p (following-char)))
436 (forward-char 1))))))
437 (when (eq (following-char) ? )
438 (forward-char 1))))
439 (not failed)))
441 (defun shr-expand-url (url)
442 (cond
443 ;; Absolute URL.
444 ((or (not url)
445 (string-match "\\`[a-z]*:" url)
446 (not shr-base))
447 url)
448 ((and (string-match "\\`//" url)
449 (string-match "\\`[a-z]*:" shr-base))
450 (concat (match-string 0 shr-base) url))
451 ((and (not (string-match "/\\'" shr-base))
452 (not (string-match "\\`/" url)))
453 (concat shr-base "/" url))
455 (concat shr-base url))))
457 (defun shr-ensure-newline ()
458 (unless (zerop (current-column))
459 (insert "\n")))
461 (defun shr-ensure-paragraph ()
462 (unless (bobp)
463 (if (<= (current-column) shr-indentation)
464 (unless (save-excursion
465 (forward-line -1)
466 (looking-at " *$"))
467 (insert "\n"))
468 (if (save-excursion
469 (beginning-of-line)
470 (looking-at " *$"))
471 (insert "\n")
472 (insert "\n\n")))))
474 (defun shr-indent ()
475 (when (> shr-indentation 0)
476 (insert (make-string shr-indentation ? ))))
478 (defun shr-fontize-cont (cont &rest types)
479 (let (shr-start)
480 (shr-generic cont)
481 (dolist (type types)
482 (shr-add-font (or shr-start (point)) (point) type))))
484 ;; Add an overlay in the region, but avoid putting the font properties
485 ;; on blank text at the start of the line, and the newline at the end,
486 ;; to avoid ugliness.
487 (defun shr-add-font (start end type)
488 (save-excursion
489 (goto-char start)
490 (while (< (point) end)
491 (when (bolp)
492 (skip-chars-forward " "))
493 (let ((overlay (make-overlay (point) (min (line-end-position) end))))
494 (overlay-put overlay 'face type))
495 (if (< (line-end-position) end)
496 (forward-line 1)
497 (goto-char end)))))
499 (defun shr-browse-url ()
500 "Browse the URL under point."
501 (interactive)
502 (let ((url (get-text-property (point) 'shr-url)))
503 (cond
504 ((not url)
505 (message "No link under point"))
506 ((string-match "^mailto:" url)
507 (browse-url-mail url))
509 (browse-url url)))))
511 (defun shr-save-contents (directory)
512 "Save the contents from URL in a file."
513 (interactive "DSave contents of URL to directory: ")
514 (let ((url (get-text-property (point) 'shr-url)))
515 (if (not url)
516 (message "No link under point")
517 (url-retrieve (shr-encode-url url)
518 'shr-store-contents (list url directory)
519 nil t))))
521 (defun shr-store-contents (status url directory)
522 (unless (plist-get status :error)
523 (when (or (search-forward "\n\n" nil t)
524 (search-forward "\r\n\r\n" nil t))
525 (write-region (point) (point-max)
526 (expand-file-name (file-name-nondirectory url)
527 directory)))))
529 (defun shr-image-fetched (status buffer start end)
530 (let ((image-buffer (current-buffer)))
531 (when (and (buffer-name buffer)
532 (not (plist-get status :error)))
533 (url-store-in-cache image-buffer)
534 (when (or (search-forward "\n\n" nil t)
535 (search-forward "\r\n\r\n" nil t))
536 (let ((data (buffer-substring (point) (point-max))))
537 (with-current-buffer buffer
538 (save-excursion
539 (let ((alt (buffer-substring start end))
540 (inhibit-read-only t))
541 (delete-region start end)
542 (goto-char start)
543 (funcall shr-put-image-function data alt)))))))
544 (kill-buffer image-buffer)))
546 (defun shr-put-image (data alt)
547 "Put image DATA with a string ALT. Return image."
548 (if (display-graphic-p)
549 (let ((image (ignore-errors
550 (shr-rescale-image data))))
551 (when image
552 ;; When inserting big-ish pictures, put them at the
553 ;; beginning of the line.
554 (when (and (> (current-column) 0)
555 (> (car (image-size image t)) 400))
556 (insert "\n"))
557 (insert-image image (or alt "*"))
558 (when (image-animated-p image)
559 (image-animate image nil 60)))
560 image)
561 (insert alt)))
563 (defun shr-rescale-image (data)
564 (let ((image (create-image data nil t :ascent 100)))
565 (if (or (not (fboundp 'imagemagick-types))
566 (not (get-buffer-window (current-buffer))))
567 image
568 (let* ((size (image-size image t))
569 (width (car size))
570 (height (cdr size))
571 (edges (window-inside-pixel-edges
572 (get-buffer-window (current-buffer))))
573 (window-width (truncate (* shr-max-image-proportion
574 (- (nth 2 edges) (nth 0 edges)))))
575 (window-height (truncate (* shr-max-image-proportion
576 (- (nth 3 edges) (nth 1 edges)))))
577 scaled-image)
578 (when (> height window-height)
579 (setq image (or (create-image data 'imagemagick t
580 :height window-height
581 :ascent 100)
582 image))
583 (setq size (image-size image t)))
584 (when (> (car size) window-width)
585 (setq image (or
586 (create-image data 'imagemagick t
587 :width window-width
588 :ascent 100)
589 image)))
590 image))))
592 ;; url-cache-extract autoloads url-cache.
593 (declare-function url-cache-create-filename "url-cache" (url))
594 (autoload 'mm-disable-multibyte "mm-util")
595 (autoload 'browse-url-mail "browse-url")
597 (defun shr-get-image-data (url)
598 "Get image data for URL.
599 Return a string with image data."
600 (with-temp-buffer
601 (mm-disable-multibyte)
602 (when (ignore-errors
603 (url-cache-extract (url-cache-create-filename (shr-encode-url url)))
605 (when (or (search-forward "\n\n" nil t)
606 (search-forward "\r\n\r\n" nil t))
607 (buffer-substring (point) (point-max))))))
609 (defun shr-image-displayer (content-function)
610 "Return a function to display an image.
611 CONTENT-FUNCTION is a function to retrieve an image for a cid url that
612 is an argument. The function to be returned takes three arguments URL,
613 START, and END. Note that START and END should be markers."
614 `(lambda (url start end)
615 (when url
616 (if (string-match "\\`cid:" url)
617 ,(when content-function
618 `(let ((image (funcall ,content-function
619 (substring url (match-end 0)))))
620 (when image
621 (goto-char start)
622 (funcall shr-put-image-function
623 image (buffer-substring start end))
624 (delete-region (point) end))))
625 (url-retrieve url 'shr-image-fetched
626 (list (current-buffer) start end)
627 t t)))))
629 (defun shr-heading (cont &rest types)
630 (shr-ensure-paragraph)
631 (apply #'shr-fontize-cont cont types)
632 (shr-ensure-paragraph))
634 (autoload 'widget-convert-button "wid-edit")
636 (defun shr-urlify (start url &optional title)
637 (widget-convert-button
638 'url-link start (point)
639 :help-echo (if title (format "%s (%s)" url title) url)
640 :keymap shr-map
641 url)
642 (shr-add-font start (point) 'shr-link)
643 (put-text-property start (point) 'shr-url url))
645 (defun shr-encode-url (url)
646 "Encode URL."
647 (browse-url-url-encode-chars url "[)$ ]"))
649 (autoload 'shr-color-visible "shr-color")
650 (autoload 'shr-color->hexadecimal "shr-color")
652 (defun shr-color-check (fg bg)
653 "Check that FG is visible on BG.
654 Returns (fg bg) with corrected values.
655 Returns nil if the colors that would be used are the default
656 ones, in case fg and bg are nil."
657 (when (or fg bg)
658 (let ((fixed (cond ((null fg) 'fg)
659 ((null bg) 'bg))))
660 ;; Convert colors to hexadecimal, or set them to default.
661 (let ((fg (or (shr-color->hexadecimal fg)
662 (frame-parameter nil 'foreground-color)))
663 (bg (or (shr-color->hexadecimal bg)
664 (frame-parameter nil 'background-color))))
665 (cond ((eq fixed 'bg)
666 ;; Only return the new fg
667 (list nil (cadr (shr-color-visible bg fg t))))
668 ((eq fixed 'fg)
669 ;; Invert args and results and return only the new bg
670 (list (cadr (shr-color-visible fg bg t)) nil))
672 (shr-color-visible bg fg)))))))
674 (defun shr-colorize-region (start end fg &optional bg)
675 (when (or fg bg)
676 (let ((new-colors (shr-color-check fg bg)))
677 (when new-colors
678 (when fg
679 (shr-put-color start end :foreground (cadr new-colors)))
680 (when bg
681 (shr-put-color start end :background (car new-colors))))
682 new-colors)))
684 ;; Put a color in the region, but avoid putting colors on blank
685 ;; text at the start of the line, and the newline at the end, to avoid
686 ;; ugliness. Also, don't overwrite any existing color information,
687 ;; since this can be called recursively, and we want the "inner" color
688 ;; to win.
689 (defun shr-put-color (start end type color)
690 (save-excursion
691 (goto-char start)
692 (while (< (point) end)
693 (when (and (bolp)
694 (not (eq type :background)))
695 (skip-chars-forward " "))
696 (when (> (line-end-position) (point))
697 (shr-put-color-1 (point) (min (line-end-position) end) type color))
698 (if (< (line-end-position) end)
699 (forward-line 1)
700 (goto-char end)))
701 (when (and (eq type :background)
702 (= shr-table-depth 0))
703 (shr-expand-newlines start end color))))
705 (defun shr-expand-newlines (start end color)
706 (save-restriction
707 ;; Skip past all white space at the start and ends.
708 (goto-char start)
709 (skip-chars-forward " \t\n")
710 (beginning-of-line)
711 (setq start (point))
712 (goto-char end)
713 (skip-chars-backward " \t\n")
714 (forward-line 1)
715 (setq end (point))
716 (narrow-to-region start end)
717 (let ((width (shr-buffer-width))
718 column)
719 (goto-char (point-min))
720 (while (not (eobp))
721 (end-of-line)
722 (when (and (< (setq column (current-column)) width)
723 (< (setq column (shr-previous-newline-padding-width column))
724 width))
725 (let ((overlay (make-overlay (point) (1+ (point)))))
726 (overlay-put overlay 'before-string
727 (concat
728 (mapconcat
729 (lambda (overlay)
730 (let ((string (plist-get
731 (overlay-properties overlay)
732 'before-string)))
733 (if (not string)
735 (overlay-put overlay 'before-string "")
736 string)))
737 (overlays-at (point))
739 (propertize (make-string (- width column) ? )
740 'face (list :background color))))))
741 (forward-line 1)))))
743 (defun shr-previous-newline-padding-width (width)
744 (let ((overlays (overlays-at (point)))
745 (previous-width 0))
746 (if (null overlays)
747 width
748 (dolist (overlay overlays)
749 (setq previous-width
750 (+ previous-width
751 (length (plist-get (overlay-properties overlay)
752 'before-string)))))
753 (+ width previous-width))))
755 (defun shr-put-color-1 (start end type color)
756 (let* ((old-props (get-text-property start 'face))
757 (do-put (and (listp old-props)
758 (not (memq type old-props))))
759 change)
760 (while (< start end)
761 (setq change (next-single-property-change start 'face nil end))
762 (when do-put
763 (put-text-property start change 'face
764 (nconc (list type color) old-props)))
765 (setq old-props (get-text-property change 'face))
766 (setq do-put (and (listp old-props)
767 (not (memq type old-props))))
768 (setq start change))
769 (when (and do-put
770 (> end start))
771 (put-text-property start end 'face
772 (nconc (list type color old-props))))))
774 ;;; Tag-specific rendering rules.
776 (defun shr-tag-body (cont)
777 (let* ((start (point))
778 (fgcolor (cdr (or (assq :fgcolor cont)
779 (assq :text cont))))
780 (bgcolor (cdr (assq :bgcolor cont)))
781 (shr-stylesheet (list (cons 'color fgcolor)
782 (cons 'background-color bgcolor))))
783 (shr-generic cont)
784 (shr-colorize-region start (point) fgcolor bgcolor)))
786 (defun shr-tag-style (cont)
789 (defun shr-tag-script (cont)
792 (defun shr-tag-comment (cont)
795 (defun shr-tag-sup (cont)
796 (let ((start (point)))
797 (shr-generic cont)
798 (put-text-property start (point) 'display '(raise 0.5))))
800 (defun shr-tag-sub (cont)
801 (let ((start (point)))
802 (shr-generic cont)
803 (put-text-property start (point) 'display '(raise -0.5))))
805 (defun shr-tag-label (cont)
806 (shr-generic cont)
807 (shr-ensure-paragraph))
809 (defun shr-tag-p (cont)
810 (shr-ensure-paragraph)
811 (shr-indent)
812 (shr-generic cont)
813 (shr-ensure-paragraph))
815 (defun shr-tag-div (cont)
816 (shr-ensure-newline)
817 (shr-indent)
818 (shr-generic cont)
819 (shr-ensure-newline))
821 (defun shr-tag-s (cont)
822 (shr-fontize-cont cont 'shr-strike-through))
824 (defun shr-tag-del (cont)
825 (shr-fontize-cont cont 'shr-strike-through))
827 (defun shr-tag-b (cont)
828 (shr-fontize-cont cont 'bold))
830 (defun shr-tag-i (cont)
831 (shr-fontize-cont cont 'italic))
833 (defun shr-tag-em (cont)
834 (shr-fontize-cont cont 'bold))
836 (defun shr-tag-strong (cont)
837 (shr-fontize-cont cont 'bold))
839 (defun shr-tag-u (cont)
840 (shr-fontize-cont cont 'underline))
842 (defun shr-parse-style (style)
843 (when style
844 (save-match-data
845 (when (string-match "\n" style)
846 (setq style (replace-match " " t t style))))
847 (let ((plist nil))
848 (dolist (elem (split-string style ";"))
849 (when elem
850 (setq elem (split-string elem ":"))
851 (when (and (car elem)
852 (cadr elem))
853 (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem)))
854 (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem))))
855 (when (string-match " *!important\\'" value)
856 (setq value (substring value 0 (match-beginning 0))))
857 (push (cons (intern name obarray)
858 value)
859 plist)))))
860 plist)))
862 (defun shr-tag-base (cont)
863 (setq shr-base (cdr (assq :href cont))))
865 (defun shr-tag-a (cont)
866 (let ((url (cdr (assq :href cont)))
867 (title (cdr (assq :title cont)))
868 (start (point))
869 shr-start)
870 (shr-generic cont)
871 (shr-urlify (or shr-start start) (shr-expand-url url) title)))
873 (defun shr-tag-object (cont)
874 (let ((start (point))
875 url)
876 (dolist (elem cont)
877 (when (eq (car elem) 'embed)
878 (setq url (or url (cdr (assq :src (cdr elem))))))
879 (when (and (eq (car elem) 'param)
880 (equal (cdr (assq :name (cdr elem))) "movie"))
881 (setq url (or url (cdr (assq :value (cdr elem)))))))
882 (when url
883 (shr-insert " [multimedia] ")
884 (shr-urlify start (shr-expand-url url)))
885 (shr-generic cont)))
887 (defun shr-tag-video (cont)
888 (let ((image (cdr (assq :poster cont)))
889 (url (cdr (assq :src cont)))
890 (start (point)))
891 (shr-tag-img nil image)
892 (shr-urlify start (shr-expand-url url))))
894 (defun shr-tag-img (cont &optional url)
895 (when (or url
896 (and cont
897 (cdr (assq :src cont))))
898 (when (and (> (current-column) 0)
899 (not (eq shr-state 'image)))
900 (insert "\n"))
901 (let ((alt (cdr (assq :alt cont)))
902 (url (shr-expand-url (or url (cdr (assq :src cont))))))
903 (let ((start (point-marker)))
904 (when (zerop (length alt))
905 (setq alt "*"))
906 (cond
907 ((or (member (cdr (assq :height cont)) '("0" "1"))
908 (member (cdr (assq :width cont)) '("0" "1")))
909 ;; Ignore zero-sized or single-pixel images.
911 ((and (not shr-inhibit-images)
912 (string-match "\\`cid:" url))
913 (let ((url (substring url (match-end 0)))
914 image)
915 (if (or (not shr-content-function)
916 (not (setq image (funcall shr-content-function url))))
917 (insert alt)
918 (funcall shr-put-image-function image alt))))
919 ((or shr-inhibit-images
920 (and shr-blocked-images
921 (string-match shr-blocked-images url)))
922 (setq shr-start (point))
923 (let ((shr-state 'space))
924 (if (> (string-width alt) 8)
925 (shr-insert (truncate-string-to-width alt 8))
926 (shr-insert alt))))
927 ((and (not shr-ignore-cache)
928 (url-is-cached (shr-encode-url url)))
929 (funcall shr-put-image-function (shr-get-image-data url) alt))
931 (insert alt " ")
932 (when (and shr-ignore-cache
933 (url-is-cached (shr-encode-url url)))
934 (let ((file (url-cache-create-filename (shr-encode-url url))))
935 (when (file-exists-p file)
936 (delete-file file))))
937 (url-queue-retrieve
938 (shr-encode-url url) 'shr-image-fetched
939 (list (current-buffer) start (set-marker (make-marker) (1- (point))))
940 t t)))
941 (when (zerop shr-table-depth) ;; We are not in a table.
942 (put-text-property start (point) 'keymap shr-map)
943 (put-text-property start (point) 'shr-alt alt)
944 (put-text-property start (point) 'image-url url)
945 (put-text-property start (point) 'image-displayer
946 (shr-image-displayer shr-content-function))
947 (put-text-property start (point) 'help-echo alt))
948 (setq shr-state 'image)))))
950 (defun shr-tag-pre (cont)
951 (let ((shr-folding-mode 'none))
952 (shr-ensure-newline)
953 (shr-indent)
954 (shr-generic cont)
955 (shr-ensure-newline)))
957 (defun shr-tag-blockquote (cont)
958 (shr-ensure-paragraph)
959 (shr-indent)
960 (let ((shr-indentation (+ shr-indentation 4)))
961 (shr-generic cont))
962 (shr-ensure-paragraph))
964 (defun shr-tag-ul (cont)
965 (shr-ensure-paragraph)
966 (let ((shr-list-mode 'ul))
967 (shr-generic cont))
968 (shr-ensure-paragraph))
970 (defun shr-tag-ol (cont)
971 (shr-ensure-paragraph)
972 (let ((shr-list-mode 1))
973 (shr-generic cont))
974 (shr-ensure-paragraph))
976 (defun shr-tag-li (cont)
977 (shr-ensure-paragraph)
978 (shr-indent)
979 (let* ((bullet
980 (if (numberp shr-list-mode)
981 (prog1
982 (format "%d " shr-list-mode)
983 (setq shr-list-mode (1+ shr-list-mode)))
984 "* "))
985 (shr-indentation (+ shr-indentation (length bullet))))
986 (insert bullet)
987 (shr-generic cont)))
989 (defun shr-tag-br (cont)
990 (unless (bobp)
991 (insert "\n")
992 (shr-indent))
993 (shr-generic cont))
995 (defun shr-tag-h1 (cont)
996 (shr-heading cont 'bold 'underline))
998 (defun shr-tag-h2 (cont)
999 (shr-heading cont 'bold))
1001 (defun shr-tag-h3 (cont)
1002 (shr-heading cont 'italic))
1004 (defun shr-tag-h4 (cont)
1005 (shr-heading cont))
1007 (defun shr-tag-h5 (cont)
1008 (shr-heading cont))
1010 (defun shr-tag-h6 (cont)
1011 (shr-heading cont))
1013 (defun shr-tag-hr (cont)
1014 (shr-ensure-newline)
1015 (insert (make-string shr-width shr-hr-line) "\n"))
1017 (defun shr-tag-title (cont)
1018 (shr-heading cont 'bold 'underline))
1020 (defun shr-tag-font (cont)
1021 (let* ((start (point))
1022 (color (cdr (assq :color cont)))
1023 (shr-stylesheet (nconc (list (cons 'color color))
1024 shr-stylesheet)))
1025 (shr-generic cont)
1026 (when color
1027 (shr-colorize-region start (point) color
1028 (cdr (assq 'background-color shr-stylesheet))))))
1030 ;;; Table rendering algorithm.
1032 ;; Table rendering is the only complicated thing here. We do this by
1033 ;; first counting how many TDs there are in each TR, and registering
1034 ;; how wide they think they should be ("width=45%", etc). Then we
1035 ;; render each TD separately (this is done in temporary buffers, so
1036 ;; that we can use all the rendering machinery as if we were in the
1037 ;; main buffer). Now we know how much space each TD really takes, so
1038 ;; we then render everything again with the new widths, and finally
1039 ;; insert all these boxes into the main buffer.
1040 (defun shr-tag-table-1 (cont)
1041 (setq cont (or (cdr (assq 'tbody cont))
1042 cont))
1043 (let* ((shr-inhibit-images t)
1044 (shr-table-depth (1+ shr-table-depth))
1045 (shr-kinsoku-shorten t)
1046 ;; Find all suggested widths.
1047 (columns (shr-column-specs cont))
1048 ;; Compute how many characters wide each TD should be.
1049 (suggested-widths (shr-pro-rate-columns columns))
1050 ;; Do a "test rendering" to see how big each TD is (this can
1051 ;; be smaller (if there's little text) or bigger (if there's
1052 ;; unbreakable text).
1053 (sketch (shr-make-table cont suggested-widths))
1054 ;; Compute the "natural" width by setting each column to 500
1055 ;; characters and see how wide they really render.
1056 (natural (shr-make-table cont (make-vector (length columns) 500)))
1057 (sketch-widths (shr-table-widths sketch natural suggested-widths)))
1058 ;; This probably won't work very well.
1059 (when (> (+ (loop for width across sketch-widths
1060 summing (1+ width))
1061 shr-indentation 1)
1062 (frame-width))
1063 (setq truncate-lines t))
1064 ;; Then render the table again with these new "hard" widths.
1065 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
1066 ;; Finally, insert all the images after the table. The Emacs buffer
1067 ;; model isn't strong enough to allow us to put the images actually
1068 ;; into the tables.
1069 (when (zerop shr-table-depth)
1070 (dolist (elem (shr-find-elements cont 'img))
1071 (shr-tag-img (cdr elem)))))
1073 (defun shr-tag-table (cont)
1074 (shr-ensure-paragraph)
1075 (let* ((caption (cdr (assq 'caption cont)))
1076 (header (cdr (assq 'thead cont)))
1077 (body (or (cdr (assq 'tbody cont)) cont))
1078 (footer (cdr (assq 'tfoot cont)))
1079 (bgcolor (cdr (assq :bgcolor cont)))
1080 (start (point))
1081 (shr-stylesheet (nconc (list (cons 'background-color bgcolor))
1082 shr-stylesheet))
1083 (nheader (if header (shr-max-columns header)))
1084 (nbody (if body (shr-max-columns body)))
1085 (nfooter (if footer (shr-max-columns footer))))
1086 (if (and (not caption)
1087 (not header)
1088 (not (cdr (assq 'tbody cont)))
1089 (not (cdr (assq 'tr cont)))
1090 (not footer))
1091 ;; The table is totally invalid and just contains random junk.
1092 ;; Try to output it anyway.
1093 (shr-generic cont)
1094 ;; It's a real table, so render it.
1095 (shr-tag-table-1
1096 (nconc
1097 (if caption `((tr (td ,@caption))))
1098 (if header
1099 (if footer
1100 ;; hader + body + footer
1101 (if (= nheader nbody)
1102 (if (= nbody nfooter)
1103 `((tr (td (table (tbody ,@header ,@body ,@footer)))))
1104 (nconc `((tr (td (table (tbody ,@header ,@body)))))
1105 (if (= nfooter 1)
1106 footer
1107 `((tr (td (table (tbody ,@footer))))))))
1108 (nconc `((tr (td (table (tbody ,@header)))))
1109 (if (= nbody nfooter)
1110 `((tr (td (table (tbody ,@body ,@footer)))))
1111 (nconc `((tr (td (table (tbody ,@body)))))
1112 (if (= nfooter 1)
1113 footer
1114 `((tr (td (table (tbody ,@footer))))))))))
1115 ;; header + body
1116 (if (= nheader nbody)
1117 `((tr (td (table (tbody ,@header ,@body)))))
1118 (if (= nheader 1)
1119 `(,@header (tr (td (table (tbody ,@body)))))
1120 `((tr (td (table (tbody ,@header))))
1121 (tr (td (table (tbody ,@body))))))))
1122 (if footer
1123 ;; body + footer
1124 (if (= nbody nfooter)
1125 `((tr (td (table (tbody ,@body ,@footer)))))
1126 (nconc `((tr (td (table (tbody ,@body)))))
1127 (if (= nfooter 1)
1128 footer
1129 `((tr (td (table (tbody ,@footer))))))))
1130 (if caption
1131 `((tr (td (table (tbody ,@body)))))
1132 body))))))
1133 (when bgcolor
1134 (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet))
1135 bgcolor))))
1137 (defun shr-find-elements (cont type)
1138 (let (result)
1139 (dolist (elem cont)
1140 (cond ((eq (car elem) type)
1141 (push elem result))
1142 ((consp (cdr elem))
1143 (setq result (nconc (shr-find-elements (cdr elem) type) result)))))
1144 (nreverse result)))
1146 (defun shr-insert-table (table widths)
1147 (shr-insert-table-ruler widths)
1148 (dolist (row table)
1149 (let ((start (point))
1150 (height (let ((max 0))
1151 (dolist (column row)
1152 (setq max (max max (cadr column))))
1153 max)))
1154 (dotimes (i height)
1155 (shr-indent)
1156 (insert shr-table-vertical-line "\n"))
1157 (dolist (column row)
1158 (goto-char start)
1159 (let ((lines (nth 2 column))
1160 (overlay-lines (nth 3 column))
1161 overlay overlay-line)
1162 (dolist (line lines)
1163 (setq overlay-line (pop overlay-lines))
1164 (end-of-line)
1165 (insert line shr-table-vertical-line)
1166 (dolist (overlay overlay-line)
1167 (let ((o (make-overlay (- (point) (nth 0 overlay) 1)
1168 (- (point) (nth 1 overlay) 1)))
1169 (properties (nth 2 overlay)))
1170 (while properties
1171 (overlay-put o (pop properties) (pop properties)))))
1172 (forward-line 1))
1173 ;; Add blank lines at padding at the bottom of the TD,
1174 ;; possibly.
1175 (dotimes (i (- height (length lines)))
1176 (end-of-line)
1177 (let ((start (point)))
1178 (insert (make-string (string-width (car lines)) ? )
1179 shr-table-vertical-line)
1180 (when (nth 4 column)
1181 (shr-put-color start (1- (point)) :background (nth 4 column))))
1182 (forward-line 1)))))
1183 (shr-insert-table-ruler widths)))
1185 (defun shr-insert-table-ruler (widths)
1186 (when (and (bolp)
1187 (> shr-indentation 0))
1188 (shr-indent))
1189 (insert shr-table-corner)
1190 (dotimes (i (length widths))
1191 (insert (make-string (aref widths i) shr-table-horizontal-line)
1192 shr-table-corner))
1193 (insert "\n"))
1195 (defun shr-table-widths (table natural-table suggested-widths)
1196 (let* ((length (length suggested-widths))
1197 (widths (make-vector length 0))
1198 (natural-widths (make-vector length 0)))
1199 (dolist (row table)
1200 (let ((i 0))
1201 (dolist (column row)
1202 (aset widths i (max (aref widths i) column))
1203 (setq i (1+ i)))))
1204 (dolist (row natural-table)
1205 (let ((i 0))
1206 (dolist (column row)
1207 (aset natural-widths i (max (aref natural-widths i) column))
1208 (setq i (1+ i)))))
1209 (let ((extra (- (apply '+ (append suggested-widths nil))
1210 (apply '+ (append widths nil))))
1211 (expanded-columns 0))
1212 ;; We have extra, unused space, so divide this space amongst the
1213 ;; columns.
1214 (when (> extra 0)
1215 ;; If the natural width is wider than the rendered width, we
1216 ;; want to allow the column to expand.
1217 (dotimes (i length)
1218 (when (> (aref natural-widths i) (aref widths i))
1219 (setq expanded-columns (1+ expanded-columns))))
1220 (dotimes (i length)
1221 (when (> (aref natural-widths i) (aref widths i))
1222 (aset widths i (min
1223 (aref natural-widths i)
1224 (+ (/ extra expanded-columns)
1225 (aref widths i))))))))
1226 widths))
1228 (defun shr-make-table (cont widths &optional fill)
1229 (let ((trs nil))
1230 (dolist (row cont)
1231 (when (eq (car row) 'tr)
1232 (let ((tds nil)
1233 (columns (cdr row))
1234 (i 0)
1235 column)
1236 (while (< i (length widths))
1237 (setq column (pop columns))
1238 (when (or (memq (car column) '(td th))
1239 (null column))
1240 (push (shr-render-td (cdr column) (aref widths i) fill)
1241 tds)
1242 (setq i (1+ i))))
1243 (push (nreverse tds) trs))))
1244 (nreverse trs)))
1246 (defun shr-render-td (cont width fill)
1247 (with-temp-buffer
1248 (let ((bgcolor (cdr (assq :bgcolor cont)))
1249 (fgcolor (cdr (assq :fgcolor cont)))
1250 (style (cdr (assq :style cont)))
1251 (shr-stylesheet shr-stylesheet)
1252 overlays actual-colors)
1253 (when style
1254 (setq style (and (string-match "color" style)
1255 (shr-parse-style style))))
1256 (when bgcolor
1257 (setq style (nconc (list (cons 'background-color bgcolor)) style)))
1258 (when fgcolor
1259 (setq style (nconc (list (cons 'color fgcolor)) style)))
1260 (when style
1261 (setq shr-stylesheet (append style shr-stylesheet)))
1262 (let ((cache (cdr (assoc (cons width cont) shr-content-cache))))
1263 (if cache
1264 (progn
1265 (insert (car cache))
1266 (let ((end (length (car cache))))
1267 (dolist (overlay (cadr cache))
1268 (let ((new-overlay
1269 (make-overlay (1+ (- end (nth 0 overlay)))
1270 (1+ (- end (nth 1 overlay)))))
1271 (properties (nth 2 overlay)))
1272 (while properties
1273 (overlay-put new-overlay
1274 (pop properties) (pop properties)))))))
1275 (let ((shr-width width)
1276 (shr-indentation 0))
1277 (shr-descend (cons 'td cont)))
1278 ;; Delete padding at the bottom of the TDs.
1279 (delete-region
1280 (point)
1281 (progn
1282 (skip-chars-backward " \t\n")
1283 (end-of-line)
1284 (point)))
1285 (push (list (cons width cont) (buffer-string)
1286 (shr-overlays-in-region (point-min) (point-max)))
1287 shr-content-cache)))
1288 (goto-char (point-min))
1289 (let ((max 0))
1290 (while (not (eobp))
1291 (end-of-line)
1292 (setq max (max max (current-column)))
1293 (forward-line 1))
1294 (when fill
1295 (goto-char (point-min))
1296 ;; If the buffer is totally empty, then put a single blank
1297 ;; line here.
1298 (if (zerop (buffer-size))
1299 (insert (make-string width ? ))
1300 ;; Otherwise, fill the buffer.
1301 (while (not (eobp))
1302 (end-of-line)
1303 (when (> (- width (current-column)) 0)
1304 (insert (make-string (- width (current-column)) ? )))
1305 (forward-line 1)))
1306 (when style
1307 (setq actual-colors
1308 (shr-colorize-region
1309 (point-min) (point-max)
1310 (cdr (assq 'color shr-stylesheet))
1311 (cdr (assq 'background-color shr-stylesheet))))))
1312 (if fill
1313 (list max
1314 (count-lines (point-min) (point-max))
1315 (split-string (buffer-string) "\n")
1316 (shr-collect-overlays)
1317 (car actual-colors))
1318 max)))))
1320 (defun shr-buffer-width ()
1321 (goto-char (point-min))
1322 (let ((max 0))
1323 (while (not (eobp))
1324 (end-of-line)
1325 (setq max (max max (current-column)))
1326 (forward-line 1))
1327 max))
1329 (defun shr-collect-overlays ()
1330 (save-excursion
1331 (goto-char (point-min))
1332 (let ((overlays nil))
1333 (while (not (eobp))
1334 (push (shr-overlays-in-region (point) (line-end-position))
1335 overlays)
1336 (forward-line 1))
1337 (nreverse overlays))))
1339 (defun shr-overlays-in-region (start end)
1340 (let (result)
1341 (dolist (overlay (overlays-in start end))
1342 (push (list (if (> start (overlay-start overlay))
1343 (- end start)
1344 (- end (overlay-start overlay)))
1345 (if (< end (overlay-end overlay))
1347 (- end (overlay-end overlay)))
1348 (overlay-properties overlay))
1349 result))
1350 (nreverse result)))
1352 (defun shr-pro-rate-columns (columns)
1353 (let ((total-percentage 0)
1354 (widths (make-vector (length columns) 0)))
1355 (dotimes (i (length columns))
1356 (setq total-percentage (+ total-percentage (aref columns i))))
1357 (setq total-percentage (/ 1.0 total-percentage))
1358 (dotimes (i (length columns))
1359 (aset widths i (max (truncate (* (aref columns i)
1360 total-percentage
1361 (- shr-width (1+ (length columns)))))
1362 10)))
1363 widths))
1365 ;; Return a summary of the number and shape of the TDs in the table.
1366 (defun shr-column-specs (cont)
1367 (let ((columns (make-vector (shr-max-columns cont) 1)))
1368 (dolist (row cont)
1369 (when (eq (car row) 'tr)
1370 (let ((i 0))
1371 (dolist (column (cdr row))
1372 (when (memq (car column) '(td th))
1373 (let ((width (cdr (assq :width (cdr column)))))
1374 (when (and width
1375 (string-match "\\([0-9]+\\)%" width)
1376 (not (zerop (setq width (string-to-number
1377 (match-string 1 width))))))
1378 (aset columns i (/ width 100.0))))
1379 (setq i (1+ i)))))))
1380 columns))
1382 (defun shr-count (cont elem)
1383 (let ((i 0))
1384 (dolist (sub cont)
1385 (when (eq (car sub) elem)
1386 (setq i (1+ i))))
1389 (defun shr-max-columns (cont)
1390 (let ((max 0))
1391 (dolist (row cont)
1392 (when (eq (car row) 'tr)
1393 (setq max (max max (+ (shr-count (cdr row) 'td)
1394 (shr-count (cdr row) 'th))))))
1395 max))
1397 (provide 'shr)
1399 ;; Local Variables:
1400 ;; coding: iso-8859-1
1401 ;; End:
1403 ;;; shr.el ends here