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