| 1 | ;;; shr.el --- Simple HTML Renderer |
| 2 | |
| 3 | ;; Copyright (C) 2010-2013 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
| 6 | ;; Keywords: html |
| 7 | |
| 8 | ;; This file is part of GNU Emacs. |
| 9 | |
| 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 11 | ;; it under the terms of the GNU General Public License as published by |
| 12 | ;; the Free Software Foundation, either version 3 of the License, or |
| 13 | ;; (at your option) any later version. |
| 14 | |
| 15 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | ;; GNU General Public License for more details. |
| 19 | |
| 20 | ;; You should have received a copy of the GNU General Public License |
| 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | ;;; Commentary: |
| 24 | |
| 25 | ;; This package takes a HTML parse tree (as provided by |
| 26 | ;; libxml-parse-html-region) and renders it in the current buffer. It |
| 27 | ;; does not do CSS, JavaScript or anything advanced: It's geared |
| 28 | ;; towards rendering typical short snippets of HTML, like what you'd |
| 29 | ;; find in HTML email and the like. |
| 30 | |
| 31 | ;;; Code: |
| 32 | |
| 33 | (eval-when-compile (require 'cl)) |
| 34 | (eval-when-compile (require 'url)) ;For url-filename's setf handler. |
| 35 | (require 'browse-url) |
| 36 | |
| 37 | (defgroup shr nil |
| 38 | "Simple HTML Renderer" |
| 39 | :version "24.1" |
| 40 | :group 'hypermedia) |
| 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 |
| 56 | :type '(choice (const nil) regexp)) |
| 57 | |
| 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)) |
| 63 | |
| 64 | (defcustom shr-table-vertical-line ?\s |
| 65 | "Character used to draw vertical table lines." |
| 66 | :group 'shr |
| 67 | :type 'character) |
| 68 | |
| 69 | (defcustom shr-table-corner ?\s |
| 70 | "Character used to draw table corners." |
| 71 | :group 'shr |
| 72 | :type 'character) |
| 73 | |
| 74 | (defcustom shr-hr-line ?- |
| 75 | "Character used to draw hr lines." |
| 76 | :group 'shr |
| 77 | :type 'character) |
| 78 | |
| 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) |
| 87 | |
| 88 | (defcustom shr-bullet "* " |
| 89 | "Bullet used for unordered lists. |
| 90 | Alternative suggestions are: |
| 91 | - \" \" |
| 92 | - \" \"" |
| 93 | :type 'string |
| 94 | :group 'shr) |
| 95 | |
| 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 | |
| 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 | |
| 107 | (defvar shr-put-image-function 'shr-put-image |
| 108 | "Function called to put image and alt string.") |
| 109 | |
| 110 | (defface shr-strike-through '((t (:strike-through t))) |
| 111 | "Font for <s> elements." |
| 112 | :group 'shr) |
| 113 | |
| 114 | (defface shr-link |
| 115 | '((t (:inherit link))) |
| 116 | "Font for link elements." |
| 117 | :group 'shr) |
| 118 | |
| 119 | ;;; Internal variables. |
| 120 | |
| 121 | (defvar shr-folding-mode nil) |
| 122 | (defvar shr-state nil) |
| 123 | (defvar shr-start nil) |
| 124 | (defvar shr-indentation 0) |
| 125 | (defvar shr-inhibit-images nil) |
| 126 | (defvar shr-list-mode nil) |
| 127 | (defvar shr-content-cache nil) |
| 128 | (defvar shr-kinsoku-shorten nil) |
| 129 | (defvar shr-table-depth 0) |
| 130 | (defvar shr-stylesheet nil) |
| 131 | (defvar shr-base nil) |
| 132 | (defvar shr-ignore-cache nil) |
| 133 | (defvar shr-external-rendering-functions nil) |
| 134 | (defvar shr-target-id nil) |
| 135 | (defvar shr-inhibit-decoration nil) |
| 136 | (defvar shr-table-separator-length 1) |
| 137 | |
| 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) |
| 142 | (define-key map "z" 'shr-zoom-image) |
| 143 | (define-key map [tab] 'shr-next-link) |
| 144 | (define-key map [backtab] 'shr-previous-link) |
| 145 | (define-key map [follow-link] 'mouse-face) |
| 146 | (define-key map [mouse-2] 'shr-browse-url) |
| 147 | (define-key map "I" 'shr-insert-image) |
| 148 | (define-key map "w" 'shr-copy-url) |
| 149 | (define-key map "u" 'shr-copy-url) |
| 150 | (define-key map "v" 'shr-browse-url) |
| 151 | (define-key map "o" 'shr-save-contents) |
| 152 | (define-key map "\r" 'shr-browse-url) |
| 153 | map)) |
| 154 | |
| 155 | ;; Public functions and commands. |
| 156 | (declare-function libxml-parse-html-region "xml.c" |
| 157 | (start end &optional base-url)) |
| 158 | |
| 159 | (defun shr-render-buffer (buffer) |
| 160 | "Display the HTML rendering of the current buffer." |
| 161 | (interactive (list (current-buffer))) |
| 162 | (or (fboundp 'libxml-parse-html-region) |
| 163 | (error "This function requires Emacs to be compiled with libxml2")) |
| 164 | (pop-to-buffer "*html*") |
| 165 | (erase-buffer) |
| 166 | (shr-insert-document |
| 167 | (with-current-buffer buffer |
| 168 | (libxml-parse-html-region (point-min) (point-max)))) |
| 169 | (goto-char (point-min))) |
| 170 | |
| 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 | |
| 182 | ;;;###autoload |
| 183 | (defun shr-insert-document (dom) |
| 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." |
| 187 | (setq shr-content-cache nil) |
| 188 | (let ((start (point)) |
| 189 | (shr-state nil) |
| 190 | (shr-start nil) |
| 191 | (shr-base nil) |
| 192 | (shr-width (or shr-width (1- (window-width))))) |
| 193 | (shr-descend (shr-transform-dom dom)) |
| 194 | (shr-remove-trailing-whitespace start (point)))) |
| 195 | |
| 196 | (defun shr-remove-trailing-whitespace (start end) |
| 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) |
| 203 | (when (> (shr-previous-newline-padding-width (current-column)) width) |
| 204 | (dolist (overlay (overlays-at (point))) |
| 205 | (when (overlay-get overlay 'before-string) |
| 206 | (overlay-put overlay 'before-string nil)))) |
| 207 | (forward-line 1))))) |
| 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)) |
| 232 | (copy-region-as-kill (point-min) (point-max))))) |
| 233 | nil t)) |
| 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 | |
| 241 | (defun shr-next-link () |
| 242 | "Skip to the next link." |
| 243 | (interactive) |
| 244 | (let ((skip (text-property-any (point) (point-max) 'help-echo nil))) |
| 245 | (if (not (setq skip (text-property-not-all skip (point-max) |
| 246 | 'help-echo nil))) |
| 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)) |
| 258 | (get-text-property (point) 'help-echo)) |
| 259 | (forward-char -1)) |
| 260 | ;; Find the previous link. |
| 261 | (while (and (not (bobp)) |
| 262 | (not (setq found (get-text-property (point) 'help-echo)))) |
| 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)) |
| 270 | (get-text-property (point) 'help-echo)) |
| 271 | (forward-char -1)) |
| 272 | (forward-char 1) |
| 273 | (message "%s" (get-text-property (point) 'help-echo))))) |
| 274 | |
| 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 | |
| 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") |
| 288 | (let ((url (get-text-property (point) 'image-url))) |
| 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 |
| 298 | (message "Browsing %s..." url) |
| 299 | (browse-url url))))) |
| 300 | |
| 301 | (defun shr-insert-image () |
| 302 | "Insert the image under point into the buffer." |
| 303 | (interactive) |
| 304 | (let ((url (get-text-property (point) 'image-url))) |
| 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)) |
| 310 | t t)))) |
| 311 | |
| 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 | |
| 346 | ;;; Utility functions. |
| 347 | |
| 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) |
| 356 | (push (cons 'text sub) result) |
| 357 | (push (shr-transform-dom sub) result))) |
| 358 | (nreverse result))) |
| 359 | |
| 360 | (defun shr-descend (dom) |
| 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))) |
| 367 | (style (cdr (assq :style (cdr dom)))) |
| 368 | (shr-stylesheet shr-stylesheet) |
| 369 | (start (point))) |
| 370 | (when style |
| 371 | (if (string-match "color\\|display\\|border-collapse" style) |
| 372 | (setq shr-stylesheet (nconc (shr-parse-style style) |
| 373 | shr-stylesheet)) |
| 374 | (setq style nil))) |
| 375 | ;; If we have a display:none, then just ignore this part of the DOM. |
| 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))))))) |
| 388 | |
| 389 | (defun shr-generic (cont) |
| 390 | (dolist (sub cont) |
| 391 | (cond |
| 392 | ((eq (car sub) 'text) |
| 393 | (shr-insert (cdr sub))) |
| 394 | ((listp (cdr sub)) |
| 395 | (shr-descend sub))))) |
| 396 | |
| 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." |
| 410 | `(let ((char ,char)) |
| 411 | (and (not (eq char ?')) |
| 412 | (aref (char-category-set char) ?>)))) |
| 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 | |
| 419 | (defun shr-insert (text) |
| 420 | (when (and (eq shr-state 'image) |
| 421 | (not (bolp)) |
| 422 | (not (string-match "\\`[ \t\n]+\\'" text))) |
| 423 | (insert "\n") |
| 424 | (setq shr-state nil)) |
| 425 | (cond |
| 426 | ((eq shr-folding-mode 'none) |
| 427 | (insert text)) |
| 428 | (t |
| 429 | (when (and (string-match "\\`[ \t\n ]" text) |
| 430 | (not (bolp)) |
| 431 | (not (eq (char-after (1- (point))) ? ))) |
| 432 | (insert " ")) |
| 433 | (dolist (elem (split-string text "[ \f\t\n\r\v ]+" t)) |
| 434 | (when (and (bolp) |
| 435 | (> shr-indentation 0)) |
| 436 | (shr-indent)) |
| 437 | ;; No space is needed behind a wide character categorized as |
| 438 | ;; kinsoku-bol, between characters both categorized as nospace, |
| 439 | ;; or at the beginning of a line. |
| 440 | (let (prev) |
| 441 | (when (and (> (current-column) shr-indentation) |
| 442 | (eq (preceding-char) ? ) |
| 443 | (or (= (line-beginning-position) (1- (point))) |
| 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))))) |
| 449 | (delete-char -1))) |
| 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))) |
| 455 | (insert elem) |
| 456 | (setq shr-state nil) |
| 457 | (let (found) |
| 458 | (when (and (> (current-column) shr-width) |
| 459 | (progn |
| 460 | (setq found (shr-find-fill-point)) |
| 461 | (not (eolp)))) |
| 462 | (when (eq (preceding-char) ? ) |
| 463 | (delete-char -1)) |
| 464 | (insert "\n") |
| 465 | (unless found |
| 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 " "))) |
| 473 | (unless (string-match "[ \t\r\n ]\\'" text) |
| 474 | (delete-char -1))))) |
| 475 | |
| 476 | (defun shr-find-fill-point () |
| 477 | (when (> (move-to-column shr-width) shr-width) |
| 478 | (backward-char 1)) |
| 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)) |
| 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)))) |
| 489 | (shr-char-kinsoku-eol-p (following-char)))) |
| 490 | (backward-char 1)) |
| 491 | (if failed |
| 492 | ;; There's no breakable point, so we give it up. |
| 493 | (let (found) |
| 494 | (goto-char bp) |
| 495 | (unless shr-kinsoku-shorten |
| 496 | (while (setq found (re-search-forward |
| 497 | "\\(\\c>\\)\\| \\|\\c<\\|\\c|" |
| 498 | (line-end-position) 'move))) |
| 499 | (if (and found (not (match-beginning 1))) |
| 500 | (goto-char (match-beginning 0))))) |
| 501 | (or |
| 502 | (eolp) |
| 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)) |
| 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))))))) |
| 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)))) |
| 536 | ((shr-char-kinsoku-bol-p (following-char)) |
| 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) |
| 542 | (shr-char-kinsoku-bol-p (following-char)) |
| 543 | (shr-char-breakable-p (following-char)))))))) |
| 544 | (when (eq (following-char) ? ) |
| 545 | (forward-char 1)))) |
| 546 | (not failed))) |
| 547 | |
| 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)))) |
| 593 | |
| 594 | (defun shr-ensure-newline () |
| 595 | (unless (zerop (current-column)) |
| 596 | (insert "\n"))) |
| 597 | |
| 598 | (defun shr-ensure-paragraph () |
| 599 | (unless (bobp) |
| 600 | (if (<= (current-column) shr-indentation) |
| 601 | (unless (save-excursion |
| 602 | (forward-line -1) |
| 603 | (looking-at " *$")) |
| 604 | (insert "\n")) |
| 605 | (if (save-excursion |
| 606 | (beginning-of-line) |
| 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))))) |
| 615 | (delete-region (match-beginning 0) (match-end 0)) |
| 616 | (insert "\n\n"))))) |
| 617 | |
| 618 | (defun shr-indent () |
| 619 | (when (> shr-indentation 0) |
| 620 | (insert (make-string shr-indentation ? )))) |
| 621 | |
| 622 | (defun shr-fontize-cont (cont &rest types) |
| 623 | (let (shr-start) |
| 624 | (shr-generic cont) |
| 625 | (dolist (type types) |
| 626 | (shr-add-font (or shr-start (point)) (point) type)))) |
| 627 | |
| 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. |
| 631 | (defun shr-add-font (start end type) |
| 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)))))) |
| 642 | |
| 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 | |
| 649 | (defun shr-browse-url (&optional external mouse-event) |
| 650 | "Browse the URL under point. |
| 651 | If EXTERNAL, browse the URL using `shr-external-browser'." |
| 652 | (interactive (list current-prefix-arg last-nonmenu-event)) |
| 653 | (mouse-set-point mouse-event) |
| 654 | (let ((url (get-text-property (point) 'shr-url))) |
| 655 | (cond |
| 656 | ((not url) |
| 657 | (message "No link under point")) |
| 658 | ((string-match "^mailto:" url) |
| 659 | (browse-url-mail url)) |
| 660 | (t |
| 661 | (if external |
| 662 | (funcall shr-external-browser url) |
| 663 | (browse-url url)))))) |
| 664 | |
| 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) |
| 672 | 'shr-store-contents (list url directory) |
| 673 | nil t)))) |
| 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 | |
| 683 | (defun shr-image-fetched (status buffer start end &optional flags) |
| 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)) |
| 690 | (let ((data (shr-parse-image-data))) |
| 691 | (with-current-buffer buffer |
| 692 | (save-excursion |
| 693 | (let ((alt (buffer-substring start end)) |
| 694 | (properties (text-properties-at start)) |
| 695 | (inhibit-read-only t)) |
| 696 | (delete-region start end) |
| 697 | (goto-char start) |
| 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)))))))))) |
| 704 | (kill-buffer image-buffer))) |
| 705 | |
| 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 | |
| 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 | |
| 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." |
| 725 | (if (display-graphic-p) |
| 726 | (let* ((size (cdr (assq 'size flags))) |
| 727 | (data (if (consp spec) |
| 728 | (car spec) |
| 729 | spec)) |
| 730 | (content-type (and (consp spec) |
| 731 | (cadr spec))) |
| 732 | (start (point)) |
| 733 | (image (cond |
| 734 | ((eq size 'original) |
| 735 | (create-image data nil t :ascent 100 |
| 736 | :format content-type)) |
| 737 | ((eq size 'full) |
| 738 | (ignore-errors |
| 739 | (shr-rescale-image data content-type))) |
| 740 | (t |
| 741 | (ignore-errors |
| 742 | (shr-rescale-image data content-type)))))) |
| 743 | (when image |
| 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")) |
| 749 | (if (eq size 'original) |
| 750 | (insert-sliced-image image (or alt "*") nil 20 1) |
| 751 | (insert-image image (or alt "*"))) |
| 752 | (put-text-property start (point) 'image-size size) |
| 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))) |
| 760 | (image-animate image nil 60))) |
| 761 | image) |
| 762 | (insert alt))) |
| 763 | |
| 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)))) |
| 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 |
| 777 | (- (nth 3 edges) (nth 1 edges)))) |
| 778 | :format content-type)))) |
| 779 | |
| 780 | ;; url-cache-extract autoloads url-cache. |
| 781 | (declare-function url-cache-create-filename "url-cache" (url)) |
| 782 | (autoload 'mm-disable-multibyte "mm-util") |
| 783 | (autoload 'browse-url-mail "browse-url") |
| 784 | |
| 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) |
| 790 | (when (ignore-errors |
| 791 | (url-cache-extract (url-cache-create-filename (shr-encode-url url))) |
| 792 | t) |
| 793 | (when (or (search-forward "\n\n" nil t) |
| 794 | (search-forward "\r\n\r\n" nil t)) |
| 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))))))) |
| 806 | |
| 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, |
| 811 | START, and END. Note that START and END should be markers." |
| 812 | `(lambda (url start end) |
| 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) |
| 820 | (funcall shr-put-image-function |
| 821 | image (buffer-substring start end)) |
| 822 | (delete-region (point) end)))) |
| 823 | (url-retrieve url 'shr-image-fetched |
| 824 | (list (current-buffer) start end) |
| 825 | t t))))) |
| 826 | |
| 827 | (defun shr-heading (cont &rest types) |
| 828 | (shr-ensure-paragraph) |
| 829 | (apply #'shr-fontize-cont cont types) |
| 830 | (shr-ensure-paragraph)) |
| 831 | |
| 832 | (defun shr-urlify (start url &optional title) |
| 833 | (shr-add-font start (point) 'shr-link) |
| 834 | (add-text-properties |
| 835 | start (point) |
| 836 | (list 'shr-url url |
| 837 | 'help-echo (if title (format "%s (%s)" url title) url) |
| 838 | 'follow-link t |
| 839 | 'mouse-face 'highlight |
| 840 | 'keymap shr-map))) |
| 841 | |
| 842 | (defun shr-encode-url (url) |
| 843 | "Encode URL." |
| 844 | (browse-url-url-encode-chars url "[)$ ]")) |
| 845 | |
| 846 | (autoload 'shr-color-visible "shr-color") |
| 847 | (autoload 'shr-color->hexadecimal "shr-color") |
| 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 | |
| 871 | (defun shr-colorize-region (start end fg &optional bg) |
| 872 | (when (and (not shr-inhibit-decoration) |
| 873 | (or fg bg)) |
| 874 | (let ((new-colors (shr-color-check fg bg))) |
| 875 | (when new-colors |
| 876 | (when fg |
| 877 | (add-face-text-property start end |
| 878 | (list :foreground (cadr new-colors)) |
| 879 | t)) |
| 880 | (when bg |
| 881 | (add-face-text-property start end |
| 882 | (list :background (car new-colors)) |
| 883 | t))) |
| 884 | new-colors))) |
| 885 | |
| 886 | (defun shr-expand-newlines (start end color) |
| 887 | (save-restriction |
| 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)) |
| 897 | (narrow-to-region start end) |
| 898 | (let ((width (shr-buffer-width)) |
| 899 | column) |
| 900 | (goto-char (point-min)) |
| 901 | (while (not (eobp)) |
| 902 | (end-of-line) |
| 903 | (when (and (< (setq column (current-column)) width) |
| 904 | (< (setq column (shr-previous-newline-padding-width column)) |
| 905 | width)) |
| 906 | (let ((overlay (make-overlay (point) (1+ (point))))) |
| 907 | (overlay-put overlay 'before-string |
| 908 | (concat |
| 909 | (mapconcat |
| 910 | (lambda (overlay) |
| 911 | (let ((string (plist-get |
| 912 | (overlay-properties overlay) |
| 913 | 'before-string))) |
| 914 | (if (not string) |
| 915 | "" |
| 916 | (overlay-put overlay 'before-string "") |
| 917 | string))) |
| 918 | (overlays-at (point)) |
| 919 | "") |
| 920 | (propertize (make-string (- width column) ? ) |
| 921 | 'face (list :background color)))))) |
| 922 | (forward-line 1))))) |
| 923 | |
| 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 |
| 932 | (length (plist-get (overlay-properties overlay) |
| 933 | 'before-string))))) |
| 934 | (+ width previous-width)))) |
| 935 | |
| 936 | ;;; Tag-specific rendering rules. |
| 937 | |
| 938 | (defun shr-tag-body (cont) |
| 939 | (let* ((start (point)) |
| 940 | (fgcolor (cdr (or (assq :fgcolor cont) |
| 941 | (assq :text cont)))) |
| 942 | (bgcolor (cdr (assq :bgcolor cont))) |
| 943 | (shr-stylesheet (list (cons 'color fgcolor) |
| 944 | (cons 'background-color bgcolor)))) |
| 945 | (shr-generic cont) |
| 946 | (shr-colorize-region start (point) fgcolor bgcolor))) |
| 947 | |
| 948 | (defun shr-tag-style (_cont) |
| 949 | ) |
| 950 | |
| 951 | (defun shr-tag-script (_cont) |
| 952 | ) |
| 953 | |
| 954 | (defun shr-tag-comment (_cont) |
| 955 | ) |
| 956 | |
| 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 | |
| 977 | (defun shr-tag-svg (cont) |
| 978 | (when (image-type-available-p 'svg) |
| 979 | (funcall shr-put-image-function |
| 980 | (shr-dom-to-xml (cons 'svg cont)) |
| 981 | "SVG Image"))) |
| 982 | |
| 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 | |
| 993 | (defun shr-tag-label (cont) |
| 994 | (shr-generic cont) |
| 995 | (shr-ensure-paragraph)) |
| 996 | |
| 997 | (defun shr-tag-p (cont) |
| 998 | (shr-ensure-paragraph) |
| 999 | (shr-indent) |
| 1000 | (shr-generic cont) |
| 1001 | (shr-ensure-paragraph)) |
| 1002 | |
| 1003 | (defun shr-tag-div (cont) |
| 1004 | (shr-ensure-newline) |
| 1005 | (shr-indent) |
| 1006 | (shr-generic cont) |
| 1007 | (shr-ensure-newline)) |
| 1008 | |
| 1009 | (defun shr-tag-s (cont) |
| 1010 | (shr-fontize-cont cont 'shr-strike-through)) |
| 1011 | |
| 1012 | (defun shr-tag-del (cont) |
| 1013 | (shr-fontize-cont cont 'shr-strike-through)) |
| 1014 | |
| 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) |
| 1022 | (shr-fontize-cont cont 'italic)) |
| 1023 | |
| 1024 | (defun shr-tag-strong (cont) |
| 1025 | (shr-fontize-cont cont 'bold)) |
| 1026 | |
| 1027 | (defun shr-tag-u (cont) |
| 1028 | (shr-fontize-cont cont 'underline)) |
| 1029 | |
| 1030 | (defun shr-parse-style (style) |
| 1031 | (when style |
| 1032 | (save-match-data |
| 1033 | (when (string-match "\n" style) |
| 1034 | (setq style (replace-match " " t t style)))) |
| 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)))) |
| 1043 | (when (string-match " *!important\\'" value) |
| 1044 | (setq value (substring value 0 (match-beginning 0)))) |
| 1045 | (push (cons (intern name obarray) |
| 1046 | value) |
| 1047 | plist))))) |
| 1048 | plist))) |
| 1049 | |
| 1050 | (defun shr-tag-base (cont) |
| 1051 | (let ((base (cdr (assq :href cont)))) |
| 1052 | (when base |
| 1053 | (setq shr-base (shr-parse-base base)))) |
| 1054 | (shr-generic cont)) |
| 1055 | |
| 1056 | (defun shr-tag-a (cont) |
| 1057 | (let ((url (cdr (assq :href cont))) |
| 1058 | (title (cdr (assq :title cont))) |
| 1059 | (start (point)) |
| 1060 | shr-start) |
| 1061 | (shr-generic cont) |
| 1062 | (when (and shr-target-id |
| 1063 | (equal (cdr (assq :name cont)) shr-target-id)) |
| 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)) |
| 1070 | (when (and url |
| 1071 | (not shr-inhibit-decoration)) |
| 1072 | (shr-urlify (or shr-start start) (shr-expand-url url) title)))) |
| 1073 | |
| 1074 | (defun shr-tag-object (cont) |
| 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))))))) |
| 1083 | (when url |
| 1084 | (shr-insert " [multimedia] ") |
| 1085 | (shr-urlify start (shr-expand-url url))) |
| 1086 | (shr-generic cont))) |
| 1087 | |
| 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) |
| 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))) |
| 1128 | ;; libxml's html parser isn't HTML5 compliant and non terminated |
| 1129 | ;; source tags might end up as children. So recursion it is... |
| 1130 | (dolist (child (cdr elem)) |
| 1131 | (when (eq (car child) 'source) |
| 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 | |
| 1138 | (defun shr-tag-video (cont) |
| 1139 | (let ((image (cdr (assq :poster cont))) |
| 1140 | (url (cdr (assq :src cont))) |
| 1141 | (start (point))) |
| 1142 | (unless url |
| 1143 | (setq url (car (shr--extract-best-source cont)))) |
| 1144 | (if image |
| 1145 | (shr-tag-img nil image) |
| 1146 | (shr-insert " [video] ")) |
| 1147 | (shr-urlify start (shr-expand-url url)))) |
| 1148 | |
| 1149 | (defun shr-tag-audio (cont) |
| 1150 | (let ((url (cdr (assq :src cont))) |
| 1151 | (start (point))) |
| 1152 | (unless url |
| 1153 | (setq url (car (shr--extract-best-source cont)))) |
| 1154 | (shr-insert " [audio] ") |
| 1155 | (shr-urlify start (shr-expand-url url)))) |
| 1156 | |
| 1157 | (defun shr-tag-img (cont &optional url) |
| 1158 | (when (or url |
| 1159 | (and cont |
| 1160 | (> (length (cdr (assq :src cont))) 0))) |
| 1161 | (when (and (> (current-column) 0) |
| 1162 | (not (eq shr-state 'image))) |
| 1163 | (insert "\n")) |
| 1164 | (let ((alt (cdr (assq :alt cont))) |
| 1165 | (url (shr-expand-url (or url (cdr (assq :src cont)))))) |
| 1166 | (let ((start (point-marker))) |
| 1167 | (when (zerop (length alt)) |
| 1168 | (setq alt "*")) |
| 1169 | (cond |
| 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 | ) |
| 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)))) |
| 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) |
| 1187 | (funcall shr-put-image-function image alt)))) |
| 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)) |
| 1193 | (if (> (string-width alt) 8) |
| 1194 | (shr-insert (truncate-string-to-width alt 8)) |
| 1195 | (shr-insert alt)))) |
| 1196 | ((and (not shr-ignore-cache) |
| 1197 | (url-is-cached (shr-encode-url url))) |
| 1198 | (funcall shr-put-image-function (shr-get-image-data url) alt)) |
| 1199 | (t |
| 1200 | (insert alt " ") |
| 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)))) |
| 1206 | (url-queue-retrieve |
| 1207 | (shr-encode-url url) 'shr-image-fetched |
| 1208 | (list (current-buffer) start (set-marker (make-marker) (1- (point)))) |
| 1209 | t t))) |
| 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)) |
| 1217 | (setq shr-state 'image))))) |
| 1218 | |
| 1219 | (defun shr-tag-pre (cont) |
| 1220 | (let ((shr-folding-mode 'none)) |
| 1221 | (shr-ensure-newline) |
| 1222 | (shr-indent) |
| 1223 | (shr-generic cont) |
| 1224 | (shr-ensure-newline))) |
| 1225 | |
| 1226 | (defun shr-tag-blockquote (cont) |
| 1227 | (shr-ensure-paragraph) |
| 1228 | (shr-indent) |
| 1229 | (let ((shr-indentation (+ shr-indentation 4))) |
| 1230 | (shr-generic cont)) |
| 1231 | (shr-ensure-paragraph)) |
| 1232 | |
| 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 | |
| 1248 | (defun shr-tag-ul (cont) |
| 1249 | (shr-ensure-paragraph) |
| 1250 | (let ((shr-list-mode 'ul)) |
| 1251 | (shr-generic cont)) |
| 1252 | (shr-ensure-paragraph)) |
| 1253 | |
| 1254 | (defun shr-tag-ol (cont) |
| 1255 | (shr-ensure-paragraph) |
| 1256 | (let ((shr-list-mode 1)) |
| 1257 | (shr-generic cont)) |
| 1258 | (shr-ensure-paragraph)) |
| 1259 | |
| 1260 | (defun shr-tag-li (cont) |
| 1261 | (shr-ensure-newline) |
| 1262 | (shr-indent) |
| 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))) |
| 1268 | shr-bullet)) |
| 1269 | (shr-indentation (+ shr-indentation (length bullet)))) |
| 1270 | (insert bullet) |
| 1271 | (shr-generic cont))) |
| 1272 | |
| 1273 | (defun shr-tag-br (cont) |
| 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))))) |
| 1280 | (insert "\n") |
| 1281 | (shr-indent)) |
| 1282 | (shr-generic cont)) |
| 1283 | |
| 1284 | (defun shr-tag-span (cont) |
| 1285 | (shr-generic cont)) |
| 1286 | |
| 1287 | (defun shr-tag-h1 (cont) |
| 1288 | (shr-heading cont 'bold 'underline)) |
| 1289 | |
| 1290 | (defun shr-tag-h2 (cont) |
| 1291 | (shr-heading cont 'bold)) |
| 1292 | |
| 1293 | (defun shr-tag-h3 (cont) |
| 1294 | (shr-heading cont 'italic)) |
| 1295 | |
| 1296 | (defun shr-tag-h4 (cont) |
| 1297 | (shr-heading cont)) |
| 1298 | |
| 1299 | (defun shr-tag-h5 (cont) |
| 1300 | (shr-heading cont)) |
| 1301 | |
| 1302 | (defun shr-tag-h6 (cont) |
| 1303 | (shr-heading cont)) |
| 1304 | |
| 1305 | (defun shr-tag-hr (_cont) |
| 1306 | (shr-ensure-newline) |
| 1307 | (insert (make-string shr-width shr-hr-line) "\n")) |
| 1308 | |
| 1309 | (defun shr-tag-title (cont) |
| 1310 | (shr-heading cont 'bold 'underline)) |
| 1311 | |
| 1312 | (defun shr-tag-font (cont) |
| 1313 | (let* ((start (point)) |
| 1314 | (color (cdr (assq :color cont))) |
| 1315 | (shr-stylesheet (nconc (list (cons 'color color)) |
| 1316 | shr-stylesheet))) |
| 1317 | (shr-generic cont) |
| 1318 | (when color |
| 1319 | (shr-colorize-region start (point) color |
| 1320 | (cdr (assq 'background-color shr-stylesheet)))))) |
| 1321 | |
| 1322 | ;;; Table rendering algorithm. |
| 1323 | |
| 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. |
| 1332 | (defun shr-tag-table-1 (cont) |
| 1333 | (setq cont (or (cdr (assq 'tbody cont)) |
| 1334 | cont)) |
| 1335 | (let* ((shr-inhibit-images t) |
| 1336 | (shr-table-depth (1+ shr-table-depth)) |
| 1337 | (shr-kinsoku-shorten t) |
| 1338 | ;; Find all suggested widths. |
| 1339 | (columns (shr-column-specs cont)) |
| 1340 | ;; Compute how many characters wide each TD should be. |
| 1341 | (suggested-widths (shr-pro-rate-columns columns)) |
| 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). |
| 1345 | (sketch (shr-make-table cont suggested-widths)) |
| 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))) |
| 1350 | ;; This probably won't work very well. |
| 1351 | (when (> (+ (loop for width across sketch-widths |
| 1352 | summing (1+ width)) |
| 1353 | shr-indentation 1) |
| 1354 | (frame-width)) |
| 1355 | (setq truncate-lines t)) |
| 1356 | ;; Then render the table again with these new "hard" widths. |
| 1357 | (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))) |
| 1358 | |
| 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))) |
| 1365 | (bgcolor (cdr (assq :bgcolor cont))) |
| 1366 | (start (point)) |
| 1367 | (shr-stylesheet (nconc (list (cons 'background-color bgcolor)) |
| 1368 | shr-stylesheet)) |
| 1369 | (nheader (if header (shr-max-columns header))) |
| 1370 | (nbody (if body (shr-max-columns body))) |
| 1371 | (nfooter (if footer (shr-max-columns footer)))) |
| 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 |
| 1386 | ;; header + body + footer |
| 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 |
| 1402 | (if (= nheader nbody) |
| 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)))))) |
| 1419 | (when bgcolor |
| 1420 | (shr-colorize-region start (point) (cdr (assq 'color shr-stylesheet)) |
| 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)))))) |
| 1428 | |
| 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))) |
| 1437 | |
| 1438 | (defun shr-insert-table (table widths) |
| 1439 | (let* ((collapse (equal (cdr (assq 'border-collapse shr-stylesheet)) |
| 1440 | "collapse")) |
| 1441 | (shr-table-separator-length (if collapse 0 1)) |
| 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))))) |
| 1474 | |
| 1475 | (defun shr-insert-table-ruler (widths) |
| 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"))) |
| 1485 | |
| 1486 | (defun shr-table-widths (table natural-table suggested-widths) |
| 1487 | (let* ((length (length suggested-widths)) |
| 1488 | (widths (make-vector length 0)) |
| 1489 | (natural-widths (make-vector length 0))) |
| 1490 | (dolist (row table) |
| 1491 | (let ((i 0)) |
| 1492 | (dolist (column row) |
| 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)) |
| 1499 | (setq i (1+ i))))) |
| 1500 | (let ((extra (- (apply '+ (append suggested-widths nil)) |
| 1501 | (apply '+ (append widths nil)))) |
| 1502 | (expanded-columns 0)) |
| 1503 | ;; We have extra, unused space, so divide this space amongst the |
| 1504 | ;; columns. |
| 1505 | (when (> extra 0) |
| 1506 | ;; If the natural width is wider than the rendered width, we |
| 1507 | ;; want to allow the column to expand. |
| 1508 | (dotimes (i length) |
| 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 |
| 1514 | (aref natural-widths i) |
| 1515 | (+ (/ extra expanded-columns) |
| 1516 | (aref widths i)))))))) |
| 1517 | widths)) |
| 1518 | |
| 1519 | (defun shr-make-table (cont widths &optional fill) |
| 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) |
| 1527 | (let ((trs nil) |
| 1528 | (shr-inhibit-decoration (not fill)) |
| 1529 | (rowspans (make-vector (length widths) 0)) |
| 1530 | width colspan) |
| 1531 | (dolist (row cont) |
| 1532 | (when (eq (car row) 'tr) |
| 1533 | (let ((tds nil) |
| 1534 | (columns (cdr row)) |
| 1535 | (i 0) |
| 1536 | (width-column 0) |
| 1537 | column) |
| 1538 | (while (< i (length widths)) |
| 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))) |
| 1548 | (when (or (memq (car column) '(td th)) |
| 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)))))))) |
| 1554 | ;; Sanity check for invalid column-spans. |
| 1555 | (when (>= width-column (length widths)) |
| 1556 | (setq width-column 0)) |
| 1557 | (setq width |
| 1558 | (if column |
| 1559 | (aref widths width-column) |
| 1560 | 10)) |
| 1561 | (when (and fill |
| 1562 | (setq colspan (cdr (assq :colspan (cdr column))))) |
| 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))) |
| 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)))) |
| 1581 | (push (nreverse tds) trs)))) |
| 1582 | (nreverse trs))) |
| 1583 | |
| 1584 | (defun shr-render-td (cont width fill) |
| 1585 | (with-temp-buffer |
| 1586 | (let ((bgcolor (cdr (assq :bgcolor cont))) |
| 1587 | (fgcolor (cdr (assq :fgcolor cont))) |
| 1588 | (style (cdr (assq :style cont))) |
| 1589 | (shr-stylesheet shr-stylesheet) |
| 1590 | actual-colors) |
| 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))) |
| 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))) |
| 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. |
| 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)))) |
| 1640 | (when style |
| 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)))))) |
| 1646 | (if fill |
| 1647 | (list max |
| 1648 | (count-lines (point-min) (point-max)) |
| 1649 | (split-string (buffer-string) "\n") |
| 1650 | nil |
| 1651 | (car actual-colors)) |
| 1652 | max))))) |
| 1653 | |
| 1654 | (defun shr-buffer-width () |
| 1655 | (goto-char (point-min)) |
| 1656 | (let ((max 0)) |
| 1657 | (while (not (eobp)) |
| 1658 | (end-of-line) |
| 1659 | (setq max (max max (current-column))) |
| 1660 | (forward-line 1)) |
| 1661 | max)) |
| 1662 | |
| 1663 | (defun shr-pro-rate-columns (columns) |
| 1664 | (let ((total-percentage 0) |
| 1665 | (widths (make-vector (length columns) 0))) |
| 1666 | (dotimes (i (length columns)) |
| 1667 | (setq total-percentage (+ total-percentage (aref columns i)))) |
| 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 |
| 1672 | (- shr-width (1+ (length columns))))) |
| 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 |
| 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)))) |
| 1690 | (setq i (1+ i))))))) |
| 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) |
| 1704 | (setq max (max max (+ (shr-count (cdr row) 'td) |
| 1705 | (shr-count (cdr row) 'th)))))) |
| 1706 | max)) |
| 1707 | |
| 1708 | (provide 'shr) |
| 1709 | |
| 1710 | ;; Local Variables: |
| 1711 | ;; coding: utf-8 |
| 1712 | ;; End: |
| 1713 | |
| 1714 | ;;; shr.el ends here |