Class: Scavenger::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/scavenger/compressor.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Compressor

Returns a new instance of Compressor.



3
4
5
# File 'lib/scavenger/compressor.rb', line 3

def initialize(path)
  @path = path
end

Instance Method Details

#allowed_tagsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/scavenger/compressor.rb', line 7

def allowed_tags
  @allowed_tags ||=
    %w(a altGlyph altGlyphDef altGlyphItem animate animateColor
       animateMotion animateTransform audio canvas circle clipPath
       color-profile cursor discard ellipse feBlend feColorMatrix
       feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting
       feDisplacementMap feDistantLight feDropShadow feFlood feFuncA
       feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode
       feMorphology feOffset fePointLight feSpecularLighting feSpotLight
       feTile feTurbulence filter font font-face font-face-format
       font-face-name font-face-src font-face-uri foreignObject g glyph
       glyphRef hatch hatchpath hkern iframe image line linearGradient
       marker mask mesh meshgradient meshpatch meshrow metadata
       missing-glyph mpath path pattern polygon polyline radialGradient
       rect script set solidcolor stop style svg switch symbol text
       textPath tref tspan unknown use video view vkern)
end

#compress_dirObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/scavenger/compressor.rb', line 25

def compress_dir
  sheet = Dir.entries(@path)
             .select { |f| should_compress? f }
             .sort
             .map { |f| compress_file(File.join(@path, f), f) }
             .join("")

  File.write(Scavenger::Config.sprite_path, "<svg>#{sheet}</svg>") unless Scavenger::Config.sprite_path.nil?
  sheet
end

#compress_file(filename, id) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/scavenger/compressor.rb', line 36

def compress_file(filename, id)
  doc = Nokogiri::XML(File.open(filename), &:noblanks)
  doc = doc.remove_namespaces!.root
  remove_comments(doc)
  remove_tags(doc)
  remove_ids(doc)
  symbolize(doc, id)
  doc.to_html
end