gnus-html.el 19 KB

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