esvg 4.6.1 → 4.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/esvg.rb +19 -4
- data/lib/esvg/svg.rb +16 -12
- data/lib/esvg/svgs.rb +44 -27
- data/lib/esvg/symbol.rb +20 -16
- data/lib/esvg/version.rb +1 -1
- 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: 41bb830e6af4939063aeca838f5c69c550a851f71e701599ed3d4cbb52a42b5c
|
4
|
+
data.tar.gz: f494f42e0e9f9f847145a8f9d65658a9bf3104741f4a4bf513866cb6d92f942b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bbce86545a336224d47db5219352407210fa16b16efc85c6f984058ee3ba09ea559a24122488c03e02bef076f141895107a70a2ce5f5868898f73c3dd708037e
|
7
|
+
data.tar.gz: e62b4ad90f511af5671df2d0c39c9c54e9506b25f580da403f84015cefb77f4fc843d2def09b42382bb52bb23991f70beaca2c19402883af4ac233c6aaddab14
|
data/lib/esvg.rb
CHANGED
@@ -79,7 +79,9 @@ module Esvg
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def embed(names=nil)
|
82
|
-
html_safe find_svgs(names).map{|s|
|
82
|
+
html_safe( find_svgs(names).map{ |s|
|
83
|
+
s.embed_script(names)
|
84
|
+
}.join )
|
83
85
|
end
|
84
86
|
|
85
87
|
def build_paths(names=nil)
|
@@ -108,13 +110,18 @@ module Esvg
|
|
108
110
|
defined?(Rails) || File.exist?("./bin/rails")
|
109
111
|
end
|
110
112
|
|
113
|
+
def rails_production?
|
114
|
+
rails? && Rails.env.production?
|
115
|
+
end
|
116
|
+
|
111
117
|
def html_safe(input)
|
112
118
|
input = input.html_safe if rails?
|
113
119
|
input
|
114
120
|
end
|
115
121
|
|
122
|
+
# Add SVG build task to `rake assets:precompile`
|
116
123
|
def precompile_assets
|
117
|
-
if
|
124
|
+
if rails_production? && defined?(Rake)
|
118
125
|
::Rake::Task['assets:precompile'].enhance do
|
119
126
|
new(gzip: true, print: true).build
|
120
127
|
end
|
@@ -161,14 +168,17 @@ module Esvg
|
|
161
168
|
|
162
169
|
config = CONFIG.dup
|
163
170
|
|
164
|
-
if
|
171
|
+
if rails? || options[:rails]
|
165
172
|
config.merge!(CONFIG_RAILS)
|
166
173
|
elsif defined?(Jekyll)
|
167
174
|
config.merge!(CONFIG_JEKYLL)
|
168
175
|
end
|
169
176
|
|
170
177
|
if path = paths.select{ |p| File.exist?(p)}.first
|
178
|
+
options[:config_file] = path
|
171
179
|
config.merge!(deep_symbolize_keys(YAML.load(File.read(path) || {})))
|
180
|
+
else
|
181
|
+
options[:config_file] = false
|
172
182
|
end
|
173
183
|
|
174
184
|
config.merge!(options)
|
@@ -189,10 +199,15 @@ module Esvg
|
|
189
199
|
config[:temp] = File.expand_path File.join(config[:temp], '.esvg-cache')
|
190
200
|
|
191
201
|
config[:aliases] = load_aliases(config[:alias])
|
192
|
-
config[:
|
202
|
+
config[:flatten_dir] = [config[:flatten]].flatten.map { |dir| File.join(dir, '/') }.join('|')
|
203
|
+
config[:read] = Time.now.to_i
|
193
204
|
|
194
205
|
config
|
195
206
|
end
|
207
|
+
|
208
|
+
def update_config ( options )
|
209
|
+
config( { config_file: options[:config_file] } )
|
210
|
+
end
|
196
211
|
|
197
212
|
# Load aliases from configuration.
|
198
213
|
# returns a hash of aliasees mapped to a name.
|
data/lib/esvg/svg.rb
CHANGED
@@ -7,13 +7,13 @@ module Esvg
|
|
7
7
|
|
8
8
|
attr_reader :asset, :vesion, :name
|
9
9
|
|
10
|
-
def initialize(name, symbols,
|
10
|
+
def initialize(name, symbols, parent)
|
11
|
+
@parent = parent
|
11
12
|
@name = name
|
12
|
-
@config = config
|
13
13
|
@symbols = symbols
|
14
14
|
@symbol_defs = []
|
15
15
|
@asset = File.basename(name).start_with?('_')
|
16
|
-
@version =
|
16
|
+
@version = parent.config[:version] || Digest::MD5.hexdigest(symbols.map(&:mtime).join)
|
17
17
|
end
|
18
18
|
|
19
19
|
def embed
|
@@ -22,6 +22,10 @@ module Esvg
|
|
22
22
|
}}
|
23
23
|
end
|
24
24
|
|
25
|
+
def config
|
26
|
+
@parent.config
|
27
|
+
end
|
28
|
+
|
25
29
|
def named?(names=[])
|
26
30
|
[names].flatten.map {
|
27
31
|
|n| dasherize(n)
|
@@ -32,7 +36,7 @@ module Esvg
|
|
32
36
|
if @name == '.'
|
33
37
|
'symbols'
|
34
38
|
else
|
35
|
-
dasherize "#{
|
39
|
+
dasherize "#{config[:prefix]}-#{name_key}"
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
@@ -41,14 +45,14 @@ module Esvg
|
|
41
45
|
name = name_key
|
42
46
|
|
43
47
|
if name.start_with?('_') # Is it an asset?
|
44
|
-
File.join
|
48
|
+
File.join config[:assets], "#{name}.js"
|
45
49
|
else # or a build file?
|
46
50
|
|
47
51
|
# User doesn't want a fingerprinted build file and hasn't set a version
|
48
|
-
if
|
49
|
-
File.join
|
52
|
+
if !config[:fingerprint] && !config[:version]
|
53
|
+
File.join config[:build], "#{name}.js"
|
50
54
|
else
|
51
|
-
File.join
|
55
|
+
File.join config[:build], "#{name}-#{@version}.js"
|
52
56
|
end
|
53
57
|
end
|
54
58
|
end
|
@@ -58,9 +62,9 @@ module Esvg
|
|
58
62
|
|
59
63
|
def name_key
|
60
64
|
if @name == '_' # Root level asset file
|
61
|
-
"_#{
|
65
|
+
"_#{config[:filename]}".sub(/_+/, '_')
|
62
66
|
elsif @name == '.' # Root level build file
|
63
|
-
|
67
|
+
config[:filename]
|
64
68
|
else
|
65
69
|
@name
|
66
70
|
end
|
@@ -71,8 +75,8 @@ module Esvg
|
|
71
75
|
@svg
|
72
76
|
else
|
73
77
|
attr = {
|
74
|
-
"data-symbol-class" =>
|
75
|
-
"data-prefix" =>
|
78
|
+
"data-symbol-class" => config[:class],
|
79
|
+
"data-prefix" => config[:prefix],
|
76
80
|
"version" => "1.1",
|
77
81
|
"style" => "height:0;position:absolute"
|
78
82
|
}
|
data/lib/esvg/svgs.rb
CHANGED
@@ -9,25 +9,42 @@ module Esvg
|
|
9
9
|
include Esvg::Utils
|
10
10
|
|
11
11
|
attr_reader :symbols
|
12
|
-
attr_reader :config
|
13
12
|
|
14
13
|
def initialize(options={})
|
15
14
|
@config = options
|
16
15
|
@symbols = []
|
17
16
|
@svgs = []
|
18
|
-
@
|
17
|
+
@files_loaded = 0
|
19
18
|
read_cache
|
20
19
|
|
21
20
|
load_files
|
22
21
|
end
|
23
22
|
|
23
|
+
def config
|
24
|
+
# use cached configuration
|
25
|
+
if !Esvg.rails_production? && config_expired? && config_changed?
|
26
|
+
puts "LOADING CONFIG"
|
27
|
+
@config = Esvg.update_config( @config )
|
28
|
+
else
|
29
|
+
@config
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def config_expired?
|
34
|
+
@config[:throttle_read] < (Time.now.to_i - @config[:read])
|
35
|
+
end
|
36
|
+
|
37
|
+
def config_changed?
|
38
|
+
@config[:config_file] && @config[:read] < File.mtime( @config[:config_file] ).to_i
|
39
|
+
end
|
40
|
+
|
24
41
|
def load_files
|
25
|
-
return if (Time.now.to_i - @
|
42
|
+
return if @config[:throttle_read] < (Time.now.to_i - @files_loaded)
|
26
43
|
|
27
|
-
files = Dir[File.join(config[:source], '**/*.svg')].uniq.sort
|
44
|
+
files = Dir[File.join(@config[:source], '**/*.svg')].uniq.sort
|
28
45
|
|
29
|
-
if files.empty? && config[:print]
|
30
|
-
puts "No svgs found at #{config[:source]}"
|
46
|
+
if files.empty? && @config[:print]
|
47
|
+
puts "No svgs found at #{@config[:source]}"
|
31
48
|
return
|
32
49
|
end
|
33
50
|
|
@@ -36,19 +53,19 @@ module Esvg
|
|
36
53
|
|
37
54
|
files.each do |path|
|
38
55
|
unless @symbols.find { |s| s.path == path }
|
39
|
-
@symbols << Symbol.new(path,
|
56
|
+
@symbols << Symbol.new(path, self)
|
40
57
|
end
|
41
58
|
end
|
42
59
|
|
43
60
|
@svgs.clear
|
44
61
|
|
45
62
|
sort(@symbols.group_by(&:group)).each do |name, symbols|
|
46
|
-
@svgs << Svg.new(name, symbols,
|
63
|
+
@svgs << Svg.new(name, symbols, self)
|
47
64
|
end
|
48
65
|
|
49
|
-
@
|
66
|
+
@files_loaded = Time.now.to_i
|
50
67
|
|
51
|
-
puts "Read #{@symbols.size} files from #{config[:source]}" if config[:print]
|
68
|
+
puts "Read #{@symbols.size} files from #{@config[:source]}" if @config[:print]
|
52
69
|
|
53
70
|
end
|
54
71
|
|
@@ -56,8 +73,8 @@ module Esvg
|
|
56
73
|
|
57
74
|
paths = []
|
58
75
|
|
59
|
-
if config[:core]
|
60
|
-
path = File.join config[:assets], "_esvg.js"
|
76
|
+
if @config[:core]
|
77
|
+
path = File.join @config[:assets], "_esvg.js"
|
61
78
|
write_file path, js_core
|
62
79
|
paths << path
|
63
80
|
end
|
@@ -65,11 +82,11 @@ module Esvg
|
|
65
82
|
@svgs.each do |file|
|
66
83
|
write_file file.path, js(file.embed)
|
67
84
|
|
68
|
-
puts "Writing #{file.path}" if config[:print]
|
85
|
+
puts "Writing #{file.path}" if @config[:print]
|
69
86
|
paths << file.path
|
70
87
|
|
71
|
-
if !file.asset && config[:gzip] && gz = compress(file.path)
|
72
|
-
puts "Writing #{gz}" if config[:print]
|
88
|
+
if !file.asset && @config[:gzip] && gz = compress(file.path)
|
89
|
+
puts "Writing #{gz}" if @config[:print]
|
73
90
|
paths << gz
|
74
91
|
end
|
75
92
|
end
|
@@ -79,20 +96,20 @@ module Esvg
|
|
79
96
|
end
|
80
97
|
|
81
98
|
def write_cache
|
82
|
-
puts "Writing cache" if config[:print]
|
83
|
-
write_tmp config[:cache_file], @symbols.map(&:data).to_yaml
|
99
|
+
puts "Writing cache" if @config[:print]
|
100
|
+
write_tmp @config[:cache_file], @symbols.map(&:data).to_yaml
|
84
101
|
end
|
85
102
|
|
86
103
|
def read_cache
|
87
|
-
(YAML.load(read_tmp config[:cache_file]) || []).each do |c|
|
88
|
-
config[:cache] = c
|
89
|
-
@symbols << Symbol.new(c[:path],
|
104
|
+
(YAML.load(read_tmp @config[:cache_file]) || []).each do |c|
|
105
|
+
@config[:cache] = c
|
106
|
+
@symbols << Symbol.new(c[:path], self)
|
90
107
|
end
|
91
108
|
end
|
92
109
|
|
93
110
|
# Embed svg symbols
|
94
111
|
def embed_script(names=nil)
|
95
|
-
if Esvg.
|
112
|
+
if Esvg.rails_production?
|
96
113
|
embeds = buildable_svgs(names).map(&:embed)
|
97
114
|
else
|
98
115
|
embeds = find_svgs(names).map(&:embed)
|
@@ -107,7 +124,7 @@ module Esvg
|
|
107
124
|
end
|
108
125
|
|
109
126
|
def cache_stale?
|
110
|
-
path = File.join(config[:temp], config[:cache_file])
|
127
|
+
path = File.join(@config[:temp], @config[:cache_file])
|
111
128
|
|
112
129
|
# No cache file exists or cache file is older than a new symbol
|
113
130
|
!File.exist?(path) || @symbols.size > 0 && File.mtime(path).to_i < @symbols.map(&:mtime).sort.last
|
@@ -119,7 +136,7 @@ module Esvg
|
|
119
136
|
|
120
137
|
def find_symbol(name, fallback=nil)
|
121
138
|
# Ensure that file changes are picked up in development
|
122
|
-
load_files unless Esvg.
|
139
|
+
load_files unless Esvg.rails_production?
|
123
140
|
|
124
141
|
name = get_alias dasherize(name)
|
125
142
|
|
@@ -177,7 +194,7 @@ module Esvg
|
|
177
194
|
}
|
178
195
|
|
179
196
|
function svgName( name ) {
|
180
|
-
return "#{config[:prefix]}-"+dasherize( name )
|
197
|
+
return "#{@config[:prefix]}-"+dasherize( name )
|
181
198
|
}
|
182
199
|
|
183
200
|
function use( name, options ) {
|
@@ -239,18 +256,18 @@ module Esvg
|
|
239
256
|
end
|
240
257
|
|
241
258
|
def get_alias(name)
|
242
|
-
config[:aliases][dasherize(name).to_sym] || name
|
259
|
+
@config[:aliases][dasherize(name).to_sym] || name
|
243
260
|
end
|
244
261
|
|
245
262
|
def write_tmp(name, content)
|
246
|
-
path = File.join(config[:temp], name)
|
263
|
+
path = File.join(@config[:temp], name)
|
247
264
|
FileUtils.mkdir_p(File.dirname(path))
|
248
265
|
write_file path, content
|
249
266
|
path
|
250
267
|
end
|
251
268
|
|
252
269
|
def read_tmp(name)
|
253
|
-
path = File.join(config[:temp], name)
|
270
|
+
path = File.join(@config[:temp], name)
|
254
271
|
if File.exist? path
|
255
272
|
File.read path
|
256
273
|
else
|
data/lib/esvg/symbol.rb
CHANGED
@@ -6,14 +6,18 @@ module Esvg
|
|
6
6
|
|
7
7
|
include Esvg::Utils
|
8
8
|
|
9
|
-
def initialize(path,
|
10
|
-
@
|
9
|
+
def initialize(path, parent)
|
10
|
+
@parent = parent
|
11
11
|
@path = path
|
12
12
|
@last_checked = 0
|
13
13
|
load_data
|
14
14
|
read
|
15
15
|
end
|
16
16
|
|
17
|
+
def config
|
18
|
+
@parent.config
|
19
|
+
end
|
20
|
+
|
17
21
|
def read
|
18
22
|
return if !File.exist?(@path)
|
19
23
|
|
@@ -102,12 +106,12 @@ module Esvg
|
|
102
106
|
def use(options={})
|
103
107
|
|
104
108
|
# If preset key is set, merge presets from configuration
|
105
|
-
if options[:preset] && preset =
|
109
|
+
if options[:preset] && preset = config[:presets][ options.delete(:preset).to_sym ]
|
106
110
|
options = options.merge( preset )
|
107
111
|
end
|
108
112
|
|
109
113
|
# If size key is set, merge size class from configuration
|
110
|
-
if options[:size] && size_class =
|
114
|
+
if options[:size] && size_class = config[:sizes][ options.delete(:size).to_sym ]
|
111
115
|
options = options.merge( size_class )
|
112
116
|
end
|
113
117
|
|
@@ -124,7 +128,7 @@ module Esvg
|
|
124
128
|
use_attr = options.delete(:use) || {}
|
125
129
|
|
126
130
|
svg_attr = {
|
127
|
-
class: [
|
131
|
+
class: [config[:class], config[:prefix]+"-"+@name, options.delete(:class)].compact.join(' '),
|
128
132
|
viewBox: @size[:viewBox],
|
129
133
|
role: 'img'
|
130
134
|
}.merge(options)
|
@@ -143,7 +147,7 @@ module Esvg
|
|
143
147
|
def use_tag(options={})
|
144
148
|
options["xlink:href"] = "##{@id}"
|
145
149
|
|
146
|
-
if options[:scale] &&
|
150
|
+
if options[:scale] && config[:scale]
|
147
151
|
# User doesn't want dimensions to be set
|
148
152
|
options.delete(:scale)
|
149
153
|
else
|
@@ -157,7 +161,7 @@ module Esvg
|
|
157
161
|
end
|
158
162
|
|
159
163
|
def svgo?
|
160
|
-
|
164
|
+
config[:optimize] && !!Esvg.node_module('svgo')
|
161
165
|
end
|
162
166
|
|
163
167
|
def optimize
|
@@ -192,15 +196,15 @@ module Esvg
|
|
192
196
|
private
|
193
197
|
|
194
198
|
def load_data
|
195
|
-
if
|
196
|
-
|
199
|
+
if config[:cache]
|
200
|
+
config.delete(:cache).each do |name, value|
|
197
201
|
instance_variable_set("@#{name}", value)
|
198
202
|
end
|
199
203
|
end
|
200
204
|
end
|
201
205
|
|
202
206
|
def last_modified
|
203
|
-
if Time.now.to_i - @last_checked <
|
207
|
+
if Time.now.to_i - @last_checked < config[:throttle_read]
|
204
208
|
@last_modified
|
205
209
|
else
|
206
210
|
@last_checked = Time.now.to_i
|
@@ -209,11 +213,11 @@ module Esvg
|
|
209
213
|
end
|
210
214
|
|
211
215
|
def file_id(name)
|
212
|
-
dasherize "#{
|
216
|
+
dasherize "#{config[:prefix]}-#{name}"
|
213
217
|
end
|
214
218
|
|
215
219
|
def local_path
|
216
|
-
@local_path ||= sub_path(
|
220
|
+
@local_path ||= sub_path(config[:source], @path)
|
217
221
|
end
|
218
222
|
|
219
223
|
def file_name
|
@@ -230,7 +234,7 @@ module Esvg
|
|
230
234
|
# Flattened paths which should be treated as assets will use '_' as their dir key
|
231
235
|
# - flatten: _foo - _foo/icon.svg will have a dirkey of _
|
232
236
|
# - filename: _icons - treats all root or flattened files as assets
|
233
|
-
if dir == '.' && ( local_path.start_with?('_') ||
|
237
|
+
if dir == '.' && ( local_path.start_with?('_') || config[:filename].start_with?('_') )
|
234
238
|
'_'
|
235
239
|
else
|
236
240
|
dir
|
@@ -238,14 +242,14 @@ module Esvg
|
|
238
242
|
end
|
239
243
|
|
240
244
|
def flatten_path
|
241
|
-
@flattened_path ||= local_path.sub(Regexp.new(
|
245
|
+
@flattened_path ||= local_path.sub(Regexp.new(config[:flatten_dir]), '')
|
242
246
|
end
|
243
247
|
|
244
248
|
def name_key(key)
|
245
249
|
if key == '_' # Root level asset file
|
246
|
-
"_#{
|
250
|
+
"_#{config[:filename]}".sub(/_+/, '_')
|
247
251
|
elsif key == '.' # Root level build file
|
248
|
-
|
252
|
+
config[:filename]
|
249
253
|
else
|
250
254
|
"#{key}"
|
251
255
|
end
|
data/lib/esvg/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: esvg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.6.
|
4
|
+
version: 4.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Mathis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-12-
|
11
|
+
date: 2018-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|