esvg 4.0.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/esvg/helpers.rb +20 -8
- data/lib/esvg/svg.rb +62 -49
- data/lib/esvg/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1529e25e48e1086a9ac6756093c81e4e96192400
|
4
|
+
data.tar.gz: d36d81e3e2fec5665acf2320a658d41c1579a36d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 976ec5f1418b143edb27698ccfedc89eb48a8eda4bfa741725d9a73412bc9b7a55ce42b7dafee6b9e9636ff41d5c6a924624d2a2ded858c5e7f2fcbd8658f651
|
7
|
+
data.tar.gz: 8ca64f5d17cc615f18e3202e0b2e79a5abe5c58ab80fce208fa757ce72661c6b1a053256424772e9778dc57500fbdcd461f1849e1126aa908ba6f857382ea6e6
|
data/lib/esvg/helpers.rb
CHANGED
@@ -1,24 +1,36 @@
|
|
1
1
|
module Esvg::Helpers
|
2
2
|
|
3
|
+
def esvg
|
4
|
+
svgs = Esvg.svgs || Esvg.new()
|
5
|
+
|
6
|
+
svgs.read_files if Rails.env.development?
|
7
|
+
|
8
|
+
svgs
|
9
|
+
end
|
10
|
+
|
11
|
+
|
3
12
|
def embed_svgs(*keys)
|
4
13
|
if Rails.env.production?
|
5
|
-
|
14
|
+
esvg.build_paths(keys).each do |path|
|
6
15
|
javascript_include_tag(path)
|
7
16
|
end.join("\n")
|
8
17
|
else
|
9
|
-
|
18
|
+
esvg.embed_script(keys).html_safe
|
10
19
|
end
|
11
20
|
end
|
12
21
|
|
13
|
-
def use_svg(name, options={})
|
14
|
-
|
22
|
+
def use_svg(name, options={}, &block)
|
23
|
+
use_svg_with_files(esvg, name, options, &block)
|
15
24
|
end
|
16
25
|
|
17
|
-
|
18
|
-
svgs = Esvg.svgs || Esvg.new()
|
26
|
+
private
|
19
27
|
|
20
|
-
|
28
|
+
def use_svg_with_files(files, name, options, &block)
|
21
29
|
|
22
|
-
|
30
|
+
if block_given?
|
31
|
+
options[:content] = capture(&block)
|
32
|
+
end
|
33
|
+
|
34
|
+
files.use(name, options).html_safe
|
23
35
|
end
|
24
36
|
end
|
data/lib/esvg/svg.rb
CHANGED
@@ -133,7 +133,7 @@ module Esvg
|
|
133
133
|
|
134
134
|
svg_symbols[dir] = data.merge({
|
135
135
|
name: dir,
|
136
|
-
symbols:
|
136
|
+
symbols: symbols,
|
137
137
|
optimized: optimize?,
|
138
138
|
version: config[:version] || Digest::MD5.hexdigest(symbols),
|
139
139
|
asset: File.basename(dir).start_with?('_')
|
@@ -197,14 +197,14 @@ module Esvg
|
|
197
197
|
|
198
198
|
def process_file(file, mtime, name)
|
199
199
|
content = File.read(file)
|
200
|
-
|
200
|
+
id = id(name)
|
201
201
|
size_attr = dimensions(content)
|
202
202
|
|
203
203
|
svg = {
|
204
204
|
name: name,
|
205
|
-
use: %Q{<use xlink:href="##{
|
205
|
+
use: %Q{<use xlink:href="##{id}"/>},
|
206
206
|
last_modified: mtime,
|
207
|
-
attr: {
|
207
|
+
attr: { id: id }.merge(size_attr)
|
208
208
|
}
|
209
209
|
# Add attributes
|
210
210
|
svg[:content] = prep_svg(content, svg[:attr])
|
@@ -224,8 +224,8 @@ module Esvg
|
|
224
224
|
attr = {
|
225
225
|
fill: options[:fill],
|
226
226
|
style: options[:style],
|
227
|
-
|
228
|
-
|
227
|
+
viewBox: svg[:attr][:viewBox],
|
228
|
+
class: [config[:class], svg[:attr][:id], options[:class]].compact.join(' ')
|
229
229
|
}
|
230
230
|
|
231
231
|
# If user doesn't pass a size or set scale: true
|
@@ -241,7 +241,7 @@ module Esvg
|
|
241
241
|
attr[:height] = options[:height]
|
242
242
|
end
|
243
243
|
|
244
|
-
use = %Q{<svg #{attributes(attr)}>#{svg[:use]}#{title(options)}#{desc(options)}</svg>}
|
244
|
+
use = %Q{<svg #{attributes(attr)}>#{svg[:use]}#{title(options)}#{desc(options)}#{options[:content]||''}</svg>}
|
245
245
|
|
246
246
|
if Esvg.rails?
|
247
247
|
use.html_safe
|
@@ -261,13 +261,17 @@ module Esvg
|
|
261
261
|
|
262
262
|
def dimensions(input)
|
263
263
|
viewbox = input.scan(/<svg.+(viewBox=["'](.+?)["'])/).flatten.last
|
264
|
-
|
264
|
+
if viewbox
|
265
|
+
coords = viewbox.split(' ')
|
265
266
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
267
|
+
{
|
268
|
+
viewBox: viewbox,
|
269
|
+
width: coords[2].to_i - coords[0].to_i,
|
270
|
+
height: coords[3].to_i - coords[1].to_i
|
271
|
+
}
|
272
|
+
else
|
273
|
+
{}
|
274
|
+
end
|
271
275
|
end
|
272
276
|
|
273
277
|
def attributes(hash)
|
@@ -291,7 +295,8 @@ module Esvg
|
|
291
295
|
|
292
296
|
alias_method :exists?, :exist?
|
293
297
|
|
294
|
-
def
|
298
|
+
def id(name)
|
299
|
+
name = name_key(name)
|
295
300
|
if config[:namespace_before]
|
296
301
|
dasherize "#{config[:namespace]}-#{name}"
|
297
302
|
else
|
@@ -339,15 +344,13 @@ module Esvg
|
|
339
344
|
paths = []
|
340
345
|
|
341
346
|
files.each do |file|
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
paths << gz
|
350
|
-
end
|
347
|
+
write_file(file[:path], js(file[:name]))
|
348
|
+
puts "Writing #{file[:path]}" if config[:print]
|
349
|
+
paths << file[:path]
|
350
|
+
|
351
|
+
if !file[:asset] && gz = compress(file[:path])
|
352
|
+
puts "Writing #{gz}" if config[:print]
|
353
|
+
paths << gz
|
351
354
|
end
|
352
355
|
end
|
353
356
|
|
@@ -495,19 +498,22 @@ module Esvg
|
|
495
498
|
paths.flatten.map { |p| file_key(p) }
|
496
499
|
end
|
497
500
|
|
498
|
-
def
|
499
|
-
|
501
|
+
def name_key(key)
|
502
|
+
if key == '_' # Root level asset file
|
500
503
|
"_#{config[:filename]}".sub(/_+/, '_')
|
501
504
|
elsif key == '.' # Root level build file
|
502
505
|
config[:filename]
|
503
506
|
else
|
504
507
|
"#{key}"
|
505
508
|
end
|
509
|
+
end
|
506
510
|
|
507
|
-
|
508
|
-
|
511
|
+
def write_path(key)
|
512
|
+
name = name_key(key)
|
513
|
+
|
514
|
+
if name.start_with?('_') # Is it an asset?
|
509
515
|
File.join config[:assets], "#{name}.js"
|
510
|
-
else
|
516
|
+
else # or a build file?
|
511
517
|
File.join config[:build], "#{name}-#{version(key)}.js"
|
512
518
|
end
|
513
519
|
end
|
@@ -521,7 +527,14 @@ module Esvg
|
|
521
527
|
.gsub(/style="([^"]*?)fill:(.+?);/m, 'fill="\2" style="\1') # Make fill a property instead of a style
|
522
528
|
.gsub(/style="([^"]*?)fill-opacity:(.+?);/m, 'fill-opacity="\2" style="\1') # Move fill-opacity a property instead of a style
|
523
529
|
|
524
|
-
sub_def_ids
|
530
|
+
content = sub_def_ids content, attr[:id]
|
531
|
+
content = strip_attributes content
|
532
|
+
content = optimize(content) if optimize?
|
533
|
+
content = set_attributes content, attr
|
534
|
+
content.gsub(/<\/svg/,'</symbol') # Replace svgs with symbols
|
535
|
+
.gsub(/class="def-/,'id="def-') # Replace <def> classes with ids (classes are generated in sub_def_ids)
|
536
|
+
.gsub(/\s{2,}/,'') # Remove extra spaces
|
537
|
+
.gsub(/\w+=""/,'') # Remove empty attributes
|
525
538
|
end
|
526
539
|
|
527
540
|
# Scans <def> blocks for IDs
|
@@ -529,14 +542,14 @@ module Esvg
|
|
529
542
|
# Only replace IDs if urls exist to avoid replacing defs
|
530
543
|
# used in other svg files
|
531
544
|
#
|
532
|
-
def sub_def_ids(content,
|
545
|
+
def sub_def_ids(content, name)
|
533
546
|
return content unless !!content.match(/<defs>/)
|
534
547
|
|
535
548
|
content.scan(/<defs>.+<\/defs>/m).flatten.each do |defs|
|
536
549
|
defs.scan(/id="(.+?)"/).flatten.uniq.each_with_index do |id, index|
|
537
550
|
|
538
551
|
if content.match(/url\(##{id}\)/)
|
539
|
-
new_id = "
|
552
|
+
new_id = "def-#{name}-#{index}"
|
540
553
|
|
541
554
|
content = content.gsub(/id="#{id}"/, %Q{class="#{new_id}"})
|
542
555
|
.gsub(/url\(##{id}\)/, "url(##{new_id})" )
|
@@ -549,6 +562,17 @@ module Esvg
|
|
549
562
|
content
|
550
563
|
end
|
551
564
|
|
565
|
+
def strip_attributes(svg)
|
566
|
+
reg = %w(xmlns xmlns:xlink xml:space version).map { |m| "#{m}=\".+?\"" }.join('|')
|
567
|
+
|
568
|
+
svg.gsub(Regexp.new(reg), '')
|
569
|
+
end
|
570
|
+
|
571
|
+
def set_attributes(svg, attr)
|
572
|
+
attr.keys.each { |key| svg.sub!(/ #{key}=".+?"/,'') }
|
573
|
+
svg.sub(/<svg/, "<symbol #{attributes(attr)}")
|
574
|
+
end
|
575
|
+
|
552
576
|
def optimize?
|
553
577
|
!!(config[:optimize] && svgo_cmd)
|
554
578
|
end
|
@@ -558,24 +582,13 @@ module Esvg
|
|
558
582
|
end
|
559
583
|
|
560
584
|
|
561
|
-
def optimize(svg
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
FileUtils.rm(path) if File.exist? path
|
567
|
-
end
|
568
|
-
|
569
|
-
id_symbols(svg, attributes)
|
570
|
-
end
|
585
|
+
def optimize(svg)
|
586
|
+
path = write_tmp '.svgo-tmp', svg
|
587
|
+
command = "#{svgo_cmd} --disable=removeUselessDefs '#{path}' -o -"
|
588
|
+
svg = `#{command}`
|
589
|
+
FileUtils.rm(path) if File.exist? path
|
571
590
|
|
572
|
-
|
573
|
-
svg.gsub(/<svg.+?>/).with_index do |match, index|
|
574
|
-
%Q{<symbol #{attributes(attr[index])}>} # Remove clutter from svg declaration
|
575
|
-
end
|
576
|
-
.gsub(/<\/svg/,'</symbol') # Replace svgs with symbols
|
577
|
-
.gsub(/class=/,'id=') # Replace classes with ids (classes are generated here)
|
578
|
-
.gsub(/\w+=""/,'') # Remove empty attributes
|
591
|
+
svg
|
579
592
|
end
|
580
593
|
|
581
594
|
def compress(file)
|
@@ -627,7 +640,7 @@ module Esvg
|
|
627
640
|
|
628
641
|
def key_id(keys)
|
629
642
|
keys.map do |key|
|
630
|
-
(key == '.') ? 'symbols' :
|
643
|
+
(key == '.') ? 'symbols' : id(key)
|
631
644
|
end.join('-')
|
632
645
|
end
|
633
646
|
|
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.
|
4
|
+
version: 4.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Mathis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05-
|
11
|
+
date: 2017-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|