class Scavenger::Compressor
Public Class Methods
new(path)
click to toggle source
# File lib/scavenger/compressor.rb, line 3 def initialize(path) @path = path end
Public Instance Methods
compress_dir()
click to toggle source
# File lib/scavenger/compressor.rb, line 25 def compress_dir sheet = Dir.glob(File.join(@path, "**/*.svg")) .select { |f| should_compress? f } .sort .map { |f| f.sub(%r{^#{@path}/?}, "") } .map { |f| compress_file(f) } .join("") File.write(Scavenger::Config.sprite_path, "<svg>#{sheet}</svg>") unless Scavenger::Config.sprite_path.nil? sheet end
compress_file(filename)
click to toggle source
# File lib/scavenger/compressor.rb, line 37 def compress_file(filename) id = filename.gsub(%r{/}, "--") doc = Nokogiri::XML(File.open(File.join(@path, filename)), &:noblanks) doc = doc.remove_namespaces!.root remove_comments(doc) remove_tags(doc) remove_ids(doc) symbolize(doc, id) doc.to_html end
Private Instance Methods
remove_comments(doc)
click to toggle source
# File lib/scavenger/compressor.rb, line 55 def remove_comments(doc) doc.xpath("//comment()").remove end
remove_ids(doc)
click to toggle source
# File lib/scavenger/compressor.rb, line 65 def remove_ids(doc) doc.xpath("//@id").remove end
should_compress?(filename)
click to toggle source
# File lib/scavenger/compressor.rb, line 50 def should_compress?(filename) !File.directory?(filename) && !File.basename(filename).start_with?(".") end
symbolize(doc, id)
click to toggle source
# File lib/scavenger/compressor.rb, line 69 def symbolize(doc, id) doc.xpath("@*").each do |attribute| attribute.remove unless attribute.name == "viewBox" end doc.name = "symbol" doc["id"] = Scavenger::Config.id_prefix + id.chomp(".svg") doc["preserveAspectRatio"] = Scavenger::Config.aspect_ratio end