sqwish 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.md +35 -9
  2. data/Rakefile +6 -0
  3. data/lib/sqwish.rb +7 -4
  4. data/sqwish/src/index.js +38 -36
  5. metadata +36 -39
data/README.md CHANGED
@@ -1,21 +1,47 @@
1
1
  # Sqwish-ruby
2
2
  #### A CSS compressor
3
3
 
4
- This as a Ruby bridge to [Sqwish](https://github.com/ded/sqwish) by Dustin
5
- Diaz. Just install it:
4
+ This as a Ruby bridge to [Sqwish](https://github.com/ded/sqwish) by Dustin Diaz
5
+ to compress CSS very efficiently.
6
+
7
+ ### Ruby usage
8
+
9
+ Just install it:
6
10
 
7
11
  $ gem install sqwish
8
12
 
9
- And use it like so:
13
+ Then use it like so:
14
+
15
+ ``` ruby
16
+ require 'sqwish'
17
+
18
+ Sqwish.minify "div { color: red; } div { background: black; }"
19
+ #=> "div{color:red}div{background:black}"
20
+ ````
21
+
22
+ You can also use the `strict: true` flag (it defaults to false):
23
+
24
+ ``` ruby
25
+ Sqwish.minify "div { color: red; } div { background: black; }", strict: true
26
+ #=> "div{color:red;background:black}"
27
+ ```
28
+
29
+ ## Rails usage
30
+
31
+ In your Gemfile:
32
+
33
+ ``` ruby
34
+ gem 'sqwish'
10
35
 
11
- require 'sqwish'
12
- Sqwish.minify "div { color: red; } div { background: black; }"
13
- #=> "div{color:red;background:black}"
36
+ # or optionally:
37
+ gem 'sqwish', github: 'rstacruz/sqwish.rb'
38
+ ```
14
39
 
15
- You can also use the `strict: false` flag (it defaults to true):
40
+ In your `config/environments/production.rb`:
16
41
 
17
- Sqwish.minify "div { color: red; } div { background: black; }", strict: false
18
- #=> "div{color:red}div{background:black}"
42
+ ``` ruby
43
+ config.assets.css_compressor = Sqwish
44
+ ```
19
45
 
20
46
  ## Authors
21
47
 
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ desc "Test"
4
+ task :test do
5
+ require './test/sqwish_test.rb'
6
+ end
data/lib/sqwish.rb CHANGED
@@ -2,16 +2,19 @@ require 'execjs'
2
2
 
3
3
  module Sqwish
4
4
  class << self
5
- def minify(src, options={:strict => true})
6
- sqwish_js.call "sqwish", src, !! options[:strict]
5
+ def minify(src, options={:strict => false})
6
+ is_strict = !! options[:strict]
7
+ sqwish_js.call "sqwish", src, is_strict
7
8
  end
8
9
 
10
+ alias compress minify
11
+
9
12
  def sqwish_js
10
13
  @squish_js ||= ExecJS.compile(sqwish_wrapper)
11
14
  end
12
15
 
13
16
  def sqwish_wrapper
14
- # If we're not using node, some things will have to be stubbed.
17
+ # Since we're not using node, some things will have to be stubbed.
15
18
  %{
16
19
  var require = function() { return {}; };
17
20
  var module = { exports: {} };
@@ -28,7 +31,7 @@ module Sqwish
28
31
  end
29
32
 
30
33
  def version
31
- "0.1.2"
34
+ "0.2.0"
32
35
  end
33
36
  end
34
37
  end
data/sqwish/src/index.js CHANGED
@@ -13,12 +13,12 @@ function uniq(ar) {
13
13
  for (i = ar.length - 1; i >= 0; i--) {
14
14
  for (j = a.length - 1; j >= 0; j--) {
15
15
  if (a[j] == ar[i]) {
16
- continue label;
16
+ continue label
17
17
  }
18
18
  }
19
- a[a.length] = ar[i];
19
+ a[a.length] = ar[i]
20
20
  }
21
- return a;
21
+ return a
22
22
  }
23
23
 
24
24
  function sqwish(css, strict) {
@@ -50,24 +50,24 @@ function sqwish(css, strict) {
50
50
  .replace(/#([a-fA-F0-9])\1([a-fA-F0-9])\2([a-fA-F0-9])\3/g, '#$1$2$3')
51
51
 
52
52
  // replace longhand values with shorthand '5px 5px 5px 5px' => '5px'
53
- .replace(/(\d+[a-z]{2}) \1 \1 \1/gi, '$1')
53
+ .replace(/\b(\d+[a-z]{2}) \1 \1 \1/gi, '$1')
54
54
 
55
55
  // replace double-specified longhand values with shorthand '5px 2px 5px 2px' => '5px 2px'
56
- .replace(/(\d+[a-z]{2}) (\d+[a-z]{2}) \1 \2/gi, '$1 $2')
56
+ .replace(/\b(\d+[a-z]{2}) (\d+[a-z]{2}) \1 \2/gi, '$1 $2')
57
57
 
58
58
  // replace 0px with 0
59
- .replace(/([\s|:])[0]+px/g, '$10');
59
+ .replace(/([\s|:])[0]+px/g, '$10')
60
60
 
61
61
  if (strict) {
62
- css = strict_css(css);
62
+ css = strict_css(css)
63
63
  }
64
64
 
65
65
  // put back in copyrights
66
66
  if (comments) {
67
- comments = comments ? comments.join('\n') : '';
68
- css = comments + '\n' + css;
67
+ comments = comments ? comments.join('\n') : ''
68
+ css = comments + '\n' + css
69
69
  }
70
- return css;
70
+ return css
71
71
  }
72
72
 
73
73
  function strict_css(css) {
@@ -75,59 +75,61 @@ function strict_css(css) {
75
75
  // into combined rules
76
76
 
77
77
  // store global dict of all rules
78
- var ruleList = {},
79
- rules = css.match(/([^{]+\{[^}]+\})+?/g);
78
+ var ruleList = {}
79
+ , rules = css.match(/([^{]+\{[^}]+\})+?/g)
80
80
 
81
81
  // lets find the dups
82
82
  rules.forEach(function (rule) {
83
83
  // break rule into selector|declaration parts
84
- var parts = rule.match(/([^{]+)\{([^}]+)/),
85
- selector = parts[1],
86
- declarations = parts[2];
84
+ var parts = rule.match(/([^{]+)\{([^}]+)/)
85
+ , selector = parts[1]
86
+ , declarations = parts[2]
87
87
 
88
88
  // start new list if it wasn't created already
89
89
  if (!ruleList[selector]) {
90
- ruleList[selector] = [];
90
+ ruleList[selector] = []
91
91
  }
92
92
 
93
- declarations = declarations.split(';');
93
+ declarations = declarations.split(';')
94
94
  // filter out duplicate properties
95
95
  ruleList[selector] = ruleList[selector].filter(function (decl) {
96
- var prop = decl.match(/[^:]+/)[0];
96
+ var prop = decl.match(/[^:]+/)[0]
97
97
  // pre-existing properties are not wanted anymore
98
98
  return !declarations.some(function (dec) {
99
99
  // must include '^' as to not confuse "color" with "border-color" etc.
100
- return dec.match(new RegExp('^' + prop + ':'));
101
- });
102
- });
100
+ return dec.match(new RegExp('^' + prop + ':'))
101
+ })
102
+ })
103
103
 
104
104
  // latter takes presedence :)
105
105
  ruleList[selector] = ruleList[selector].concat(declarations);
106
106
  // still dups? just in case
107
- ruleList[selector] = uniq(ruleList[selector]);
108
- });
107
+ ruleList[selector] = uniq(ruleList[selector])
108
+ })
109
109
 
110
110
  // reset css because we're gonna recreate the whole shabang.
111
- css = '';
111
+ css = ''
112
112
  for (var selector in ruleList) {
113
- var joinedRuleList = ruleList[selector].join(';');
114
- css += selector + '{' + (joinedRuleList).replace(/;$/, '') + '}';
113
+ var joinedRuleList = ruleList[selector].join(';')
114
+ css += selector + '{' + (joinedRuleList).replace(/;$/, '') + '}'
115
115
  }
116
- return css;
116
+ return css
117
117
  }
118
118
 
119
119
  module.exports.exec = function (args) {
120
- var out;
121
- var read = args[0];
120
+ var out, data
121
+ , read = args[0]
122
122
  if (out = args.indexOf('-o') != -1) {
123
- out = args[out + 1];
123
+ out = args[out + 1]
124
124
  } else {
125
- out = read.replace(/\.css$/, '.min.css');
125
+ out = read.replace(/\.css$/, '.min.css')
126
126
  }
127
- console.log('compressing ' + read + ' to ' + out + '...');
128
- var data = fs.readFileSync(read, 'utf8');
129
- fs.writeFileSync(out, sqwish(data, (args.indexOf('--strict') != -1)), 'utf8');
127
+ if (args.indexOf('-v') != -1) {
128
+ console.log('compressing ' + read + ' to ' + out + '...')
129
+ }
130
+ data = fs.readFileSync(read, 'utf8')
131
+ fs.writeFileSync(out, sqwish(data, (~args.indexOf('--strict'))), 'utf8')
130
132
  };
131
133
  module.exports.minify = function (css, strict) {
132
- return sqwish(css, strict);
133
- };
134
+ return sqwish(css, strict)
135
+ };
metadata CHANGED
@@ -1,71 +1,68 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sqwish
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
4
5
  prerelease:
5
- version: 0.1.2
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Rico Sta. Cruz
9
9
  - Dustin Diaz
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
-
14
- date: 2011-05-05 00:00:00 +08:00
15
- default_executable:
16
- dependencies:
17
- - !ruby/object:Gem::Dependency
13
+ date: 2012-07-18 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
18
16
  name: execjs
19
- prerelease: false
20
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
21
18
  none: false
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: "0"
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
26
23
  type: :runtime
27
- version_requirements: *id001
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
28
31
  description: Compresses CSS.
29
- email:
32
+ email:
30
33
  - rico@sinefunc.com
31
34
  executables: []
32
-
33
35
  extensions: []
34
-
35
36
  extra_rdoc_files: []
36
-
37
- files:
37
+ files:
38
38
  - lib/sqwish.rb
39
39
  - sqwish/src/index.js
40
40
  - HISTORY.md
41
41
  - README.md
42
- has_rdoc: true
43
- homepage: http://github.com/rstacruz/sqwish
42
+ - Rakefile
43
+ homepage: http://github.com/rstacruz/sqwish.rb
44
44
  licenses: []
45
-
46
45
  post_install_message:
47
46
  rdoc_options: []
48
-
49
- require_paths:
47
+ require_paths:
50
48
  - lib
51
- required_ruby_version: !ruby/object:Gem::Requirement
49
+ required_ruby_version: !ruby/object:Gem::Requirement
52
50
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: "0"
57
- required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
56
  none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: "0"
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
63
61
  requirements: []
64
-
65
62
  rubyforge_project:
66
- rubygems_version: 1.6.2
63
+ rubygems_version: 1.8.23
67
64
  signing_key:
68
65
  specification_version: 3
69
66
  summary: A node-based CSS compressor
70
67
  test_files: []
71
-
68
+ has_rdoc: