Created
December 4, 2009 03:09
-
-
Save ashrewdmint/248799 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cdn_url(count, path) | |
max = 98 | |
range = 8 | |
number = count / 8 | |
number = (number > max ? max : number).to_s | |
number = number.length == 1 ? '0' + number : number | |
return "http://cdn-static-#{number}.viddler.com/css/#{path}" | |
end | |
def prepare_for_cdn(css) | |
count = 1 | |
css.scan(/\(["']([^"']+){1}["']\)/).uniq.each do |match| | |
url = match.first | |
new_url = cdn_url(count, url) | |
css.gsub!(url, new_url) | |
count += 1 | |
end | |
return css | |
end | |
def compress_css(css) | |
# Remove whitespace | |
css.gsub!(/[\r\t\n]| (\{)|(:) |(,) /) { $1 or $2 or $3 } | |
# Remove comments | |
css.gsub!(Regexp.new('/\*[^*]*\*+([^/][^*]*\*+)*/'), '') | |
# Remove semicolons next to end brackets | |
css.gsub!(/;\}/, '}') | |
return css | |
end | |
def read_files | |
css = ''; | |
# Loop through directory, reading files | |
Dir.foreach(Dir.pwd).each do |file| | |
if file.match(/\.css$/) and file != 'style.css' | |
css += File.read(file) | |
end | |
end | |
return css | |
end | |
def save(css, name) | |
File.open(name, 'w') do |file| | |
file.puts(css) | |
end | |
end | |
compiled = compress_css(read_files()) | |
save(compiled, 'style.css') | |
save(prepare_for_cdn(compiled), 'style.cdn.css') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment