esvg 4.3.9 → 4.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4c725ea2fad5a3ce5df37270bb5d49d85d58e012
4
- data.tar.gz: 9a1311c39d423b886216d966e45521f1ac2bb592
3
+ metadata.gz: 80613ecb52693e3f1540e8d538460138a96b0271
4
+ data.tar.gz: a6c0fe63c64bd8f7ea4dedddefbf6ec47f07d544
5
5
  SHA512:
6
- metadata.gz: 248cd982eb3cbb1b16b68532be97fb62b6ec192a5ad69b854bfba5b40082f86682fcf59758616263782f3f8493698643d00ec4addba57f3630b5edf553de15ca
7
- data.tar.gz: 593cc46f94f68f770019ba967f4d590c3b670054f5e1d8f7bae972592747d10fb74401a115ad6edcc96e4750d58ce530134b42d3d5244d2298f2176bfe49cb74
6
+ metadata.gz: 13ce53f86132b512ab4fff84afcc87996c1ea086053d6e1a3e4c48e5431de76e4c1e0e0ff9a3fec5fe457398533329ce156e4ab5e1bfe9e63f1852dce063c108
7
+ data.tar.gz: d49a75c05c53f09184d49db85f9e25a66b5352d69818f77bb5e7320338fddfea7b8c3ccab3e9340f877e8540ef01b172aa97268e78ba6080216e6e5b37d5da98
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ### 4.4.0 (2018-08-06)
4
+ - New: Jekyll support. Just add esvg to the jekyll_plugins group in Gemfile and use {% esvg %} and {% use_svg %} as you'd expect.
5
+
3
6
  ### 4.3.9 (2017-09-05)
4
7
  - Fix: Esvg javascript now properly appends prefixes when generating use tags.
5
8
 
@@ -10,12 +10,49 @@ if defined?(Rails)
10
10
  require "esvg/railties"
11
11
  end
12
12
 
13
+ if defined?(Jekyll)
14
+ require "esvg/jekyll_hooks"
15
+ end
16
+
17
+ CONFIG = {
18
+ filename: 'svgs',
19
+ class: 'svg-symbol',
20
+ prefix: 'svg',
21
+ cache_file: '.symbols',
22
+ core: true,
23
+ optimize: false,
24
+ gzip: false,
25
+ scale: false,
26
+ fingerprint: true,
27
+ throttle_read: 4,
28
+ flatten: [],
29
+ alias: {}
30
+ }
31
+
32
+ CONFIG_RAILS = {
33
+ source: "app/assets/svgs",
34
+ assets: "app/assets/javascripts",
35
+ build: "public/javascripts",
36
+ temp: "tmp"
37
+ }
38
+
39
+ CONFIG_JEKYLL = {
40
+ source: "_svgs",
41
+ build: "_site/javascripts",
42
+ core: false
43
+ }
44
+
13
45
  module Esvg
46
+
14
47
  extend self
15
48
 
49
+ include Esvg::Utils
16
50
  def new(options={})
17
51
  @svgs ||=[]
18
- @svgs << Svgs.new(options)
52
+ c = config(options)
53
+ unless @svgs.find { |s| s.config[:source] == c[:source] }
54
+ @svgs << Svgs.new(c)
55
+ end
19
56
  @svgs.last
20
57
  end
21
58
 
@@ -44,10 +81,11 @@ module Esvg
44
81
  end
45
82
 
46
83
  def seed_cache(options)
47
- svgs = Svgs.new(options)
84
+ svgs = new(options)
48
85
  puts "Optimizing SVGs" if options[:print]
49
86
  svgs.symbols.map(&:optimize)
50
87
  svgs.write_cache
88
+ svgs
51
89
  end
52
90
 
53
91
  def find_svgs(names=nil)
@@ -72,7 +110,7 @@ module Esvg
72
110
  def precompile_assets
73
111
  if defined?(Rails) && Rails.env.production? && defined?(Rake)
74
112
  ::Rake::Task['assets:precompile'].enhance do
75
- Svgs.new(gzip: true, print: true).build
113
+ new(gzip: true, print: true).build
76
114
  end
77
115
  end
78
116
  end
@@ -94,4 +132,67 @@ module Esvg
94
132
  false
95
133
  end
96
134
  end
135
+
136
+ def symbolize_keys(hash)
137
+ h = {}
138
+ hash.each {|k,v| h[k.to_sym] = v }
139
+ h
140
+ end
141
+
142
+ def config(options={})
143
+ paths = [options[:config_file], 'config/esvg.yml', 'esvg.yml'].compact
144
+
145
+ config = CONFIG.dup
146
+
147
+ if Esvg.rails? || options[:rails]
148
+ config.merge!(CONFIG_RAILS)
149
+ elsif defined?(Jekyll)
150
+ config.merge!(CONFIG_JEKYLL)
151
+ end
152
+
153
+ if path = paths.select{ |p| File.exist?(p)}.first
154
+ config.merge!(symbolize_keys(YAML.load(File.read(path) || {})))
155
+ end
156
+
157
+ config.merge!(options)
158
+
159
+ config[:filename] = File.basename(config[:filename], '.*')
160
+
161
+ config[:pwd] = File.expand_path Dir.pwd
162
+ config[:source] = File.expand_path config[:source] || config[:pwd]
163
+ config[:build] = File.expand_path config[:build] || config[:pwd]
164
+ config[:assets] = File.expand_path config[:assets] || config[:pwd]
165
+
166
+ config[:temp] = config[:pwd] if config[:temp].nil?
167
+ config[:temp] = File.expand_path File.join(config[:temp], '.esvg-cache')
168
+
169
+ config[:aliases] = load_aliases(config[:alias])
170
+ config[:flatten] = [config[:flatten]].flatten.map { |dir| File.join(dir, '/') }.join('|')
171
+
172
+ config
173
+ end
174
+
175
+ # Load aliases from configuration.
176
+ # returns a hash of aliasees mapped to a name.
177
+ # Converts configuration YAML:
178
+ # alias:
179
+ # foo: bar
180
+ # baz: zip, zop
181
+ # To output:
182
+ # { :bar => "foo", :zip => "baz", :zop => "baz" }
183
+ #
184
+ def load_aliases(aliases)
185
+ a = {}
186
+ aliases.each do |name,alternates|
187
+ alternates.split(',').each do |val|
188
+ a[dasherize(val.strip).to_sym] = dasherize(name.to_s)
189
+ end
190
+ end
191
+ a
192
+ end
193
+
194
+ def dasherize(input)
195
+ input.gsub(/[\W,_]/, '-').sub(/^-/,'').gsub(/-{2,}/, '-')
196
+ end
197
+
97
198
  end
@@ -0,0 +1,67 @@
1
+ module Jekyll
2
+ class << self
3
+ attr_accessor :esvg
4
+ attr_accessor :esvg_embedded
5
+ end
6
+ end
7
+
8
+ Jekyll::Hooks.register :site, :post_read do |site|
9
+ site.exclude.push '.esvg-cache'
10
+ Jekyll.esvg = Esvg.seed_cache(site.config["esvg"] || {})
11
+ end
12
+
13
+ Jekyll::Hooks.register :site, :post_write do |site|
14
+ Jekyll.esvg.build unless Jekyll.esvg_embedded
15
+ end
16
+
17
+ module Jekyll
18
+ module Tags
19
+ class EmbedSvgs < Liquid::Tag
20
+ def initialize(tag_name, markup, tokens)
21
+ super
22
+ @markup = markup.gsub(/["']/,'').split(/,\s*/)
23
+ end
24
+
25
+ def render(context)
26
+ super
27
+ if Jekyll.env == 'production'
28
+
29
+ config = context.registers[:site].config
30
+ dest = config["destination"]
31
+ url = Jekyll.esvg.config[:build].sub(dest, '')
32
+ root_url = File.join config["baseurl"], url
33
+
34
+ Esvg.build_paths(@markup).map { |path| %Q{<script src="#{File.join(root_url, path)}" async="true"></script>} }.join("\n")
35
+ else
36
+ Jekyll.esvg_embedded = true
37
+ Esvg.embed()
38
+ end
39
+ end
40
+ end
41
+
42
+ class UseSvg < Liquid::Tag
43
+ def initialize(tag_name, markup, tokens)
44
+ super
45
+ markup.sub!(/(\S+) /) do
46
+ @name = $1.gsub(/[",']/, '')
47
+ ''
48
+ end
49
+
50
+ @options = markup.strip.split(/,\s*/).join("\n")
51
+
52
+ if @options.empty?
53
+ @options = {}
54
+ else
55
+ @options = Jekyll::Utils.symbolize_hash_keys(YAML.load(@options)) unless @options.empty?
56
+ end
57
+ end
58
+
59
+ def render(context)
60
+ Esvg.use(@name, @options)
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ Liquid::Template.register_tag('use_svg', Jekyll::Tags::UseSvg)
67
+ Liquid::Template.register_tag('esvg', Jekyll::Tags::EmbedSvgs)
@@ -82,7 +82,7 @@ module Esvg
82
82
 
83
83
  optimized = @symbols.map(&:optimize).join.gsub("\n",'')
84
84
 
85
- @svg = %Q{<svg id="esvg-#{id}" #{attributes(attr)}>#{defs}#{optimized}</svg>}
85
+ @svg = %Q{<svg id="esvg-#{id}" #{attributes(attr)} data-turbolinks-permanent>#{defs}#{optimized}</svg>}
86
86
  end
87
87
  end
88
88
 
@@ -9,31 +9,10 @@ module Esvg
9
9
  include Esvg::Utils
10
10
 
11
11
  attr_reader :symbols
12
-
13
- CONFIG = {
14
- filename: 'svgs',
15
- class: 'svg-symbol',
16
- prefix: 'svg',
17
- cache_file: '.symbols',
18
- core: true,
19
- optimize: false,
20
- gzip: false,
21
- scale: false,
22
- fingerprint: true,
23
- throttle_read: 4,
24
- flatten: [],
25
- alias: {}
26
- }
27
-
28
- CONFIG_RAILS = {
29
- source: "app/assets/svgs",
30
- assets: "app/assets/javascripts",
31
- build: "public/javascripts",
32
- temp: "tmp"
33
- }
12
+ attr_reader :config
34
13
 
35
14
  def initialize(options={})
36
- config(options)
15
+ @config = options
37
16
  @symbols = []
38
17
  @svgs = []
39
18
  @last_read = 0
@@ -42,39 +21,6 @@ module Esvg
42
21
  load_files
43
22
  end
44
23
 
45
- def config(options={})
46
- @config ||= begin
47
- paths = [options[:config_file], 'config/esvg.yml', 'esvg.yml'].compact
48
-
49
- config = CONFIG.dup
50
-
51
- if Esvg.rails? || options[:rails]
52
- config.merge!(CONFIG_RAILS)
53
- end
54
-
55
- if path = paths.select{ |p| File.exist?(p)}.first
56
- config.merge!(symbolize_keys(YAML.load(File.read(path) || {})))
57
- end
58
-
59
- config.merge!(options)
60
-
61
- config[:filename] = File.basename(config[:filename], '.*')
62
-
63
- config[:pwd] = File.expand_path Dir.pwd
64
- config[:source] = File.expand_path config[:source] || config[:pwd]
65
- config[:build] = File.expand_path config[:build] || config[:pwd]
66
- config[:assets] = File.expand_path config[:assets] || config[:pwd]
67
-
68
- config[:temp] = config[:pwd] if config[:temp].nil?
69
- config[:temp] = File.expand_path File.join(config[:temp], '.esvg-cache')
70
-
71
- config[:aliases] = load_aliases(config[:alias])
72
- config[:flatten] = [config[:flatten]].flatten.map { |dir| File.join(dir, '/') }.join('|')
73
-
74
- config
75
- end
76
- end
77
-
78
24
  def load_files
79
25
  return if (Time.now.to_i - @last_read) < config[:throttle_read]
80
26
 
@@ -291,25 +237,6 @@ module Esvg
291
237
  config[:aliases][dasherize(name).to_sym] || name
292
238
  end
293
239
 
294
- # Load aliases from configuration.
295
- # returns a hash of aliasees mapped to a name.
296
- # Converts configuration YAML:
297
- # alias:
298
- # foo: bar
299
- # baz: zip, zop
300
- # To output:
301
- # { :bar => "foo", :zip => "baz", :zop => "baz" }
302
- #
303
- def load_aliases(aliases)
304
- a = {}
305
- aliases.each do |name,alternates|
306
- alternates.split(',').each do |val|
307
- a[dasherize(val.strip).to_sym] = dasherize(name.to_s)
308
- end
309
- end
310
- a
311
- end
312
-
313
240
  def write_tmp(name, content)
314
241
  path = File.join(config[:temp], name)
315
242
  FileUtils.mkdir_p(File.dirname(path))
@@ -8,12 +8,6 @@ module Esvg
8
8
  path.sub(File.join(root,''),'')
9
9
  end
10
10
 
11
- def symbolize_keys(hash)
12
- h = {}
13
- hash.each {|k,v| h[k.to_sym] = v }
14
- h
15
- end
16
-
17
11
  def attributes(hash)
18
12
  att = []
19
13
  hash.each do |key, value|
@@ -1,3 +1,3 @@
1
1
  module Esvg
2
- VERSION = "4.3.9"
2
+ VERSION = "4.4.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: 4.3.9
4
+ version: 4.4.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-09-05 00:00:00.000000000 Z
11
+ date: 2018-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - exe/esvg
89
89
  - lib/esvg.rb
90
90
  - lib/esvg/helpers.rb
91
+ - lib/esvg/jekyll_hooks.rb
91
92
  - lib/esvg/railties.rb
92
93
  - lib/esvg/svg.rb
93
94
  - lib/esvg/svgs.rb