Skip to content

Commit 1479141

Browse files
committed
initial commit
0 parents  commit 1479141

21 files changed

+2819
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.rvmrc
2+
*.gem
3+
.bundle
4+
*.rbc
5+
.yardoc
6+
Gemfile.lock

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "lib/less/js"]
2+
path = lib/less/js
3+
url = https://github.com/cloudhead/less.js.git

.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--color

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source :rubygems
2+
3+
gemspec

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
csso-rails: Stylesheet Optimizer(CSSO) for Rails Asset pipeline
2+
=======
3+
4+
Ruby adapter for <https://github.com/afelix/csso>
5+
6+
about
7+
-----
8+
CSSO does structure-optimization for css.
9+
(readme and tests/comparison - coming later)
10+
Css is usually reduced more that in half in uncompressed and around 15% in gzipped.
11+
12+
usage
13+
------
14+
15+
From ruby:
16+
require 'csso'
17+
Csso.optimize("a{ color: #FF0000; }")
18+
# produces "a{color:red}"
19+
20+
In Rails 3.1+:
21+
add this gem to your gemfile, and that's it!
22+
gem 'csso-rails', :git => ''
23+
Upon including it becomes the default compressor,
24+
More explicit way - set in config: config.assets.css_compressor = :csso
25+
26+
In command line:
27+
ruby_csso non_optimized.css > optimized.css
28+
29+
30+
MIT-License
31+
-------
32+
33+
> Original CSSO code - Copyright (C) 2011 by Sergey Kryzhanovsky
34+
> ruby gem - Copyright(C) 2012 Vasily Fedoseyev
35+
36+
Permission is hereby granted, free of charge, to any person obtaining a copy
37+
of this software and associated documentation files (the "Software"), to deal
38+
in the Software without restriction, including without limitation the rights
39+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40+
copies of the Software, and to permit persons to whom the Software is
41+
furnished to do so, subject to the following conditions:
42+
43+
The above copyright notice and this permission notice shall be included in
44+
all copies or substantial portions of the Software.
45+
46+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
52+
THE SOFTWARE.

Rakefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'bundler'
2+
require 'bundler/setup'
3+
require "rspec/core/rake_task"
4+
5+
Bundler::GemHelper.install_tasks
6+
RSpec::Core::RakeTask.new(:spec)
7+
8+
task :default => :spec
9+

bin/ruby_csso

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'csso-rails'
4+
5+
def help
6+
puts <<-HLP
7+
csso- CSS Optimizer (ruby bindings by vasfed)
8+
Usage:
9+
$ ruby_csso < style.css - read stdin and output to stdout
10+
$ ruby_csso file1.css [file2.css [...]] - read all files and output to stdout (concatenating, no optimization between files yet)
11+
--help - print this help
12+
--maniac - enable maniac mode (optimize file multiple times until optimization stops to give any results)
13+
HLP
14+
end
15+
16+
maniac = ARGV.delete('--maniac') && true || false
17+
18+
if ($stdin.tty? && ARGV.empty?) || ARGV.delete('-h') || ARGV.delete('--help')
19+
help
20+
else
21+
# Well, lets read those files
22+
css = ARGF.read
23+
24+
puts Csso.optimize(css, maniac)
25+
end

csso-rails.gemspec

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# -*- encoding: utf-8 -*-
2+
$:.push File.expand_path("../lib", __FILE__)
3+
require "csso/version"
4+
5+
Gem::Specification.new do |s|
6+
s.name = "csso-rails"
7+
s.version = Csso::VERSION
8+
s.platform = Gem::Platform::RUBY
9+
s.authors = ["Vasily Fedoseyev"]
10+
s.email = [""]
11+
s.homepage = "https://github.com/Vasfed/csso-rails"
12+
s.summary = %q{CSS Stylesheet optimizer/compressor for Rails}
13+
s.description = %q{Invoke the CSSO from Ruby}
14+
15+
s.rubyforge_project = "csso-rails"
16+
17+
s.files = `git ls-files`.split("\n")
18+
js_root = 'lib/csso/js'
19+
Dir.chdir(js_root) do
20+
s.files += `git ls-files`.split("\n").map {|f| File.join(js_root,f)}
21+
end
22+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24+
s.require_paths = ["lib"]
25+
26+
s.add_dependency "therubyracer", "~> 0.9.9"
27+
s.add_dependency "commonjs", "~> 0.2.0"
28+
29+
s.add_development_dependency "rake"
30+
s.add_development_dependency "rspec", "~> 2.0"
31+
end

lib/csso-rails.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'csso'
2+
3+
if defined?(Rails)
4+
require 'csso/rails'
5+
else
6+
require 'csso'
7+
end

lib/csso.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'v8'
2+
require 'csso/version'
3+
require 'csso/utils'
4+
require 'csso/loader'
5+
6+
module Csso
7+
8+
@loader = Csso::Loader.new
9+
@csso = @loader.require('cssoapi')
10+
11+
def self.js_api
12+
@csso
13+
end
14+
15+
def self.optimize(css, maniac_mode=false, structural_optimization=true)
16+
if maniac_mode
17+
maniac_mode = 4 unless maniac_mode.is_a?(Fixnum) && maniac_mode > 0
18+
begin
19+
prev_css = css
20+
css = Optimizer.new.optimize(css, structural_optimization)
21+
maniac_mode -= 1
22+
end while maniac_mode > 0 && prev_css != css
23+
css
24+
else
25+
Optimizer.new.optimize(css, structural_optimization)
26+
end
27+
end
28+
29+
30+
class Optimizer
31+
include CallJS
32+
33+
def optimize(css, structural_optimization=true)
34+
return nil unless css.is_a?(String)
35+
return css if css.size <= 3
36+
calljs do
37+
Csso.js_api['justDoIt'].call(css, !structural_optimization)
38+
end
39+
end
40+
end
41+
42+
end

0 commit comments

Comments
 (0)