class SvgSprite

Constants

NOKOGIRI_SAVE_OPTIONS
VERSION

Attributes

css_path[R]
fill[R]
input[R]
input_files[R]
name[R]
optimize[R]
sprite_path[R]
stroke[R]

Public Class Methods

call( name:, input:, sprite_path:, css_path:, optimize: true, stroke: nil, fill: nil ) click to toggle source
# File lib/svg_sprite.rb, line 19
def self.call(
  name:, input:, sprite_path:, css_path:,
  optimize: true, stroke: nil, fill: nil
)
  new(
    name: name,
    input: input,
    sprite_path: sprite_path,
    css_path: css_path,
    optimize: optimize,
    stroke: stroke,
    fill: fill
  ).call
end
new( name:, input:, sprite_path:, css_path:, optimize:, stroke:, fill: ) click to toggle source
# File lib/svg_sprite.rb, line 37
def initialize(
  name:, input:, sprite_path:, css_path:, optimize:, stroke:, fill:
)
  @name = name
  @input = input
  @input_files = Dir["#{input}/**/*.svg"].sort
  @sprite_path = sprite_path
  @css_path = css_path
  @optimize = optimize
  @stroke = stroke
  @fill = fill
end

Public Instance Methods

call() click to toggle source
# File lib/svg_sprite.rb, line 50
def call
  save_file sprite_path, svg_sprite
  save_file css_path, manifest(css_definitions.chomp)
end

Private Instance Methods

add_symbols(defs) click to toggle source
# File lib/svg_sprite.rb, line 131
        def add_symbols(defs)
  svgs.each do |svg|
    defs << svg.symbol
  end
end
command() click to toggle source
# File lib/svg_sprite.rb, line 68
        def command
  cwd = Pathname.new(Dir.pwd)

  cmd = [
    "svg_sprite",
    "generate",
    "--input",
    Pathname.new(input).relative_path_from(cwd).to_s,
    "--sprite-path",
    Pathname.new(sprite_path).relative_path_from(cwd).to_s,
    "--css-path",
    Pathname.new(css_path).relative_path_from(cwd).to_s
  ]

  cmd.push("--optimize") if optimize
  cmd.push("--stroke", stroke) if stroke
  cmd.push("--fill", fill) if fill

  cmd.map {|segment| Shellwords.escape(segment) }.join(" ")
end
css_definitions() click to toggle source
# File lib/svg_sprite.rb, line 95
        def css_definitions
  svgs.each_with_object(StringIO.new) do |file, io|
    io << ".#{name}--#{file.id} {\n"
    io << "  width: #{file.width}px;\n"
    io << "  height: #{file.height}px;\n"
    io << "}\n\n"
  end.tap(&:rewind).read.chomp
end
manifest(css) click to toggle source
# File lib/svg_sprite.rb, line 55
          def manifest(css)
    <<~TEXT
      /*
      This file was generated by https://rubygems.org/gems/svg_sprite with the
      following command:

      #{command}
      */

      #{css}
    TEXT
  end
save_file(filepath, content) click to toggle source
# File lib/svg_sprite.rb, line 104
        def save_file(filepath, content)
  FileUtils.mkdir_p(File.dirname(filepath))

  File.open(filepath, "w") do |io|
    io << content
  end
end
sprite_builder() click to toggle source
# File lib/svg_sprite.rb, line 120
        def sprite_builder
  Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.svg(
      xmlns: "http://www.w3.org/2000/svg",
      style: "display: none"
    ) do |svg|
      svg.defs { }
    end
  end
end
svg_sprite() click to toggle source
# File lib/svg_sprite.rb, line 112
        def svg_sprite
  builder = sprite_builder
  add_symbols(builder.doc.css("defs").first)

  Nokogiri::XML(builder.to_xml) {|doc| doc.default_xml.noblanks }
          .to_xml(NOKOGIRI_SAVE_OPTIONS.dup.merge(indent: 2))
end
svgs() click to toggle source
# File lib/svg_sprite.rb, line 89
        def svgs
  @svgs ||= input_files.map do |file|
    SVG.new(file, optimize: optimize, stroke: stroke, fill: fill)
  end
end