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