datauris 1.0.1 → 1.1.0

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: 7709f8cf824f237c4aed38259191ecb6fd4cf39d3b29664a93ea1ee16902f492
4
+ data.tar.gz: 8961a0856b232df9c7a5995db91e4f4ae08c072e7d4d655d51ae0be6b8226009
5
5
  SHA512:
6
- metadata.gz: b60dd18069dee8bcb18b6fbb834964f4a55423fe595c8a8b3cad3f50219c39283920f2b14a4dedf72870eddc0575b555729e5763b3369e43aac809fb61b26ef0
7
- data.tar.gz: 6872aa3cc3aeffeaebf0632e6dbc287dec2e17d7d3151bf4fd52ec22a9b93580db2ed61821145fc61b2522345ddbc6828e27279abf523990bc72c58190339921
6
+ metadata.gz: 85584d023850013b98801c46775c5a43ea76d08eedef815457921684063081a0786517919190c1b9c0b2fbd2d9923773739c0f029338d78d44fcfe88513b9459
7
+ data.tar.gz: e4a6796902329a3efb962f3733012bbce8b9dd2baf2f315f92b033e41c627630a4ef5c0981368c84526623ed9226bd573e7f079cae6b03c0beb3f0382cdc98d8
data/CHANGELOG.md CHANGED
@@ -1,4 +1,4 @@
1
- ### 1.0.1
1
+ ### 1.1.0
2
2
  ### 0.0.1 / 2023-11-23
3
3
 
4
4
  * Everything is new. First release
@@ -1,8 +1,8 @@
1
1
 
2
2
  module DataUri
3
3
  MAJOR = 1 ## todo: namespace inside version or something - why? why not??
4
- MINOR = 0
5
- PATCH = 1
4
+ MINOR = 1
5
+ PATCH = 0
6
6
  VERSION = [MAJOR,MINOR,PATCH].join('.')
7
7
 
8
8
  def self.version
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,8 +168,10 @@ 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 )
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 )
170
175
  uri = "data:"
171
176
  uri += type if type ## note: allow optional / no type
172
177
 
@@ -191,12 +196,16 @@ module DataUri
191
196
 
192
197
  if base64
193
198
  uri += ";base64," + Base64.strict_encode64( data )
199
+ elsif utf8
200
+ uri += ";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
+ uri += "," + (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.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-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