Commit | Line | Data |
---|---|---|
367f7f81 LMI |
1 | ;;; shr.el --- Simple HTML Renderer |
2 | ||
3 | ;; Copyright (C) 2010 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
6 | ;; Keywords: html | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is free software: you can redistribute it and/or modify | |
11 | ;; it under the terms of the GNU General Public License as published by | |
12 | ;; the Free Software Foundation, either version 3 of the License, or | |
13 | ;; (at your option) any later version. | |
14 | ||
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. | |
22 | ||
23 | ;;; Commentary: | |
24 | ||
25 | ;; This package takes a HTML parse tree (as provided by | |
26 | ;; libxml-parse-html-region) and renders it in the current buffer. It | |
27 | ;; does not do CSS, JavaScript or anything advanced: It's geared | |
28 | ;; towards rendering typical short snippets of HTML, like what you'd | |
29 | ;; find in HTML email and the like. | |
30 | ||
31 | ;;; Code: | |
32 | ||
3d319c8f | 33 | (eval-when-compile (require 'cl)) |
71e691a5 G |
34 | (require 'browse-url) |
35 | ||
870409d4 G |
36 | (defgroup shr nil |
37 | "Simple HTML Renderer" | |
38 | :group 'mail) | |
39 | ||
40 | (defcustom shr-max-image-proportion 0.9 | |
41 | "How big pictures displayed are in relation to the window they're in. | |
42 | A value of 0.7 means that they are allowed to take up 70% of the | |
43 | width and height of the window. If they are larger than this, | |
44 | and Emacs supports it, then the images will be rescaled down to | |
45 | fit these criteria." | |
46 | :version "24.1" | |
47 | :group 'shr | |
48 | :type 'float) | |
49 | ||
50 | (defcustom shr-blocked-images nil | |
51 | "Images that have URLs matching this regexp will be blocked." | |
52 | :version "24.1" | |
53 | :group 'shr | |
54 | :type 'regexp) | |
55 | ||
d3098750 LMI |
56 | (defcustom shr-table-horizontal-line ?- |
57 | "Character used to draw horizontal table lines." | |
58 | :group 'shr | |
59 | :type 'character) | |
60 | ||
61 | (defcustom shr-table-vertical-line ?| | |
62 | "Character used to draw vertical table lines." | |
afba0c4b | 63 | :group 'shr |
030158f3 | 64 | :type 'character) |
afba0c4b JD |
65 | |
66 | (defcustom shr-table-corner ?+ | |
d3098750 | 67 | "Character used to draw table corners." |
6b7df8d3 | 68 | :group 'shr |
030158f3 | 69 | :type 'character) |
6b7df8d3 G |
70 | |
71 | (defcustom shr-hr-line ?- | |
d3098750 | 72 | "Character used to draw hr lines." |
afba0c4b | 73 | :group 'shr |
030158f3 | 74 | :type 'character) |
afba0c4b | 75 | |
d0e0de31 JD |
76 | (defcustom shr-width fill-column |
77 | "Frame width to use for rendering." | |
78 | :type 'integer | |
79 | :group 'shr) | |
80 | ||
130e977f LMI |
81 | (defvar shr-content-function nil |
82 | "If bound, this should be a function that will return the content. | |
83 | This is used for cid: URLs, and the function is called with the | |
84 | cid: URL as the argument.") | |
85 | ||
66627fa9 G |
86 | ;;; Internal variables. |
87 | ||
870409d4 G |
88 | (defvar shr-folding-mode nil) |
89 | (defvar shr-state nil) | |
90 | (defvar shr-start nil) | |
a41c2e6d | 91 | (defvar shr-indentation 0) |
130e977f | 92 | (defvar shr-inhibit-images nil) |
66627fa9 | 93 | (defvar shr-list-mode nil) |
3d319c8f | 94 | (defvar shr-content-cache nil) |
83ffd571 | 95 | (defvar shr-kinsoku-shorten nil) |
99e65b2d | 96 | (defvar shr-table-depth 0) |
04db63bc | 97 | (defvar shr-stylesheet nil) |
870409d4 | 98 | |
71e691a5 G |
99 | (defvar shr-map |
100 | (let ((map (make-sparse-keymap))) | |
101 | (define-key map "a" 'shr-show-alt-text) | |
102 | (define-key map "i" 'shr-browse-image) | |
103 | (define-key map "I" 'shr-insert-image) | |
104 | (define-key map "u" 'shr-copy-url) | |
105 | (define-key map "v" 'shr-browse-url) | |
cdf1fca4 | 106 | (define-key map "o" 'shr-save-contents) |
71e691a5 G |
107 | (define-key map "\r" 'shr-browse-url) |
108 | map)) | |
109 | ||
66627fa9 G |
110 | ;; Public functions and commands. |
111 | ||
112 | ;;;###autoload | |
113 | (defun shr-insert-document (dom) | |
3d319c8f | 114 | (setq shr-content-cache nil) |
66627fa9 G |
115 | (let ((shr-state nil) |
116 | (shr-start nil)) | |
117 | (shr-descend (shr-transform-dom dom)))) | |
118 | ||
119 | (defun shr-copy-url () | |
120 | "Copy the URL under point to the kill ring. | |
121 | If called twice, then try to fetch the URL and see whether it | |
122 | redirects somewhere else." | |
123 | (interactive) | |
124 | (let ((url (get-text-property (point) 'shr-url))) | |
125 | (cond | |
126 | ((not url) | |
127 | (message "No URL under point")) | |
128 | ;; Resolve redirected URLs. | |
129 | ((equal url (car kill-ring)) | |
130 | (url-retrieve | |
131 | url | |
132 | (lambda (a) | |
133 | (when (and (consp a) | |
134 | (eq (car a) :redirect)) | |
135 | (with-temp-buffer | |
136 | (insert (cadr a)) | |
137 | (goto-char (point-min)) | |
138 | ;; Remove common tracking junk from the URL. | |
139 | (when (re-search-forward ".utm_.*" nil t) | |
140 | (replace-match "" t t)) | |
141 | (message "Copied %s" (buffer-string)) | |
142 | (copy-region-as-kill (point-min) (point-max))))))) | |
143 | ;; Copy the URL to the kill ring. | |
144 | (t | |
145 | (with-temp-buffer | |
146 | (insert url) | |
147 | (copy-region-as-kill (point-min) (point-max)) | |
148 | (message "Copied %s" url)))))) | |
149 | ||
150 | (defun shr-show-alt-text () | |
151 | "Show the ALT text of the image under point." | |
152 | (interactive) | |
153 | (let ((text (get-text-property (point) 'shr-alt))) | |
154 | (if (not text) | |
155 | (message "No image under point") | |
156 | (message "%s" text)))) | |
157 | ||
158 | (defun shr-browse-image () | |
159 | "Browse the image under point." | |
160 | (interactive) | |
8b6f6573 | 161 | (let ((url (get-text-property (point) 'image-url))) |
66627fa9 G |
162 | (if (not url) |
163 | (message "No image under point") | |
164 | (message "Browsing %s..." url) | |
165 | (browse-url url)))) | |
166 | ||
3d319c8f LMI |
167 | (defun shr-insert-image () |
168 | "Insert the image under point into the buffer." | |
169 | (interactive) | |
8b6f6573 | 170 | (let ((url (get-text-property (point) 'image-url))) |
3d319c8f LMI |
171 | (if (not url) |
172 | (message "No image under point") | |
173 | (message "Inserting %s..." url) | |
174 | (url-retrieve url 'shr-image-fetched | |
175 | (list (current-buffer) (1- (point)) (point-marker)) | |
176 | t)))) | |
177 | ||
66627fa9 G |
178 | ;;; Utility functions. |
179 | ||
870409d4 G |
180 | (defun shr-transform-dom (dom) |
181 | (let ((result (list (pop dom)))) | |
182 | (dolist (arg (pop dom)) | |
183 | (push (cons (intern (concat ":" (symbol-name (car arg))) obarray) | |
184 | (cdr arg)) | |
185 | result)) | |
186 | (dolist (sub dom) | |
187 | (if (stringp sub) | |
953d41c4 | 188 | (push (cons 'text sub) result) |
870409d4 G |
189 | (push (shr-transform-dom sub) result))) |
190 | (nreverse result))) | |
191 | ||
870409d4 | 192 | (defun shr-descend (dom) |
ebe79557 LMI |
193 | (let ((function (intern (concat "shr-tag-" (symbol-name (car dom))) obarray)) |
194 | (style (cdr (assq :style (cdr dom)))) | |
04db63bc | 195 | (shr-stylesheet shr-stylesheet) |
ebe79557 | 196 | (start (point))) |
b31b26b4 G |
197 | (when style |
198 | (if (string-match "color" style) | |
199 | (setq shr-stylesheet (nconc (shr-parse-style style) | |
200 | shr-stylesheet)) | |
201 | (setq style nil))) | |
870409d4 G |
202 | (if (fboundp function) |
203 | (funcall function (cdr dom)) | |
ebe79557 | 204 | (shr-generic (cdr dom))) |
b31b26b4 G |
205 | ;; If style is set, then this node has set the color. |
206 | (when style | |
207 | (shr-colorize-region start (point) | |
208 | (cdr (assq 'color shr-stylesheet)) | |
209 | (cdr (assq 'background-color shr-stylesheet)))))) | |
870409d4 G |
210 | |
211 | (defun shr-generic (cont) | |
212 | (dolist (sub cont) | |
213 | (cond | |
953d41c4 | 214 | ((eq (car sub) 'text) |
870409d4 | 215 | (shr-insert (cdr sub))) |
a41c2e6d | 216 | ((listp (cdr sub)) |
870409d4 G |
217 | (shr-descend sub))))) |
218 | ||
ed797193 G |
219 | (defmacro shr-char-breakable-p (char) |
220 | "Return non-nil if a line can be broken before and after CHAR." | |
221 | `(aref fill-find-break-point-function-table ,char)) | |
222 | (defmacro shr-char-nospace-p (char) | |
223 | "Return non-nil if no space is required before and after CHAR." | |
224 | `(aref fill-nospace-between-words-table ,char)) | |
225 | ||
226 | ;; KINSOKU is a Japanese word meaning a rule that should not be violated. | |
227 | ;; In Emacs, it is a term used for characters, e.g. punctuation marks, | |
228 | ;; parentheses, and so on, that should not be placed in the beginning | |
229 | ;; of a line or the end of a line. | |
230 | (defmacro shr-char-kinsoku-bol-p (char) | |
231 | "Return non-nil if a line ought not to begin with CHAR." | |
232 | `(aref (char-category-set ,char) ?>)) | |
233 | (defmacro shr-char-kinsoku-eol-p (char) | |
234 | "Return non-nil if a line ought not to end with CHAR." | |
235 | `(aref (char-category-set ,char) ?<)) | |
236 | (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35)) | |
237 | (load "kinsoku" nil t)) | |
238 | ||
66627fa9 | 239 | (defun shr-insert (text) |
6b7df8d3 G |
240 | (when (and (eq shr-state 'image) |
241 | (not (string-match "\\`[ \t\n]+\\'" text))) | |
66627fa9 G |
242 | (insert "\n") |
243 | (setq shr-state nil)) | |
244 | (cond | |
245 | ((eq shr-folding-mode 'none) | |
246 | (insert text)) | |
247 | (t | |
73db8b08 KY |
248 | (when (and (string-match "\\`[ \t\n]" text) |
249 | (not (bolp)) | |
250 | (not (eq (char-after (1- (point))) ? ))) | |
251 | (insert " ")) | |
252 | (dolist (elem (split-string text)) | |
253 | (when (and (bolp) | |
254 | (> shr-indentation 0)) | |
255 | (shr-indent)) | |
256 | ;; The shr-start is a special variable that is used to pass | |
257 | ;; upwards the first point in the buffer where the text really | |
258 | ;; starts. | |
259 | (unless shr-start | |
260 | (setq shr-start (point))) | |
261 | ;; No space is needed behind a wide character categorized as | |
b41c2f65 KY |
262 | ;; kinsoku-bol, between characters both categorized as nospace, |
263 | ;; or at the beginning of a line. | |
73db8b08 | 264 | (let (prev) |
20438017 KY |
265 | (when (and (eq (preceding-char) ? ) |
266 | (or (= (line-beginning-position) (1- (point))) | |
ed797193 G |
267 | (and (shr-char-breakable-p |
268 | (setq prev (char-after (- (point) 2)))) | |
269 | (shr-char-kinsoku-bol-p prev)) | |
270 | (and (shr-char-nospace-p prev) | |
271 | (shr-char-nospace-p (aref elem 0))))) | |
73db8b08 KY |
272 | (delete-char -1))) |
273 | (insert elem) | |
e7102c0a KY |
274 | (let (found) |
275 | (while (and (> (current-column) shr-width) | |
276 | (progn | |
277 | (setq found (shr-find-fill-point)) | |
b40950bf | 278 | (not (eolp)))) |
fe98a42f KY |
279 | (when (eq (preceding-char) ? ) |
280 | (delete-char -1)) | |
281 | (insert "\n") | |
282 | (unless found | |
e7102c0a KY |
283 | (put-text-property (1- (point)) (point) 'shr-break t) |
284 | ;; No space is needed at the beginning of a line. | |
285 | (when (eq (following-char) ? ) | |
286 | (delete-char 1))) | |
287 | (when (> shr-indentation 0) | |
288 | (shr-indent)) | |
289 | (end-of-line)) | |
290 | (insert " "))) | |
73db8b08 KY |
291 | (unless (string-match "[ \t\n]\\'" text) |
292 | (delete-char -1))))) | |
66627fa9 | 293 | |
6b7df8d3 | 294 | (defun shr-find-fill-point () |
83ffd571 KY |
295 | (when (> (move-to-column shr-width) shr-width) |
296 | (backward-char 1)) | |
ed797193 G |
297 | (let ((bp (point)) |
298 | failed) | |
299 | (while (not (or (setq failed (= (current-column) shr-indentation)) | |
300 | (eq (preceding-char) ? ) | |
301 | (eq (following-char) ? ) | |
302 | (shr-char-breakable-p (preceding-char)) | |
303 | (shr-char-breakable-p (following-char)) | |
7454326a G |
304 | (if (eq (preceding-char) ?') |
305 | (not (memq (char-after (- (point) 2)) | |
306 | (list nil ?\n ? ))) | |
7454326a | 307 | (and (shr-char-kinsoku-bol-p (preceding-char)) |
6568edea | 308 | (shr-char-breakable-p (following-char)) |
7454326a | 309 | (not (shr-char-kinsoku-bol-p (following-char))))) |
ed797193 | 310 | (shr-char-kinsoku-eol-p (following-char)))) |
83ffd571 | 311 | (backward-char 1)) |
ed797193 G |
312 | (if (and (not (or failed (eolp))) |
313 | (eq (preceding-char) ?')) | |
314 | (while (not (or (setq failed (eolp)) | |
315 | (eq (following-char) ? ) | |
316 | (shr-char-breakable-p (following-char)) | |
317 | (shr-char-kinsoku-eol-p (following-char)))) | |
318 | (forward-char 1))) | |
83ffd571 | 319 | (if failed |
20438017 | 320 | ;; There's no breakable point, so we give it up. |
ed797193 G |
321 | (let (found) |
322 | (goto-char bp) | |
323 | (unless shr-kinsoku-shorten | |
324 | (while (and (setq found (re-search-forward | |
325 | "\\(\\c>\\)\\| \\|\\c<\\|\\c|" | |
326 | (line-end-position) 'move)) | |
327 | (eq (preceding-char) ?'))) | |
328 | (if (and found (not (match-beginning 1))) | |
329 | (goto-char (match-beginning 0))))) | |
b40950bf KY |
330 | (or |
331 | (eolp) | |
ed797193 G |
332 | ;; Don't put kinsoku-bol characters at the beginning of a line, |
333 | ;; or kinsoku-eol characters at the end of a line. | |
334 | (cond | |
335 | (shr-kinsoku-shorten | |
336 | (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? ))) | |
337 | (shr-char-kinsoku-eol-p (preceding-char))) | |
338 | (backward-char 1)) | |
339 | (when (setq failed (= (current-column) shr-indentation)) | |
340 | ;; There's no breakable point that doesn't violate kinsoku, | |
341 | ;; so we look for the second best position. | |
342 | (while (and (progn | |
343 | (forward-char 1) | |
344 | (<= (current-column) shr-width)) | |
345 | (progn | |
346 | (setq bp (point)) | |
347 | (shr-char-kinsoku-eol-p (following-char))))) | |
348 | (goto-char bp))) | |
349 | ((shr-char-kinsoku-eol-p (preceding-char)) | |
350 | (if (shr-char-kinsoku-eol-p (following-char)) | |
351 | ;; There are consecutive kinsoku-eol characters. | |
352 | (setq failed t) | |
353 | (let ((count 4)) | |
354 | (while | |
355 | (progn | |
356 | (backward-char 1) | |
357 | (and (> (setq count (1- count)) 0) | |
358 | (not (memq (preceding-char) (list ?\C-@ ?\n ? ))) | |
359 | (or (shr-char-kinsoku-eol-p (preceding-char)) | |
360 | (shr-char-kinsoku-bol-p (following-char))))))) | |
361 | (if (setq failed (= (current-column) shr-indentation)) | |
b40950bf | 362 | ;; There's no breakable point that doesn't violate kinsoku, |
ed797193 G |
363 | ;; so we go to the second best position. |
364 | (if (looking-at "\\(\\c<+\\)\\c<") | |
365 | (goto-char (match-end 1)) | |
366 | (forward-char 1))))) | |
367 | (t | |
368 | (if (shr-char-kinsoku-bol-p (preceding-char)) | |
369 | ;; There are consecutive kinsoku-bol characters. | |
370 | (setq failed t) | |
b40950bf KY |
371 | (let ((count 4)) |
372 | (while (and (>= (setq count (1- count)) 0) | |
ed797193 G |
373 | (shr-char-kinsoku-bol-p (following-char)) |
374 | (shr-char-breakable-p (following-char))) | |
375 | (forward-char 1)))))) | |
376 | (when (eq (following-char) ? ) | |
377 | (forward-char 1)))) | |
378 | (not failed))) | |
6b7df8d3 | 379 | |
66627fa9 G |
380 | (defun shr-ensure-newline () |
381 | (unless (zerop (current-column)) | |
382 | (insert "\n"))) | |
a41c2e6d G |
383 | |
384 | (defun shr-ensure-paragraph () | |
385 | (unless (bobp) | |
f7aa248a | 386 | (if (<= (current-column) shr-indentation) |
71e691a5 G |
387 | (unless (save-excursion |
388 | (forward-line -1) | |
389 | (looking-at " *$")) | |
a41c2e6d G |
390 | (insert "\n")) |
391 | (if (save-excursion | |
392 | (beginning-of-line) | |
5d2ef6db | 393 | (looking-at " *$")) |
a41c2e6d G |
394 | (insert "\n") |
395 | (insert "\n\n"))))) | |
396 | ||
66627fa9 | 397 | (defun shr-indent () |
f7aa248a G |
398 | (when (> shr-indentation 0) |
399 | (insert (make-string shr-indentation ? )))) | |
870409d4 | 400 | |
a41c2e6d | 401 | (defun shr-fontize-cont (cont &rest types) |
870409d4 G |
402 | (let (shr-start) |
403 | (shr-generic cont) | |
a41c2e6d G |
404 | (dolist (type types) |
405 | (shr-add-font (or shr-start (point)) (point) type)))) | |
870409d4 | 406 | |
6c85a14f LMI |
407 | ;; Add an overlay in the region, but avoid putting the font properties |
408 | ;; on blank text at the start of the line, and the newline at the end, | |
409 | ;; to avoid ugliness. | |
870409d4 | 410 | (defun shr-add-font (start end type) |
6c85a14f LMI |
411 | (save-excursion |
412 | (goto-char start) | |
413 | (while (< (point) end) | |
414 | (when (bolp) | |
415 | (skip-chars-forward " ")) | |
416 | (let ((overlay (make-overlay (point) (min (line-end-position) end)))) | |
417 | (overlay-put overlay 'face type)) | |
418 | (if (< (line-end-position) end) | |
419 | (forward-line 1) | |
420 | (goto-char end))))) | |
870409d4 | 421 | |
71e691a5 G |
422 | (defun shr-browse-url () |
423 | "Browse the URL under point." | |
424 | (interactive) | |
425 | (let ((url (get-text-property (point) 'shr-url))) | |
181cb5fb G |
426 | (cond |
427 | ((not url) | |
428 | (message "No link under point")) | |
429 | ((string-match "^mailto:" url) | |
be3c11b3 | 430 | (browse-url-mailto url)) |
181cb5fb G |
431 | (t |
432 | (browse-url url))))) | |
71e691a5 | 433 | |
cdf1fca4 LMI |
434 | (defun shr-save-contents (directory) |
435 | "Save the contents from URL in a file." | |
436 | (interactive "DSave contents of URL to directory: ") | |
437 | (let ((url (get-text-property (point) 'shr-url))) | |
438 | (if (not url) | |
439 | (message "No link under point") | |
440 | (url-retrieve (shr-encode-url url) | |
441 | 'shr-store-contents (list url directory))))) | |
442 | ||
443 | (defun shr-store-contents (status url directory) | |
444 | (unless (plist-get status :error) | |
445 | (when (or (search-forward "\n\n" nil t) | |
446 | (search-forward "\r\n\r\n" nil t)) | |
447 | (write-region (point) (point-max) | |
448 | (expand-file-name (file-name-nondirectory url) | |
449 | directory))))) | |
450 | ||
870409d4 G |
451 | (defun shr-image-fetched (status buffer start end) |
452 | (when (and (buffer-name buffer) | |
453 | (not (plist-get status :error))) | |
454 | (url-store-in-cache (current-buffer)) | |
455 | (when (or (search-forward "\n\n" nil t) | |
456 | (search-forward "\r\n\r\n" nil t)) | |
457 | (let ((data (buffer-substring (point) (point-max)))) | |
458 | (with-current-buffer buffer | |
459 | (let ((alt (buffer-substring start end)) | |
460 | (inhibit-read-only t)) | |
461 | (delete-region start end) | |
99e65b2d G |
462 | (goto-char start) |
463 | (shr-put-image data alt)))))) | |
870409d4 G |
464 | (kill-buffer (current-buffer))) |
465 | ||
99e65b2d | 466 | (defun shr-put-image (data alt) |
4abff904 JD |
467 | (if (display-graphic-p) |
468 | (let ((image (ignore-errors | |
469 | (shr-rescale-image data)))) | |
470 | (when image | |
c0f9edce G |
471 | ;; When inserting big-ish pictures, put them at the |
472 | ;; beginning of the line. | |
473 | (when (and (> (current-column) 0) | |
474 | (> (car (image-size image t)) 400)) | |
475 | (insert "\n")) | |
99e65b2d G |
476 | (insert-image image (or alt "*")))) |
477 | (insert alt))) | |
870409d4 G |
478 | |
479 | (defun shr-rescale-image (data) | |
480 | (if (or (not (fboundp 'imagemagick-types)) | |
481 | (not (get-buffer-window (current-buffer)))) | |
482 | (create-image data nil t) | |
483 | (let* ((image (create-image data nil t)) | |
a41c2e6d | 484 | (size (image-size image t)) |
870409d4 G |
485 | (width (car size)) |
486 | (height (cdr size)) | |
487 | (edges (window-inside-pixel-edges | |
488 | (get-buffer-window (current-buffer)))) | |
489 | (window-width (truncate (* shr-max-image-proportion | |
490 | (- (nth 2 edges) (nth 0 edges))))) | |
491 | (window-height (truncate (* shr-max-image-proportion | |
492 | (- (nth 3 edges) (nth 1 edges))))) | |
493 | scaled-image) | |
494 | (when (> height window-height) | |
495 | (setq image (or (create-image data 'imagemagick t | |
496 | :height window-height) | |
497 | image)) | |
498 | (setq size (image-size image t))) | |
499 | (when (> (car size) window-width) | |
500 | (setq image (or | |
501 | (create-image data 'imagemagick t | |
502 | :width window-width) | |
503 | image))) | |
504 | image))) | |
505 | ||
85a45a69 GM |
506 | ;; url-cache-extract autoloads url-cache. |
507 | (declare-function url-cache-create-filename "url-cache" (url)) | |
508 | (autoload 'mm-disable-multibyte "mm-util") | |
be3c11b3 | 509 | (autoload 'browse-url-mailto "browse-url") |
85a45a69 | 510 | |
870409d4 G |
511 | (defun shr-get-image-data (url) |
512 | "Get image data for URL. | |
513 | Return a string with image data." | |
514 | (with-temp-buffer | |
515 | (mm-disable-multibyte) | |
71e691a5 | 516 | (when (ignore-errors |
ab67634f | 517 | (url-cache-extract (url-cache-create-filename (shr-encode-url url))) |
71e691a5 G |
518 | t) |
519 | (when (or (search-forward "\n\n" nil t) | |
520 | (search-forward "\r\n\r\n" nil t)) | |
521 | (buffer-substring (point) (point-max)))))) | |
870409d4 | 522 | |
40de2c6d KY |
523 | (defun shr-image-displayer (content-function) |
524 | "Return a function to display an image. | |
525 | CONTENT-FUNCTION is a function to retrieve an image for a cid url that | |
526 | is an argument. The function to be returned takes three arguments URL, | |
527 | START, and END." | |
528 | `(lambda (url start end) | |
f8d8a97b KY |
529 | (when url |
530 | (if (string-match "\\`cid:" url) | |
531 | ,(when content-function | |
532 | `(let ((image (funcall ,content-function | |
533 | (substring url (match-end 0))))) | |
534 | (when image | |
535 | (goto-char start) | |
536 | (shr-put-image image | |
537 | (prog1 | |
538 | (buffer-substring-no-properties start end) | |
539 | (delete-region start end)))))) | |
540 | (url-retrieve url 'shr-image-fetched | |
541 | (list (current-buffer) start end) | |
542 | t))))) | |
40de2c6d | 543 | |
66627fa9 G |
544 | (defun shr-heading (cont &rest types) |
545 | (shr-ensure-paragraph) | |
546 | (apply #'shr-fontize-cont cont types) | |
547 | (shr-ensure-paragraph)) | |
548 | ||
85a45a69 GM |
549 | (autoload 'widget-convert-button "wid-edit") |
550 | ||
04db63bc | 551 | (defun shr-urlify (start url &optional title) |
de635afe G |
552 | (widget-convert-button |
553 | 'url-link start (point) | |
04db63bc | 554 | :help-echo (if title (format "%s (%s)" url title) url) |
de635afe G |
555 | :keymap shr-map |
556 | url) | |
557 | (put-text-property start (point) 'shr-url url)) | |
558 | ||
559 | (defun shr-encode-url (url) | |
560 | "Encode URL." | |
561 | (browse-url-url-encode-chars url "[)$ ]")) | |
562 | ||
ebe79557 LMI |
563 | (autoload 'shr-color-visible "shr-color") |
564 | (autoload 'shr-color->hexadecimal "shr-color") | |
144b7b5c G |
565 | |
566 | (defun shr-color-check (fg bg) | |
567 | "Check that FG is visible on BG. | |
568 | Returns (fg bg) with corrected values. | |
569 | Returns nil if the colors that would be used are the default | |
570 | ones, in case fg and bg are nil." | |
571 | (when (or fg bg) | |
572 | (let ((fixed (cond ((null fg) 'fg) | |
573 | ((null bg) 'bg)))) | |
574 | ;; Convert colors to hexadecimal, or set them to default. | |
575 | (let ((fg (or (shr-color->hexadecimal fg) | |
576 | (frame-parameter nil 'foreground-color))) | |
577 | (bg (or (shr-color->hexadecimal bg) | |
578 | (frame-parameter nil 'background-color)))) | |
579 | (cond ((eq fixed 'bg) | |
580 | ;; Only return the new fg | |
581 | (list nil (cadr (shr-color-visible bg fg t)))) | |
582 | ((eq fixed 'fg) | |
583 | ;; Invert args and results and return only the new bg | |
584 | (list (cadr (shr-color-visible fg bg t)) nil)) | |
585 | (t | |
586 | (shr-color-visible bg fg))))))) | |
587 | ||
04db63bc | 588 | (defun shr-colorize-region (start end fg &optional bg) |
b31b26b4 | 589 | (when (or fg bg) |
04db63bc | 590 | (let ((new-colors (shr-color-check fg bg))) |
144b7b5c | 591 | (when new-colors |
04db63bc G |
592 | (shr-put-color start end :foreground (cadr new-colors)) |
593 | (when bg | |
594 | (shr-put-color start end :background (car new-colors))))))) | |
595 | ||
596 | ;; Put a color in the region, but avoid putting colors on on blank | |
597 | ;; text at the start of the line, and the newline at the end, to avoid | |
598 | ;; ugliness. Also, don't overwrite any existing color information, | |
599 | ;; since this can be called recursively, and we want the "inner" color | |
600 | ;; to win. | |
601 | (defun shr-put-color (start end type color) | |
602 | (save-excursion | |
603 | (goto-char start) | |
604 | (while (< (point) end) | |
605 | (when (bolp) | |
606 | (skip-chars-forward " ")) | |
607 | (when (> (line-end-position) (point)) | |
608 | (shr-put-color-1 (point) (min (line-end-position) end) type color)) | |
609 | (if (< (line-end-position) end) | |
610 | (forward-line 1) | |
611 | (goto-char end))))) | |
612 | ||
613 | (defun shr-put-color-1 (start end type color) | |
614 | (let* ((old-props (get-text-property start 'face)) | |
615 | (do-put (not (memq type old-props))) | |
616 | change) | |
617 | (while (< start end) | |
618 | (setq change (next-single-property-change start 'face nil end)) | |
619 | (when do-put | |
620 | (put-text-property start change 'face | |
621 | (nconc (list type color) old-props))) | |
622 | (setq old-props (get-text-property change 'face)) | |
623 | (setq do-put (not (memq type old-props))) | |
624 | (setq start change)) | |
625 | (when (and do-put | |
626 | (> end start)) | |
627 | (put-text-property start end 'face | |
628 | (nconc (list type color old-props)))))) | |
ebe79557 | 629 | |
66627fa9 G |
630 | ;;; Tag-specific rendering rules. |
631 | ||
144b7b5c | 632 | (defun shr-tag-body (cont) |
04db63bc G |
633 | (let* ((start (point)) |
634 | (fgcolor (cdr (assq :fgcolor cont))) | |
635 | (bgcolor (cdr (assq :bgcolor cont))) | |
b31b26b4 G |
636 | (shr-stylesheet (list (cons 'color fgcolor) |
637 | (cons 'background-color bgcolor)))) | |
144b7b5c | 638 | (shr-generic cont) |
04db63bc | 639 | (shr-colorize-region start (point) fgcolor bgcolor))) |
144b7b5c | 640 | |
b31b26b4 G |
641 | (defun shr-tag-style (cont) |
642 | ) | |
643 | ||
66627fa9 G |
644 | (defun shr-tag-p (cont) |
645 | (shr-ensure-paragraph) | |
f7aa248a | 646 | (shr-indent) |
66627fa9 G |
647 | (shr-generic cont) |
648 | (shr-ensure-paragraph)) | |
649 | ||
036d93bc KY |
650 | (defun shr-tag-div (cont) |
651 | (shr-ensure-newline) | |
652 | (shr-indent) | |
653 | (shr-generic cont) | |
654 | (shr-ensure-newline)) | |
655 | ||
66627fa9 G |
656 | (defun shr-tag-b (cont) |
657 | (shr-fontize-cont cont 'bold)) | |
658 | ||
659 | (defun shr-tag-i (cont) | |
660 | (shr-fontize-cont cont 'italic)) | |
661 | ||
662 | (defun shr-tag-em (cont) | |
663 | (shr-fontize-cont cont 'bold)) | |
664 | ||
530f7b67 LMI |
665 | (defun shr-tag-strong (cont) |
666 | (shr-fontize-cont cont 'bold)) | |
667 | ||
66627fa9 G |
668 | (defun shr-tag-u (cont) |
669 | (shr-fontize-cont cont 'underline)) | |
670 | ||
671 | (defun shr-tag-s (cont) | |
672 | (shr-fontize-cont cont 'strike-through)) | |
673 | ||
2e76c12c LMI |
674 | (defun shr-parse-style (style) |
675 | (when style | |
a2994808 JD |
676 | (save-match-data |
677 | (when (string-match "\n" style) | |
678 | (setq style (replace-match " " t t style)))) | |
2e76c12c LMI |
679 | (let ((plist nil)) |
680 | (dolist (elem (split-string style ";")) | |
681 | (when elem | |
682 | (setq elem (split-string elem ":")) | |
683 | (when (and (car elem) | |
684 | (cadr elem)) | |
685 | (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem))) | |
686 | (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem)))) | |
144b7b5c G |
687 | (when (string-match " *!important\\'" value) |
688 | (setq value (substring value 0 (match-beginning 0)))) | |
2e76c12c LMI |
689 | (push (cons (intern name obarray) |
690 | value) | |
691 | plist))))) | |
692 | plist))) | |
693 | ||
66627fa9 G |
694 | (defun shr-tag-a (cont) |
695 | (let ((url (cdr (assq :href cont))) | |
04db63bc | 696 | (title (cdr (assq :title cont))) |
66627fa9 G |
697 | (start (point)) |
698 | shr-start) | |
699 | (shr-generic cont) | |
04db63bc | 700 | (shr-urlify (or shr-start start) url title))) |
de635afe G |
701 | |
702 | (defun shr-tag-object (cont) | |
99e65b2d G |
703 | (let ((start (point)) |
704 | url) | |
705 | (dolist (elem cont) | |
706 | (when (eq (car elem) 'embed) | |
707 | (setq url (or url (cdr (assq :src (cdr elem)))))) | |
708 | (when (and (eq (car elem) 'param) | |
709 | (equal (cdr (assq :name (cdr elem))) "movie")) | |
710 | (setq url (or url (cdr (assq :value (cdr elem))))))) | |
de635afe G |
711 | (when url |
712 | (shr-insert " [multimedia] ") | |
99e65b2d G |
713 | (shr-urlify start url)) |
714 | (shr-generic cont))) | |
715 | ||
716 | (defun shr-tag-video (cont) | |
717 | (let ((image (cdr (assq :poster cont))) | |
718 | (url (cdr (assq :src cont))) | |
719 | (start (point))) | |
720 | (shr-tag-img nil image) | |
721 | (shr-urlify start url))) | |
ab67634f | 722 | |
99e65b2d G |
723 | (defun shr-tag-img (cont &optional url) |
724 | (when (or url | |
725 | (and cont | |
726 | (cdr (assq :src cont)))) | |
68f6bd17 KY |
727 | (when (and (> (current-column) 0) |
728 | (not (eq shr-state 'image))) | |
729 | (insert "\n")) | |
730 | (let ((alt (cdr (assq :alt cont))) | |
99e65b2d | 731 | (url (or url (cdr (assq :src cont))))) |
68f6bd17 KY |
732 | (let ((start (point-marker))) |
733 | (when (zerop (length alt)) | |
953d41c4 | 734 | (setq alt "*")) |
68f6bd17 | 735 | (cond |
99e65b2d G |
736 | ((or (member (cdr (assq :height cont)) '("0" "1")) |
737 | (member (cdr (assq :width cont)) '("0" "1"))) | |
738 | ;; Ignore zero-sized or single-pixel images. | |
739 | ) | |
68f6bd17 KY |
740 | ((and (not shr-inhibit-images) |
741 | (string-match "\\`cid:" url)) | |
742 | (let ((url (substring url (match-end 0))) | |
743 | image) | |
744 | (if (or (not shr-content-function) | |
745 | (not (setq image (funcall shr-content-function url)))) | |
746 | (insert alt) | |
99e65b2d | 747 | (shr-put-image image alt)))) |
68f6bd17 KY |
748 | ((or shr-inhibit-images |
749 | (and shr-blocked-images | |
750 | (string-match shr-blocked-images url))) | |
751 | (setq shr-start (point)) | |
752 | (let ((shr-state 'space)) | |
b354bc53 KY |
753 | (if (> (string-width alt) 8) |
754 | (shr-insert (truncate-string-to-width alt 8)) | |
68f6bd17 KY |
755 | (shr-insert alt)))) |
756 | ((url-is-cached (shr-encode-url url)) | |
99e65b2d | 757 | (shr-put-image (shr-get-image-data url) alt)) |
68f6bd17 KY |
758 | (t |
759 | (insert alt) | |
760 | (ignore-errors | |
761 | (url-retrieve (shr-encode-url url) 'shr-image-fetched | |
762 | (list (current-buffer) start (point-marker)) | |
763 | t)))) | |
68f6bd17 KY |
764 | (put-text-property start (point) 'keymap shr-map) |
765 | (put-text-property start (point) 'shr-alt alt) | |
8b6f6573 LMI |
766 | (put-text-property start (point) 'image-url url) |
767 | (put-text-property start (point) 'image-displayer | |
40de2c6d | 768 | (shr-image-displayer shr-content-function)) |
99e65b2d | 769 | (put-text-property start (point) 'help-echo alt) |
68f6bd17 | 770 | (setq shr-state 'image))))) |
66627fa9 G |
771 | |
772 | (defun shr-tag-pre (cont) | |
773 | (let ((shr-folding-mode 'none)) | |
774 | (shr-ensure-newline) | |
f7aa248a | 775 | (shr-indent) |
66627fa9 G |
776 | (shr-generic cont) |
777 | (shr-ensure-newline))) | |
778 | ||
779 | (defun shr-tag-blockquote (cont) | |
780 | (shr-ensure-paragraph) | |
f7aa248a | 781 | (shr-indent) |
66627fa9 G |
782 | (let ((shr-indentation (+ shr-indentation 4))) |
783 | (shr-generic cont)) | |
784 | (shr-ensure-paragraph)) | |
a41c2e6d G |
785 | |
786 | (defun shr-tag-ul (cont) | |
787 | (shr-ensure-paragraph) | |
788 | (let ((shr-list-mode 'ul)) | |
3d319c8f LMI |
789 | (shr-generic cont)) |
790 | (shr-ensure-paragraph)) | |
a41c2e6d G |
791 | |
792 | (defun shr-tag-ol (cont) | |
3d319c8f | 793 | (shr-ensure-paragraph) |
a41c2e6d | 794 | (let ((shr-list-mode 1)) |
3d319c8f LMI |
795 | (shr-generic cont)) |
796 | (shr-ensure-paragraph)) | |
a41c2e6d G |
797 | |
798 | (defun shr-tag-li (cont) | |
f7aa248a G |
799 | (shr-ensure-paragraph) |
800 | (shr-indent) | |
8028ed5c LMI |
801 | (let* ((bullet |
802 | (if (numberp shr-list-mode) | |
803 | (prog1 | |
804 | (format "%d " shr-list-mode) | |
805 | (setq shr-list-mode (1+ shr-list-mode))) | |
806 | "* ")) | |
807 | (shr-indentation (+ shr-indentation (length bullet)))) | |
808 | (insert bullet) | |
809 | (shr-generic cont))) | |
a41c2e6d G |
810 | |
811 | (defun shr-tag-br (cont) | |
1e463294 | 812 | (unless (bobp) |
f7aa248a G |
813 | (insert "\n") |
814 | (shr-indent)) | |
a41c2e6d G |
815 | (shr-generic cont)) |
816 | ||
817 | (defun shr-tag-h1 (cont) | |
818 | (shr-heading cont 'bold 'underline)) | |
819 | ||
820 | (defun shr-tag-h2 (cont) | |
821 | (shr-heading cont 'bold)) | |
822 | ||
823 | (defun shr-tag-h3 (cont) | |
824 | (shr-heading cont 'italic)) | |
825 | ||
826 | (defun shr-tag-h4 (cont) | |
827 | (shr-heading cont)) | |
828 | ||
829 | (defun shr-tag-h5 (cont) | |
830 | (shr-heading cont)) | |
831 | ||
832 | (defun shr-tag-h6 (cont) | |
833 | (shr-heading cont)) | |
834 | ||
3d319c8f LMI |
835 | (defun shr-tag-hr (cont) |
836 | (shr-ensure-newline) | |
6b7df8d3 | 837 | (insert (make-string shr-width shr-hr-line) "\n")) |
3d319c8f | 838 | |
144b7b5c G |
839 | (defun shr-tag-title (cont) |
840 | (shr-heading cont 'bold 'underline)) | |
841 | ||
1110d53b | 842 | (defun shr-tag-font (cont) |
b31b26b4 G |
843 | (let* ((start (point)) |
844 | (color (cdr (assq :color cont))) | |
845 | (shr-stylesheet (nconc (list (cons 'color color)) | |
846 | shr-stylesheet))) | |
1110d53b | 847 | (shr-generic cont) |
b31b26b4 G |
848 | (when color |
849 | (shr-colorize-region start (point) color | |
850 | (cdr (assq 'background-color shr-stylesheet)))))) | |
1110d53b | 851 | |
66627fa9 | 852 | ;;; Table rendering algorithm. |
a41c2e6d | 853 | |
a0ec382a LMI |
854 | ;; Table rendering is the only complicated thing here. We do this by |
855 | ;; first counting how many TDs there are in each TR, and registering | |
856 | ;; how wide they think they should be ("width=45%", etc). Then we | |
857 | ;; render each TD separately (this is done in temporary buffers, so | |
858 | ;; that we can use all the rendering machinery as if we were in the | |
859 | ;; main buffer). Now we know how much space each TD really takes, so | |
860 | ;; we then render everything again with the new widths, and finally | |
861 | ;; insert all these boxes into the main buffer. | |
6c769311 | 862 | (defun shr-tag-table-1 (cont) |
71e691a5 G |
863 | (setq cont (or (cdr (assq 'tbody cont)) |
864 | cont)) | |
130e977f | 865 | (let* ((shr-inhibit-images t) |
99e65b2d | 866 | (shr-table-depth (1+ shr-table-depth)) |
83ffd571 | 867 | (shr-kinsoku-shorten t) |
a0ec382a | 868 | ;; Find all suggested widths. |
130e977f | 869 | (columns (shr-column-specs cont)) |
a0ec382a | 870 | ;; Compute how many characters wide each TD should be. |
71e691a5 | 871 | (suggested-widths (shr-pro-rate-columns columns)) |
a0ec382a LMI |
872 | ;; Do a "test rendering" to see how big each TD is (this can |
873 | ;; be smaller (if there's little text) or bigger (if there's | |
874 | ;; unbreakable text). | |
71e691a5 | 875 | (sketch (shr-make-table cont suggested-widths)) |
a7dcc87b | 876 | (sketch-widths (shr-table-widths sketch suggested-widths))) |
030158f3 | 877 | ;; This probably won't work very well. |
83ffd571 KY |
878 | (when (> (+ (loop for width across sketch-widths |
879 | summing (1+ width)) | |
880 | shr-indentation 1) | |
030158f3 G |
881 | (frame-width)) |
882 | (setq truncate-lines t)) | |
a0ec382a | 883 | ;; Then render the table again with these new "hard" widths. |
130e977f | 884 | (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)) |
a0ec382a LMI |
885 | ;; Finally, insert all the images after the table. The Emacs buffer |
886 | ;; model isn't strong enough to allow us to put the images actually | |
887 | ;; into the tables. | |
99e65b2d G |
888 | (when (zerop shr-table-depth) |
889 | (dolist (elem (shr-find-elements cont 'img)) | |
890 | (shr-tag-img (cdr elem))))) | |
130e977f | 891 | |
6c769311 KY |
892 | (defun shr-tag-table (cont) |
893 | (shr-ensure-paragraph) | |
894 | (let* ((caption (cdr (assq 'caption cont))) | |
895 | (header (cdr (assq 'thead cont))) | |
896 | (body (or (cdr (assq 'tbody cont)) cont)) | |
897 | (footer (cdr (assq 'tfoot cont))) | |
144b7b5c | 898 | (bgcolor (cdr (assq :bgcolor cont))) |
6c769311 KY |
899 | (nheader (if header (shr-max-columns header))) |
900 | (nbody (if body (shr-max-columns body))) | |
901 | (nfooter (if footer (shr-max-columns footer)))) | |
902 | (shr-tag-table-1 | |
3c066373 KY |
903 | (nconc |
904 | (if caption `((tr (td ,@caption)))) | |
905 | (if header | |
906 | (if footer | |
907 | ;; hader + body + footer | |
908 | (if (= nheader nbody) | |
909 | (if (= nbody nfooter) | |
910 | `((tr (td (table (tbody ,@header ,@body ,@footer))))) | |
911 | (nconc `((tr (td (table (tbody ,@header ,@body))))) | |
912 | (if (= nfooter 1) | |
913 | footer | |
914 | `((tr (td (table (tbody ,@footer)))))))) | |
915 | (nconc `((tr (td (table (tbody ,@header))))) | |
916 | (if (= nbody nfooter) | |
917 | `((tr (td (table (tbody ,@body ,@footer))))) | |
918 | (nconc `((tr (td (table (tbody ,@body))))) | |
919 | (if (= nfooter 1) | |
920 | footer | |
921 | `((tr (td (table (tbody ,@footer)))))))))) | |
922 | ;; header + body | |
923 | (if (= nheader nbody) | |
924 | `((tr (td (table (tbody ,@header ,@body))))) | |
925 | (if (= nheader 1) | |
926 | `(,@header (tr (td (table (tbody ,@body))))) | |
927 | `((tr (td (table (tbody ,@header)))) | |
928 | (tr (td (table (tbody ,@body)))))))) | |
929 | (if footer | |
930 | ;; body + footer | |
931 | (if (= nbody nfooter) | |
932 | `((tr (td (table (tbody ,@body ,@footer))))) | |
933 | (nconc `((tr (td (table (tbody ,@body))))) | |
2526f423 | 934 | (if (= nfooter 1) |
3c066373 KY |
935 | footer |
936 | `((tr (td (table (tbody ,@footer)))))))) | |
937 | (if caption | |
938 | `((tr (td (table (tbody ,@body))))) | |
939 | body))))))) | |
6c769311 | 940 | |
130e977f LMI |
941 | (defun shr-find-elements (cont type) |
942 | (let (result) | |
943 | (dolist (elem cont) | |
944 | (cond ((eq (car elem) type) | |
945 | (push elem result)) | |
946 | ((consp (cdr elem)) | |
947 | (setq result (nconc (shr-find-elements (cdr elem) type) result))))) | |
948 | (nreverse result))) | |
71e691a5 G |
949 | |
950 | (defun shr-insert-table (table widths) | |
951 | (shr-insert-table-ruler widths) | |
952 | (dolist (row table) | |
953 | (let ((start (point)) | |
954 | (height (let ((max 0)) | |
955 | (dolist (column row) | |
956 | (setq max (max max (cadr column)))) | |
957 | max))) | |
958 | (dotimes (i height) | |
959 | (shr-indent) | |
d3098750 | 960 | (insert shr-table-vertical-line "\n")) |
71e691a5 G |
961 | (dolist (column row) |
962 | (goto-char start) | |
a7dcc87b | 963 | (let ((lines (nth 2 column)) |
130e977f LMI |
964 | (overlay-lines (nth 3 column)) |
965 | overlay overlay-line) | |
71e691a5 | 966 | (dolist (line lines) |
130e977f | 967 | (setq overlay-line (pop overlay-lines)) |
3d319c8f | 968 | (end-of-line) |
d3098750 | 969 | (insert line shr-table-vertical-line) |
3d319c8f LMI |
970 | (dolist (overlay overlay-line) |
971 | (let ((o (make-overlay (- (point) (nth 0 overlay) 1) | |
972 | (- (point) (nth 1 overlay) 1))) | |
973 | (properties (nth 2 overlay))) | |
974 | (while properties | |
975 | (overlay-put o (pop properties) (pop properties))))) | |
976 | (forward-line 1)) | |
71e691a5 G |
977 | ;; Add blank lines at padding at the bottom of the TD, |
978 | ;; possibly. | |
979 | (dotimes (i (- height (length lines))) | |
980 | (end-of-line) | |
d3098750 LMI |
981 | (insert (make-string (string-width (car lines)) ? ) |
982 | shr-table-vertical-line) | |
71e691a5 G |
983 | (forward-line 1))))) |
984 | (shr-insert-table-ruler widths))) | |
985 | ||
986 | (defun shr-insert-table-ruler (widths) | |
83ffd571 KY |
987 | (when (and (bolp) |
988 | (> shr-indentation 0)) | |
989 | (shr-indent)) | |
afba0c4b | 990 | (insert shr-table-corner) |
71e691a5 | 991 | (dotimes (i (length widths)) |
d3098750 LMI |
992 | (insert (make-string (aref widths i) shr-table-horizontal-line) |
993 | shr-table-corner)) | |
71e691a5 G |
994 | (insert "\n")) |
995 | ||
a7dcc87b G |
996 | (defun shr-table-widths (table suggested-widths) |
997 | (let* ((length (length suggested-widths)) | |
998 | (widths (make-vector length 0)) | |
999 | (natural-widths (make-vector length 0))) | |
71e691a5 G |
1000 | (dolist (row table) |
1001 | (let ((i 0)) | |
1002 | (dolist (column row) | |
1003 | (aset widths i (max (aref widths i) | |
1004 | (car column))) | |
a7dcc87b G |
1005 | (aset natural-widths i (max (aref natural-widths i) |
1006 | (cadr column))) | |
1007 | (setq i (1+ i))))) | |
863b61d6 KY |
1008 | (let ((extra (- (apply '+ (append suggested-widths nil)) |
1009 | (apply '+ (append widths nil)))) | |
a7dcc87b G |
1010 | (expanded-columns 0)) |
1011 | (when (> extra 0) | |
1012 | (dotimes (i length) | |
1013 | ;; If the natural width is wider than the rendered width, we | |
1014 | ;; want to allow the column to expand. | |
1015 | (when (> (aref natural-widths i) (aref widths i)) | |
1016 | (setq expanded-columns (1+ expanded-columns)))) | |
1017 | (dotimes (i length) | |
1018 | (when (> (aref natural-widths i) (aref widths i)) | |
1019 | (aset widths i (min | |
1020 | (1+ (aref natural-widths i)) | |
1021 | (+ (/ extra expanded-columns) | |
1022 | (aref widths i)))))))) | |
71e691a5 G |
1023 | widths)) |
1024 | ||
1025 | (defun shr-make-table (cont widths &optional fill) | |
1026 | (let ((trs nil)) | |
1027 | (dolist (row cont) | |
1028 | (when (eq (car row) 'tr) | |
a0ec382a LMI |
1029 | (let ((tds nil) |
1030 | (columns (cdr row)) | |
1031 | (i 0) | |
1032 | column) | |
1033 | (while (< i (length widths)) | |
1034 | (setq column (pop columns)) | |
1035 | (when (or (memq (car column) '(td th)) | |
1036 | (null column)) | |
71e691a5 G |
1037 | (push (shr-render-td (cdr column) (aref widths i) fill) |
1038 | tds) | |
1039 | (setq i (1+ i)))) | |
1040 | (push (nreverse tds) trs)))) | |
1041 | (nreverse trs))) | |
1042 | ||
1043 | (defun shr-render-td (cont width fill) | |
04db63bc G |
1044 | (with-temp-buffer |
1045 | (let ((cache (cdr (assoc (cons width cont) shr-content-cache)))) | |
1046 | (if cache | |
1047 | (insert cache) | |
1048 | (let ((shr-width width) | |
1049 | (shr-indentation 0)) | |
b31b26b4 | 1050 | (shr-descend (cons 'td cont))) |
04db63bc G |
1051 | (delete-region |
1052 | (point) | |
1053 | (+ (point) | |
1054 | (skip-chars-backward " \t\n"))) | |
1055 | (push (cons (cons width cont) (buffer-string)) | |
1056 | shr-content-cache))) | |
1057 | (goto-char (point-min)) | |
1058 | (let ((max 0)) | |
1059 | (while (not (eobp)) | |
1060 | (end-of-line) | |
1061 | (setq max (max max (current-column))) | |
1062 | (forward-line 1)) | |
1063 | (when fill | |
1064 | (goto-char (point-min)) | |
1065 | ;; If the buffer is totally empty, then put a single blank | |
1066 | ;; line here. | |
1067 | (if (zerop (buffer-size)) | |
1068 | (insert (make-string width ? )) | |
1069 | ;; Otherwise, fill the buffer. | |
1070 | (while (not (eobp)) | |
1071 | (end-of-line) | |
1072 | (when (> (- width (current-column)) 0) | |
1073 | (insert (make-string (- width (current-column)) ? ))) | |
1074 | (forward-line 1)))) | |
1075 | (if fill | |
1076 | (list max | |
1077 | (count-lines (point-min) (point-max)) | |
1078 | (split-string (buffer-string) "\n") | |
1079 | (shr-collect-overlays)) | |
1080 | (list max | |
1081 | (shr-natural-width)))))) | |
a7dcc87b G |
1082 | |
1083 | (defun shr-natural-width () | |
1084 | (goto-char (point-min)) | |
1085 | (let ((current 0) | |
1086 | (max 0)) | |
1087 | (while (not (eobp)) | |
1088 | (end-of-line) | |
1089 | (setq current (+ current (current-column))) | |
1090 | (unless (get-text-property (point) 'shr-break) | |
1091 | (setq max (max max current) | |
1092 | current 0)) | |
1093 | (forward-line 1)) | |
1094 | max)) | |
130e977f LMI |
1095 | |
1096 | (defun shr-collect-overlays () | |
1097 | (save-excursion | |
1098 | (goto-char (point-min)) | |
1099 | (let ((overlays nil)) | |
1100 | (while (not (eobp)) | |
1101 | (push (shr-overlays-in-region (point) (line-end-position)) | |
1102 | overlays) | |
1103 | (forward-line 1)) | |
1104 | (nreverse overlays)))) | |
1105 | ||
1106 | (defun shr-overlays-in-region (start end) | |
1107 | (let (result) | |
1108 | (dolist (overlay (overlays-in start end)) | |
1109 | (push (list (if (> start (overlay-start overlay)) | |
1110 | (- end start) | |
1111 | (- end (overlay-start overlay))) | |
1112 | (if (< end (overlay-end overlay)) | |
1113 | 0 | |
1114 | (- end (overlay-end overlay))) | |
1115 | (overlay-properties overlay)) | |
1116 | result)) | |
1117 | (nreverse result))) | |
71e691a5 G |
1118 | |
1119 | (defun shr-pro-rate-columns (columns) | |
1120 | (let ((total-percentage 0) | |
1121 | (widths (make-vector (length columns) 0))) | |
1122 | (dotimes (i (length columns)) | |
a7dcc87b | 1123 | (setq total-percentage (+ total-percentage (aref columns i)))) |
71e691a5 G |
1124 | (setq total-percentage (/ 1.0 total-percentage)) |
1125 | (dotimes (i (length columns)) | |
1126 | (aset widths i (max (truncate (* (aref columns i) | |
1127 | total-percentage | |
a7dcc87b | 1128 | (- shr-width (1+ (length columns))))) |
71e691a5 G |
1129 | 10))) |
1130 | widths)) | |
1131 | ||
1132 | ;; Return a summary of the number and shape of the TDs in the table. | |
1133 | (defun shr-column-specs (cont) | |
1134 | (let ((columns (make-vector (shr-max-columns cont) 1))) | |
1135 | (dolist (row cont) | |
1136 | (when (eq (car row) 'tr) | |
1137 | (let ((i 0)) | |
1138 | (dolist (column (cdr row)) | |
1139 | (when (memq (car column) '(td th)) | |
1140 | (let ((width (cdr (assq :width (cdr column))))) | |
1141 | (when (and width | |
1142 | (string-match "\\([0-9]+\\)%" width)) | |
1143 | (aset columns i | |
1144 | (/ (string-to-number (match-string 1 width)) | |
130e977f LMI |
1145 | 100.0)))) |
1146 | (setq i (1+ i))))))) | |
71e691a5 G |
1147 | columns)) |
1148 | ||
1149 | (defun shr-count (cont elem) | |
1150 | (let ((i 0)) | |
1151 | (dolist (sub cont) | |
1152 | (when (eq (car sub) elem) | |
1153 | (setq i (1+ i)))) | |
1154 | i)) | |
1155 | ||
1156 | (defun shr-max-columns (cont) | |
1157 | (let ((max 0)) | |
1158 | (dolist (row cont) | |
1159 | (when (eq (car row) 'tr) | |
130e977f LMI |
1160 | (setq max (max max (+ (shr-count (cdr row) 'td) |
1161 | (shr-count (cdr row) 'th)))))) | |
71e691a5 G |
1162 | max)) |
1163 | ||
f3fd95db | 1164 | (provide 'shr) |
367f7f81 LMI |
1165 | |
1166 | ;;; shr.el ends here |