| 1 |
ruby comment # == About |
|---|
| 2 |
ruby comment # |
|---|
| 3 |
ruby comment # Rainpress is a compressor for CSS. It's written in ruby, but should not be |
|---|
| 4 |
ruby comment # limited to ruby projects. |
|---|
| 5 |
ruby comment # |
|---|
| 6 |
ruby comment # Rainpress does not apply common compression algorithms like gzip, it removes |
|---|
| 7 |
ruby comment # unnecessary characters and replaces some attributes with a shorter equivalent |
|---|
| 8 |
ruby comment # name. |
|---|
| 9 |
ruby comment # |
|---|
| 10 |
ruby comment # Copyright:: Copyright (c) 2007-2008 Uwe L. Korn |
|---|
| 11 |
ruby comment # |
|---|
| 12 |
ruby comment # == License |
|---|
| 13 |
ruby comment # |
|---|
| 14 |
ruby comment # Copyright (c) 2007-2008 Uwe L. Korn |
|---|
| 15 |
ruby comment # |
|---|
| 16 |
ruby comment # Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 17 |
ruby comment # of this software and associated documentation files (the "Software"), to deal |
|---|
| 18 |
ruby comment # in the Software without restriction, including without limitation the rights |
|---|
| 19 |
ruby comment # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 20 |
ruby comment # copies of the Software, and to permit persons to whom the Software is |
|---|
| 21 |
ruby comment # furnished to do so, subject to the following conditions: |
|---|
| 22 |
ruby comment # |
|---|
| 23 |
ruby comment # The above copyright notice and this permission notice shall be included in |
|---|
| 24 |
ruby comment # all copies or substantial portions of the Software. |
|---|
| 25 |
ruby comment # |
|---|
| 26 |
ruby comment # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 27 |
ruby comment # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 28 |
ruby comment # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 29 |
ruby comment # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 30 |
ruby comment # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 31 |
ruby comment # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 32 |
ruby comment # THE SOFTWARE. |
|---|
| 33 |
ruby comment # |
|---|
| 34 |
ruby comment # Remark(not part of the license text): This is a MIT-style license |
|---|
| 35 |
ruby comment # |
|---|
| 36 |
ruby comment # == Links |
|---|
| 37 |
ruby comment # |
|---|
| 38 |
ruby comment # * {Rainpress Website}[http://rainpress.xhochy.com/] |
|---|
| 39 |
ruby comment # * {SVN repository}[http://code.google.com/p/rainpress/source] |
|---|
| 40 |
ruby comment # * {Bugtracker}[https://bugs.launchpad.net/rainpress/] |
|---|
| 41 |
ruby comment # * {Wiki}[http://code.google.com/p/rainpress/w/list] |
|---|
| 42 |
ruby comment # * {Translations}[https://translations.launchpad.net/rainpress/] |
|---|
| 43 |
ruby comment # * {XhochY Weblog (for Announcements about Rainpress)}[http://xhochy.org/en/] |
|---|
| 44 |
ruby comment # * {Mailinglist}[http://groups.google.com/group/xy-oss-projects-discussion] |
|---|
| 45 |
ruby comment # * {Continous Integration Builds and Tests}[http://cruisecontrol-rb.xhochy.com/builds/rainpress] |
|---|
| 46 |
ruby comment # * {Freshmeat Record}[http://freshmeat.net/projects/rainpress] |
|---|
| 47 |
ruby comment # * {Ohloh listing}[http://www.ohloh.net/projects/12620/] |
|---|
| 48 |
ruby code module Rainpress |
|---|
| 49 |
ruby blank |
|---|
| 50 |
ruby comment # == Information |
|---|
| 51 |
ruby comment # |
|---|
| 52 |
ruby comment # This is the main class of Rainpress, create an instance of it to compress |
|---|
| 53 |
ruby comment # your CSS-styles. |
|---|
| 54 |
ruby comment # |
|---|
| 55 |
ruby comment # Author:: Uwe L. Korn <uwelk@xhochy.org> |
|---|
| 56 |
ruby comment # |
|---|
| 57 |
ruby comment # == Simple Usage |
|---|
| 58 |
ruby comment # |
|---|
| 59 |
ruby comment # packer = Rainpress::Packer.new |
|---|
| 60 |
ruby comment # compressed_style = packer.compress(style) |
|---|
| 61 |
ruby code class Packer |
|---|
| 62 |
ruby blank |
|---|
| 63 |
ruby comment # Use always this functions if you want to compress your CSS-style |
|---|
| 64 |
ruby comment # |
|---|
| 65 |
ruby comment # <b>Options:</b> |
|---|
| 66 |
ruby comment # |
|---|
| 67 |
ruby comment # * <tt>:preserveComments</tt> - if set to true, comments will not be |
|---|
| 68 |
ruby comment # removed |
|---|
| 69 |
ruby comment # * <tt>:preserveNewline</tt> - if set to true, newlines will not be removed |
|---|
| 70 |
ruby comment # * <tt>:preserveSpaces</tt> - if set to true, spaces will not be removed |
|---|
| 71 |
ruby comment # * <tt>:preserveColors</tt> - if set to true, colors will not be modified |
|---|
| 72 |
ruby comment # * <tt>:skipMisc</tt> - if set to true, miscellaneous compression parts |
|---|
| 73 |
ruby comment # will be skipped |
|---|
| 74 |
ruby code def compress(style, options = {}) |
|---|
| 75 |
ruby comment # remove comments |
|---|
| 76 |
ruby code style = remove_comments(style) unless options[:preserveComments] |
|---|
| 77 |
ruby blank |
|---|
| 78 |
ruby comment # remove newlines |
|---|
| 79 |
ruby code style = remove_newlines(style) unless options[:preserveNewlines] |
|---|
| 80 |
ruby blank |
|---|
| 81 |
ruby comment # remove unneeded spaces |
|---|
| 82 |
ruby code style = remove_spaces(style) unless options[:preserveSpaces] |
|---|
| 83 |
ruby blank |
|---|
| 84 |
ruby comment # replace colours with shorter names |
|---|
| 85 |
ruby code style = shorten_colors(style) unless options[:preserveColors] |
|---|
| 86 |
ruby blank |
|---|
| 87 |
ruby comment # make all other things |
|---|
| 88 |
ruby code style = do_misc(style) unless options[:skipMisc] |
|---|
| 89 |
ruby blank |
|---|
| 90 |
ruby code style |
|---|
| 91 |
ruby code end |
|---|
| 92 |
ruby blank |
|---|
| 93 |
ruby comment # Remove all comments out of the CSS-Document |
|---|
| 94 |
ruby comment # |
|---|
| 95 |
ruby comment # Only /* text */ comments are supported. |
|---|
| 96 |
ruby comment # Attention: If you are doing css hacks for IE using the comment tricks, |
|---|
| 97 |
ruby comment # they will be removed using this function. Please consider for IE css style |
|---|
| 98 |
ruby comment # corrections the usage of conditionals comments in your (X)HTML document. |
|---|
| 99 |
ruby code def remove_comments(script) |
|---|
| 100 |
ruby code input = script |
|---|
| 101 |
ruby code script = '' |
|---|
| 102 |
ruby blank |
|---|
| 103 |
ruby code while input.length > 0 do |
|---|
| 104 |
ruby code pos = input.index("/*"); |
|---|
| 105 |
ruby blank |
|---|
| 106 |
ruby comment # No more comments |
|---|
| 107 |
ruby code if pos == nil |
|---|
| 108 |
ruby code script += input |
|---|
| 109 |
ruby code input = ''; |
|---|
| 110 |
ruby code else # Comment beginning at pos |
|---|
| 111 |
ruby code script += input[0..(pos-1)] if pos > 0 # only append text if there is some |
|---|
| 112 |
ruby code input = input[(pos+2)..-1] |
|---|
| 113 |
ruby comment # Comment ending at pos |
|---|
| 114 |
ruby code pos = input.index("*/") |
|---|
| 115 |
ruby code input = input[(pos+2)..-1] |
|---|
| 116 |
ruby code end |
|---|
| 117 |
ruby code end |
|---|
| 118 |
ruby blank |
|---|
| 119 |
ruby comment # return |
|---|
| 120 |
ruby code script |
|---|
| 121 |
ruby code end |
|---|
| 122 |
ruby blank |
|---|
| 123 |
ruby comment # Remove all newline characters |
|---|
| 124 |
ruby comment # |
|---|
| 125 |
ruby comment # We take care of Windows(\r\n), Unix(\n) and Mac(\r) newlines. |
|---|
| 126 |
ruby code def remove_newlines(script) |
|---|
| 127 |
ruby code script.gsub(/\n|\r/,'') |
|---|
| 128 |
ruby code end |
|---|
| 129 |
ruby blank |
|---|
| 130 |
ruby comment # Remove unneeded spaces |
|---|
| 131 |
ruby comment # |
|---|
| 132 |
ruby comment # 1. Turn mutiple spaces into a single |
|---|
| 133 |
ruby comment # 2. Remove spaces around ;:{}, |
|---|
| 134 |
ruby comment # 3. Remove tabs |
|---|
| 135 |
ruby code def remove_spaces(script) |
|---|
| 136 |
ruby code script = script.gsub(/(\s(\s)+)/, ' ') |
|---|
| 137 |
ruby code script = script.gsub(/\s*;\s*/,';') |
|---|
| 138 |
ruby code script = script.gsub(/\s*:\s*/,':') |
|---|
| 139 |
ruby code script = script.gsub(/\s*\{\s*/,'{') |
|---|
| 140 |
ruby code script = script.gsub(/\s*\}\s*/,'}') |
|---|
| 141 |
ruby code script = script.gsub(/\s*,\s*/,',') |
|---|
| 142 |
ruby code script.gsub("\t",''); |
|---|
| 143 |
ruby code end |
|---|
| 144 |
ruby blank |
|---|
| 145 |
ruby comment # Replace color values with their shorter equivalent |
|---|
| 146 |
ruby comment # |
|---|
| 147 |
ruby comment # 1. Turn rgb(,,)-colors into #-values |
|---|
| 148 |
ruby comment # 2. Shorten #AABBCC down to #ABC |
|---|
| 149 |
ruby comment # 3. Replace names with their shorter hex-equivalent |
|---|
| 150 |
ruby comment # * white -> #fff |
|---|
| 151 |
ruby comment # * black -> #000 |
|---|
| 152 |
ruby comment # 4. Replace #-values with their shorter name |
|---|
| 153 |
ruby comment # * #f00 -> red |
|---|
| 154 |
ruby code def shorten_colors(style) |
|---|
| 155 |
ruby comment # rgb(50,101,152) to #326598 |
|---|
| 156 |
ruby code style = style.gsub(/rgb\s*\(\s*([0-9,\s]+)\s*\)/) do |match| |
|---|
| 157 |
ruby code out = '#' |
|---|
| 158 |
ruby code $1.split(',').each do |num| |
|---|
| 159 |
ruby code if num.to_i < 16 |
|---|
| 160 |
ruby code out += '0' |
|---|
| 161 |
ruby code end |
|---|
| 162 |
ruby code out += num.to_i.to_s(16) # convert to hex |
|---|
| 163 |
ruby code end |
|---|
| 164 |
ruby code out |
|---|
| 165 |
ruby code end |
|---|
| 166 |
ruby comment # Convert #AABBCC to #ABC, keep if preceed by a '=' |
|---|
| 167 |
ruby code style = style.gsub(/([^\"'=\s])(\s*)#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/) do |match| |
|---|
| 168 |
ruby code out = match |
|---|
| 169 |
ruby code if ($3.downcase == $4.downcase) and ($5.downcase == $6.downcase) and ($7.downcase == $8.downcase) |
|---|
| 170 |
ruby code out = $1 + '#' + $3.downcase + $5.downcase + $7.downcase |
|---|
| 171 |
ruby code end |
|---|
| 172 |
ruby code out |
|---|
| 173 |
ruby code end |
|---|
| 174 |
ruby blank |
|---|
| 175 |
ruby code # At the moment we assume that colours only appear before ';' or '}' and |
|---|
| 176 |
ruby code # after a ':', if there could be an occurence of a color before or after |
|---|
| 177 |
ruby code # an other character, submit either a bug report or, better, a patch that |
|---|
| 178 |
ruby code # enables Rainpress to take care of this. |
|---|
| 179 |
ruby blank |
|---|
| 180 |
ruby code # shorten several names to numbers |
|---|
| 181 |
ruby code ## shorten white -> #fff |
|---|
| 182 |
ruby code style = style.gsub(/:[\s]*white[\s]*;/, ':#fff;') |
|---|
| 183 |
ruby code style = style.gsub(/:[\s]*white[\s]*\}/, ':#fff}') |
|---|
| 184 |
ruby code ## shorten black -> #000 |
|---|
| 185 |
ruby code style = style.gsub(/:[\s]*black[\s]*;/, ':#000;') |
|---|
| 186 |
ruby code style = style.gsub(/:[\s]*black[\s]*\}/, ':#000}') |
|---|
| 187 |
ruby code # shotern several numbers to names |
|---|
| 188 |
ruby code ## shorten #f00 or #ff0000 -> red |
|---|
| 189 |
ruby code style = style.gsub(/:[\s]*#([fF]00|[fF]{2}0000);/, ':red;') |
|---|
| 190 |
ruby code style = style.gsub(/:[\s]*#([fF]00|[fF]{2}0000)\}/, ':red}') |
|---|
| 191 |
ruby blank |
|---|
| 192 |
ruby code style |
|---|
| 193 |
ruby code end |
|---|
| 194 |
ruby blank |
|---|
| 195 |
ruby code # Do miscellaneous compression methods on the style. |
|---|
| 196 |
ruby code def do_misc(script) |
|---|
| 197 |
ruby code # Replace 0(pt,px,em,%) with 0 but only when preceded by : or a white-space |
|---|
| 198 |
ruby code script = script.gsub(/([\s:]+)(0)(px|em|%|in|cm|mm|pc|pt|ex)/) do |match| |
|---|
| 199 |
ruby code match.gsub(/(px|em|%|in|cm|mm|pc|pt|ex)/,'') |
|---|
| 200 |
ruby code end |
|---|
| 201 |
ruby code # Replace :0 0 0 0(;|}) with :0(;|}) |
|---|
| 202 |
ruby code script = script.gsub(':0 0 0 0;', ':0;') |
|---|
| 203 |
ruby code script = script.gsub(':0 0 0 0}', ':0}') |
|---|
| 204 |
ruby code # Replace :0 0 0(;|}) with :0(;|}) |
|---|
| 205 |
ruby code script = script.gsub(':0 0 0;', ':0;') |
|---|
| 206 |
ruby code script = script.gsub(':0 0 0}', ':0}') |
|---|
| 207 |
ruby code # Replace :0 0(;|}) with :0(;|}) |
|---|
| 208 |
ruby code script = script.gsub(':0 0}', ':0}') |
|---|
| 209 |
ruby code script = script.gsub(':0 0;', ':0;') |
|---|
| 210 |
ruby code # Replace background-position:0; with background-position:0 0; |
|---|
| 211 |
ruby code script = script.gsub('background-position:0;', 'background-position:0 0;'); |
|---|
| 212 |
ruby code # Replace 0.6 to .6, but only when preceded by : or a white-space |
|---|
| 213 |
ruby code script = script.gsub(/[:\s]0+\.(\d+)/) do |match| |
|---|
| 214 |
ruby code match.sub('0', '') # only first '0' !! |
|---|
| 215 |
ruby code end |
|---|
| 216 |
ruby code # Replace multiple ';' with a single ';' |
|---|
| 217 |
ruby code script = script.gsub(/[;]+/, ';') |
|---|
| 218 |
ruby code # Replace ;} with } |
|---|
| 219 |
ruby code script = script.gsub(';}', '}') |
|---|
| 220 |
ruby code # Replace background-color: with background: |
|---|
| 221 |
ruby code script = script.gsub('background-color:', 'background:') |
|---|
| 222 |
ruby code # Replace font-weight:normal; with 400 |
|---|
| 223 |
ruby code script = script.gsub(/font-weight[\s]*:[\s]*normal[\s]*;/,'font-weight:400;') |
|---|
| 224 |
ruby code script = script.gsub(/font-weight[\s]*:[\s]*normal[\s]*\}/,'font-weight:400}') |
|---|
| 225 |
ruby code script = script.gsub(/font[\s]*:[\s]*normal[\s;\}]*/) do |match| |
|---|
| 226 |
ruby code match.sub('normal', '400') |
|---|
| 227 |
ruby code end |
|---|
| 228 |
ruby code # Replace font-weight:bold; with 700 |
|---|
| 229 |
ruby code script = script.gsub(/font-weight[\s]*:[\s]*bold[\s]*;/,'font-weight:700;') |
|---|
| 230 |
ruby code script = script.gsub(/font-weight[\s]*:[\s]*bold[\s]*\}/,'font-weight:700}') |
|---|
| 231 |
ruby code script = script.gsub(/font[\s]*:[\s]*bold[\s;\}]*/) do |match| |
|---|
| 232 |
ruby code match.sub('bold', '700') |
|---|
| 233 |
ruby code end |
|---|
| 234 |
ruby blank |
|---|
| 235 |
ruby code script |
|---|
| 236 |
ruby code end |
|---|
| 237 |
ruby blank |
|---|
| 238 |
ruby code end |
|---|
| 239 |
ruby blank |
|---|
| 240 |
ruby code end |
|---|