Class: CSSMin

Inherits:
Object
  • Object
show all
Defined in:
lib/mack-asset_packager/cssmin.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ CSSMin

Returns a new instance of CSSMin.



4
5
6
# File 'lib/mack-asset_packager/cssmin.rb', line 4

def initialize(content)
  self.data = content
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/mack-asset_packager/cssmin.rb', line 3

def data
  @data
end

Instance Method Details

#minimizeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mack-asset_packager/cssmin.rb', line 8

def minimize
  # find all elements in between { and }, and remove \n and \t
  # self.data.scan(/\{([.|\s\S]*?)\}/).flatten.each do |elt|
  #   str = elt.gsub("\n", "")
  #   str = str.gsub("\t", "")
  #   self.data.gsub!(elt, str)
  # end
  
  self.data = self.data.squeeze(" ").squeeze("\t").squeeze("\n")
  self.data.gsub!("\t", "")
  
  # remove comments in the form of /* ... */ 
  self.data.gsub!(/\/\*[.|\s|\S]*?\*\//, "")
  
  # remove newline before and after {
  self.data.gsub!("\n\{", " \{")
  self.data.gsub!("\{\n", " \{")
  
  # remove newline after ,
  self.data.gsub!(",\n", ", ")
  
  # remove spaces after/before ...
  self.data.gsub!(": ", ":")
  self.data.gsub!(" :", ":")
  self.data.gsub!(", ", ",")
  self.data.gsub!(" ,", ",")
  self.data.gsub!("; ", ";")
  self.data.gsub!(" ;", ";")
  self.data.gsub!(";\}", "\}")
  self.data.gsub!(" \}", "\}")
  self.data.gsub!("\{ ", "\{")
  self.data.gsub!(" \{", "\{")
      
  # remove all new line, but add one after }
  self.data.gsub!("\n", "")
  self.data.gsub!("\}", "\}\n")
  
  self.data = self.data.squeeze(" ").squeeze("\t").squeeze("\n").strip
  
  return self.data
end