Gnus development mailing list
 help / color / mirror / Atom feed
* <img src="data:..."> and shr
@ 2013-04-11 10:37 David Edmondson
  2013-04-15  4:39 ` Katsumi Yamaoka
  0 siblings, 1 reply; 6+ messages in thread
From: David Edmondson @ 2013-04-11 10:37 UTC (permalink / raw)
  To: ding

[-- Attachment #1: Type: text/plain, Size: 1610 bytes --]

Here is a rudimentary patch to support the display of images inlined
using data: URIs. It works in the few cases that I have available to
test, but would benefit from examination and improvement, I'm sure.

diff --git a/lisp/shr.el b/lisp/shr.el
index 5df5297..0cf8d39 100644
--- a/lisp/shr.el
+++ b/lisp/shr.el
@@ -593,6 +593,20 @@ size, and full-buffer size."
 		      (put-text-property start (point) type value))))))))))
     (kill-buffer image-buffer)))
 
+(defun shr-image-from-data (data)
+  "Return an image from the data: URI content DATA."
+  (when (string-match "\\([a-z]+/[a-z]+\\)\\(;[a-z0-9]+\\)?,\\(.*\\)" data)
+    (let ((content-type (match-string 1 data))
+	  (encoding (match-string 2 data))
+	  (payload (match-string 3 data)))
+      (cond
+       ((string= encoding ""))
+       ((string= encoding ";base64")
+	(setq payload (base64-decode-string payload)))
+       (t
+	(error "Unknown inline data encoding: %s" encoding)))
+      payload)))
+
 (defun shr-put-image (data alt &optional flags)
   "Put image DATA with a string ALT.  Return image."
   (if (display-graphic-p)
@@ -983,6 +997,12 @@ ones, in case fg and bg are nil."
 	  ;; Ignore zero-sized or single-pixel images.
 	  )
 	 ((and (not shr-inhibit-images)
+	       (string-match "\\`data:" url))
+	  (let ((image (shr-image-from-data (substring url (match-end 0)))))
+	    (if image
+		(funcall shr-put-image-function image alt)
+	      (insert alt))))
+	 ((and (not shr-inhibit-images)
 	       (string-match "\\`cid:" url))
 	  (let ((url (substring url (match-end 0)))
 		image)

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: <img src="data:..."> and shr
  2013-04-11 10:37 <img src="data:..."> and shr David Edmondson
@ 2013-04-15  4:39 ` Katsumi Yamaoka
  2013-04-15 18:01   ` David Edmondson
  0 siblings, 1 reply; 6+ messages in thread
From: Katsumi Yamaoka @ 2013-04-15  4:39 UTC (permalink / raw)
  To: ding

[-- Attachment #1: Type: text/plain, Size: 501 bytes --]

Hi,

David Edmondson wrote:
> Here is a rudimentary patch to support the display of images inlined
> using data: URIs. It works in the few cases that I have available to
> test, but would benefit from examination and improvement, I'm sure.

It seems necessary to add a code that decodes "%2b%2d%3d" into
"/+=", etc. in `payload'.  Naohiro Aota did it in emacs-w3m last
summer.  Though I've never seen such mails so far, I confirmed
at least Firefox 20 supports it (try `K H' on the attached example).

[-- Attachment #2: html_mail.gz --]
[-- Type: application/x-gunzip, Size: 349 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: <img src="data:..."> and shr
  2013-04-15  4:39 ` Katsumi Yamaoka
@ 2013-04-15 18:01   ` David Edmondson
  2013-04-15 20:05     ` Andreas Schwab
  0 siblings, 1 reply; 6+ messages in thread
From: David Edmondson @ 2013-04-15 18:01 UTC (permalink / raw)
  To: ding

[-- Attachment #1: Type: text/plain, Size: 592 bytes --]

On Mon, Apr 15 2013, Katsumi Yamaoka wrote:
> David Edmondson wrote:
>> Here is a rudimentary patch to support the display of images inlined
>> using data: URIs. It works in the few cases that I have available to
>> test, but would benefit from examination and improvement, I'm sure.
>
> It seems necessary to add a code that decodes "%2b%2d%3d" into
> "/+=", etc. in `payload'.

That's good to know, thanks. I was surprised that I couldn't quickly
find a non-w3m function to do the URL decoding already in emacs. It's
easy to write a new one, of course, but did I miss an existing function?

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: <img src="data:..."> and shr
  2013-04-15 18:01   ` David Edmondson
@ 2013-04-15 20:05     ` Andreas Schwab
  2013-04-16  6:12       ` David Edmondson
  0 siblings, 1 reply; 6+ messages in thread
From: Andreas Schwab @ 2013-04-15 20:05 UTC (permalink / raw)
  To: David Edmondson; +Cc: ding

David Edmondson <dme@dme.org> writes:

> That's good to know, thanks. I was surprised that I couldn't quickly
> find a non-w3m function to do the URL decoding already in emacs. It's
> easy to write a new one, of course, but did I miss an existing function?

(url-unhex-string "%2b%2d%3d") => "+-="

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: <img src="data:..."> and shr
  2013-04-15 20:05     ` Andreas Schwab
@ 2013-04-16  6:12       ` David Edmondson
  2013-04-16  6:44         ` Katsumi Yamaoka
  0 siblings, 1 reply; 6+ messages in thread
From: David Edmondson @ 2013-04-16  6:12 UTC (permalink / raw)
  To: ding

[-- Attachment #1: Type: text/plain, Size: 1702 bytes --]

On Mon, Apr 15 2013, Andreas Schwab wrote:
> David Edmondson <dme@dme.org> writes:
>
>> That's good to know, thanks. I was surprised that I couldn't quickly
>> find a non-w3m function to do the URL decoding already in emacs. It's
>> easy to write a new one, of course, but did I miss an existing function?
>
> (url-unhex-string "%2b%2d%3d") => "+-="

Thank you. Here is an improved patch.

diff --git a/lisp/shr.el b/lisp/shr.el
index 5df5297..eaf83e2 100644
--- a/lisp/shr.el
+++ b/lisp/shr.el
@@ -593,6 +593,16 @@ size, and full-buffer size."
 		      (put-text-property start (point) type value))))))))))
     (kill-buffer image-buffer)))
 
+(defun shr-image-from-data (data)
+  "Return an image from the data: URI content DATA."
+  (when (string-match "\\(\\([^/;,]+\\(/[^;,]+\\)?\\)\\(;[^;,]+\\)*\\)?,\\(.*\\)"
+		      data)
+    (let ((param (match-string 4 data))
+	  (payload (url-unhex-string (match-string 5 data))))
+      (when (string-match "^.*\\(;[ \t]*base64\\)$" param)
+	(setq payload (base64-decode-string payload)))
+      payload)))
+
 (defun shr-put-image (data alt &optional flags)
   "Put image DATA with a string ALT.  Return image."
   (if (display-graphic-p)
@@ -983,6 +993,12 @@ ones, in case fg and bg are nil."
 	  ;; Ignore zero-sized or single-pixel images.
 	  )
 	 ((and (not shr-inhibit-images)
+	       (string-match "\\`data:" url))
+	  (let ((image (shr-image-from-data (substring url (match-end 0)))))
+	    (if image
+		(funcall shr-put-image-function image alt)
+	      (insert alt))))
+	 ((and (not shr-inhibit-images)
 	       (string-match "\\`cid:" url))
 	  (let ((url (substring url (match-end 0)))
 		image)

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: <img src="data:..."> and shr
  2013-04-16  6:12       ` David Edmondson
@ 2013-04-16  6:44         ` Katsumi Yamaoka
  0 siblings, 0 replies; 6+ messages in thread
From: Katsumi Yamaoka @ 2013-04-16  6:44 UTC (permalink / raw)
  To: ding

David Edmondson wrote:
> On Mon, Apr 15 2013, Andreas Schwab wrote:
>> David Edmondson <dme@dme.org> writes:
>>
>>> That's good to know, thanks. I was surprised that I couldn't quickly
>>> find a non-w3m function to do the URL decoding already in emacs. It's
>>> easy to write a new one, of course, but did I miss an existing function?
>>
>> (url-unhex-string "%2b%2d%3d") => "+-="

> Thank you. Here is an improved patch.

I've applied your patch to Gnus master and Emacs trunk.  Thanks.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-04-16  6:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-11 10:37 <img src="data:..."> and shr David Edmondson
2013-04-15  4:39 ` Katsumi Yamaoka
2013-04-15 18:01   ` David Edmondson
2013-04-15 20:05     ` Andreas Schwab
2013-04-16  6:12       ` David Edmondson
2013-04-16  6:44         ` Katsumi Yamaoka

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).