datauris 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1 -1
- data/README.md +12 -12
- data/lib/datauris/version.rb +1 -1
- data/lib/datauris.rb +37 -24
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7709f8cf824f237c4aed38259191ecb6fd4cf39d3b29664a93ea1ee16902f492
|
4
|
+
data.tar.gz: 8961a0856b232df9c7a5995db91e4f4ae08c072e7d4d655d51ae0be6b8226009
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85584d023850013b98801c46775c5a43ea76d08eedef815457921684063081a0786517919190c1b9c0b2fbd2d9923773739c0f029338d78d44fcfe88513b9459
|
7
|
+
data.tar.gz: e4a6796902329a3efb962f3733012bbce8b9dd2baf2f315f92b033e41c627630a4ef5c0981368c84526623ed9226bd573e7f079cae6b03c0beb3f0382cdc98d8
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Data URI Helpers
|
2
2
|
|
3
|
-
datauris - helpers to parse (decode) and build (encode) data uris incl. (strict)base64-encoded/decoded images and more
|
3
|
+
datauris - helpers to parse (decode) and build (encode) data uris incl. (strict) base64-encoded/decoded images and more
|
4
4
|
|
5
5
|
* home :: [github.com/s6ruby/rubidity](https://github.com/s6ruby/rubidity)
|
6
6
|
* bugs :: [github.com/s6ruby/rubidity/issues](https://github.com/s6ruby/rubidity/issues)
|
@@ -58,21 +58,21 @@ Let's try and parse the Wikipedia Data URI examples:
|
|
58
58
|
uri = "data:text/vnd-example+xyz;foo=bar;base64,R0lGODdh"
|
59
59
|
DataUri.valid?( uri )
|
60
60
|
#=> true
|
61
|
-
|
62
|
-
#=> "text/vnd-example+xyz;foo=bar"
|
61
|
+
data, mediatype = DataUri.parse( uri ) ## returns 1) data, 2) mediatype (+parameters)
|
62
|
+
#=> "<blob>", "text/vnd-example+xyz;foo=bar"
|
63
63
|
|
64
64
|
uri = "data:text/plain;charset=UTF-8;page=21,the%20data:1234,5678"
|
65
65
|
DataUri.valid?( uri )
|
66
66
|
#=> true
|
67
|
-
|
68
|
-
#=> "text/plain;charset=UTF-8;page=21"
|
67
|
+
data, mediatype = DataUri.parse( uri ) ## returns 1) data, 2) mediatype (+parameters)
|
68
|
+
#=> "the data:1234,5678", "text/plain;charset=UTF-8;page=21"
|
69
69
|
|
70
70
|
|
71
71
|
uri = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/"
|
72
72
|
DataUri.valid?( uri )
|
73
73
|
#=> true
|
74
|
-
|
75
|
-
#=> "
|
74
|
+
data, mediatype = DataUri.parse( uri ) ## returns 1) data, 2) mediatype (+parameters)
|
75
|
+
#=> "<blob>", "image/jpeg"
|
76
76
|
```
|
77
77
|
|
78
78
|
|
@@ -87,7 +87,7 @@ uri = "data:,"
|
|
87
87
|
|
88
88
|
DataUri.valid?( uri )
|
89
89
|
#=> true
|
90
|
-
|
90
|
+
data, mediatype = DataUri.parse( uri ) ## returns 1) data, 2) mediatype (+parameters)
|
91
91
|
#=> "", ""
|
92
92
|
```
|
93
93
|
|
@@ -98,8 +98,8 @@ uri = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHV..."
|
|
98
98
|
|
99
99
|
DataUri.valid?( uri )
|
100
100
|
#=> true
|
101
|
-
|
102
|
-
#=> "
|
101
|
+
data, mediatype = DataUri.parse( uri ) ## returns 1) data, 2) mediatype (+parameters)
|
102
|
+
#=> "<blob>", "image/jpeg"
|
103
103
|
|
104
104
|
## let's save the jpeg image (blob)
|
105
105
|
write_blob( "0.jpeg", data )
|
@@ -117,8 +117,8 @@ uri = "data:image/png;base64,/9j/4gxYSUNDX1BST0ZJTEUAAQEAAAxITGlubwIQAAB..."
|
|
117
117
|
|
118
118
|
DataUri.valid?( uri )
|
119
119
|
#=> true
|
120
|
-
|
121
|
-
#=> "
|
120
|
+
data, mediatype = DataUri.parse( uri ) ## returns 1) data, 2) mediatype (+parameters)
|
121
|
+
#=> "<blob>", "image/png"
|
122
122
|
|
123
123
|
## let's save the png image (blob)
|
124
124
|
write_blob( "15.png", data )
|
data/lib/datauris/version.rb
CHANGED
data/lib/datauris.rb
CHANGED
@@ -8,17 +8,6 @@ require_relative 'datauris/version' # let version go first
|
|
8
8
|
|
9
9
|
|
10
10
|
module DataUri
|
11
|
-
REGEX_V0 = %r{
|
12
|
-
\Adata:
|
13
|
-
(?<mediatype>
|
14
|
-
(?<type> .+? / .+? )?
|
15
|
-
(?<parameters> (?: ; .+? = .+? )* )
|
16
|
-
)?
|
17
|
-
(?<base64extension>;base64)?
|
18
|
-
,
|
19
|
-
(?<data>.*)\z
|
20
|
-
}x
|
21
|
-
|
22
11
|
## allow type only - why? why not?
|
23
12
|
## split subtype into [tree prefix] and subtype
|
24
13
|
## check if type can include dash (-) - why? why not?
|
@@ -58,13 +47,15 @@ module DataUri
|
|
58
47
|
(?<subtype> [\w.+-]+? )
|
59
48
|
)?
|
60
49
|
(?<parameters> (?: ;
|
61
|
-
[\w
|
50
|
+
[\w-]+?
|
62
51
|
=
|
63
52
|
.+?
|
64
53
|
)*
|
65
54
|
)
|
66
55
|
)?
|
67
|
-
(
|
56
|
+
(?:;
|
57
|
+
(?<extension>base64|utf8)
|
58
|
+
)?
|
68
59
|
,
|
69
60
|
(?<data>.*)
|
70
61
|
\z
|
@@ -73,41 +64,57 @@ module DataUri
|
|
73
64
|
|
74
65
|
def self._parse( str ) REGEX.match( str ); end
|
75
66
|
|
76
|
-
def self.parse( str )
|
67
|
+
def self.parse( str, utf8: false ) ## allow force utf8 with utf8: true
|
77
68
|
m = _parse( str )
|
78
69
|
|
79
70
|
if m
|
80
71
|
## 1) return mediatype (mimetype PLUS optional parameters)
|
81
72
|
## 2) return data (base64 decoded or not)
|
82
73
|
|
74
|
+
## todo/check:
|
75
|
+
## add force_encoding( 'UTF-8' ) to returned data (if not base64) - why? why not?
|
76
|
+
|
83
77
|
mediatype = m[:mediatype]
|
84
|
-
data = if m[:
|
78
|
+
data = if m[:extension] && m[:extension] == 'base64' ## assume base64 encoded
|
85
79
|
Base64.strict_decode64(m[:data])
|
80
|
+
elsif utf8 || (m[:extension] && m[:extension] == 'utf8')
|
81
|
+
m[:data] ## note: no decode; assume "plain" utf8 string
|
86
82
|
else
|
87
83
|
## e.g. %20 => space(20)
|
88
84
|
## etc.
|
89
85
|
## todo/double check - use a different URI decoder - why? why not?
|
90
86
|
URI.decode_uri_component(m[:data])
|
91
87
|
end
|
92
|
-
|
88
|
+
|
89
|
+
## returns data first!!!
|
90
|
+
## and mediatype second (to follow the order in build)
|
91
|
+
## - also mediatype is optional - arguable more intuitive - yes,no?
|
92
|
+
[data, mediatype]
|
93
93
|
else
|
94
94
|
raise ArgumentError, "invalid datauri - cannot match regex; sorry"
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
98
|
|
99
|
-
def self.valid?( str )
|
99
|
+
def self.valid?( str, utf8: false )
|
100
100
|
m = _parse( str )
|
101
101
|
if m
|
102
|
-
if m[:
|
102
|
+
if m[:extension] && m[:extension] == 'base64' ## assume base64
|
103
103
|
begin
|
104
104
|
Base64.strict_decode64(m[:data])
|
105
105
|
true
|
106
106
|
rescue ArgumentError
|
107
107
|
false
|
108
108
|
end
|
109
|
+
elsif utf8 || (m[:extension] && m[:extension] == 'utf8')
|
110
|
+
true ## pass through as is; assume always true (check valid utf8 encoding - why? why not?)
|
109
111
|
else
|
110
|
-
|
112
|
+
begin
|
113
|
+
URI.decode_uri_component(m[:data])
|
114
|
+
true
|
115
|
+
rescue ArgumentError ## check if decode errors are argument errors??
|
116
|
+
false
|
117
|
+
end
|
111
118
|
end
|
112
119
|
else
|
113
120
|
false
|
@@ -161,8 +168,10 @@ module DataUri
|
|
161
168
|
end
|
162
169
|
|
163
170
|
|
164
|
-
## base64 - force base64 encoding
|
165
|
-
|
171
|
+
## base64 - force base64 encoding instead of "automagic" (base64: true)
|
172
|
+
## utf8 - or force utf8 encoding (utf8: true)
|
173
|
+
## uri - or force STOPPING uri encoding (uri: false)
|
174
|
+
def self.build( data, type=nil, base64: nil, utf8: nil, uri: nil )
|
166
175
|
uri = "data:"
|
167
176
|
uri += type if type ## note: allow optional / no type
|
168
177
|
|
@@ -187,12 +196,16 @@ module DataUri
|
|
187
196
|
|
188
197
|
if base64
|
189
198
|
uri += ";base64," + Base64.strict_encode64( data )
|
199
|
+
elsif utf8
|
200
|
+
uri += ";utf8," + data
|
190
201
|
else
|
191
202
|
## use encode_uri_component by default - why? why not?
|
192
203
|
## space becomes %20
|
193
204
|
## : becomes %3A
|
194
205
|
## , becomes %2C and so on
|
195
|
-
|
206
|
+
##
|
207
|
+
## note: use uri: false to turn of uri encode!!!
|
208
|
+
uri += "," + (uri == false ? data : encode_uri( data ))
|
196
209
|
end
|
197
210
|
end
|
198
211
|
|
@@ -201,8 +214,8 @@ module DataUri
|
|
201
214
|
## add alias convenience names - why? why not?
|
202
215
|
class << self
|
203
216
|
alias_method :is_valid?, :valid?
|
204
|
-
alias_method :decode, :parse
|
205
|
-
alias_method :encode, :build
|
217
|
+
alias_method :decode, :parse ### add decode - why? why not?
|
218
|
+
alias_method :encode, :build ### add encode - why? why not?
|
206
219
|
end
|
207
220
|
end # module DataUri
|
208
221
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datauris
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gerald Bauer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rdoc
|