Commit | Line | Data |
---|---|---|
367f7f81 LMI |
1 | ;;; shr.el --- Simple HTML Renderer |
2 | ||
ab422c4d | 3 | ;; Copyright (C) 2010-2013 Free Software Foundation, Inc. |
367f7f81 LMI |
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)) |
7545bd25 | 34 | (eval-when-compile (require 'url)) ;For url-filename's setf handler. |
71e691a5 G |
35 | (require 'browse-url) |
36 | ||
870409d4 G |
37 | (defgroup shr nil |
38 | "Simple HTML Renderer" | |
2bed3f04 | 39 | :version "24.1" |
0ebd92a3 | 40 | :group 'hypermedia) |
870409d4 G |
41 | |
42 | (defcustom shr-max-image-proportion 0.9 | |
43 | "How big pictures displayed are in relation to the window they're in. | |
44 | A value of 0.7 means that they are allowed to take up 70% of the | |
45 | width and height of the window. If they are larger than this, | |
46 | and Emacs supports it, then the images will be rescaled down to | |
47 | fit these criteria." | |
48 | :version "24.1" | |
49 | :group 'shr | |
50 | :type 'float) | |
51 | ||
52 | (defcustom shr-blocked-images nil | |
53 | "Images that have URLs matching this regexp will be blocked." | |
54 | :version "24.1" | |
55 | :group 'shr | |
a931698a | 56 | :type '(choice (const nil) regexp)) |
870409d4 | 57 | |
924d6997 G |
58 | (defcustom shr-table-horizontal-line nil |
59 | "Character used to draw horizontal table lines. | |
60 | If nil, don't draw horizontal table lines." | |
d3098750 | 61 | :group 'shr |
af1c6c84 | 62 | :type '(choice (const nil) character)) |
d3098750 | 63 | |
e37df674 | 64 | (defcustom shr-table-vertical-line ?\s |
d3098750 | 65 | "Character used to draw vertical table lines." |
afba0c4b | 66 | :group 'shr |
030158f3 | 67 | :type 'character) |
afba0c4b | 68 | |
e37df674 | 69 | (defcustom shr-table-corner ?\s |
d3098750 | 70 | "Character used to draw table corners." |
6b7df8d3 | 71 | :group 'shr |
030158f3 | 72 | :type 'character) |
6b7df8d3 G |
73 | |
74 | (defcustom shr-hr-line ?- | |
d3098750 | 75 | "Character used to draw hr lines." |
afba0c4b | 76 | :group 'shr |
030158f3 | 77 | :type 'character) |
afba0c4b | 78 | |
d0e0de31 | 79 | (defcustom shr-width fill-column |
bb7f5cbc G |
80 | "Frame width to use for rendering. |
81 | May either be an integer specifying a fixed width in characters, | |
82 | or nil, meaning that the full width of the window should be | |
83 | used." | |
84 | :type '(choice (integer :tag "Fixed width in characters") | |
85 | (const :tag "Use the width of the window" nil)) | |
d0e0de31 JD |
86 | :group 'shr) |
87 | ||
c74cb344 G |
88 | (defcustom shr-bullet "* " |
89 | "Bullet used for unordered lists. | |
90 | Alternative suggestions are: | |
91 | - \" \" | |
92 | - \" \"" | |
93 | :type 'string | |
94 | :group 'shr) | |
95 | ||
0ebd92a3 LMI |
96 | (defcustom shr-external-browser 'browse-url-default-browser |
97 | "Function used to launch an external browser." | |
98 | :version "24.4" | |
99 | :group 'shr | |
100 | :type 'function) | |
101 | ||
130e977f LMI |
102 | (defvar shr-content-function nil |
103 | "If bound, this should be a function that will return the content. | |
104 | This is used for cid: URLs, and the function is called with the | |
105 | cid: URL as the argument.") | |
106 | ||
b9bdaf74 KY |
107 | (defvar shr-put-image-function 'shr-put-image |
108 | "Function called to put image and alt string.") | |
109 | ||
6eee2678 LMI |
110 | (defface shr-strike-through '((t (:strike-through t))) |
111 | "Font for <s> elements." | |
112 | :group 'shr) | |
113 | ||
f8774e35 | 114 | (defface shr-link |
7ef1d634 | 115 | '((t (:inherit link))) |
df26ce09 | 116 | "Font for link elements." |
c2f51e23 G |
117 | :group 'shr) |
118 | ||
66627fa9 G |
119 | ;;; Internal variables. |
120 | ||
870409d4 G |
121 | (defvar shr-folding-mode nil) |
122 | (defvar shr-state nil) | |
123 | (defvar shr-start nil) | |
a41c2e6d | 124 | (defvar shr-indentation 0) |
130e977f | 125 | (defvar shr-inhibit-images nil) |
66627fa9 | 126 | (defvar shr-list-mode nil) |
3d319c8f | 127 | (defvar shr-content-cache nil) |
83ffd571 | 128 | (defvar shr-kinsoku-shorten nil) |
99e65b2d | 129 | (defvar shr-table-depth 0) |
04db63bc | 130 | (defvar shr-stylesheet nil) |
dbd5ffad | 131 | (defvar shr-base nil) |
728518c3 | 132 | (defvar shr-ignore-cache nil) |
2644071e | 133 | (defvar shr-external-rendering-functions nil) |
c74cb344 | 134 | (defvar shr-target-id nil) |
be2aa135 | 135 | (defvar shr-inhibit-decoration nil) |
924d6997 | 136 | (defvar shr-table-separator-length 1) |
870409d4 | 137 | |
71e691a5 G |
138 | (defvar shr-map |
139 | (let ((map (make-sparse-keymap))) | |
140 | (define-key map "a" 'shr-show-alt-text) | |
141 | (define-key map "i" 'shr-browse-image) | |
89b163db | 142 | (define-key map "z" 'shr-zoom-image) |
7304e4dd LMI |
143 | (define-key map [tab] 'shr-next-link) |
144 | (define-key map [backtab] 'shr-previous-link) | |
970ad972 | 145 | (define-key map [follow-link] 'mouse-face) |
1211de50 | 146 | (define-key map [mouse-2] 'shr-browse-url) |
71e691a5 | 147 | (define-key map "I" 'shr-insert-image) |
16f74f10 | 148 | (define-key map "w" 'shr-copy-url) |
2d756ae0 | 149 | (define-key map "u" 'shr-copy-url) |
71e691a5 | 150 | (define-key map "v" 'shr-browse-url) |
cdf1fca4 | 151 | (define-key map "o" 'shr-save-contents) |
71e691a5 G |
152 | (define-key map "\r" 'shr-browse-url) |
153 | map)) | |
154 | ||
66627fa9 | 155 | ;; Public functions and commands. |
0143b8a3 GM |
156 | (declare-function libxml-parse-html-region "xml.c" |
157 | (start end &optional base-url)) | |
66627fa9 | 158 | |
7b953864 SM |
159 | (defun shr-render-buffer (buffer) |
160 | "Display the HTML rendering of the current buffer." | |
161 | (interactive (list (current-buffer))) | |
0143b8a3 GM |
162 | (or (fboundp 'libxml-parse-html-region) |
163 | (error "This function requires Emacs to be compiled with libxml2")) | |
1518e4f0 G |
164 | (pop-to-buffer "*html*") |
165 | (erase-buffer) | |
166 | (shr-insert-document | |
7b953864 | 167 | (with-current-buffer buffer |
edd9679c LMI |
168 | (libxml-parse-html-region (point-min) (point-max)))) |
169 | (goto-char (point-min))) | |
1518e4f0 | 170 | |
eadb6068 IK |
171 | (defun shr-render-region (begin end &optional buffer) |
172 | "Display the HTML rendering of the region between BEGIN and END." | |
173 | (interactive "r") | |
174 | (unless (fboundp 'libxml-parse-html-region) | |
175 | (error "This function requires Emacs to be compiled with libxml2")) | |
176 | (with-current-buffer (or buffer (current-buffer)) | |
177 | (let ((dom (libxml-parse-html-region begin end))) | |
178 | (delete-region begin end) | |
179 | (goto-char begin) | |
180 | (shr-insert-document dom)))) | |
181 | ||
66627fa9 G |
182 | ;;;###autoload |
183 | (defun shr-insert-document (dom) | |
9ed5a258 LI |
184 | "Render the parsed document DOM into the current buffer. |
185 | DOM should be a parse tree as generated by | |
186 | `libxml-parse-html-region' or similar." | |
3d319c8f | 187 | (setq shr-content-cache nil) |
9ed5a258 LI |
188 | (let ((start (point)) |
189 | (shr-state nil) | |
bb7f5cbc | 190 | (shr-start nil) |
dbd5ffad | 191 | (shr-base nil) |
924d6997 | 192 | (shr-width (or shr-width (1- (window-width))))) |
9ed5a258 LI |
193 | (shr-descend (shr-transform-dom dom)) |
194 | (shr-remove-trailing-whitespace start (point)))) | |
195 | ||
196 | (defun shr-remove-trailing-whitespace (start end) | |
7c4bbb69 LI |
197 | (let ((width (window-width))) |
198 | (save-restriction | |
199 | (narrow-to-region start end) | |
200 | (goto-char start) | |
201 | (while (not (eobp)) | |
202 | (end-of-line) | |
888ab661 | 203 | (when (> (shr-previous-newline-padding-width (current-column)) width) |
7c4bbb69 LI |
204 | (dolist (overlay (overlays-at (point))) |
205 | (when (overlay-get overlay 'before-string) | |
206 | (overlay-put overlay 'before-string nil)))) | |
207 | (forward-line 1))))) | |
66627fa9 G |
208 | |
209 | (defun shr-copy-url () | |
210 | "Copy the URL under point to the kill ring. | |
211 | If called twice, then try to fetch the URL and see whether it | |
212 | redirects somewhere else." | |
213 | (interactive) | |
214 | (let ((url (get-text-property (point) 'shr-url))) | |
215 | (cond | |
216 | ((not url) | |
217 | (message "No URL under point")) | |
218 | ;; Resolve redirected URLs. | |
219 | ((equal url (car kill-ring)) | |
220 | (url-retrieve | |
221 | url | |
222 | (lambda (a) | |
223 | (when (and (consp a) | |
224 | (eq (car a) :redirect)) | |
225 | (with-temp-buffer | |
226 | (insert (cadr a)) | |
227 | (goto-char (point-min)) | |
228 | ;; Remove common tracking junk from the URL. | |
229 | (when (re-search-forward ".utm_.*" nil t) | |
230 | (replace-match "" t t)) | |
231 | (message "Copied %s" (buffer-string)) | |
038b3495 LI |
232 | (copy-region-as-kill (point-min) (point-max))))) |
233 | nil t)) | |
66627fa9 G |
234 | ;; Copy the URL to the kill ring. |
235 | (t | |
236 | (with-temp-buffer | |
237 | (insert url) | |
238 | (copy-region-as-kill (point-min) (point-max)) | |
239 | (message "Copied %s" url)))))) | |
240 | ||
7304e4dd LMI |
241 | (defun shr-next-link () |
242 | "Skip to the next link." | |
243 | (interactive) | |
be2aa135 | 244 | (let ((skip (text-property-any (point) (point-max) 'help-echo nil))) |
7304e4dd | 245 | (if (not (setq skip (text-property-not-all skip (point-max) |
be2aa135 | 246 | 'help-echo nil))) |
7304e4dd LMI |
247 | (message "No next link") |
248 | (goto-char skip) | |
249 | (message "%s" (get-text-property (point) 'help-echo))))) | |
250 | ||
251 | (defun shr-previous-link () | |
252 | "Skip to the previous link." | |
253 | (interactive) | |
254 | (let ((start (point)) | |
255 | (found nil)) | |
256 | ;; Skip past the current link. | |
257 | (while (and (not (bobp)) | |
be2aa135 | 258 | (get-text-property (point) 'help-echo)) |
7304e4dd LMI |
259 | (forward-char -1)) |
260 | ;; Find the previous link. | |
261 | (while (and (not (bobp)) | |
be2aa135 | 262 | (not (setq found (get-text-property (point) 'help-echo)))) |
7304e4dd LMI |
263 | (forward-char -1)) |
264 | (if (not found) | |
265 | (progn | |
266 | (message "No previous link") | |
267 | (goto-char start)) | |
268 | ;; Put point at the start of the link. | |
269 | (while (and (not (bobp)) | |
be2aa135 | 270 | (get-text-property (point) 'help-echo)) |
7304e4dd LMI |
271 | (forward-char -1)) |
272 | (forward-char 1) | |
273 | (message "%s" (get-text-property (point) 'help-echo))))) | |
274 | ||
66627fa9 G |
275 | (defun shr-show-alt-text () |
276 | "Show the ALT text of the image under point." | |
277 | (interactive) | |
278 | (let ((text (get-text-property (point) 'shr-alt))) | |
279 | (if (not text) | |
280 | (message "No image under point") | |
281 | (message "%s" text)))) | |
282 | ||
2da9c605 G |
283 | (defun shr-browse-image (&optional copy-url) |
284 | "Browse the image under point. | |
285 | If COPY-URL (the prefix if called interactively) is non-nil, copy | |
286 | the URL of the image to the kill buffer instead." | |
287 | (interactive "P") | |
8b6f6573 | 288 | (let ((url (get-text-property (point) 'image-url))) |
2da9c605 G |
289 | (cond |
290 | ((not url) | |
291 | (message "No image under point")) | |
292 | (copy-url | |
293 | (with-temp-buffer | |
294 | (insert url) | |
295 | (copy-region-as-kill (point-min) (point-max)) | |
296 | (message "Copied %s" url))) | |
297 | (t | |
66627fa9 | 298 | (message "Browsing %s..." url) |
2da9c605 | 299 | (browse-url url))))) |
66627fa9 | 300 | |
3d319c8f LMI |
301 | (defun shr-insert-image () |
302 | "Insert the image under point into the buffer." | |
303 | (interactive) | |
8b6f6573 | 304 | (let ((url (get-text-property (point) 'image-url))) |
3d319c8f LMI |
305 | (if (not url) |
306 | (message "No image under point") | |
307 | (message "Inserting %s..." url) | |
308 | (url-retrieve url 'shr-image-fetched | |
309 | (list (current-buffer) (1- (point)) (point-marker)) | |
038b3495 | 310 | t t)))) |
3d319c8f | 311 | |
89b163db G |
312 | (defun shr-zoom-image () |
313 | "Toggle the image size. | |
314 | The size will be rotated between the default size, the original | |
315 | size, and full-buffer size." | |
316 | (interactive) | |
317 | (let ((url (get-text-property (point) 'image-url)) | |
318 | (size (get-text-property (point) 'image-size)) | |
319 | (buffer-read-only nil)) | |
320 | (if (not url) | |
321 | (message "No image under point") | |
322 | ;; Delete the old picture. | |
323 | (while (get-text-property (point) 'image-url) | |
324 | (forward-char -1)) | |
325 | (forward-char 1) | |
326 | (let ((start (point))) | |
327 | (while (get-text-property (point) 'image-url) | |
328 | (forward-char 1)) | |
329 | (forward-char -1) | |
330 | (put-text-property start (point) 'display nil) | |
331 | (when (> (- (point) start) 2) | |
332 | (delete-region start (1- (point))))) | |
333 | (message "Inserting %s..." url) | |
334 | (url-retrieve url 'shr-image-fetched | |
335 | (list (current-buffer) (1- (point)) (point-marker) | |
336 | (list (cons 'size | |
337 | (cond ((or (eq size 'default) | |
338 | (null size)) | |
339 | 'original) | |
340 | ((eq size 'original) | |
341 | 'full) | |
342 | ((eq size 'full) | |
343 | 'default))))) | |
344 | t)))) | |
345 | ||
66627fa9 G |
346 | ;;; Utility functions. |
347 | ||
870409d4 G |
348 | (defun shr-transform-dom (dom) |
349 | (let ((result (list (pop dom)))) | |
350 | (dolist (arg (pop dom)) | |
351 | (push (cons (intern (concat ":" (symbol-name (car arg))) obarray) | |
352 | (cdr arg)) | |
353 | result)) | |
354 | (dolist (sub dom) | |
355 | (if (stringp sub) | |
953d41c4 | 356 | (push (cons 'text sub) result) |
870409d4 G |
357 | (push (shr-transform-dom sub) result))) |
358 | (nreverse result))) | |
359 | ||
870409d4 | 360 | (defun shr-descend (dom) |
2644071e LMI |
361 | (let ((function |
362 | (or | |
363 | ;; Allow other packages to override (or provide) rendering | |
364 | ;; of elements. | |
365 | (cdr (assq (car dom) shr-external-rendering-functions)) | |
366 | (intern (concat "shr-tag-" (symbol-name (car dom))) obarray))) | |
ebe79557 | 367 | (style (cdr (assq :style (cdr dom)))) |
04db63bc | 368 | (shr-stylesheet shr-stylesheet) |
ebe79557 | 369 | (start (point))) |
b31b26b4 | 370 | (when style |
be2aa135 | 371 | (if (string-match "color\\|display\\|border-collapse" style) |
b31b26b4 G |
372 | (setq shr-stylesheet (nconc (shr-parse-style style) |
373 | shr-stylesheet)) | |
374 | (setq style nil))) | |
e9145444 | 375 | ;; If we have a display:none, then just ignore this part of the DOM. |
c74cb344 G |
376 | (unless (equal (cdr (assq 'display shr-stylesheet)) "none") |
377 | (if (fboundp function) | |
378 | (funcall function (cdr dom)) | |
379 | (shr-generic (cdr dom))) | |
380 | (when (and shr-target-id | |
381 | (equal (cdr (assq :id (cdr dom))) shr-target-id)) | |
382 | (put-text-property start (1+ start) 'shr-target-id shr-target-id)) | |
383 | ;; If style is set, then this node has set the color. | |
384 | (when style | |
385 | (shr-colorize-region start (point) | |
386 | (cdr (assq 'color shr-stylesheet)) | |
387 | (cdr (assq 'background-color shr-stylesheet))))))) | |
870409d4 G |
388 | |
389 | (defun shr-generic (cont) | |
390 | (dolist (sub cont) | |
391 | (cond | |
953d41c4 | 392 | ((eq (car sub) 'text) |
870409d4 | 393 | (shr-insert (cdr sub))) |
a41c2e6d | 394 | ((listp (cdr sub)) |
870409d4 G |
395 | (shr-descend sub))))) |
396 | ||
ed797193 G |
397 | (defmacro shr-char-breakable-p (char) |
398 | "Return non-nil if a line can be broken before and after CHAR." | |
399 | `(aref fill-find-break-point-function-table ,char)) | |
400 | (defmacro shr-char-nospace-p (char) | |
401 | "Return non-nil if no space is required before and after CHAR." | |
402 | `(aref fill-nospace-between-words-table ,char)) | |
403 | ||
404 | ;; KINSOKU is a Japanese word meaning a rule that should not be violated. | |
405 | ;; In Emacs, it is a term used for characters, e.g. punctuation marks, | |
406 | ;; parentheses, and so on, that should not be placed in the beginning | |
407 | ;; of a line or the end of a line. | |
408 | (defmacro shr-char-kinsoku-bol-p (char) | |
409 | "Return non-nil if a line ought not to begin with CHAR." | |
c6c20584 LI |
410 | `(let ((char ,char)) |
411 | (and (not (eq char ?')) | |
412 | (aref (char-category-set char) ?>)))) | |
ed797193 G |
413 | (defmacro shr-char-kinsoku-eol-p (char) |
414 | "Return non-nil if a line ought not to end with CHAR." | |
415 | `(aref (char-category-set ,char) ?<)) | |
416 | (unless (shr-char-kinsoku-bol-p (make-char 'japanese-jisx0208 33 35)) | |
417 | (load "kinsoku" nil t)) | |
418 | ||
66627fa9 | 419 | (defun shr-insert (text) |
6b7df8d3 | 420 | (when (and (eq shr-state 'image) |
89b163db | 421 | (not (bolp)) |
6b7df8d3 | 422 | (not (string-match "\\`[ \t\n]+\\'" text))) |
66627fa9 G |
423 | (insert "\n") |
424 | (setq shr-state nil)) | |
425 | (cond | |
426 | ((eq shr-folding-mode 'none) | |
427 | (insert text)) | |
428 | (t | |
c38e0c97 | 429 | (when (and (string-match "\\`[ \t\n ]" text) |
73db8b08 KY |
430 | (not (bolp)) |
431 | (not (eq (char-after (1- (point))) ? ))) | |
432 | (insert " ")) | |
c38e0c97 | 433 | (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t)) |
73db8b08 KY |
434 | (when (and (bolp) |
435 | (> shr-indentation 0)) | |
436 | (shr-indent)) | |
73db8b08 | 437 | ;; No space is needed behind a wide character categorized as |
b41c2f65 KY |
438 | ;; kinsoku-bol, between characters both categorized as nospace, |
439 | ;; or at the beginning of a line. | |
73db8b08 | 440 | (let (prev) |
48ba8195 KY |
441 | (when (and (> (current-column) shr-indentation) |
442 | (eq (preceding-char) ? ) | |
20438017 | 443 | (or (= (line-beginning-position) (1- (point))) |
ed797193 G |
444 | (and (shr-char-breakable-p |
445 | (setq prev (char-after (- (point) 2)))) | |
446 | (shr-char-kinsoku-bol-p prev)) | |
447 | (and (shr-char-nospace-p prev) | |
448 | (shr-char-nospace-p (aref elem 0))))) | |
73db8b08 | 449 | (delete-char -1))) |
48ba8195 KY |
450 | ;; The shr-start is a special variable that is used to pass |
451 | ;; upwards the first point in the buffer where the text really | |
452 | ;; starts. | |
453 | (unless shr-start | |
454 | (setq shr-start (point))) | |
73db8b08 | 455 | (insert elem) |
e76917e6 | 456 | (setq shr-state nil) |
e7102c0a | 457 | (let (found) |
f7f071dc LI |
458 | (when (and (> (current-column) shr-width) |
459 | (progn | |
460 | (setq found (shr-find-fill-point)) | |
461 | (not (eolp)))) | |
fe98a42f KY |
462 | (when (eq (preceding-char) ? ) |
463 | (delete-char -1)) | |
464 | (insert "\n") | |
465 | (unless found | |
e7102c0a KY |
466 | ;; No space is needed at the beginning of a line. |
467 | (when (eq (following-char) ? ) | |
468 | (delete-char 1))) | |
469 | (when (> shr-indentation 0) | |
470 | (shr-indent)) | |
471 | (end-of-line)) | |
472 | (insert " "))) | |
c38e0c97 | 473 | (unless (string-match "[ \t\r\n ]\\'" text) |
73db8b08 | 474 | (delete-char -1))))) |
66627fa9 | 475 | |
6b7df8d3 | 476 | (defun shr-find-fill-point () |
83ffd571 KY |
477 | (when (> (move-to-column shr-width) shr-width) |
478 | (backward-char 1)) | |
ed797193 G |
479 | (let ((bp (point)) |
480 | failed) | |
481 | (while (not (or (setq failed (= (current-column) shr-indentation)) | |
482 | (eq (preceding-char) ? ) | |
483 | (eq (following-char) ? ) | |
484 | (shr-char-breakable-p (preceding-char)) | |
485 | (shr-char-breakable-p (following-char)) | |
c6c20584 LI |
486 | (and (shr-char-kinsoku-bol-p (preceding-char)) |
487 | (shr-char-breakable-p (following-char)) | |
488 | (not (shr-char-kinsoku-bol-p (following-char)))) | |
ed797193 | 489 | (shr-char-kinsoku-eol-p (following-char)))) |
83ffd571 KY |
490 | (backward-char 1)) |
491 | (if failed | |
20438017 | 492 | ;; There's no breakable point, so we give it up. |
ed797193 G |
493 | (let (found) |
494 | (goto-char bp) | |
495 | (unless shr-kinsoku-shorten | |
c6c20584 LI |
496 | (while (setq found (re-search-forward |
497 | "\\(\\c>\\)\\| \\|\\c<\\|\\c|" | |
498 | (line-end-position) 'move))) | |
ed797193 G |
499 | (if (and found (not (match-beginning 1))) |
500 | (goto-char (match-beginning 0))))) | |
b40950bf KY |
501 | (or |
502 | (eolp) | |
ed797193 G |
503 | ;; Don't put kinsoku-bol characters at the beginning of a line, |
504 | ;; or kinsoku-eol characters at the end of a line. | |
505 | (cond | |
506 | (shr-kinsoku-shorten | |
507 | (while (and (not (memq (preceding-char) (list ?\C-@ ?\n ? ))) | |
508 | (shr-char-kinsoku-eol-p (preceding-char))) | |
509 | (backward-char 1)) | |
510 | (when (setq failed (= (current-column) shr-indentation)) | |
511 | ;; There's no breakable point that doesn't violate kinsoku, | |
512 | ;; so we look for the second best position. | |
513 | (while (and (progn | |
514 | (forward-char 1) | |
515 | (<= (current-column) shr-width)) | |
516 | (progn | |
517 | (setq bp (point)) | |
518 | (shr-char-kinsoku-eol-p (following-char))))) | |
519 | (goto-char bp))) | |
520 | ((shr-char-kinsoku-eol-p (preceding-char)) | |
89b163db G |
521 | ;; Find backward the point where kinsoku-eol characters begin. |
522 | (let ((count 4)) | |
523 | (while | |
524 | (progn | |
525 | (backward-char 1) | |
526 | (and (> (setq count (1- count)) 0) | |
527 | (not (memq (preceding-char) (list ?\C-@ ?\n ? ))) | |
528 | (or (shr-char-kinsoku-eol-p (preceding-char)) | |
529 | (shr-char-kinsoku-bol-p (following-char))))))) | |
f7f071dc LI |
530 | (when (setq failed (= (current-column) shr-indentation)) |
531 | ;; There's no breakable point that doesn't violate kinsoku, | |
532 | ;; so we go to the second best position. | |
533 | (if (looking-at "\\(\\c<+\\)\\c<") | |
534 | (goto-char (match-end 1)) | |
535 | (forward-char 1)))) | |
c6c20584 | 536 | ((shr-char-kinsoku-bol-p (following-char)) |
89b163db G |
537 | ;; Find forward the point where kinsoku-bol characters end. |
538 | (let ((count 4)) | |
539 | (while (progn | |
540 | (forward-char 1) | |
541 | (and (>= (setq count (1- count)) 0) | |
ed797193 | 542 | (shr-char-kinsoku-bol-p (following-char)) |
89b163db | 543 | (shr-char-breakable-p (following-char)))))))) |
ed797193 G |
544 | (when (eq (following-char) ? ) |
545 | (forward-char 1)))) | |
546 | (not failed))) | |
6b7df8d3 | 547 | |
c74cb344 G |
548 | (defun shr-parse-base (url) |
549 | ;; Always chop off anchors. | |
550 | (when (string-match "#.*" url) | |
551 | (setq url (substring url 0 (match-beginning 0)))) | |
552 | (let* ((parsed (url-generic-parse-url url)) | |
553 | (local (url-filename parsed))) | |
554 | (setf (url-filename parsed) "") | |
555 | ;; Chop off the bit after the last slash. | |
556 | (when (string-match "\\`\\(.*/\\)[^/]+\\'" local) | |
557 | (setq local (match-string 1 local))) | |
558 | ;; Always make the local bit end with a slash. | |
559 | (when (and (not (zerop (length local))) | |
560 | (not (eq (aref local (1- (length local))) ?/))) | |
561 | (setq local (concat local "/"))) | |
562 | (list (url-recreate-url parsed) | |
563 | local | |
564 | (url-type parsed) | |
565 | url))) | |
566 | ||
567 | (defun shr-expand-url (url &optional base) | |
568 | (setq base | |
569 | (if base | |
570 | (shr-parse-base base) | |
571 | ;; Bound by the parser. | |
572 | shr-base)) | |
573 | (when (zerop (length url)) | |
574 | (setq url nil)) | |
575 | (cond ((or (not url) | |
576 | (not base) | |
577 | (string-match "\\`[a-z]*:" url)) | |
578 | ;; Absolute URL. | |
579 | (or url (car base))) | |
580 | ((eq (aref url 0) ?/) | |
581 | (if (and (> (length url) 1) | |
582 | (eq (aref url 1) ?/)) | |
583 | ;; //host...; just use the protocol | |
584 | (concat (nth 2 base) ":" url) | |
585 | ;; Just use the host name part. | |
586 | (concat (car base) url))) | |
587 | ((eq (aref url 0) ?#) | |
588 | ;; A link to an anchor. | |
589 | (concat (nth 3 base) url)) | |
590 | (t | |
591 | ;; Totally relative. | |
592 | (concat (car base) (cadr base) url)))) | |
dbd5ffad | 593 | |
66627fa9 G |
594 | (defun shr-ensure-newline () |
595 | (unless (zerop (current-column)) | |
596 | (insert "\n"))) | |
a41c2e6d G |
597 | |
598 | (defun shr-ensure-paragraph () | |
599 | (unless (bobp) | |
f7aa248a | 600 | (if (<= (current-column) shr-indentation) |
71e691a5 G |
601 | (unless (save-excursion |
602 | (forward-line -1) | |
603 | (looking-at " *$")) | |
a41c2e6d G |
604 | (insert "\n")) |
605 | (if (save-excursion | |
606 | (beginning-of-line) | |
be2aa135 LMI |
607 | ;; If the current line is totally blank, and doesn't even |
608 | ;; have any face properties set, then delete the blank | |
609 | ;; space. | |
610 | (and (looking-at " *$") | |
611 | (not (get-text-property (point) 'face)) | |
612 | (not (= (next-single-property-change (point) 'face nil | |
613 | (line-end-position)) | |
614 | (line-end-position))))) | |
89b163db | 615 | (delete-region (match-beginning 0) (match-end 0)) |
a41c2e6d G |
616 | (insert "\n\n"))))) |
617 | ||
66627fa9 | 618 | (defun shr-indent () |
f7aa248a G |
619 | (when (> shr-indentation 0) |
620 | (insert (make-string shr-indentation ? )))) | |
870409d4 | 621 | |
a41c2e6d | 622 | (defun shr-fontize-cont (cont &rest types) |
870409d4 G |
623 | (let (shr-start) |
624 | (shr-generic cont) | |
a41c2e6d G |
625 | (dolist (type types) |
626 | (shr-add-font (or shr-start (point)) (point) type)))) | |
870409d4 | 627 | |
7304e4dd LMI |
628 | ;; Add face to the region, but avoid putting the font properties on |
629 | ;; blank text at the start of the line, and the newline at the end, to | |
630 | ;; avoid ugliness. | |
870409d4 | 631 | (defun shr-add-font (start end type) |
be2aa135 LMI |
632 | (unless shr-inhibit-decoration |
633 | (save-excursion | |
634 | (goto-char start) | |
635 | (while (< (point) end) | |
636 | (when (bolp) | |
637 | (skip-chars-forward " ")) | |
638 | (add-face-text-property (point) (min (line-end-position) end) type t) | |
639 | (if (< (line-end-position) end) | |
640 | (forward-line 1) | |
641 | (goto-char end)))))) | |
870409d4 | 642 | |
8ba8eec5 LMI |
643 | (defun shr-mouse-browse-url (ev) |
644 | "Browse the URL under the mouse cursor." | |
645 | (interactive "e") | |
646 | (mouse-set-point ev) | |
647 | (shr-browse-url)) | |
648 | ||
1211de50 | 649 | (defun shr-browse-url (&optional external mouse-event) |
bdaa086b LMI |
650 | "Browse the URL under point. |
651 | If EXTERNAL, browse the URL using `shr-external-browser'." | |
1211de50 LMI |
652 | (interactive (list current-prefix-arg last-nonmenu-event)) |
653 | (mouse-set-point mouse-event) | |
71e691a5 | 654 | (let ((url (get-text-property (point) 'shr-url))) |
181cb5fb G |
655 | (cond |
656 | ((not url) | |
657 | (message "No link under point")) | |
658 | ((string-match "^mailto:" url) | |
39ddff39 | 659 | (browse-url-mail url)) |
181cb5fb | 660 | (t |
bdaa086b LMI |
661 | (if external |
662 | (funcall shr-external-browser url) | |
663 | (browse-url url)))))) | |
71e691a5 | 664 | |
cdf1fca4 LMI |
665 | (defun shr-save-contents (directory) |
666 | "Save the contents from URL in a file." | |
667 | (interactive "DSave contents of URL to directory: ") | |
668 | (let ((url (get-text-property (point) 'shr-url))) | |
669 | (if (not url) | |
670 | (message "No link under point") | |
671 | (url-retrieve (shr-encode-url url) | |
038b3495 LI |
672 | 'shr-store-contents (list url directory) |
673 | nil t)))) | |
cdf1fca4 LMI |
674 | |
675 | (defun shr-store-contents (status url directory) | |
676 | (unless (plist-get status :error) | |
677 | (when (or (search-forward "\n\n" nil t) | |
678 | (search-forward "\r\n\r\n" nil t)) | |
679 | (write-region (point) (point-max) | |
680 | (expand-file-name (file-name-nondirectory url) | |
681 | directory))))) | |
682 | ||
89b163db | 683 | (defun shr-image-fetched (status buffer start end &optional flags) |
0e2cebe5 LI |
684 | (let ((image-buffer (current-buffer))) |
685 | (when (and (buffer-name buffer) | |
686 | (not (plist-get status :error))) | |
687 | (url-store-in-cache image-buffer) | |
688 | (when (or (search-forward "\n\n" nil t) | |
689 | (search-forward "\r\n\r\n" nil t)) | |
21c58ae2 | 690 | (let ((data (shr-parse-image-data))) |
0e2cebe5 LI |
691 | (with-current-buffer buffer |
692 | (save-excursion | |
693 | (let ((alt (buffer-substring start end)) | |
89b163db | 694 | (properties (text-properties-at start)) |
0e2cebe5 LI |
695 | (inhibit-read-only t)) |
696 | (delete-region start end) | |
697 | (goto-char start) | |
89b163db G |
698 | (funcall shr-put-image-function data alt flags) |
699 | (while properties | |
700 | (let ((type (pop properties)) | |
701 | (value (pop properties))) | |
702 | (unless (memq type '(display image-size)) | |
703 | (put-text-property start (point) type value)))))))))) | |
0e2cebe5 | 704 | (kill-buffer image-buffer))) |
870409d4 | 705 | |
2250b351 DE |
706 | (defun shr-image-from-data (data) |
707 | "Return an image from the data: URI content DATA." | |
708 | (when (string-match | |
709 | "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)" | |
710 | data) | |
711 | (let ((param (match-string 4 data)) | |
712 | (payload (url-unhex-string (match-string 5 data)))) | |
713 | (when (string-match "^.*\\(;[ \t]*base64\\)$" param) | |
714 | (setq payload (base64-decode-string payload))) | |
715 | payload))) | |
716 | ||
986ed135 GM |
717 | ;; Behind display-graphic-p test. |
718 | (declare-function image-size "image.c" (spec &optional pixels frame)) | |
719 | (declare-function image-animate "image" (image &optional index limit)) | |
720 | ||
21c58ae2 LMI |
721 | (defun shr-put-image (spec alt &optional flags) |
722 | "Insert image SPEC with a string ALT. Return image. | |
723 | SPEC is either an image data blob, or a list where the first | |
724 | element is the data blob and the second element is the content-type." | |
4abff904 | 725 | (if (display-graphic-p) |
89b163db | 726 | (let* ((size (cdr (assq 'size flags))) |
21c58ae2 LMI |
727 | (data (if (consp spec) |
728 | (car spec) | |
729 | spec)) | |
730 | (content-type (and (consp spec) | |
731 | (cadr spec))) | |
89b163db G |
732 | (start (point)) |
733 | (image (cond | |
734 | ((eq size 'original) | |
21c58ae2 | 735 | (create-image data nil t :ascent 100 |
c6b7ccaa | 736 | :format content-type)) |
89b163db G |
737 | ((eq size 'full) |
738 | (ignore-errors | |
e9145444 | 739 | (shr-rescale-image data content-type))) |
89b163db G |
740 | (t |
741 | (ignore-errors | |
e9145444 | 742 | (shr-rescale-image data content-type)))))) |
4abff904 | 743 | (when image |
c0f9edce G |
744 | ;; When inserting big-ish pictures, put them at the |
745 | ;; beginning of the line. | |
746 | (when (and (> (current-column) 0) | |
747 | (> (car (image-size image t)) 400)) | |
748 | (insert "\n")) | |
89b163db | 749 | (if (eq size 'original) |
7304e4dd | 750 | (insert-sliced-image image (or alt "*") nil 20 1) |
89b163db G |
751 | (insert-image image (or alt "*"))) |
752 | (put-text-property start (point) 'image-size size) | |
dd8620de GM |
753 | (when (cond ((fboundp 'image-multi-frame-p) |
754 | ;; Only animate multi-frame things that specify a | |
755 | ;; delay; eg animated gifs as opposed to | |
756 | ;; multi-page tiffs. FIXME? | |
757 | (cdr (image-multi-frame-p image))) | |
758 | ((fboundp 'image-animated-p) | |
759 | (image-animated-p image))) | |
c146ad85 | 760 | (image-animate image nil 60))) |
b9bdaf74 | 761 | image) |
99e65b2d | 762 | (insert alt))) |
870409d4 | 763 | |
e9145444 SM |
764 | (defun shr-rescale-image (data &optional content-type) |
765 | "Rescale DATA, if too big, to fit the current buffer." | |
766 | (if (not (and (fboundp 'imagemagick-types) | |
767 | (get-buffer-window (current-buffer)))) | |
f3f9606c LMI |
768 | (create-image data nil t :ascent 100) |
769 | (let ((edges (window-inside-pixel-edges | |
770 | (get-buffer-window (current-buffer))))) | |
771 | (create-image | |
772 | data 'imagemagick t | |
773 | :ascent 100 | |
774 | :max-width (truncate (* shr-max-image-proportion | |
775 | (- (nth 2 edges) (nth 0 edges)))) | |
776 | :max-height (truncate (* shr-max-image-proportion | |
21c58ae2 | 777 | (- (nth 3 edges) (nth 1 edges)))) |
c6b7ccaa | 778 | :format content-type)))) |
870409d4 | 779 | |
85a45a69 GM |
780 | ;; url-cache-extract autoloads url-cache. |
781 | (declare-function url-cache-create-filename "url-cache" (url)) | |
782 | (autoload 'mm-disable-multibyte "mm-util") | |
39ddff39 | 783 | (autoload 'browse-url-mail "browse-url") |
85a45a69 | 784 | |
870409d4 G |
785 | (defun shr-get-image-data (url) |
786 | "Get image data for URL. | |
787 | Return a string with image data." | |
788 | (with-temp-buffer | |
789 | (mm-disable-multibyte) | |
71e691a5 | 790 | (when (ignore-errors |
ab67634f | 791 | (url-cache-extract (url-cache-create-filename (shr-encode-url url))) |
71e691a5 G |
792 | t) |
793 | (when (or (search-forward "\n\n" nil t) | |
794 | (search-forward "\r\n\r\n" nil t)) | |
21c58ae2 LMI |
795 | (shr-parse-image-data))))) |
796 | ||
797 | (defun shr-parse-image-data () | |
798 | (list | |
799 | (buffer-substring (point) (point-max)) | |
800 | (save-excursion | |
801 | (save-restriction | |
802 | (narrow-to-region (point-min) (point)) | |
803 | (let ((content-type (mail-fetch-field "content-type"))) | |
804 | (and content-type | |
805 | (intern content-type obarray))))))) | |
870409d4 | 806 | |
40de2c6d KY |
807 | (defun shr-image-displayer (content-function) |
808 | "Return a function to display an image. | |
809 | CONTENT-FUNCTION is a function to retrieve an image for a cid url that | |
810 | is an argument. The function to be returned takes three arguments URL, | |
53964682 | 811 | START, and END. Note that START and END should be markers." |
40de2c6d | 812 | `(lambda (url start end) |
f8d8a97b KY |
813 | (when url |
814 | (if (string-match "\\`cid:" url) | |
815 | ,(when content-function | |
816 | `(let ((image (funcall ,content-function | |
817 | (substring url (match-end 0))))) | |
818 | (when image | |
819 | (goto-char start) | |
b9bdaf74 | 820 | (funcall shr-put-image-function |
195b2593 | 821 | image (buffer-substring start end)) |
e2d0ba98 | 822 | (delete-region (point) end)))) |
f8d8a97b KY |
823 | (url-retrieve url 'shr-image-fetched |
824 | (list (current-buffer) start end) | |
038b3495 | 825 | t t))))) |
40de2c6d | 826 | |
66627fa9 G |
827 | (defun shr-heading (cont &rest types) |
828 | (shr-ensure-paragraph) | |
829 | (apply #'shr-fontize-cont cont types) | |
830 | (shr-ensure-paragraph)) | |
831 | ||
04db63bc | 832 | (defun shr-urlify (start url &optional title) |
ca3cf0a5 | 833 | (shr-add-font start (point) 'shr-link) |
7304e4dd LMI |
834 | (add-text-properties |
835 | start (point) | |
836 | (list 'shr-url url | |
be2aa135 | 837 | 'help-echo (if title (format "%s (%s)" url title) url) |
d50fceab | 838 | 'follow-link t |
5a2a7735 | 839 | 'mouse-face 'highlight |
970ad972 | 840 | 'keymap shr-map))) |
de635afe G |
841 | |
842 | (defun shr-encode-url (url) | |
843 | "Encode URL." | |
844 | (browse-url-url-encode-chars url "[)$ ]")) | |
845 | ||
ebe79557 LMI |
846 | (autoload 'shr-color-visible "shr-color") |
847 | (autoload 'shr-color->hexadecimal "shr-color") | |
144b7b5c G |
848 | |
849 | (defun shr-color-check (fg bg) | |
850 | "Check that FG is visible on BG. | |
851 | Returns (fg bg) with corrected values. | |
852 | Returns nil if the colors that would be used are the default | |
853 | ones, in case fg and bg are nil." | |
854 | (when (or fg bg) | |
855 | (let ((fixed (cond ((null fg) 'fg) | |
856 | ((null bg) 'bg)))) | |
857 | ;; Convert colors to hexadecimal, or set them to default. | |
858 | (let ((fg (or (shr-color->hexadecimal fg) | |
859 | (frame-parameter nil 'foreground-color))) | |
860 | (bg (or (shr-color->hexadecimal bg) | |
861 | (frame-parameter nil 'background-color)))) | |
862 | (cond ((eq fixed 'bg) | |
863 | ;; Only return the new fg | |
864 | (list nil (cadr (shr-color-visible bg fg t)))) | |
865 | ((eq fixed 'fg) | |
866 | ;; Invert args and results and return only the new bg | |
867 | (list (cadr (shr-color-visible fg bg t)) nil)) | |
868 | (t | |
869 | (shr-color-visible bg fg))))))) | |
870 | ||
04db63bc | 871 | (defun shr-colorize-region (start end fg &optional bg) |
be2aa135 LMI |
872 | (when (and (not shr-inhibit-decoration) |
873 | (or fg bg)) | |
04db63bc | 874 | (let ((new-colors (shr-color-check fg bg))) |
144b7b5c | 875 | (when new-colors |
60568d74 | 876 | (when fg |
be2aa135 LMI |
877 | (add-face-text-property start end |
878 | (list :foreground (cadr new-colors)) | |
879 | t)) | |
04db63bc | 880 | (when bg |
be2aa135 LMI |
881 | (add-face-text-property start end |
882 | (list :background (car new-colors)) | |
883 | t))) | |
ec72bf63 | 884 | new-colors))) |
04db63bc | 885 | |
c5ecc769 G |
886 | (defun shr-expand-newlines (start end color) |
887 | (save-restriction | |
d709b79a LI |
888 | ;; Skip past all white space at the start and ends. |
889 | (goto-char start) | |
890 | (skip-chars-forward " \t\n") | |
891 | (beginning-of-line) | |
892 | (setq start (point)) | |
893 | (goto-char end) | |
894 | (skip-chars-backward " \t\n") | |
895 | (forward-line 1) | |
896 | (setq end (point)) | |
c5ecc769 | 897 | (narrow-to-region start end) |
160ae063 | 898 | (let ((width (shr-buffer-width)) |
c5ecc769 G |
899 | column) |
900 | (goto-char (point-min)) | |
901 | (while (not (eobp)) | |
902 | (end-of-line) | |
19e0dbe0 KY |
903 | (when (and (< (setq column (current-column)) width) |
904 | (< (setq column (shr-previous-newline-padding-width column)) | |
d709b79a | 905 | width)) |
7304e4dd | 906 | (let ((overlay (make-overlay (point) (1+ (point))))) |
c5ecc769 | 907 | (overlay-put overlay 'before-string |
d709b79a LI |
908 | (concat |
909 | (mapconcat | |
910 | (lambda (overlay) | |
14596870 KY |
911 | (let ((string (plist-get |
912 | (overlay-properties overlay) | |
913 | 'before-string))) | |
d709b79a LI |
914 | (if (not string) |
915 | "" | |
916 | (overlay-put overlay 'before-string "") | |
917 | string))) | |
918 | (overlays-at (point)) | |
919 | "") | |
19e0dbe0 | 920 | (propertize (make-string (- width column) ? ) |
d709b79a | 921 | 'face (list :background color)))))) |
c5ecc769 | 922 | (forward-line 1))))) |
04db63bc | 923 | |
d709b79a LI |
924 | (defun shr-previous-newline-padding-width (width) |
925 | (let ((overlays (overlays-at (point))) | |
926 | (previous-width 0)) | |
927 | (if (null overlays) | |
928 | width | |
929 | (dolist (overlay overlays) | |
930 | (setq previous-width | |
931 | (+ previous-width | |
14596870 KY |
932 | (length (plist-get (overlay-properties overlay) |
933 | 'before-string))))) | |
d709b79a LI |
934 | (+ width previous-width)))) |
935 | ||
66627fa9 G |
936 | ;;; Tag-specific rendering rules. |
937 | ||
144b7b5c | 938 | (defun shr-tag-body (cont) |
04db63bc | 939 | (let* ((start (point)) |
af4e5f4c G |
940 | (fgcolor (cdr (or (assq :fgcolor cont) |
941 | (assq :text cont)))) | |
04db63bc | 942 | (bgcolor (cdr (assq :bgcolor cont))) |
b31b26b4 G |
943 | (shr-stylesheet (list (cons 'color fgcolor) |
944 | (cons 'background-color bgcolor)))) | |
144b7b5c | 945 | (shr-generic cont) |
04db63bc | 946 | (shr-colorize-region start (point) fgcolor bgcolor))) |
144b7b5c | 947 | |
e9145444 | 948 | (defun shr-tag-style (_cont) |
b31b26b4 G |
949 | ) |
950 | ||
e9145444 | 951 | (defun shr-tag-script (_cont) |
f73341e2 LMI |
952 | ) |
953 | ||
e9145444 | 954 | (defun shr-tag-comment (_cont) |
fb1b0ef6 LMI |
955 | ) |
956 | ||
c74cb344 G |
957 | (defun shr-dom-to-xml (dom) |
958 | "Convert DOM into a string containing the xml representation." | |
959 | (let ((arg " ") | |
960 | (text "")) | |
961 | (dolist (sub (cdr dom)) | |
962 | (cond | |
963 | ((listp (cdr sub)) | |
964 | (setq text (concat text (shr-dom-to-xml sub)))) | |
965 | ((eq (car sub) 'text) | |
966 | (setq text (concat text (cdr sub)))) | |
967 | (t | |
968 | (setq arg (concat arg (format "%s=\"%s\" " | |
969 | (substring (symbol-name (car sub)) 1) | |
970 | (cdr sub))))))) | |
971 | (format "<%s%s>%s</%s>" | |
972 | (car dom) | |
973 | (substring arg 0 (1- (length arg))) | |
974 | text | |
975 | (car dom)))) | |
976 | ||
65e704b9 | 977 | (defun shr-tag-svg (cont) |
c74cb344 G |
978 | (when (image-type-available-p 'svg) |
979 | (funcall shr-put-image-function | |
980 | (shr-dom-to-xml (cons 'svg cont)) | |
981 | "SVG Image"))) | |
65e704b9 | 982 | |
a3af2929 LMI |
983 | (defun shr-tag-sup (cont) |
984 | (let ((start (point))) | |
985 | (shr-generic cont) | |
986 | (put-text-property start (point) 'display '(raise 0.5)))) | |
987 | ||
988 | (defun shr-tag-sub (cont) | |
989 | (let ((start (point))) | |
990 | (shr-generic cont) | |
991 | (put-text-property start (point) 'display '(raise -0.5)))) | |
992 | ||
7bafe9bc LMI |
993 | (defun shr-tag-label (cont) |
994 | (shr-generic cont) | |
995 | (shr-ensure-paragraph)) | |
996 | ||
66627fa9 G |
997 | (defun shr-tag-p (cont) |
998 | (shr-ensure-paragraph) | |
f7aa248a | 999 | (shr-indent) |
66627fa9 G |
1000 | (shr-generic cont) |
1001 | (shr-ensure-paragraph)) | |
1002 | ||
036d93bc KY |
1003 | (defun shr-tag-div (cont) |
1004 | (shr-ensure-newline) | |
1005 | (shr-indent) | |
1006 | (shr-generic cont) | |
1007 | (shr-ensure-newline)) | |
1008 | ||
6eee2678 LMI |
1009 | (defun shr-tag-s (cont) |
1010 | (shr-fontize-cont cont 'shr-strike-through)) | |
1011 | ||
55385ebc JD |
1012 | (defun shr-tag-del (cont) |
1013 | (shr-fontize-cont cont 'shr-strike-through)) | |
1014 | ||
66627fa9 G |
1015 | (defun shr-tag-b (cont) |
1016 | (shr-fontize-cont cont 'bold)) | |
1017 | ||
1018 | (defun shr-tag-i (cont) | |
1019 | (shr-fontize-cont cont 'italic)) | |
1020 | ||
1021 | (defun shr-tag-em (cont) | |
087d8265 | 1022 | (shr-fontize-cont cont 'italic)) |
66627fa9 | 1023 | |
530f7b67 LMI |
1024 | (defun shr-tag-strong (cont) |
1025 | (shr-fontize-cont cont 'bold)) | |
1026 | ||
66627fa9 G |
1027 | (defun shr-tag-u (cont) |
1028 | (shr-fontize-cont cont 'underline)) | |
1029 | ||
2e76c12c LMI |
1030 | (defun shr-parse-style (style) |
1031 | (when style | |
a2994808 JD |
1032 | (save-match-data |
1033 | (when (string-match "\n" style) | |
1034 | (setq style (replace-match " " t t style)))) | |
2e76c12c LMI |
1035 | (let ((plist nil)) |
1036 | (dolist (elem (split-string style ";")) | |
1037 | (when elem | |
1038 | (setq elem (split-string elem ":")) | |
1039 | (when (and (car elem) | |
1040 | (cadr elem)) | |
1041 | (let ((name (replace-regexp-in-string "^ +\\| +$" "" (car elem))) | |
1042 | (value (replace-regexp-in-string "^ +\\| +$" "" (cadr elem)))) | |
144b7b5c G |
1043 | (when (string-match " *!important\\'" value) |
1044 | (setq value (substring value 0 (match-beginning 0)))) | |
2e76c12c LMI |
1045 | (push (cons (intern name obarray) |
1046 | value) | |
1047 | plist))))) | |
1048 | plist))) | |
1049 | ||
dbd5ffad | 1050 | (defun shr-tag-base (cont) |
be2aa135 LMI |
1051 | (let ((base (cdr (assq :href cont)))) |
1052 | (when base | |
1053 | (setq shr-base (shr-parse-base base)))) | |
266c63b5 | 1054 | (shr-generic cont)) |
dbd5ffad | 1055 | |
66627fa9 G |
1056 | (defun shr-tag-a (cont) |
1057 | (let ((url (cdr (assq :href cont))) | |
04db63bc | 1058 | (title (cdr (assq :title cont))) |
66627fa9 G |
1059 | (start (point)) |
1060 | shr-start) | |
1061 | (shr-generic cont) | |
1c4b1e61 | 1062 | (when (and shr-target-id |
e9145444 | 1063 | (equal (cdr (assq :name cont)) shr-target-id)) |
1c4b1e61 LMI |
1064 | ;; We have a zero-length <a name="foo"> element, so just |
1065 | ;; insert... something. | |
1066 | (when (= start (point)) | |
1067 | (shr-ensure-newline) | |
1068 | (insert " ")) | |
1069 | (put-text-property start (1+ start) 'shr-target-id shr-target-id)) | |
be2aa135 LMI |
1070 | (when (and url |
1071 | (not shr-inhibit-decoration)) | |
cc21c235 | 1072 | (shr-urlify (or shr-start start) (shr-expand-url url) title)))) |
de635afe G |
1073 | |
1074 | (defun shr-tag-object (cont) | |
99e65b2d G |
1075 | (let ((start (point)) |
1076 | url) | |
1077 | (dolist (elem cont) | |
1078 | (when (eq (car elem) 'embed) | |
1079 | (setq url (or url (cdr (assq :src (cdr elem)))))) | |
1080 | (when (and (eq (car elem) 'param) | |
1081 | (equal (cdr (assq :name (cdr elem))) "movie")) | |
1082 | (setq url (or url (cdr (assq :value (cdr elem))))))) | |
de635afe G |
1083 | (when url |
1084 | (shr-insert " [multimedia] ") | |
dbd5ffad | 1085 | (shr-urlify start (shr-expand-url url))) |
99e65b2d G |
1086 | (shr-generic cont))) |
1087 | ||
ad9a773c RS |
1088 | (defcustom shr-prefer-media-type-alist '(("webm" . 1.0) |
1089 | ("ogv" . 1.0) | |
1090 | ("ogg" . 1.0) | |
1091 | ("opus" . 1.0) | |
1092 | ("flac" . 0.9) | |
1093 | ("wav" . 0.5)) | |
1094 | "Preferences for media types. | |
1095 | The key element should be a regexp matched against the type of the source or | |
1096 | url if no type is specified. The value should be a float in the range 0.0 to | |
1097 | 1.0. Media elements with higher value are preferred." | |
1098 | :version "24.4" | |
1099 | :group 'shr | |
1100 | :type '(alist :key-type regexp :value-type float)) | |
1101 | ||
1102 | (defun shr--get-media-pref (elem) | |
1103 | "Determine the preference for ELEM. | |
1104 | The preference is a float determined from `shr-prefer-media-type'." | |
1105 | (let ((type (cdr (assq :type elem))) | |
1106 | (p 0.0)) | |
1107 | (unless type | |
1108 | (setq type (cdr (assq :src elem)))) | |
1109 | (when type | |
1110 | (dolist (pref shr-prefer-media-type-alist) | |
1111 | (when (and | |
1112 | (> (cdr pref) p) | |
1113 | (string-match-p (car pref) type)) | |
1114 | (setq p (cdr pref))))) | |
1115 | p)) | |
1116 | ||
1117 | (defun shr--extract-best-source (cont &optional url pref) | |
1118 | "Extract the best `:src' property from <source> blocks in CONT." | |
1119 | (setq pref (or pref -1.0)) | |
1120 | (let (new-pref) | |
1121 | (dolist (elem cont) | |
073d5cfd LMI |
1122 | (when (and (eq (car elem) 'source) |
1123 | (< pref | |
1124 | (setq new-pref | |
1125 | (shr--get-media-pref elem)))) | |
1126 | (setq pref new-pref | |
1127 | url (cdr (assq :src elem))) | |
f224e500 | 1128 | ;; libxml's html parser isn't HTML5 compliant and non terminated |
ad9a773c RS |
1129 | ;; source tags might end up as children. So recursion it is... |
1130 | (dolist (child (cdr elem)) | |
073d5cfd | 1131 | (when (eq (car child) 'source) |
ad9a773c RS |
1132 | (let ((ret (shr--extract-best-source (list child) url pref))) |
1133 | (when (< pref (cdr ret)) | |
1134 | (setq url (car ret) | |
1135 | pref (cdr ret))))))))) | |
1136 | (cons url pref)) | |
1137 | ||
99e65b2d G |
1138 | (defun shr-tag-video (cont) |
1139 | (let ((image (cdr (assq :poster cont))) | |
ad9a773c RS |
1140 | (url (cdr (assq :src cont))) |
1141 | (start (point))) | |
1142 | (unless url | |
1143 | (setq url (car (shr--extract-best-source cont)))) | |
177948a5 RS |
1144 | (if image |
1145 | (shr-tag-img nil image) | |
1146 | (shr-insert " [video] ")) | |
dbd5ffad | 1147 | (shr-urlify start (shr-expand-url url)))) |
ab67634f | 1148 | |
eb2dd24d RS |
1149 | (defun shr-tag-audio (cont) |
1150 | (let ((url (cdr (assq :src cont))) | |
1151 | (start (point))) | |
ad9a773c RS |
1152 | (unless url |
1153 | (setq url (car (shr--extract-best-source cont)))) | |
eb2dd24d RS |
1154 | (shr-insert " [audio] ") |
1155 | (shr-urlify start (shr-expand-url url)))) | |
1156 | ||
99e65b2d G |
1157 | (defun shr-tag-img (cont &optional url) |
1158 | (when (or url | |
1159 | (and cont | |
e330b646 | 1160 | (> (length (cdr (assq :src cont))) 0))) |
68f6bd17 KY |
1161 | (when (and (> (current-column) 0) |
1162 | (not (eq shr-state 'image))) | |
1163 | (insert "\n")) | |
1164 | (let ((alt (cdr (assq :alt cont))) | |
dbd5ffad | 1165 | (url (shr-expand-url (or url (cdr (assq :src cont)))))) |
68f6bd17 KY |
1166 | (let ((start (point-marker))) |
1167 | (when (zerop (length alt)) | |
953d41c4 | 1168 | (setq alt "*")) |
68f6bd17 | 1169 | (cond |
99e65b2d G |
1170 | ((or (member (cdr (assq :height cont)) '("0" "1")) |
1171 | (member (cdr (assq :width cont)) '("0" "1"))) | |
1172 | ;; Ignore zero-sized or single-pixel images. | |
1173 | ) | |
2250b351 DE |
1174 | ((and (not shr-inhibit-images) |
1175 | (string-match "\\`data:" url)) | |
1176 | (let ((image (shr-image-from-data (substring url (match-end 0))))) | |
1177 | (if image | |
1178 | (funcall shr-put-image-function image alt) | |
1179 | (insert alt)))) | |
68f6bd17 KY |
1180 | ((and (not shr-inhibit-images) |
1181 | (string-match "\\`cid:" url)) | |
1182 | (let ((url (substring url (match-end 0))) | |
1183 | image) | |
1184 | (if (or (not shr-content-function) | |
1185 | (not (setq image (funcall shr-content-function url)))) | |
1186 | (insert alt) | |
b9bdaf74 | 1187 | (funcall shr-put-image-function image alt)))) |
68f6bd17 KY |
1188 | ((or shr-inhibit-images |
1189 | (and shr-blocked-images | |
1190 | (string-match shr-blocked-images url))) | |
1191 | (setq shr-start (point)) | |
1192 | (let ((shr-state 'space)) | |
b354bc53 KY |
1193 | (if (> (string-width alt) 8) |
1194 | (shr-insert (truncate-string-to-width alt 8)) | |
68f6bd17 | 1195 | (shr-insert alt)))) |
728518c3 LMI |
1196 | ((and (not shr-ignore-cache) |
1197 | (url-is-cached (shr-encode-url url))) | |
b9bdaf74 | 1198 | (funcall shr-put-image-function (shr-get-image-data url) alt)) |
68f6bd17 | 1199 | (t |
64522086 | 1200 | (insert alt " ") |
728518c3 LMI |
1201 | (when (and shr-ignore-cache |
1202 | (url-is-cached (shr-encode-url url))) | |
1203 | (let ((file (url-cache-create-filename (shr-encode-url url)))) | |
1204 | (when (file-exists-p file) | |
1205 | (delete-file file)))) | |
038b3495 | 1206 | (url-queue-retrieve |
f3b146e9 | 1207 | (shr-encode-url url) 'shr-image-fetched |
64522086 | 1208 | (list (current-buffer) start (set-marker (make-marker) (1- (point)))) |
038b3495 | 1209 | t t))) |
a959fc40 KY |
1210 | (when (zerop shr-table-depth) ;; We are not in a table. |
1211 | (put-text-property start (point) 'keymap shr-map) | |
1212 | (put-text-property start (point) 'shr-alt alt) | |
1213 | (put-text-property start (point) 'image-url url) | |
1214 | (put-text-property start (point) 'image-displayer | |
1215 | (shr-image-displayer shr-content-function)) | |
1216 | (put-text-property start (point) 'help-echo alt)) | |
68f6bd17 | 1217 | (setq shr-state 'image))))) |
66627fa9 G |
1218 | |
1219 | (defun shr-tag-pre (cont) | |
1220 | (let ((shr-folding-mode 'none)) | |
1221 | (shr-ensure-newline) | |
f7aa248a | 1222 | (shr-indent) |
66627fa9 G |
1223 | (shr-generic cont) |
1224 | (shr-ensure-newline))) | |
1225 | ||
1226 | (defun shr-tag-blockquote (cont) | |
1227 | (shr-ensure-paragraph) | |
f7aa248a | 1228 | (shr-indent) |
66627fa9 G |
1229 | (let ((shr-indentation (+ shr-indentation 4))) |
1230 | (shr-generic cont)) | |
1231 | (shr-ensure-paragraph)) | |
a41c2e6d | 1232 | |
d2aa9780 LMI |
1233 | (defun shr-tag-dl (cont) |
1234 | (shr-ensure-paragraph) | |
1235 | (shr-generic cont) | |
1236 | (shr-ensure-paragraph)) | |
1237 | ||
1238 | (defun shr-tag-dt (cont) | |
1239 | (shr-ensure-newline) | |
1240 | (shr-generic cont) | |
1241 | (shr-ensure-newline)) | |
1242 | ||
1243 | (defun shr-tag-dd (cont) | |
1244 | (shr-ensure-newline) | |
1245 | (let ((shr-indentation (+ shr-indentation 4))) | |
1246 | (shr-generic cont))) | |
1247 | ||
a41c2e6d G |
1248 | (defun shr-tag-ul (cont) |
1249 | (shr-ensure-paragraph) | |
1250 | (let ((shr-list-mode 'ul)) | |
3d319c8f LMI |
1251 | (shr-generic cont)) |
1252 | (shr-ensure-paragraph)) | |
a41c2e6d G |
1253 | |
1254 | (defun shr-tag-ol (cont) | |
3d319c8f | 1255 | (shr-ensure-paragraph) |
a41c2e6d | 1256 | (let ((shr-list-mode 1)) |
3d319c8f LMI |
1257 | (shr-generic cont)) |
1258 | (shr-ensure-paragraph)) | |
a41c2e6d G |
1259 | |
1260 | (defun shr-tag-li (cont) | |
c74cb344 | 1261 | (shr-ensure-newline) |
f7aa248a | 1262 | (shr-indent) |
8028ed5c LMI |
1263 | (let* ((bullet |
1264 | (if (numberp shr-list-mode) | |
1265 | (prog1 | |
1266 | (format "%d " shr-list-mode) | |
1267 | (setq shr-list-mode (1+ shr-list-mode))) | |
c74cb344 | 1268 | shr-bullet)) |
8028ed5c LMI |
1269 | (shr-indentation (+ shr-indentation (length bullet)))) |
1270 | (insert bullet) | |
1271 | (shr-generic cont))) | |
a41c2e6d G |
1272 | |
1273 | (defun shr-tag-br (cont) | |
89b163db G |
1274 | (when (and (not (bobp)) |
1275 | ;; Only add a newline if we break the current line, or | |
1276 | ;; the previous line isn't a blank line. | |
1277 | (or (not (bolp)) | |
1278 | (and (> (- (point) 2) (point-min)) | |
1279 | (not (= (char-after (- (point) 2)) ?\n))))) | |
f7aa248a G |
1280 | (insert "\n") |
1281 | (shr-indent)) | |
a41c2e6d G |
1282 | (shr-generic cont)) |
1283 | ||
308c9d24 | 1284 | (defun shr-tag-span (cont) |
be2aa135 | 1285 | (shr-generic cont)) |
308c9d24 | 1286 | |
a41c2e6d | 1287 | (defun shr-tag-h1 (cont) |
e4dbdb09 | 1288 | (shr-heading cont 'bold 'underline)) |
a41c2e6d G |
1289 | |
1290 | (defun shr-tag-h2 (cont) | |
e4dbdb09 | 1291 | (shr-heading cont 'bold)) |
a41c2e6d G |
1292 | |
1293 | (defun shr-tag-h3 (cont) | |
e4dbdb09 | 1294 | (shr-heading cont 'italic)) |
a41c2e6d G |
1295 | |
1296 | (defun shr-tag-h4 (cont) | |
e4dbdb09 | 1297 | (shr-heading cont)) |
a41c2e6d G |
1298 | |
1299 | (defun shr-tag-h5 (cont) | |
e4dbdb09 | 1300 | (shr-heading cont)) |
a41c2e6d G |
1301 | |
1302 | (defun shr-tag-h6 (cont) | |
e4dbdb09 | 1303 | (shr-heading cont)) |
a41c2e6d | 1304 | |
e9145444 | 1305 | (defun shr-tag-hr (_cont) |
3d319c8f | 1306 | (shr-ensure-newline) |
6b7df8d3 | 1307 | (insert (make-string shr-width shr-hr-line) "\n")) |
3d319c8f | 1308 | |
144b7b5c G |
1309 | (defun shr-tag-title (cont) |
1310 | (shr-heading cont 'bold 'underline)) | |
1311 | ||
1110d53b | 1312 | (defun shr-tag-font (cont) |
b31b26b4 G |
1313 | (let* ((start (point)) |
1314 | (color (cdr (assq :color cont))) | |
1315 | (shr-stylesheet (nconc (list (cons 'color color)) | |
1316 | shr-stylesheet))) | |
1110d53b | 1317 | (shr-generic cont) |
b31b26b4 G |
1318 | (when color |
1319 | (shr-colorize-region start (point) color | |
1320 | (cdr (assq 'background-color shr-stylesheet)))))) | |
1110d53b | 1321 | |
66627fa9 | 1322 | ;;; Table rendering algorithm. |
a41c2e6d | 1323 | |
a0ec382a LMI |
1324 | ;; Table rendering is the only complicated thing here. We do this by |
1325 | ;; first counting how many TDs there are in each TR, and registering | |
1326 | ;; how wide they think they should be ("width=45%", etc). Then we | |
1327 | ;; render each TD separately (this is done in temporary buffers, so | |
1328 | ;; that we can use all the rendering machinery as if we were in the | |
1329 | ;; main buffer). Now we know how much space each TD really takes, so | |
1330 | ;; we then render everything again with the new widths, and finally | |
1331 | ;; insert all these boxes into the main buffer. | |
6c769311 | 1332 | (defun shr-tag-table-1 (cont) |
71e691a5 G |
1333 | (setq cont (or (cdr (assq 'tbody cont)) |
1334 | cont)) | |
130e977f | 1335 | (let* ((shr-inhibit-images t) |
99e65b2d | 1336 | (shr-table-depth (1+ shr-table-depth)) |
83ffd571 | 1337 | (shr-kinsoku-shorten t) |
a0ec382a | 1338 | ;; Find all suggested widths. |
130e977f | 1339 | (columns (shr-column-specs cont)) |
a0ec382a | 1340 | ;; Compute how many characters wide each TD should be. |
71e691a5 | 1341 | (suggested-widths (shr-pro-rate-columns columns)) |
a0ec382a LMI |
1342 | ;; Do a "test rendering" to see how big each TD is (this can |
1343 | ;; be smaller (if there's little text) or bigger (if there's | |
1344 | ;; unbreakable text). | |
71e691a5 | 1345 | (sketch (shr-make-table cont suggested-widths)) |
160ae063 LMI |
1346 | ;; Compute the "natural" width by setting each column to 500 |
1347 | ;; characters and see how wide they really render. | |
1348 | (natural (shr-make-table cont (make-vector (length columns) 500))) | |
1349 | (sketch-widths (shr-table-widths sketch natural suggested-widths))) | |
030158f3 | 1350 | ;; This probably won't work very well. |
83ffd571 KY |
1351 | (when (> (+ (loop for width across sketch-widths |
1352 | summing (1+ width)) | |
1353 | shr-indentation 1) | |
030158f3 G |
1354 | (frame-width)) |
1355 | (setq truncate-lines t)) | |
a0ec382a | 1356 | ;; Then render the table again with these new "hard" widths. |
f462d10a | 1357 | (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))) |
130e977f | 1358 | |
6c769311 KY |
1359 | (defun shr-tag-table (cont) |
1360 | (shr-ensure-paragraph) | |
1361 | (let* ((caption (cdr (assq 'caption cont))) | |
1362 | (header (cdr (assq 'thead cont))) | |
1363 | (body (or (cdr (assq 'tbody cont)) cont)) | |
1364 | (footer (cdr (assq 'tfoot cont))) | |
144b7b5c | 1365 | (bgcolor (cdr (assq :bgcolor cont))) |
60568d74 LMI |
1366 | (start (point)) |
1367 | (shr-stylesheet (nconc (list (cons 'background-color bgcolor)) | |
1368 | shr-stylesheet)) | |
6c769311 KY |
1369 | (nheader (if header (shr-max-columns header))) |
1370 | (nbody (if body (shr-max-columns body))) | |
1371 | (nfooter (if footer (shr-max-columns footer)))) | |
2146e256 LMI |
1372 | (if (and (not caption) |
1373 | (not header) | |
1374 | (not (cdr (assq 'tbody cont))) | |
1375 | (not (cdr (assq 'tr cont))) | |
1376 | (not footer)) | |
1377 | ;; The table is totally invalid and just contains random junk. | |
1378 | ;; Try to output it anyway. | |
1379 | (shr-generic cont) | |
1380 | ;; It's a real table, so render it. | |
1381 | (shr-tag-table-1 | |
1382 | (nconc | |
1383 | (if caption `((tr (td ,@caption)))) | |
1384 | (if header | |
1385 | (if footer | |
8feacce0 | 1386 | ;; header + body + footer |
2146e256 LMI |
1387 | (if (= nheader nbody) |
1388 | (if (= nbody nfooter) | |
1389 | `((tr (td (table (tbody ,@header ,@body ,@footer))))) | |
1390 | (nconc `((tr (td (table (tbody ,@header ,@body))))) | |
1391 | (if (= nfooter 1) | |
1392 | footer | |
1393 | `((tr (td (table (tbody ,@footer)))))))) | |
1394 | (nconc `((tr (td (table (tbody ,@header))))) | |
1395 | (if (= nbody nfooter) | |
1396 | `((tr (td (table (tbody ,@body ,@footer))))) | |
1397 | (nconc `((tr (td (table (tbody ,@body))))) | |
1398 | (if (= nfooter 1) | |
1399 | footer | |
1400 | `((tr (td (table (tbody ,@footer)))))))))) | |
1401 | ;; header + body | |
3c066373 | 1402 | (if (= nheader nbody) |
2146e256 LMI |
1403 | `((tr (td (table (tbody ,@header ,@body))))) |
1404 | (if (= nheader 1) | |
1405 | `(,@header (tr (td (table (tbody ,@body))))) | |
1406 | `((tr (td (table (tbody ,@header)))) | |
1407 | (tr (td (table (tbody ,@body)))))))) | |
1408 | (if footer | |
1409 | ;; body + footer | |
1410 | (if (= nbody nfooter) | |
1411 | `((tr (td (table (tbody ,@body ,@footer))))) | |
1412 | (nconc `((tr (td (table (tbody ,@body))))) | |
1413 | (if (= nfooter 1) | |
1414 | footer | |
1415 | `((tr (td (table (tbody ,@footer)))))))) | |
1416 | (if caption | |
1417 | `((tr (td (table (tbody ,@body))))) | |
1418 | body)))))) | |
60568d74 LMI |
1419 | (when bgcolor |
1420 | (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet)) | |
f462d10a LMI |
1421 | bgcolor)) |
1422 | ;; Finally, insert all the images after the table. The Emacs buffer | |
1423 | ;; model isn't strong enough to allow us to put the images actually | |
1424 | ;; into the tables. | |
1425 | (when (zerop shr-table-depth) | |
1426 | (dolist (elem (shr-find-elements cont 'img)) | |
1427 | (shr-tag-img (cdr elem)))))) | |
6c769311 | 1428 | |
130e977f LMI |
1429 | (defun shr-find-elements (cont type) |
1430 | (let (result) | |
1431 | (dolist (elem cont) | |
1432 | (cond ((eq (car elem) type) | |
1433 | (push elem result)) | |
1434 | ((consp (cdr elem)) | |
1435 | (setq result (nconc (shr-find-elements (cdr elem) type) result))))) | |
1436 | (nreverse result))) | |
71e691a5 G |
1437 | |
1438 | (defun shr-insert-table (table widths) | |
be2aa135 LMI |
1439 | (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet)) |
1440 | "collapse")) | |
924d6997 | 1441 | (shr-table-separator-length (if collapse 0 1)) |
be2aa135 LMI |
1442 | (shr-table-vertical-line (if collapse "" shr-table-vertical-line))) |
1443 | (unless collapse | |
1444 | (shr-insert-table-ruler widths)) | |
1445 | (dolist (row table) | |
1446 | (let ((start (point)) | |
1447 | (height (let ((max 0)) | |
1448 | (dolist (column row) | |
1449 | (setq max (max max (cadr column)))) | |
1450 | max))) | |
1451 | (dotimes (i height) | |
1452 | (shr-indent) | |
1453 | (insert shr-table-vertical-line "\n")) | |
1454 | (dolist (column row) | |
1455 | (goto-char start) | |
1456 | (let ((lines (nth 2 column))) | |
1457 | (dolist (line lines) | |
1458 | (end-of-line) | |
1459 | (insert line shr-table-vertical-line) | |
1460 | (forward-line 1)) | |
1461 | ;; Add blank lines at padding at the bottom of the TD, | |
1462 | ;; possibly. | |
1463 | (dotimes (i (- height (length lines))) | |
1464 | (end-of-line) | |
1465 | (let ((start (point))) | |
1466 | (insert (make-string (string-width (car lines)) ? ) | |
1467 | shr-table-vertical-line) | |
1468 | (when (nth 4 column) | |
1469 | (shr-add-font start (1- (point)) | |
1470 | (list :background (nth 4 column))))) | |
1471 | (forward-line 1))))) | |
1472 | (unless collapse | |
1473 | (shr-insert-table-ruler widths))))) | |
71e691a5 G |
1474 | |
1475 | (defun shr-insert-table-ruler (widths) | |
924d6997 G |
1476 | (when shr-table-horizontal-line |
1477 | (when (and (bolp) | |
1478 | (> shr-indentation 0)) | |
1479 | (shr-indent)) | |
1480 | (insert shr-table-corner) | |
1481 | (dotimes (i (length widths)) | |
1482 | (insert (make-string (aref widths i) shr-table-horizontal-line) | |
1483 | shr-table-corner)) | |
1484 | (insert "\n"))) | |
71e691a5 | 1485 | |
160ae063 | 1486 | (defun shr-table-widths (table natural-table suggested-widths) |
a7dcc87b G |
1487 | (let* ((length (length suggested-widths)) |
1488 | (widths (make-vector length 0)) | |
1489 | (natural-widths (make-vector length 0))) | |
71e691a5 G |
1490 | (dolist (row table) |
1491 | (let ((i 0)) | |
1492 | (dolist (column row) | |
160ae063 LMI |
1493 | (aset widths i (max (aref widths i) column)) |
1494 | (setq i (1+ i))))) | |
1495 | (dolist (row natural-table) | |
1496 | (let ((i 0)) | |
1497 | (dolist (column row) | |
1498 | (aset natural-widths i (max (aref natural-widths i) column)) | |
a7dcc87b | 1499 | (setq i (1+ i))))) |
863b61d6 KY |
1500 | (let ((extra (- (apply '+ (append suggested-widths nil)) |
1501 | (apply '+ (append widths nil)))) | |
a7dcc87b | 1502 | (expanded-columns 0)) |
160ae063 LMI |
1503 | ;; We have extra, unused space, so divide this space amongst the |
1504 | ;; columns. | |
a7dcc87b | 1505 | (when (> extra 0) |
160ae063 LMI |
1506 | ;; If the natural width is wider than the rendered width, we |
1507 | ;; want to allow the column to expand. | |
a7dcc87b | 1508 | (dotimes (i length) |
a7dcc87b G |
1509 | (when (> (aref natural-widths i) (aref widths i)) |
1510 | (setq expanded-columns (1+ expanded-columns)))) | |
1511 | (dotimes (i length) | |
1512 | (when (> (aref natural-widths i) (aref widths i)) | |
1513 | (aset widths i (min | |
160ae063 | 1514 | (aref natural-widths i) |
a7dcc87b G |
1515 | (+ (/ extra expanded-columns) |
1516 | (aref widths i)))))))) | |
71e691a5 G |
1517 | widths)) |
1518 | ||
1519 | (defun shr-make-table (cont widths &optional fill) | |
c74cb344 G |
1520 | (or (cadr (assoc (list cont widths fill) shr-content-cache)) |
1521 | (let ((data (shr-make-table-1 cont widths fill))) | |
1522 | (push (list (list cont widths fill) data) | |
1523 | shr-content-cache) | |
1524 | data))) | |
1525 | ||
1526 | (defun shr-make-table-1 (cont widths &optional fill) | |
be2aa135 | 1527 | (let ((trs nil) |
924d6997 G |
1528 | (shr-inhibit-decoration (not fill)) |
1529 | (rowspans (make-vector (length widths) 0)) | |
1530 | width colspan) | |
71e691a5 G |
1531 | (dolist (row cont) |
1532 | (when (eq (car row) 'tr) | |
a0ec382a LMI |
1533 | (let ((tds nil) |
1534 | (columns (cdr row)) | |
1535 | (i 0) | |
924d6997 | 1536 | (width-column 0) |
a0ec382a LMI |
1537 | column) |
1538 | (while (< i (length widths)) | |
924d6997 G |
1539 | ;; If we previously had a rowspan definition, then that |
1540 | ;; means that we now have a "missing" td/th element here. | |
1541 | ;; So just insert a dummy, empty one to (sort of) emulate | |
1542 | ;; rowspan. | |
1543 | (setq column | |
1544 | (if (zerop (aref rowspans i)) | |
1545 | (pop columns) | |
1546 | (aset rowspans i (1- (aref rowspans i))) | |
1547 | '(td))) | |
a0ec382a | 1548 | (when (or (memq (car column) '(td th)) |
924d6997 G |
1549 | (not column)) |
1550 | (when (cdr (assq :rowspan (cdr column))) | |
1551 | (aset rowspans i (+ (aref rowspans i) | |
1552 | (1- (string-to-number | |
1553 | (cdr (assq :rowspan (cdr column)))))))) | |
970ad972 G |
1554 | ;; Sanity check for invalid column-spans. |
1555 | (when (>= width-column (length widths)) | |
1556 | (setq width-column 0)) | |
924d6997 G |
1557 | (setq width |
1558 | (if column | |
1559 | (aref widths width-column) | |
2fae38e5 | 1560 | 10)) |
924d6997 G |
1561 | (when (and fill |
1562 | (setq colspan (cdr (assq :colspan (cdr column))))) | |
2122cb6d LMI |
1563 | (setq colspan (min (string-to-number colspan) |
1564 | ;; The colspan may be wrong, so | |
1565 | ;; truncate it to the length of the | |
1566 | ;; remaining columns. | |
1567 | (- (length widths) i))) | |
924d6997 G |
1568 | (dotimes (j (1- colspan)) |
1569 | (if (> (+ i 1 j) (1- (length widths))) | |
1570 | (setq width (aref widths (1- (length widths)))) | |
1571 | (setq width (+ width | |
1572 | shr-table-separator-length | |
1573 | (aref widths (+ i 1 j)))))) | |
1574 | (setq width-column (+ width-column (1- colspan)))) | |
1575 | (when (or column | |
1576 | (not fill)) | |
1577 | (push (shr-render-td (cdr column) width fill) | |
1578 | tds)) | |
1579 | (setq i (1+ i) | |
1580 | width-column (1+ width-column)))) | |
71e691a5 G |
1581 | (push (nreverse tds) trs)))) |
1582 | (nreverse trs))) | |
1583 | ||
1584 | (defun shr-render-td (cont width fill) | |
04db63bc | 1585 | (with-temp-buffer |
60568d74 LMI |
1586 | (let ((bgcolor (cdr (assq :bgcolor cont))) |
1587 | (fgcolor (cdr (assq :fgcolor cont))) | |
1588 | (style (cdr (assq :style cont))) | |
1589 | (shr-stylesheet shr-stylesheet) | |
7304e4dd | 1590 | actual-colors) |
60568d74 LMI |
1591 | (when style |
1592 | (setq style (and (string-match "color" style) | |
1593 | (shr-parse-style style)))) | |
1594 | (when bgcolor | |
1595 | (setq style (nconc (list (cons 'background-color bgcolor)) style))) | |
1596 | (when fgcolor | |
1597 | (setq style (nconc (list (cons 'color fgcolor)) style))) | |
1598 | (when style | |
1599 | (setq shr-stylesheet (append style shr-stylesheet))) | |
c74cb344 G |
1600 | (let ((shr-width width) |
1601 | (shr-indentation 0)) | |
1602 | (shr-descend (cons 'td cont))) | |
1603 | ;; Delete padding at the bottom of the TDs. | |
1604 | (delete-region | |
1605 | (point) | |
1606 | (progn | |
1607 | (skip-chars-backward " \t\n") | |
1608 | (end-of-line) | |
1609 | (point))) | |
60568d74 LMI |
1610 | (goto-char (point-min)) |
1611 | (let ((max 0)) | |
1612 | (while (not (eobp)) | |
1613 | (end-of-line) | |
1614 | (setq max (max max (current-column))) | |
1615 | (forward-line 1)) | |
1616 | (when fill | |
1617 | (goto-char (point-min)) | |
1618 | ;; If the buffer is totally empty, then put a single blank | |
1619 | ;; line here. | |
1620 | (if (zerop (buffer-size)) | |
1621 | (insert (make-string width ? )) | |
1622 | ;; Otherwise, fill the buffer. | |
544d4594 LMI |
1623 | (let ((align (cdr (assq :align cont))) |
1624 | length) | |
1625 | (while (not (eobp)) | |
1626 | (end-of-line) | |
1627 | (setq length (- width (current-column))) | |
1628 | (when (> length 0) | |
1629 | (cond | |
1630 | ((equal align "right") | |
1631 | (beginning-of-line) | |
1632 | (insert (make-string length ? ))) | |
1633 | ((equal align "center") | |
1634 | (insert (make-string (/ length 2) ? )) | |
1635 | (beginning-of-line) | |
1636 | (insert (make-string (- length (/ length 2)) ? ))) | |
1637 | (t | |
1638 | (insert (make-string length ? ))))) | |
1639 | (forward-line 1)))) | |
abb97fbb | 1640 | (when style |
ec72bf63 G |
1641 | (setq actual-colors |
1642 | (shr-colorize-region | |
1643 | (point-min) (point-max) | |
1644 | (cdr (assq 'color shr-stylesheet)) | |
1645 | (cdr (assq 'background-color shr-stylesheet)))))) | |
60568d74 LMI |
1646 | (if fill |
1647 | (list max | |
1648 | (count-lines (point-min) (point-max)) | |
1649 | (split-string (buffer-string) "\n") | |
7304e4dd | 1650 | nil |
ec72bf63 | 1651 | (car actual-colors)) |
2122cb6d | 1652 | max))))) |
a7dcc87b | 1653 | |
160ae063 | 1654 | (defun shr-buffer-width () |
a7dcc87b | 1655 | (goto-char (point-min)) |
160ae063 | 1656 | (let ((max 0)) |
a7dcc87b G |
1657 | (while (not (eobp)) |
1658 | (end-of-line) | |
160ae063 | 1659 | (setq max (max max (current-column))) |
a7dcc87b G |
1660 | (forward-line 1)) |
1661 | max)) | |
130e977f | 1662 | |
71e691a5 G |
1663 | (defun shr-pro-rate-columns (columns) |
1664 | (let ((total-percentage 0) | |
1665 | (widths (make-vector (length columns) 0))) | |
1666 | (dotimes (i (length columns)) | |
a7dcc87b | 1667 | (setq total-percentage (+ total-percentage (aref columns i)))) |
71e691a5 G |
1668 | (setq total-percentage (/ 1.0 total-percentage)) |
1669 | (dotimes (i (length columns)) | |
1670 | (aset widths i (max (truncate (* (aref columns i) | |
1671 | total-percentage | |
a7dcc87b | 1672 | (- shr-width (1+ (length columns))))) |
71e691a5 G |
1673 | 10))) |
1674 | widths)) | |
1675 | ||
1676 | ;; Return a summary of the number and shape of the TDs in the table. | |
1677 | (defun shr-column-specs (cont) | |
1678 | (let ((columns (make-vector (shr-max-columns cont) 1))) | |
1679 | (dolist (row cont) | |
1680 | (when (eq (car row) 'tr) | |
1681 | (let ((i 0)) | |
1682 | (dolist (column (cdr row)) | |
1683 | (when (memq (car column) '(td th)) | |
1684 | (let ((width (cdr (assq :width (cdr column))))) | |
1685 | (when (and width | |
5d852256 LMI |
1686 | (string-match "\\([0-9]+\\)%" width) |
1687 | (not (zerop (setq width (string-to-number | |
1688 | (match-string 1 width)))))) | |
1689 | (aset columns i (/ width 100.0)))) | |
130e977f | 1690 | (setq i (1+ i))))))) |
71e691a5 G |
1691 | columns)) |
1692 | ||
1693 | (defun shr-count (cont elem) | |
1694 | (let ((i 0)) | |
1695 | (dolist (sub cont) | |
1696 | (when (eq (car sub) elem) | |
1697 | (setq i (1+ i)))) | |
1698 | i)) | |
1699 | ||
1700 | (defun shr-max-columns (cont) | |
1701 | (let ((max 0)) | |
1702 | (dolist (row cont) | |
1703 | (when (eq (car row) 'tr) | |
130e977f LMI |
1704 | (setq max (max max (+ (shr-count (cdr row) 'td) |
1705 | (shr-count (cdr row) 'th)))))) | |
71e691a5 G |
1706 | max)) |
1707 | ||
f3fd95db | 1708 | (provide 'shr) |
367f7f81 | 1709 | |
b4543a28 | 1710 | ;; Local Variables: |
c38e0c97 | 1711 | ;; coding: utf-8 |
b4543a28 G |
1712 | ;; End: |
1713 | ||
367f7f81 | 1714 | ;;; shr.el ends here |