Skip to content

Instantly share code, notes, and snippets.

@almonk
Last active February 13, 2017 13:04
Show Gist options
  • Save almonk/c8ca691a8369f7b7dc4f9f47154d2115 to your computer and use it in GitHub Desktop.
Save almonk/c8ca691a8369f7b7dc4f9f47154d2115 to your computer and use it in GitHub Desktop.
# SVG Squash
# Alasdair Monk
require 'Nokogiri'
puts "Creating sprite..."
svgs = Dir["./**.svg"]
contents =""
# Loop thru svgs in directory
svgs.each do |svg|
puts "Reading #{svg}"
file = File.open(svg, "rb")
filename = File.basename(file,File.extname(file))
doc = File.open(file) { |f| Nokogiri::XML(f) }
paths = doc.at_css("path")
viewbox = doc.at_css('svg')['viewBox']
contents += "<symbol id='#{filename}' viewbox='#{viewbox}'>#{paths}</symbol>"
file.close
end
# Write out sprite.svg
open('./sprite.svg', 'w') { |f|
f.puts %q(<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="position:absolute;width:0;height:0;visibility:hidden">
<defs>
<linearGradient x1="0%" y1="0%" x2="100%" y2="100%" id="purple-gradient">
<stop stop-color="#AC8ECE" offset="0%"></stop>
<stop stop-color="#79589F" offset="100%"></stop>
</linearGradient>
<linearGradient x1="0%" y1="0" x2="100%" y2="100%" id="green-gradient">
<stop stop-color="#ADE28F" offset="0%"></stop>
<stop stop-color="#74C080" offset="100%"></stop>
</linearGradient>
)
f.puts contents
f.puts %q(
</defs>
</svg>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment