esvg 3.1.0 → 3.2.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/esvg/svg.rb +48 -8
  3. data/lib/esvg/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 01212e0dd22ac81b0551cbd8c036adc6dae5649f
4
- data.tar.gz: 32dad04ed7e7252fdab0e0b4895cf03e994e5aea
3
+ metadata.gz: 5df426e77287c9878d1d1e4015cb0e53b587bb5a
4
+ data.tar.gz: 37c2c9e0f77f3898afdef009a59fecaae870fbec
5
5
  SHA512:
6
- metadata.gz: f45c6bacd67ebc6e96b2f3902799e4c9695a744e049af01ed481769631d7f050dbba371d89f02c9deb0a3fa35638dadef4d72683fde895d8acac5ba0ebaf92ef
7
- data.tar.gz: 56084715013ffea7f4ef6c28d99358fc3d4e04b1441a466ff50c65162db351e510aae34d307bf986db9dfd978069198bfd3d33dd11cdf1eedc6c10970fc298e6
6
+ metadata.gz: dbcb61a40159c48fef3e224b3a83723ef243d224acb6eff08b2c42032c66471b214e3d16ef6399abeb80d3c13c4f86ed5e4b6766cefa21fc4ee5d0026c5f7b32
7
+ data.tar.gz: 3305442d217788371c21e7c9f069522257c1d1f38dd85bef58ea996904a3197201ac5ab086b2c306a4db733ad092df56b107d22620bdbdfd72558a970bb77d06
@@ -150,7 +150,16 @@ module Esvg
150
150
  (svg_cache.keys - files.keys.map {|file| file_key(file) }).each do |f|
151
151
  svg_cache.delete(f)
152
152
  end
153
+ end
154
+
155
+ def build_paths
156
+ build_keys = svgs.keys.reject do |key|
157
+ key == '.' ||
158
+ key.match(/^_/) ||
159
+ config[:flatten].include?(key)
160
+ end
153
161
 
162
+ build_keys.map { | key | File.expand_path(build_path(key)) }
154
163
  end
155
164
 
156
165
  def flatten_path(path)
@@ -354,21 +363,50 @@ module Esvg
354
363
  end
355
364
  end
356
365
 
366
+ def build_path(key)
367
+ dir = config[:js_build_dir] || File.dirname(config[:js_path])
368
+
369
+ if config[:js_build_version]
370
+ key = "#{key}-#{config[:js_build_version]}"
371
+ end
372
+
373
+ File.join(dir, key+'.js')
374
+ end
375
+
357
376
  def write_path(path, key)
358
- # Write esvg-core.js
377
+ # Write root svgs
359
378
  return config[path] if key == "."
360
379
 
361
380
  if !key.start_with?('_') && path.to_s.start_with?('js')
362
- if config[:js_build_version]
363
- key = "#{key}-#{config[:js_build_version]}"
364
- end
381
+ build_path(key)
382
+ else
383
+ config[path].sub(/[^\/]+?\./, key+'.')
384
+ end
385
+ end
386
+
387
+ # Scans <def> blocks for IDs
388
+ # If urls(#id) are used, ensure these IDs are unique to this file
389
+ # Only replace IDs if urls exist to avoid replacing defs
390
+ # used in other svg files
391
+ #
392
+ def sub_def_ids(file, content)
393
+ return content unless !!content.match(/<defs>/)
394
+
395
+ content.scan(/<defs>.+<\/defs>/m).flatten.each do |defs|
396
+ defs.scan(/id="(.+?)"/).flatten.uniq.each_with_index do |id, index|
365
397
 
366
- if config[:js_build_dir]
367
- return File.join(config[:js_build_dir], key+'.js')
398
+ if content.match(/url\(##{id}\)/)
399
+ new_id = "#{classname(file)}-ref#{index}"
400
+
401
+ content = content.gsub(/id="#{id}"/, %Q{class="#{new_id}"})
402
+ .gsub(/url\(##{id}\)/, "url(##{new_id})" )
403
+ else
404
+ content = content.gsub(/id="#{id}"/, %Q{class="#{id}"})
405
+ end
368
406
  end
369
407
  end
370
408
 
371
- config[path].sub(/[^\/]+?\./, key+'.')
409
+ content
372
410
  end
373
411
 
374
412
  def prep_svg(file, content)
@@ -379,12 +417,14 @@ module Esvg
379
417
  .gsub(/\s?fill="(#0{3,6}|black|rgba?\(0,0,0\))"/,'') # Strip black fill
380
418
  .gsub(/style="([^"]*?)fill:(.+?);/m, 'fill="\2" style="\1') # Make fill a property instead of a style
381
419
  .gsub(/style="([^"]*?)fill-opacity:(.+?);/m, 'fill-opacity="\2" style="\1') # Move fill-opacity a property instead of a style
420
+
421
+ sub_def_ids(file, content)
382
422
  end
383
423
 
384
424
  def optimize(svg)
385
425
  if config[:optimize] && svgo_path = find_node_module('svgo')
386
426
  path = write_svg(svg)
387
- svg = `#{svgo_path} '#{path}' -o -`
427
+ svg = `#{svgo_path} --disable=removeUselessDefs '#{path}' -o -`
388
428
  FileUtils.rm(path)
389
429
  end
390
430
 
@@ -1,3 +1,3 @@
1
1
  module Esvg
2
- VERSION = "3.1.0"
2
+ VERSION = "3.2.0"
3
3
  end
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: 3.1.0
4
+ version: 3.2.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-03 00:00:00.000000000 Z
11
+ date: 2017-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler