multi_css 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.gitmodules ADDED
@@ -0,0 +1,3 @@
1
+ [submodule "vendor/cssminify"]
2
+ path = vendor/cssminify
3
+ url = https://github.com/matthiassiegel/cssminify.git
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - rbx-18mode
4
+ - rbx-19mode
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - 1.8.7
8
+ - 1.9.2
9
+ - 1.9.3
10
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'css_press', :require => nil
4
+ gem 'yuicssmin', :require => nil
5
+ gem 'cssminify', :require => nil
6
+ gem 'rainpress', :require => nil
7
+ # gem 'yui-compressor', :require => nil, :platforms => [:ruby, :mingw, :mswin]
8
+
9
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 sterebooster
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # MultiCss [![Build Status](https://secure.travis-ci.org/stereobooster/multi_css.png?branch=master)](https://secure.travis-ci.org/#!/stereobooster/multi_css) [![Dependency Status](https://gemnasium.com/stereobooster/multi_css.png?travis)](https://gemnasium.com/stereobooster/multi_css) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/stereobooster/multi_css)
2
+
3
+ There are lot of Ruby libraries to minify CSS.
4
+ Instead of choosing a single implementation and forcing users of your library to be
5
+ stuck with it, you can use MultiCss instead, which will simply choose the
6
+ best available CSS minifier. Here's how to use it:
7
+
8
+ ```ruby
9
+ require 'multi_css'
10
+
11
+ MultiCss.min('a { color: red; }') #=> 'a{color:red}'
12
+ ```
13
+
14
+ The `use` method, which sets the MultiCss adapter, takes either a symbol or a
15
+ class (to allow for custom CSS minifier) that responds to `.min` at the class level.
16
+
17
+ MultiCss tries to have intelligent defaulting. That is, if you have any of the
18
+ supported engines already loaded, it will utilize them before attempting to
19
+ load any.
20
+ It will load libraries in following order:
21
+
22
+ - [css_press](https://github.com/stereobooster/css_press)
23
+ - [cssminify](https://github.com/matthiassiegel/cssminify)
24
+ - [yuicssmin](https://github.com/matthiassiegel/yuicssmin)
25
+ - [ruby-yui-compressor](https://github.com/sstephenson/ruby-yui-compressor). Doesn't test with travis-ci
26
+ - [rainpress](https://github.com/sprsquish/rainpress)
27
+
28
+ If no other library is available, MultiCss falls back to vendored [cssminify](https://github.com/matthiassiegel/cssminify), a simple and reliable library.
29
+
30
+ ## TODO
31
+
32
+ - benchmark against most popular css frameworks (speed, size)
33
+ - generate benchmark report (html or md)
34
+ - write test suite
35
+ - generate comparison table (html or md) with the help of test suite
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ desc "Run all examples"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+ task :test => :spec
10
+
11
+ namespace :doc do
12
+ require 'rdoc/task'
13
+ require File.expand_path('../lib/multi_css/version', __FILE__)
14
+ RDoc::Task.new do |rdoc|
15
+ rdoc.rdoc_dir = 'rdoc'
16
+ rdoc.title = "multi_css #{MultiCss::VERSION}"
17
+ rdoc.main = 'README.md'
18
+ rdoc.rdoc_files.include('README.md', 'LICENSE.md', 'lib/**/*.rb')
19
+ end
20
+ end
data/lib/multi_css.rb ADDED
@@ -0,0 +1,119 @@
1
+ require "multi_css/version"
2
+
3
+ module MultiCss
4
+ class ParseError < StandardError
5
+ def initialize(message="", backtrace=[])
6
+ super(message)
7
+ self.set_backtrace(backtrace)
8
+ end
9
+ end
10
+
11
+ @adapter = nil
12
+
13
+ REQUIREMENT_MAP = [
14
+ ["css_press", :css_press],
15
+ ["cssminify", :cssminify],
16
+ ["yuicssmin", :yuicssmin],
17
+ ["yui/compressor", :yui_compressor],
18
+ ["rainpress", :rainpress]
19
+ ]
20
+
21
+ class << self
22
+
23
+ # The default adapter based on what you currently
24
+ # have loaded and installed. First checks to see
25
+ # if any adapters are already loaded, then checks
26
+ # to see which are installed if none are loaded.
27
+ def default_adapter
28
+ return :css_press if defined?(::CssPress)
29
+ return :cssminify if defined?(::CSSminify)
30
+ return :yuicssmin if defined?(::Yuicssmin)
31
+ return :yui_compressor if defined?(::YUI::CssCompressor)
32
+ return :rainpress if defined?(::Rainpress)
33
+
34
+ REQUIREMENT_MAP.each do |(library, adapter)|
35
+ begin
36
+ require library
37
+ return adapter
38
+ rescue LoadError
39
+ next
40
+ end
41
+ end
42
+
43
+ Kernel.warn "[WARNING] MultiCss is using the default adapter."
44
+ :vendored
45
+ end
46
+ # :nodoc:
47
+ alias :default_engine :default_adapter
48
+
49
+ # Get the current adapter class.
50
+ def adapter
51
+ return @adapter if @adapter
52
+ self.use self.default_adapter
53
+ @adapter
54
+ end
55
+ # :nodoc:
56
+ alias :engine :adapter
57
+
58
+ # Set the adapter utilizing a symbol, string, or class.
59
+ # Supported by default are:
60
+ #
61
+ # * <tt>:css_press</tt>
62
+ # * <tt>:cssminify</tt>
63
+ # * <tt>:yuicssmin</tt>
64
+ # * <tt>:yui_compressor</tt>
65
+ # * <tt>:rainpress</tt>
66
+ def use(new_adapter)
67
+ @adapter = load_adapter(new_adapter)
68
+ end
69
+ alias :adapter= :use
70
+ # :nodoc:
71
+ alias :engine= :use
72
+
73
+ def load_adapter(new_adapter)
74
+ case new_adapter
75
+ when String, Symbol
76
+ require "multi_css/adapters/#{new_adapter}"
77
+ self::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
78
+ when NilClass, FalseClass
79
+ default_adapter = self.default_adapter
80
+ require "multi_css/adapters/#{default_adapter}"
81
+ self::Adapters.const_get(:"#{default_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
82
+ when Class
83
+ new_adapter
84
+ else
85
+ raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
86
+ end
87
+ end
88
+
89
+ def current_adapter(options)
90
+ if new_adapter = (options || {}).delete(:adapter)
91
+ load_adapter(new_adapter)
92
+ else
93
+ adapter
94
+ end
95
+ end
96
+
97
+ # Minify CSS
98
+ def min(string, options={})
99
+ string = string.read if string.respond_to?(:read)
100
+
101
+ adapter = current_adapter(options)
102
+ if defined?(adapter::ParseError)
103
+ begin
104
+ adapter.min(string, options)
105
+ rescue adapter::ParseError => exception
106
+ raise ::MultiCss::ParseError.new(exception.message, exception.backtrace)
107
+ end
108
+ else
109
+ adapter.min(string, options)
110
+ end
111
+ end
112
+
113
+ # Minify partial CSS (as in style attribute)
114
+ def min_attr(string, options={})
115
+ min("a{#{string}}", options).gsub(/^a\{|\}$/, '')
116
+ end
117
+ end
118
+
119
+ end
@@ -0,0 +1,13 @@
1
+ require 'css_press' unless defined?(::CssPress)
2
+
3
+ module MultiCss
4
+ module Adapters
5
+ class CssPress
6
+ ParseError = ::Racc::ParseError
7
+
8
+ def self.min(text, options={}) #:nodoc:
9
+ ::CssPress.press(text, options)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ require 'cssminify' unless defined?(::CSSminify)
2
+
3
+ module MultiCss
4
+ module Adapters
5
+ class Cssminify
6
+ def self.min(text, options={}) #:nodoc:
7
+ length = options[:length] || 5000
8
+ ::CSSminify.compress(text, length)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'rainpress' unless defined?(::Rainpress)
2
+
3
+ module MultiCss
4
+ module Adapters
5
+ class Rainpress
6
+ def self.min(text, options={}) #:nodoc:
7
+ ::Rainpress.compress text
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../../../vendor/cssminify/lib/cssminify', __FILE__)
2
+ require 'cssminify'
3
+
4
+ module MultiCss
5
+ module Adapters
6
+ class Vendored < Cssminify
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ require 'yui/compressor' unless defined?(::YUI::CssCompressor)
2
+
3
+ module MultiCss
4
+ module Adapters
5
+ class YuiCompressor
6
+ def self.min(text, options={}) #:nodoc:
7
+ compressor = ::YUI::CssCompressor.new options
8
+ compressor.compress text
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ require 'yuicssmin' unless defined?(::Yuicssmin)
2
+
3
+ module MultiCss
4
+ module Adapters
5
+ class Yuicssmin
6
+ ParseError = ::ExecJS::RuntimeError
7
+
8
+ def self.min(text, options={}) #:nodoc:
9
+ length = options[:length] || 5000
10
+ ::Yuicssmin.compress(text, length)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module MultiCss
2
+ VERSION = "0.0.3"
3
+ end
data/multi_css.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_css/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "multi_css"
8
+ gem.version = MultiCss::VERSION
9
+ gem.authors = ["sterebooster"]
10
+ gem.email = ["stereobooster@gmail.com"]
11
+ gem.description = %q{A generic swappable back-end for CSS minification.}
12
+ gem.summary = %q{A generic swappable back-end for CSS minification.}
13
+ gem.homepage = "https://github.com/stereobooster/multi_css"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.files += `cd vendor/cssminify && git ls-files`.split($/).map{|f| 'vendor/cssminify/' + f}
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rdoc'
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'simplecov'
26
+ end
@@ -0,0 +1,27 @@
1
+ shared_examples_for "an adapter" do |adapter|
2
+
3
+ before do
4
+ begin
5
+ MultiCss.use adapter
6
+ rescue LoadError
7
+ pending "Adapter #{adapter} couldn't be loaded (not installed?)"
8
+ end
9
+ end
10
+
11
+ describe '.min' do
12
+ it 'minify' do
13
+ MultiCss.min("a { color: red;\n background: red ; } ").should eq 'a{color:red;background:red}'
14
+ MultiCss.min_attr(" color: red;\n background: red ; ").should eq 'color:red;background:red'
15
+ end
16
+
17
+ if adapter == 'css_press'
18
+ it 'throws exception if parse error occurred' do
19
+ lambda { MultiCss.min('a{b:') }.should raise_error MultiCss::ParseError
20
+ end
21
+ else
22
+ it 'doesn\'t throw exception if parse error occurred' do
23
+ lambda { MultiCss.min('a{b:') }.should_not raise_error
24
+ end
25
+ end
26
+ end
27
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,23 @@
1
+ def jruby?
2
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
3
+ end
4
+
5
+ def macruby?
6
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'macruby'
7
+ end
8
+
9
+ unless ENV['CI'] || macruby?
10
+ require 'simplecov'
11
+ SimpleCov.start do
12
+ add_filter 'spec'
13
+ end
14
+ end
15
+
16
+ require 'multi_css'
17
+ require 'rspec'
18
+
19
+ class MockDecoder
20
+ def self.min(string, options={})
21
+ 'a{color:red}'
22
+ end
23
+ end
@@ -0,0 +1,72 @@
1
+ require 'helper'
2
+ require 'adapter_shared_example'
3
+ require 'stringio'
4
+
5
+ describe 'MultiCss' do
6
+ context 'adapters' do
7
+ before do
8
+ MultiCss.use nil
9
+ end
10
+ context 'when no other implementations are available' do
11
+
12
+ CONSTS = [:CssPress, :CSSminify, :Yuicssmin, :YUI, :Rainpress]
13
+
14
+ before do
15
+ @old_map = MultiCss::REQUIREMENT_MAP
16
+ @old_consts = {}
17
+ CONSTS.each do |c|
18
+ @old_consts[c] = Object.const_get c if Object.const_defined?(c)
19
+ end
20
+
21
+ MultiCss::REQUIREMENT_MAP.each_with_index do |(library, adapter), index|
22
+ MultiCss::REQUIREMENT_MAP[index] = ["foo/#{library}", adapter]
23
+ end
24
+
25
+ CONSTS.each do |c|
26
+ Object.send :remove_const, c if @old_consts[c]
27
+ end
28
+ end
29
+
30
+ after do
31
+ @old_map.each_with_index do |(library, adapter), index|
32
+ MultiCss::REQUIREMENT_MAP[index] = [library, adapter]
33
+ end
34
+
35
+ CONSTS.each do |c|
36
+ Object.const_set c, @old_consts[c] if @old_consts[c]
37
+ end
38
+ end
39
+
40
+ it 'defaults to vendored implemention if no other implementions are available' do
41
+ MultiCss.default_adapter.should eq :vendored
42
+ end
43
+
44
+ it 'prints a warning' do
45
+ Kernel.should_receive(:warn).with(/warning/i)
46
+ MultiCss.default_adapter
47
+ end
48
+ end
49
+
50
+ it 'defaults to the best available gem' do
51
+ require 'css_press'
52
+ MultiCss.adapter.name.should eq 'MultiCss::Adapters::CssPress'
53
+ end
54
+
55
+ it 'is settable via a symbol' do
56
+ MultiCss.use :cssminify
57
+ MultiCss.adapter.name.should eq 'MultiCss::Adapters::Cssminify'
58
+ end
59
+
60
+ it 'is settable via a class' do
61
+ MultiCss.use MockDecoder
62
+ MultiCss.adapter.name.should eq 'MockDecoder'
63
+ end
64
+ end
65
+
66
+ %w(css_press cssminify yuicssmin rainpress vendored).each do |adapter|
67
+ context adapter do
68
+ it_behaves_like "an adapter", adapter
69
+ end
70
+ end
71
+ end
72
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multi_css
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -82,6 +82,25 @@ executables: []
82
82
  extensions: []
83
83
  extra_rdoc_files: []
84
84
  files:
85
+ - .gitignore
86
+ - .gitmodules
87
+ - .travis.yml
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - lib/multi_css.rb
93
+ - lib/multi_css/adapters/css_press.rb
94
+ - lib/multi_css/adapters/cssminify.rb
95
+ - lib/multi_css/adapters/rainpress.rb
96
+ - lib/multi_css/adapters/vendored.rb
97
+ - lib/multi_css/adapters/yui_compressor.rb
98
+ - lib/multi_css/adapters/yuicssmin.rb
99
+ - lib/multi_css/version.rb
100
+ - multi_css.gemspec
101
+ - spec/adapter_shared_example.rb
102
+ - spec/helper.rb
103
+ - spec/multi_css_spec.rb
85
104
  - vendor/cssminify/.gitignore
86
105
  - vendor/cssminify/.travis.yml
87
106
  - vendor/cssminify/CHANGES.md
@@ -204,7 +223,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
204
223
  version: '0'
205
224
  segments:
206
225
  - 0
207
- hash: 564547919
226
+ hash: -293339025
208
227
  required_rubygems_version: !ruby/object:Gem::Requirement
209
228
  none: false
210
229
  requirements:
@@ -213,11 +232,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
232
  version: '0'
214
233
  segments:
215
234
  - 0
216
- hash: 564547919
235
+ hash: -293339025
217
236
  requirements: []
218
237
  rubyforge_project:
219
238
  rubygems_version: 1.8.24
220
239
  signing_key:
221
240
  specification_version: 3
222
241
  summary: A generic swappable back-end for CSS minification.
223
- test_files: []
242
+ test_files:
243
+ - spec/adapter_shared_example.rb
244
+ - spec/helper.rb
245
+ - spec/multi_css_spec.rb