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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1a65cebd38591b1ea9416cbb69498707019dd6e422c0aacc72b404f546270da
4
- data.tar.gz: 559e1dbe5a26c70a872ca242b0e4c93a06b8883a0dbe080a3cd38f560019390f
3
+ metadata.gz: 89ab6159a9beb6750504017159f5d0e0391c78ea965aaea0d705531299bae7b1
4
+ data.tar.gz: 87bc327d253eb5464e47dbb2ef2d4770da2caacda008aac0ae8c8ad0b8f74483
5
5
  SHA512:
6
- metadata.gz: b60dd18069dee8bcb18b6fbb834964f4a55423fe595c8a8b3cad3f50219c39283920f2b14a4dedf72870eddc0575b555729e5763b3369e43aac809fb61b26ef0
7
- data.tar.gz: 6872aa3cc3aeffeaebf0632e6dbc287dec2e17d7d3151bf4fd52ec22a9b93580db2ed61821145fc61b2522345ddbc6828e27279abf523990bc72c58190339921
6
+ metadata.gz: bd984682f79f95f7170e023a4f1aa8a048339010e71ee984958c528a7c5bf7a8d6102d90398e636bf27c553cd88f38bae4417708ec6367e7ee713fe84b96620f
7
+ data.tar.gz: 0ebfd59f4ca9aa3ee881c5a5eab954d4ecd3691594418fc35b1709468d1012ede78139e180b47a700c25f796d8f2e827a1c76734b6aefb236f27e79fc5c0cf8d
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ### 1.0.1
1
+ ### 1.1.1
2
2
  ### 0.0.1 / 2023-11-23
3
3
 
4
4
  * Everything is new. First release
@@ -1,7 +1,7 @@
1
1
 
2
2
  module DataUri
3
3
  MAJOR = 1 ## todo: namespace inside version or something - why? why not??
4
- MINOR = 0
4
+ MINOR = 1
5
5
  PATCH = 1
6
6
  VERSION = [MAJOR,MINOR,PATCH].join('.')
7
7
 
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
- (?<base64extension>;base64)?
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[:base64extension] ## assume base64 encoded
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[:base64extension] ## assume base64
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
- true
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 (instead of "automagic")
169
- def self.build( data, type=nil, base64: nil )
170
- uri = "data:"
171
- uri += type if type ## note: allow optional / no type
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
- uri += ";base64," + Base64.strict_encode64( data )
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
- uri += "," + encode_uri( data )
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.0.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-25 00:00:00.000000000 Z
11
+ date: 2023-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rdoc