class SVGSprite::Exporter

Constants

XMLNS

Public Class Methods

export!(svgs, options) click to toggle source

Class methods

# File lib/svgsprite/exporter.rb, line 10
def self.export!(svgs, options)
  exporter = self.new(svgs, options)
  exporter.create_output_dir!

  if options[:stdout]
    exporter.export_to_stdout
  else
    exporter.export_to_file
  end
end
new(svgs, options) click to toggle source

Instance methods

# File lib/svgsprite/exporter.rb, line 22
def initialize(svgs, options)
  @options = options
  @svgs = svgs
end

Public Instance Methods

create_output_dir!() click to toggle source
# File lib/svgsprite/exporter.rb, line 27
def create_output_dir!
  FileUtils.mkdir_p(@options[:output])
end
export_to_file() click to toggle source
# File lib/svgsprite/exporter.rb, line 35
def export_to_file
  output_file = "#{@options[:output]}/#{@options[:filename]}.svg"
  File.open(output_file, 'w') { |f| f.write(svg_string) }
end
export_to_stdout() click to toggle source
# File lib/svgsprite/exporter.rb, line 31
def export_to_stdout
  STDOUT.write(svg_string)
end
icon_node(svg_hash) click to toggle source
# File lib/svgsprite/exporter.rb, line 47
def icon_node(svg_hash)
  prefix = @options[:prefix]
  svg = svg_hash[:svg]
  filename = svg_hash[:filename]
  id = "#{prefix}-#{filename}"

  %(<g id="#{id}">#{svg}</g>)
end
svg_string() click to toggle source
# File lib/svgsprite/exporter.rb, line 40
def svg_string
  icon_nodes = @svgs.map { |svg_hash| icon_node(svg_hash) }
  xmlns = XMLNS.map { |k, v| %(#{k}="#{v}") }.join(' ')

  %(<svg style="display: none" #{xmlns}><defs>#{icon_nodes.join('')}</defs></svg>)
end