module Esvg
Constants
- VERSION
Public Instance Methods
build_paths(names=nil)
click to toggle source
# File lib/esvg.rb, line 90 def build_paths(names=nil) find_svgs(names).map{|s| s.build_paths(names) }.flatten end
config(options={})
click to toggle source
# 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)
click to toggle source
# File lib/esvg.rb, line 239 def dasherize(input) input.to_s.strip.gsub(/[\W,_]+/, '-') end
deep_symbolize_keys(hash)
click to toggle source
# 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) { |key| ... }
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/esvg.rb, line 102 def find_svgs(names=nil) @svgs.select {|s| s.buildable_svgs(names) } end
find_symbol(name, options={})
click to toggle source
# 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)
click to toggle source
# File lib/esvg.rb, line 116 def html_safe(input) input = input.html_safe if rails? input end
load_aliases(aliases)
click to toggle source
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" }
# 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={})
click to toggle source
# 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)
click to toggle source
Determine if an NPM module is installed by checking paths with `npm bin` Returns path to binary if installed
# 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_assets()
click to toggle source
Add SVG build task to `rake assets:precompile`
# 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?()
click to toggle source
# File lib/esvg.rb, line 112 def rails? defined?(Rails) || File.exist?("./bin/rails") end
seed_cache(options)
click to toggle source
# 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
svgs()
click to toggle source
# File lib/esvg.rb, line 68 def svgs @svgs end
update_config( options )
click to toggle source
# File lib/esvg.rb, line 216 def update_config ( options ) config( { config_file: options[:config_file] } ) end
use(name, options={})
click to toggle source
# 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={})
click to toggle source
# 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