gnus-html.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. ;;; gnus-html.el --- Render HTML in a buffer.
  2. ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: html, web
  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. ;; The idea is to provide a simple, fast and pretty minimal way to
  18. ;; render HTML (including links and images) in a buffer, based on an
  19. ;; external HTML renderer (i.e., w3m).
  20. ;;; Code:
  21. (eval-when-compile (require 'cl))
  22. (require 'gnus-art)
  23. (eval-when-compile (require 'mm-decode))
  24. (require 'mm-url)
  25. (require 'url)
  26. (require 'url-cache)
  27. (require 'xml)
  28. (require 'browse-url)
  29. (require 'mm-util)
  30. (eval-and-compile (unless (featurep 'xemacs) (require 'help-fns)))
  31. (defcustom gnus-html-image-cache-ttl (days-to-time 7)
  32. "Time used to determine if we should use images from the cache."
  33. :version "24.1"
  34. :group 'gnus-art
  35. ;; FIXME hardly the friendliest type. The allowed value is actually
  36. ;; any time value, but we are assuming no-one cares about USEC and
  37. ;; PSEC here. It would be better to eg make it a number of minutes.
  38. :type '(list integer integer))
  39. (defcustom gnus-html-image-automatic-caching t
  40. "Whether automatically cache retrieve images."
  41. :version "24.1"
  42. :group 'gnus-art
  43. :type 'boolean)
  44. (defcustom gnus-html-frame-width 70
  45. "What width to use when rendering HTML."
  46. :version "24.1"
  47. :group 'gnus-art
  48. :type 'integer)
  49. (defcustom gnus-max-image-proportion 0.9
  50. "How big pictures displayed are in relation to the window they're in.
  51. A value of 0.7 means that they are allowed to take up 70% of the
  52. width and height of the window. If they are larger than this,
  53. and Emacs supports it, then the images will be rescaled down to
  54. fit these criteria."
  55. :version "24.1"
  56. :group 'gnus-art
  57. :type 'float)
  58. (defvar gnus-html-image-map
  59. (let ((map (make-sparse-keymap)))
  60. (define-key map "u" 'gnus-article-copy-string)
  61. (define-key map "i" 'gnus-html-insert-image)
  62. (define-key map "v" 'gnus-html-browse-url)
  63. map))
  64. (defvar gnus-html-displayed-image-map
  65. (let ((map (make-sparse-keymap)))
  66. (define-key map "a" 'gnus-html-show-alt-text)
  67. (define-key map "i" 'gnus-html-browse-image)
  68. (define-key map "\r" 'gnus-html-browse-url)
  69. (define-key map "u" 'gnus-article-copy-string)
  70. (define-key map [tab] 'widget-forward)
  71. map))
  72. (eval-and-compile
  73. (defalias 'gnus-html-encode-url-chars
  74. (if (fboundp 'browse-url-url-encode-chars)
  75. 'browse-url-url-encode-chars
  76. (lambda (text chars)
  77. "URL-encode the chars in TEXT that match CHARS.
  78. CHARS is a regexp-like character alternative (e.g., \"[)$]\")."
  79. (let ((encoded-text (copy-sequence text))
  80. (s 0))
  81. (while (setq s (string-match chars encoded-text s))
  82. (setq encoded-text
  83. (replace-match (format "%%%x"
  84. (string-to-char
  85. (match-string 0 encoded-text)))
  86. t t encoded-text)
  87. s (1+ s)))
  88. encoded-text)))))
  89. (defun gnus-html-encode-url (url)
  90. "Encode URL."
  91. (gnus-html-encode-url-chars url "[)$ ]"))
  92. (defun gnus-html-cache-expired (url ttl)
  93. "Check if URL is cached for more than TTL."
  94. (cond (url-standalone-mode
  95. (not (file-exists-p (url-cache-create-filename url))))
  96. (t (let ((cache-time (url-is-cached url)))
  97. (if cache-time
  98. (time-less-p
  99. (time-add
  100. cache-time
  101. ttl)
  102. (current-time))
  103. t)))))
  104. ;;;###autoload
  105. (defun gnus-article-html (&optional handle)
  106. (let ((article-buffer (current-buffer)))
  107. (unless handle
  108. (setq handle (mm-dissect-buffer t)))
  109. (save-restriction
  110. (narrow-to-region (point) (point))
  111. (save-excursion
  112. (mm-with-part handle
  113. (let* ((coding-system-for-read 'utf-8)
  114. (coding-system-for-write 'utf-8)
  115. (default-process-coding-system
  116. (cons coding-system-for-read coding-system-for-write))
  117. (charset (mail-content-type-get (mm-handle-type handle)
  118. 'charset)))
  119. (when (and charset
  120. (setq charset (mm-charset-to-coding-system
  121. charset nil t))
  122. (not (eq charset 'ascii)))
  123. (insert (prog1
  124. (mm-decode-coding-string (buffer-string) charset)
  125. (erase-buffer)
  126. (mm-enable-multibyte))))
  127. (call-process-region (point-min) (point-max)
  128. "w3m"
  129. nil article-buffer nil
  130. "-halfdump"
  131. "-no-cookie"
  132. "-I" "UTF-8"
  133. "-O" "UTF-8"
  134. "-o" "ext_halfdump=1"
  135. "-o" "display_ins_del=2"
  136. "-o" "pre_conv=1"
  137. "-t" (format "%s" tab-width)
  138. "-cols" (format "%s" gnus-html-frame-width)
  139. "-o" "display_image=on"
  140. "-T" "text/html"))))
  141. (gnus-html-wash-tags))))
  142. (defvar gnus-article-mouse-face)
  143. (defun gnus-html-pre-wash ()
  144. (goto-char (point-min))
  145. (while (re-search-forward " *<pre_int> *</pre_int> *\n" nil t)
  146. (replace-match "" t t))
  147. (goto-char (point-min))
  148. (while (re-search-forward "<a name[^\n>]+>" nil t)
  149. (replace-match "" t t)))
  150. (defun gnus-html-wash-images ()
  151. "Run through current buffer and replace img tags by images."
  152. (let (tag parameters string start end images url alt-text
  153. inhibit-images blocked-images)
  154. (if (buffer-live-p gnus-summary-buffer)
  155. (with-current-buffer gnus-summary-buffer
  156. (setq inhibit-images gnus-inhibit-images
  157. blocked-images (gnus-blocked-images)))
  158. (setq inhibit-images gnus-inhibit-images
  159. blocked-images (gnus-blocked-images)))
  160. (goto-char (point-min))
  161. ;; Search for all the images first.
  162. (while (re-search-forward "<img_alt \\([^>]*\\)>" nil t)
  163. (setq parameters (match-string 1)
  164. start (match-beginning 0))
  165. (delete-region start (point))
  166. (when (search-forward "</img_alt>" (line-end-position) t)
  167. (delete-region (match-beginning 0) (match-end 0)))
  168. (setq end (point))
  169. (when (string-match "src=\"\\([^\"]+\\)" parameters)
  170. (gnus-message 8 "gnus-html-wash-tags: fetching image URL %s" url)
  171. (setq url (gnus-html-encode-url (match-string 1 parameters))
  172. alt-text (when (string-match "\\(alt\\|title\\)=\"\\([^\"]+\\)"
  173. parameters)
  174. (xml-substitute-special (match-string 2 parameters))))
  175. (gnus-add-text-properties
  176. start end
  177. (list 'image-url url
  178. 'image-displayer `(lambda (url start end)
  179. (gnus-html-display-image url start end
  180. ,alt-text))
  181. 'gnus-image (list url start end alt-text)))
  182. (widget-convert-button
  183. 'url-link start (point)
  184. :help-echo alt-text
  185. :keymap gnus-html-image-map
  186. url)
  187. (if (string-match "\\`cid:" url)
  188. ;; URLs with cid: have their content stashed in other
  189. ;; parts of the MIME structure, so just insert them
  190. ;; immediately.
  191. (let* ((handle (mm-get-content-id (substring url (match-end 0))))
  192. (image (when (and handle
  193. (not inhibit-images))
  194. (gnus-create-image
  195. (mm-with-part handle (buffer-string))
  196. nil t))))
  197. (if image
  198. (gnus-add-image
  199. 'cid
  200. (gnus-put-image
  201. (gnus-rescale-image
  202. image (gnus-html-maximum-image-size))
  203. (gnus-string-or (prog1
  204. (buffer-substring start end)
  205. (delete-region start end))
  206. "*")
  207. 'cid))
  208. (widget-convert-button
  209. 'link start end
  210. :action 'gnus-html-insert-image
  211. :help-echo url
  212. :keymap gnus-html-image-map
  213. :button-keymap gnus-html-image-map)))
  214. ;; Normal, external URL.
  215. (if (or inhibit-images
  216. (gnus-html-image-url-blocked-p url blocked-images))
  217. (widget-convert-button
  218. 'link start end
  219. :action 'gnus-html-insert-image
  220. :help-echo url
  221. :keymap gnus-html-image-map
  222. :button-keymap gnus-html-image-map)
  223. ;; Non-blocked url
  224. (let ((width
  225. (when (string-match "width=\"?\\([0-9]+\\)" parameters)
  226. (string-to-number (match-string 1 parameters))))
  227. (height
  228. (when (string-match "height=\"?\\([0-9]+\\)" parameters)
  229. (string-to-number (match-string 1 parameters)))))
  230. ;; Don't fetch images that are really small. They're
  231. ;; probably tracking pictures.
  232. (when (and (or (null height)
  233. (> height 4))
  234. (or (null width)
  235. (> width 4)))
  236. (gnus-html-display-image url start end alt-text)))))))))
  237. (defun gnus-html-display-image (url start end &optional alt-text)
  238. "Display image at URL on text from START to END.
  239. Use ALT-TEXT for the image string."
  240. (or alt-text (setq alt-text "*"))
  241. (if (string-match "\\`cid:" url)
  242. (let ((handle (mm-get-content-id (substring url (match-end 0)))))
  243. (when handle
  244. (gnus-html-put-image (mm-with-part handle (buffer-string))
  245. url alt-text)))
  246. (if (gnus-html-cache-expired url gnus-html-image-cache-ttl)
  247. ;; We don't have it, so schedule it for fetching
  248. ;; asynchronously.
  249. (gnus-html-schedule-image-fetching
  250. (current-buffer)
  251. (list url alt-text))
  252. ;; It's already cached, so just insert it.
  253. (gnus-html-put-image (gnus-html-get-image-data url) url alt-text))))
  254. (defun gnus-html-wash-tags ()
  255. (let (tag parameters string start end images url)
  256. (gnus-html-pre-wash)
  257. (gnus-html-wash-images)
  258. (goto-char (point-min))
  259. ;; Then do the other tags.
  260. (while (re-search-forward "<\\([^ />]+\\)\\([^>]*\\)>" nil t)
  261. (setq tag (match-string 1)
  262. parameters (match-string 2)
  263. start (match-beginning 0))
  264. (when (> (length parameters) 0)
  265. (set-text-properties 0 (1- (length parameters)) nil parameters))
  266. (delete-region start (point))
  267. (when (search-forward (concat "</" tag ">") nil t)
  268. (delete-region (match-beginning 0) (match-end 0)))
  269. (setq end (point))
  270. (cond
  271. ;; Fetch and insert a picture.
  272. ((equal tag "img_alt"))
  273. ;; Add a link.
  274. ((or (equal tag "a")
  275. (equal tag "A"))
  276. (when (string-match "href=\"\\([^\"]+\\)" parameters)
  277. (setq url (match-string 1 parameters))
  278. (gnus-message 8 "gnus-html-wash-tags: fetching link URL %s" url)
  279. (gnus-article-add-button start end
  280. 'browse-url (mm-url-decode-entities-string url)
  281. url)
  282. (let ((overlay (make-overlay start end)))
  283. (overlay-put overlay 'evaporate t)
  284. (overlay-put overlay 'gnus-button-url url)
  285. (gnus-put-text-property start end 'gnus-string url)
  286. (when gnus-article-mouse-face
  287. (overlay-put overlay 'mouse-face gnus-article-mouse-face)))))
  288. ;; The upper-case IMG_ALT is apparently just an artifact that
  289. ;; should be deleted.
  290. ((equal tag "IMG_ALT")
  291. (delete-region start end))
  292. ;; w3m does not normalize the case
  293. ((or (equal tag "b")
  294. (equal tag "B"))
  295. (overlay-put (make-overlay start end) 'face 'gnus-emphasis-bold))
  296. ((or (equal tag "u")
  297. (equal tag "U"))
  298. (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
  299. ((or (equal tag "i")
  300. (equal tag "I"))
  301. (overlay-put (make-overlay start end) 'face 'gnus-emphasis-italic))
  302. ((or (equal tag "s")
  303. (equal tag "S"))
  304. (overlay-put (make-overlay start end) 'face 'gnus-emphasis-strikethru))
  305. ((or (equal tag "ins")
  306. (equal tag "INS"))
  307. (overlay-put (make-overlay start end) 'face 'gnus-emphasis-underline))
  308. ;; Handle different UL types
  309. ((equal tag "_SYMBOL")
  310. (when (string-match "TYPE=\\(.+\\)" parameters)
  311. (let ((type (string-to-number (match-string 1 parameters))))
  312. (delete-region start end)
  313. (cond ((= type 33) (insert " "))
  314. ((= type 34) (insert " "))
  315. ((= type 35) (insert " "))
  316. ((= type 36) (insert " "))
  317. ((= type 37) (insert " "))
  318. ((= type 38) (insert " "))
  319. ((= type 39) (insert " "))
  320. ((= type 40) (insert " "))
  321. ((= type 42) (insert " "))
  322. ((= type 43) (insert " "))
  323. (t (insert " "))))))
  324. ;; Whatever. Just ignore the tag.
  325. (t
  326. ))
  327. (goto-char start))
  328. (goto-char (point-min))
  329. ;; The output from -halfdump isn't totally regular, so strip
  330. ;; off any </pre_int>s that were left over.
  331. (while (re-search-forward "</pre_int>\\|</internal>" nil t)
  332. (replace-match "" t t))
  333. (mm-url-decode-entities)))
  334. (defun gnus-html-insert-image (&rest args)
  335. "Fetch and insert the image under point."
  336. (interactive)
  337. (apply 'gnus-html-display-image (get-text-property (point) 'gnus-image)))
  338. (defun gnus-html-show-alt-text ()
  339. "Show the ALT text of the image under point."
  340. (interactive)
  341. (message "%s" (get-text-property (point) 'gnus-alt-text)))
  342. (defun gnus-html-browse-image ()
  343. "Browse the image under point."
  344. (interactive)
  345. (browse-url (get-text-property (point) 'image-url)))
  346. (defun gnus-html-browse-url ()
  347. "Browse the image under point."
  348. (interactive)
  349. (let ((url (get-text-property (point) 'gnus-string)))
  350. (cond
  351. ((not url)
  352. (message "No link under point"))
  353. ((string-match "^mailto:" url)
  354. (gnus-url-mailto url))
  355. (t
  356. (browse-url url)))))
  357. (defun gnus-html-schedule-image-fetching (buffer image)
  358. "Retrieve IMAGE, and place it into BUFFER on arrival."
  359. (gnus-message 8 "gnus-html-schedule-image-fetching: buffer %s, image %s"
  360. buffer image)
  361. (if (fboundp 'url-queue-retrieve)
  362. (url-queue-retrieve (car image)
  363. 'gnus-html-image-fetched
  364. (list buffer image) t t)
  365. (ignore-errors
  366. (url-retrieve (car image)
  367. 'gnus-html-image-fetched
  368. (list buffer image)))))
  369. (defun gnus-html-image-fetched (status buffer image)
  370. "Callback function called when image has been fetched."
  371. (unless (plist-get status :error)
  372. (when (and (or (search-forward "\n\n" nil t)
  373. (search-forward "\r\n\r\n" nil t))
  374. (not (eobp)))
  375. (when gnus-html-image-automatic-caching
  376. (url-store-in-cache (current-buffer)))
  377. (when (buffer-live-p buffer)
  378. (let ((data (buffer-substring (point) (point-max))))
  379. (with-current-buffer buffer
  380. (let ((inhibit-read-only t))
  381. (gnus-html-put-image data (car image) (cadr image))))))))
  382. (kill-buffer (current-buffer)))
  383. (defun gnus-html-get-image-data (url)
  384. "Get image data for URL.
  385. Return a string with image data."
  386. (with-temp-buffer
  387. (mm-disable-multibyte)
  388. (url-cache-extract (url-cache-create-filename url))
  389. (when (or (search-forward "\n\n" nil t)
  390. (search-forward "\r\n\r\n" nil t))
  391. (buffer-substring (point) (point-max)))))
  392. (defun gnus-html-maximum-image-size ()
  393. "Return the maximum size of an image according to `gnus-max-image-proportion'."
  394. (let ((edges (gnus-window-inside-pixel-edges
  395. (get-buffer-window (current-buffer)))))
  396. ;; (width . height)
  397. (cons
  398. ;; Aimed width
  399. (truncate
  400. (* gnus-max-image-proportion
  401. (- (nth 2 edges) (nth 0 edges))))
  402. ;; Aimed height
  403. (truncate (* gnus-max-image-proportion
  404. (- (nth 3 edges) (nth 1 edges)))))))
  405. ;; Behind display-graphic-p test.
  406. (declare-function image-size "image.c" (spec &optional pixels frame))
  407. (defun gnus-html-put-image (data url &optional alt-text)
  408. "Put an image with DATA from URL and optional ALT-TEXT."
  409. (when (gnus-graphic-display-p)
  410. (let* ((start (text-property-any (point-min) (point-max)
  411. 'image-url url))
  412. (end (when start
  413. (next-single-property-change start 'image-url))))
  414. ;; Image found?
  415. (when start
  416. (let* ((image
  417. (ignore-errors
  418. (gnus-create-image data nil t)))
  419. (size (and image
  420. (if (featurep 'xemacs)
  421. (cons (glyph-width image) (glyph-height image))
  422. (image-size image t)))))
  423. (save-excursion
  424. (goto-char start)
  425. (let ((alt-text (or alt-text
  426. (buffer-substring-no-properties start end)))
  427. (inhibit-read-only t))
  428. (if (and image
  429. ;; Kludge to avoid displaying 30x30 gif images, which
  430. ;; seems to be a signal of a broken image.
  431. (not (and (if (featurep 'xemacs)
  432. (glyphp image)
  433. (listp image))
  434. (eq (if (featurep 'xemacs)
  435. (let ((d (cdadar
  436. (specifier-spec-list
  437. (glyph-image image)))))
  438. (and (vectorp d)
  439. (aref d 0)))
  440. (plist-get (cdr image) :type))
  441. 'gif)
  442. (= (car size) 30)
  443. (= (cdr size) 30))))
  444. ;; Good image, add it!
  445. (let ((image (gnus-rescale-image image (gnus-html-maximum-image-size))))
  446. (delete-region start end)
  447. (gnus-put-image image alt-text 'external)
  448. (widget-convert-button
  449. 'url-link start (point)
  450. :help-echo alt-text
  451. :keymap gnus-html-displayed-image-map
  452. url)
  453. (gnus-put-text-property start (point)
  454. 'gnus-alt-text alt-text)
  455. (when url
  456. (gnus-add-text-properties
  457. start (point)
  458. `(image-url
  459. ,url
  460. image-displayer
  461. (lambda (url start end)
  462. (gnus-html-display-image url start end
  463. ,alt-text)))))
  464. (gnus-add-image 'external image)
  465. t)
  466. ;; Bad image, try to show something else
  467. (when (fboundp 'find-image)
  468. (delete-region start end)
  469. (setq image (find-image
  470. '((:type xpm :file "lock-broken.xpm"))))
  471. (gnus-put-image image alt-text 'internal)
  472. (gnus-add-image 'internal image))
  473. nil))))))))
  474. (defun gnus-html-image-url-blocked-p (url blocked-images)
  475. "Find out if URL is blocked by BLOCKED-IMAGES."
  476. (let ((ret (and blocked-images
  477. (string-match blocked-images url))))
  478. (if ret
  479. (gnus-message 8 "gnus-html-image-url-blocked-p: %s blocked by regex %s"
  480. url blocked-images)
  481. (gnus-message 9 "gnus-html-image-url-blocked-p: %s passes regex %s"
  482. url blocked-images))
  483. ret))
  484. ;;;###autoload
  485. (defun gnus-html-prefetch-images (summary)
  486. (when (buffer-live-p summary)
  487. (let (inhibit-images blocked-images)
  488. (with-current-buffer summary
  489. (setq inhibit-images gnus-inhibit-images
  490. blocked-images (gnus-blocked-images)))
  491. (save-match-data
  492. (while (re-search-forward "<img[^>]+src=[\"']\\(http[^\"']+\\)" nil t)
  493. (let ((url (gnus-html-encode-url
  494. (mm-url-decode-entities-string (match-string 1)))))
  495. (unless (or inhibit-images
  496. (gnus-html-image-url-blocked-p url blocked-images))
  497. (when (gnus-html-cache-expired url gnus-html-image-cache-ttl)
  498. (gnus-html-schedule-image-fetching nil
  499. (list url))))))))))
  500. (provide 'gnus-html)
  501. ;;; gnus-html.el ends here