shr.el 43 KB

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