Class: SVGSprite::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/svgsprite/exporter.rb

Constant Summary collapse

XMLNS =
{
  'xmlns' => 'http://www.w3.org/2000/svg',
  'xmlns:sketch' => 'http://www.bohemiancoding.com/sketch/ns',
  'xmlns:xlink' => 'http://www.w3.org/1999/xlink'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(svgs, options) ⇒ Exporter

Instance methods



22
23
24
25
# File 'lib/svgsprite/exporter.rb', line 22

def initialize(svgs, options)
  @options = options
  @svgs = svgs
end

Class Method Details

.export!(svgs, options) ⇒ Object

Class methods



10
11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#create_output_dir!Object



27
28
29
# File 'lib/svgsprite/exporter.rb', line 27

def create_output_dir!
  FileUtils.mkdir_p(@options[:output])
end

#export_to_fileObject



35
36
37
38
# 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_stdoutObject



31
32
33
# File 'lib/svgsprite/exporter.rb', line 31

def export_to_stdout
  STDOUT.write(svg_string)
end

#icon_node(svg_hash) ⇒ Object



47
48
49
50
51
52
53
54
# 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_stringObject



40
41
42
43
44
45
# 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