class Rainpress

Information

This is the main class of Rainpress, create an instance of it to compress your CSS-styles.

Author

Uwe L. Korn <uwelk@xhochy.org>

Options:

Public Class Methods

compress(style, options = {}) click to toggle source

Quick-compress the styles. This eliminates the need to create an instance of the class

   # File lib/webgen/vendor/rainpress.rb
18 def self.compress(style, options = {})
19   self.new(style, options).compress!
20 end
new(style, opts = {}) click to toggle source
   # File lib/webgen/vendor/rainpress.rb
22 def initialize(style, opts = {})
23   @style = style
24   @opts = {
25     :comments => true,
26     :newlines => true,
27     :spaces   => true,
28     :colors   => true,
29     :misc     => true
30   }
31   @opts.merge! opts
32 end

Public Instance Methods

compress!() click to toggle source

Run the compressions and return the newly compressed text

   # File lib/webgen/vendor/rainpress.rb
35 def compress!
36   remove_comments!  if @opts[:comments]
37   remove_newlines!  if @opts[:newlines]
38   remove_spaces!    if @opts[:spaces]
39   shorten_colors!   if @opts[:colors]
40   do_misc!          if @opts[:misc]
41   @style
42 end
do_misc!() click to toggle source

Do miscellaneous compression methods on the style.

    # File lib/webgen/vendor/rainpress.rb
128 def do_misc!
129   # Replace 0(pt,px,em,%) with 0 but only when preceded by : or a white-space
130   @style.gsub!(/([\s:]+)(0)(px|em|%|in|cm|mm|pc|pt|ex)/i, '\1\2')
131 
132   # Replace :0 0 0 0(;|}) with :0(;|})
133   @style.gsub!(/:0 0 0 0(;|\})/, ':0\1')
134 
135   # Replace :0 0 0(;|}) with :0(;|})
136   @style.gsub!(/:0 0 0(;|\})/, ':0\1')
137 
138   # Replace :0 0(;|}) with :0(;|})
139   @style.gsub!(/:0 0(;|\})/, ':0\1')
140 
141   # Replace background-position:0; with background-position:0 0;
142   @style.gsub!('background-position:0;', 'background-position:0 0;')
143 
144   # Replace 0.6 to .6, but only when preceded by : or a white-space
145   @style.gsub!(/[:\s]0+\.(\d+)/) do |match|
146     match.sub('0', '') # only first '0' !!
147   end
148 
149   # Replace multiple ';' with a single ';'
150   @style.gsub!(/[;]+/, ';')
151 
152   # Replace ;} with }
153   @style.gsub!(';}', '}')
154 
155   # Replace font-weight:normal; with 400
156   @style.gsub!(/font-weight[\s]*:[\s]*normal[\s]*(;|\})/i,'font-weight:400\1')
157   @style.gsub!(/font[\s]*:[\s]*normal[\s;\}]*/) do |match|
158     match.sub('normal', '400')
159   end
160 
161   # Replace font-weight:bold; with 700
162   @style.gsub!(/font-weight[\s]*:[\s]*bold[\s]*(;|\})/,'font-weight:700\1')
163   @style.gsub!(/font[\s]*:[\s]*bold[\s;\}]*/) do |match|
164     match.sub('bold', '700')
165   end
166 end
remove_comments!() click to toggle source

Remove all comments out of the CSS-Document

Only /* text */ comments are supported. Attention: If you are doing css hacks for IE using the comment tricks, they will be removed using this function. Please consider for IE css style corrections the usage of conditionals comments in your (X)HTML document.

   # File lib/webgen/vendor/rainpress.rb
50     def remove_comments!
51 input = @style
52 @style = ''
53 
54 while input.length > 0 do
55   pos = input.index("/*");
56 
57   # No more comments
58   if pos == nil
59     @style += input
60     input = '';
61   else # Comment beginning at pos
62     @style += input[0..(pos-1)] if pos > 0 # only append text if there is some
63     input = input[(pos+2)..-1]
64     # Comment ending at pos
65     pos = input.index("*/")
66     input = input[(pos+2)..-1]
67   end
68 end
69     end
remove_newlines!() click to toggle source

Remove all newline characters

We take care of Windows(rn), Unix(n) and Mac(r) newlines.

   # File lib/webgen/vendor/rainpress.rb
74 def remove_newlines!
75         @style.gsub!(/\n|\r/, '')
76 end
remove_spaces!() click to toggle source

Remove unneeded spaces

  1. Turn mutiple spaces into a single

  2. Remove spaces around ;:{},

  3. Remove tabs

   # File lib/webgen/vendor/rainpress.rb
83 def remove_spaces!
84   @style.gsub!(/\s*(\s|;|:|\}|\{|,)\s*/, '\1')
85   @style.gsub!("\t", '')
86 end
shorten_colors!() click to toggle source

Replace color values with their shorter equivalent

  1. Turn rgb(,,)-colors into -values

  2. Shorten #AABBCC down to #ABC

  3. Replace names with their shorter hex-equivalent

    • white -> fff

    • black -> #000

  4. Replace -values with their shorter name

    • f00 -> red

    # File lib/webgen/vendor/rainpress.rb
 97 def shorten_colors!
 98         # rgb(50,101,152) to #326598
 99   @style.gsub!(/rgb\s*\(\s*([0-9,\s]+)\s*\)/) do |match|
100     out = '#'
101     $1.split(',').each do |num|
102       out += '0' if num.to_i < 16
103       out += num.to_i.to_s(16) # convert to hex
104     end
105     out
106   end
107   # Convert #AABBCC to #ABC, keep if preceed by a '='
108   @style.gsub!(/([^\"'=\s])(\s*)#([\da-f])\3([\da-f])\4([\da-f])\5/i, '\1#\3\4\5')
109 
110   # At the moment we assume that colours only appear before ';' or '}' and
111   # after a ':', if there could be an occurence of a color before or after
112   # an other character, submit either a bug report or, better, a patch that
113   # enables Rainpress to take care of this.
114 
115   # shorten several names to numbers
116   ## shorten white -> #fff
117   @style.gsub!(/:\s*white\s*(;|\})/, ':#fff\1')
118 
119   ## shorten black -> #000
120   @style.gsub!(/:\s*black\s*(;|\})/, ':#000\1')
121 
122   # shotern several numbers to names
123   ## shorten #f00 or #ff0000 -> red
124   @style.gsub!(/:\s*#f{1,2}0{2,4}(;|\})/i, ':red\1')
125 end