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.6.11"

Instance Method Summary collapse

Methods included from Utils

#attributes, #compress, #sort, #sub_path

Instance Method Details

#build_paths(names = nil) ⇒ Object



90
91
92
# File 'lib/esvg.rb', line 90

def build_paths(names=nil)
  find_svgs(names).map{|s| s.build_paths(names) }.flatten
end

#config(options = {}) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/esvg.rb', line 165

def config(options={})
  paths = [options[:config_file], 'config/esvg.yml', 'esvg.yml'].compact

  config = CONFIG.dup

  if rails? || options[:rails]
    config.merge!(CONFIG_RAILS)
    config[:env] ||= Rails.env
  elsif defined?(Jekyll)
    config.merge!(CONFIG_JEKYLL)
    config[:env] ||= Jekyll.env
  end

  config.merge!(options)

  if path = paths.select{ |p| File.exist?(p)}.first
    options[:config_file] = path
    config.merge!(deep_symbolize_keys(YAML.load(File.read(path) || {})))

    # Allow CLI passed options to override config file
    config.merge!(options) if options[:cli]
  else
    options[:config_file] = false
  end

  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[:root]      ||= config[:pwd] # For relative paths on print

  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_dir] = [config[:flatten]].flatten.map { |dir| File.join(dir, '/') }.join('|')
  config[:read]        = Time.now.to_i
  config[:env]       ||= 'development'
  config[:print]     ||= true unless config[:env] == 'production'
  config[:optimize]    = true if config[:env] == 'production'

  config
end

#dasherize(input) ⇒ Object



239
240
241
# File 'lib/esvg.rb', line 239

def dasherize(input)
  input.to_s.strip.gsub(/[\W,_]+/, '-')
end

#deep_symbolize_keys(hash) ⇒ Object



161
162
163
# File 'lib/esvg.rb', line 161

def deep_symbolize_keys(hash)
  deep_transform_keys_in_object( hash ){ |key| key.to_sym rescue key }
end

#deep_transform_keys_in_object(object, &block) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/esvg.rb', line 148

def deep_transform_keys_in_object(object, &block)
  case object
  when Hash
    object.each_with_object({}) do |(key, value), result|
      result[yield(key)] = deep_transform_keys_in_object(value, &block)
    end
  when Array
    object.map {|e| deep_transform_keys_in_object(e, &block) }
  else
    object
  end
end

#embed(names = nil) ⇒ Object



84
85
86
87
88
# File 'lib/esvg.rb', line 84

def embed(names=nil)
  html_safe( find_svgs(names).map{ |s| 
    s.embed_script(names) 
  }.join )
end

#find_svgs(names = nil) ⇒ Object



102
103
104
# File 'lib/esvg.rb', line 102

def find_svgs(names=nil)
  @svgs.select {|s| s.buildable_svgs(names) }
end

#find_symbol(name, options = {}) ⇒ Object



106
107
108
109
110
# File 'lib/esvg.rb', line 106

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



116
117
118
119
# File 'lib/esvg.rb', line 116

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" }


229
230
231
232
233
234
235
236
237
# File 'lib/esvg.rb', line 229

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



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/esvg.rb', line 55

def new(options={})
  @svgs ||=[]
  c = config(options)

  # If the source path is the same continue
  # Otherwise add a new SVG group for this path
  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



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/esvg.rb', line 132

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_assetsObject

Add SVG build task to ‘rake assets:precompile`



122
123
124
125
126
127
128
# File 'lib/esvg.rb', line 122

def precompile_assets
  if rails? && defined?(Rake)
    ::Rake::Task['assets:precompile'].enhance do
      svgs.map(&:build)
    end
  end
end

#rails?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/esvg.rb', line 112

def rails?
  defined?(Rails) || File.exist?("./bin/rails")
end

#seed_cache(options) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/esvg.rb', line 94

def seed_cache(options)
  svgs = new(options)
  puts "Optimizing SVGs" if options[:print]
  svgs.symbols.map(&:optimize)
  svgs.write_cache
  svgs
end

#svgsObject



68
69
70
# File 'lib/esvg.rb', line 68

def svgs
  @svgs
end

#update_config(options) ⇒ Object



216
217
218
# File 'lib/esvg.rb', line 216

def update_config ( options )
  config( { config_file: options[:config_file] } )
end

#use(name, options = {}) ⇒ Object



72
73
74
75
76
# File 'lib/esvg.rb', line 72

def use(name, options={})
  if symbol = find_symbol(name, options)
    html_safe symbol.use options
  end
end

#use_tag(name, options = {}) ⇒ Object



78
79
80
81
82
# File 'lib/esvg.rb', line 78

def use_tag(name, options={})
  if symbol = find_symbol(name, options)
    html_safe symbol.use_tag options
  end
end