Scheme for Encoding/Decoding Data URIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rebol [ | |
Title: "Data URI Scheme" | |
Date: 13-Feb-2021 | |
Author: "Christopher Ross-Gill" | |
] | |
use [chars type] [ | |
chars: complement charset "^-^/^M ,/;" | |
sys/make-scheme [ | |
name: 'data | |
init: func [port] [ | |
for-each word [ | |
type base64 data | |
][ | |
any [ | |
in port/spec word | |
extend port/spec word _ | |
] | |
] | |
case [ | |
not url? port/spec/ref [ | |
fail "Must use URL! to initiate a data uri" | |
] | |
not parse port/spec/ref [ | |
"data:" | |
copy type [some chars #"/" some chars] | |
(port/spec/type: :type) | |
[ | |
";base64" | |
(port/spec/base64: yes) | |
| | |
(port/spec/base64: no) | |
] | |
#"," port/spec/data: | |
][ | |
fail "Not a valid data URI" | |
] | |
] | |
] | |
actor: [ | |
read: func [port] [ | |
either port/spec/base64 [ | |
debase/base form port/spec/data 64 | |
][ | |
as binary! dehex form port/spec/data | |
] | |
] | |
write: func [port data] [ | |
head insert clear port/spec/data either port/spec/base64 [ | |
trim/all enbase/base as binary! data 64 | |
][ | |
enhex data | |
] | |
] | |
] | |
] | |
] |
Author
rgchris
commented
Feb 13, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment