datauris 1.0.1 → 1.1.1
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/lib/datauris/version.rb +1 -1
- data/lib/datauris.rb +32 -23
- 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: 89ab6159a9beb6750504017159f5d0e0391c78ea965aaea0d705531299bae7b1
|
4
|
+
data.tar.gz: 87bc327d253eb5464e47dbb2ef2d4770da2caacda008aac0ae8c8ad0b8f74483
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd984682f79f95f7170e023a4f1aa8a048339010e71ee984958c528a7c5bf7a8d6102d90398e636bf27c553cd88f38bae4417708ec6367e7ee713fe84b96620f
|
7
|
+
data.tar.gz: 0ebfd59f4ca9aa3ee881c5a5eab954d4ecd3691594418fc35b1709468d1012ede78139e180b47a700c25f796d8f2e827a1c76734b6aefb236f27e79fc5c0cf8d
|
data/CHANGELOG.md
CHANGED
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?
|
@@ -64,7 +53,9 @@ module DataUri
|
|
64
53
|
)*
|
65
54
|
)
|
66
55
|
)?
|
67
|
-
(
|
56
|
+
(?:;
|
57
|
+
(?<extension>base64|utf8)
|
58
|
+
)?
|
68
59
|
,
|
69
60
|
(?<data>.*)
|
70
61
|
\z
|
@@ -73,16 +64,21 @@ 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.
|
@@ -100,18 +96,25 @@ module DataUri
|
|
100
96
|
end
|
101
97
|
|
102
98
|
|
103
|
-
def self.valid?( str )
|
99
|
+
def self.valid?( str, utf8: false )
|
104
100
|
m = _parse( str )
|
105
101
|
if m
|
106
|
-
if m[:
|
102
|
+
if m[:extension] && m[:extension] == 'base64' ## assume base64
|
107
103
|
begin
|
108
104
|
Base64.strict_decode64(m[:data])
|
109
105
|
true
|
110
106
|
rescue ArgumentError
|
111
107
|
false
|
112
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?)
|
113
111
|
else
|
114
|
-
|
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
|
115
118
|
end
|
116
119
|
else
|
117
120
|
false
|
@@ -165,10 +168,12 @@ module DataUri
|
|
165
168
|
end
|
166
169
|
|
167
170
|
|
168
|
-
## base64 - force base64 encoding
|
169
|
-
|
170
|
-
|
171
|
-
|
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 )
|
175
|
+
str = "data:"
|
176
|
+
str += type if type ## note: allow optional / no type
|
172
177
|
|
173
178
|
## puts " type: #{type.inspect}, base64: #{base64.inspect}"
|
174
179
|
|
@@ -190,13 +195,17 @@ module DataUri
|
|
190
195
|
end
|
191
196
|
|
192
197
|
if base64
|
193
|
-
|
198
|
+
str += ";base64," + Base64.strict_encode64( data )
|
199
|
+
elsif utf8
|
200
|
+
str += ";utf8," + data
|
194
201
|
else
|
195
202
|
## use encode_uri_component by default - why? why not?
|
196
203
|
## space becomes %20
|
197
204
|
## : becomes %3A
|
198
205
|
## , becomes %2C and so on
|
199
|
-
|
206
|
+
##
|
207
|
+
## note: use uri: false to turn of uri encode!!!
|
208
|
+
str += "," + (uri == false ? data : encode_uri( data ))
|
200
209
|
end
|
201
210
|
end
|
202
211
|
|
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.1
|
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
|