Skip to content

Instantly share code, notes, and snippets.

@ashrewdmint
Created December 4, 2009 03:09
Show Gist options
  • Save ashrewdmint/248799 to your computer and use it in GitHub Desktop.
Save ashrewdmint/248799 to your computer and use it in GitHub Desktop.
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