Module: Esvg
- Extended by:
- Esvg
- Includes:
- Utils
- Included in:
- Esvg
- Defined in:
- lib/esvg.rb,
lib/esvg/svg.rb,
lib/esvg/svgs.rb,
lib/esvg/utils.rb,
lib/esvg/symbol.rb,
lib/esvg/version.rb,
lib/esvg/railties.rb
Defined Under Namespace
Modules: Helpers, Utils
Classes: Railtie, Svg, Svgs, Symbol
Constant Summary
collapse
- VERSION =
"4.4.1"
Instance Method Summary
collapse
Methods included from Utils
#attributes, #compress, #sort, #sub_path
Instance Method Details
#build_paths(names = nil) ⇒ Object
79
80
81
|
# File 'lib/esvg.rb', line 79
def build_paths(names=nil)
find_svgs(names).map{|s| s.build_paths(names) }.flatten
end
|
#config(options = {}) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/esvg.rb', line 142
def config(options={})
paths = [options[:config_file], 'config/esvg.yml', 'esvg.yml'].compact
config = CONFIG.dup
if Esvg.rails? || options[:rails]
config.merge!(CONFIG_RAILS)
elsif defined?(Jekyll)
config.merge!(CONFIG_JEKYLL)
end
if path = paths.select{ |p| File.exist?(p)}.first
config.merge!(symbolize_keys(YAML.load(File.read(path) || {})))
end
config.merge!(options)
if defined? Jekyll
config[:build] = File.join(config[:destination], config[:build])
config[:source] = File.join(config[:source_dir], config[:source])
end
config[:filename] = File.basename(config[:filename], '.*')
config[:pwd] = File.expand_path Dir.pwd
config[:source] = File.expand_path config[:source] || config[:pwd]
config[:build] = File.expand_path config[:build] || config[:pwd]
config[:assets] = File.expand_path config[:assets] || config[:pwd]
config[:temp] = config[:pwd] if config[:temp].nil?
config[:temp] = File.expand_path File.join(config[:temp], '.esvg-cache')
config[:aliases] = load_aliases(config[:alias])
config[:flatten] = [config[:flatten]].flatten.map { |dir| File.join(dir, '/') }.join('|')
config
end
|
#dasherize(input) ⇒ Object
199
200
201
|
# File 'lib/esvg.rb', line 199
def dasherize(input)
input.gsub(/[\W,_]/, '-').sub(/^-/,'').gsub(/-{2,}/, '-')
end
|
#embed(names = nil) ⇒ Object
75
76
77
|
# File 'lib/esvg.rb', line 75
def embed(names=nil)
html_safe find_svgs(names).map{|s| s.embed_script(names) }.join
end
|
#find_svgs(names = nil) ⇒ Object
91
92
93
|
# File 'lib/esvg.rb', line 91
def find_svgs(names=nil)
@svgs.select {|s| s.buildable_svgs(names) }
end
|
#find_symbol(name, options = {}) ⇒ Object
95
96
97
98
99
|
# File 'lib/esvg.rb', line 95
def find_symbol(name, options={})
if group = @svgs.find {|s| s.find_symbol(name, options[:fallback]) }
group.find_symbol(name, options[:fallback])
end
end
|
#html_safe(input) ⇒ Object
105
106
107
108
|
# File 'lib/esvg.rb', line 105
def html_safe(input)
input = input.html_safe if rails?
input
end
|
#load_aliases(aliases) ⇒ Object
Load aliases from configuration.
returns a hash of aliasees mapped to a name.
Converts configuration YAML:
alias:
foo: bar
baz: zip, zop
To output:
{ :bar => "foo", :zip => "baz", :zop => "baz" }
189
190
191
192
193
194
195
196
197
|
# File 'lib/esvg.rb', line 189
def load_aliases(aliases)
a = {}
aliases.each do |name,alternates|
alternates.split(',').each do |val|
a[dasherize(val.strip).to_sym] = dasherize(name.to_s)
end
end
a
end
|
#new(options = {}) ⇒ Object
50
51
52
53
54
55
56
57
|
# File 'lib/esvg.rb', line 50
def new(options={})
@svgs ||=[]
c = config(options)
unless @svgs.find { |s| s.config[:source] == c[:source] }
@svgs << Svgs.new(c)
end
@svgs.last
end
|
#node_module(cmd) ⇒ Object
Determine if an NPM module is installed by checking paths with ‘npm bin` Returns path to binary if installed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/esvg.rb', line 120
def node_module(cmd)
@modules ||={}
return @modules[cmd] if !@modules[cmd].nil?
local = "$(npm bin)/#{cmd}"
global = "$(npm -g bin)/#{cmd}"
@modules[cmd] = if Open3.capture3(local)[2].success?
local
elsif Open3.capture3(global)[2].success?
global
else
false
end
end
|
#precompile_assets ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/esvg.rb', line 110
def precompile_assets
if defined?(Rails) && Rails.env.production? && defined?(Rake)
::Rake::Task['assets:precompile'].enhance do
new(gzip: true, print: true).build
end
end
end
|
#rails? ⇒ Boolean
101
102
103
|
# File 'lib/esvg.rb', line 101
def rails?
defined?(Rails) || File.exist?("./bin/rails")
end
|
#seed_cache(options) ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/esvg.rb', line 83
def seed_cache(options)
svgs = new(options)
puts "Optimizing SVGs" if options[:print]
svgs.symbols.map(&:optimize)
svgs.write_cache
svgs
end
|
#svgs ⇒ Object
59
60
61
|
# File 'lib/esvg.rb', line 59
def svgs
@svgs
end
|
#symbolize_keys(hash) ⇒ Object
136
137
138
139
140
|
# File 'lib/esvg.rb', line 136
def symbolize_keys(hash)
h = {}
hash.each {|k,v| h[k.to_sym] = v }
h
end
|
#use(name, options = {}) ⇒ Object
63
64
65
66
67
|
# File 'lib/esvg.rb', line 63
def use(name, options={})
if symbol = find_symbol(name, options)
html_safe symbol.use options
end
end
|
#use_tag(name, options = {}) ⇒ Object
69
70
71
72
73
|
# File 'lib/esvg.rb', line 69
def use_tag(name, options={})
if symbol = find_symbol(name, options)
html_safe symbol.use_tag options
end
end
|