class SVGProcessor

Public Class Methods

new(path, prefix, sprite_path) click to toggle source
# File lib/svg_processor.rb, line 5
def initialize(path, prefix, sprite_path)
  @path = path
  @prefix = prefix
  @sprite_path = sprite_path
end

Public Instance Methods

rebuild() click to toggle source
# File lib/svg_processor.rb, line 11
def rebuild
  @svgs = Dir["#{@path}/*.svg"].map { |file| get_svg(file) }
  logger.info "rebuilding: #{@svgs.length} svgs found"
  @symbols = @svgs.map { |svg| convert_to_symbol(svg) }

  @symbol_string = @symbols.join("\n")

  if @sprite_path != nil
    File.write(@sprite_path, "<svg xmlns=\"http://www.w3.org/2000/svg\">#{@symbol_string}</svg>")
  end
end
to_s() click to toggle source
# File lib/svg_processor.rb, line 23
def to_s
  @symbol_string
end

Private Instance Methods

convert_to_symbol(svg) click to toggle source
# File lib/svg_processor.rb, line 44
def convert_to_symbol(svg)
  svg[:xml].xpath('//@id').remove
  content = svg[:xml].at_css("svg").children
  viewbox_size = svg[:xml].xpath("//@viewBox").first.value
  "<symbol viewBox=\"#{viewbox_size}\" id=\"#{@prefix}#{svg[:filename]}\">#{content.to_s.strip}</symbol>"
end
get_svg(file) click to toggle source
# File lib/svg_processor.rb, line 33
def get_svg(file)
  f = File.open(file)
  doc = Nokogiri::XML(f)
  f.close

  {
    filename: File.basename(file, ".svg"),
    xml: doc
  }
end
logger() click to toggle source
# File lib/svg_processor.rb, line 29
def logger
  ::Middleman::Logger.singleton(1)
end