htmlcompressor 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92b6265bccddfa29250def7fd2807fb2335347cf
4
+ data.tar.gz: 924523a9f8eb60ce59fdd4986ae2e1393bfd183e
5
+ SHA512:
6
+ metadata.gz: 974bd79294afdb72d616d06bc4f55fc5838cf76733eb894bd22c9a5751fdb1c84c6e5472862fabae249d3fcee6aceacc2b794317fb0ae9d3f0d0a983467e0129
7
+ data.tar.gz: 22cdb3b24b803a1557180151c09abdef893175a073773082ffb67356b890633b5c42bebafbdae3e78d3c56379f561798e4f9e892aed78e7043836a404eca2b77
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - 1.8.7
9
+ before_install:
10
+ - gem install bundler
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Htmlcompressor
2
+ [![Build Status](https://travis-ci.org/paolochiodi/htmlcompressor.svg?branch=master)](https://travis-ci.org/paolochiodi/htmlcompressor)
2
3
 
3
4
  ## Put your html on a diet
4
5
 
@@ -79,6 +80,57 @@ Rails 2.3 users may need to add
79
80
  require 'htmlcompressor'
80
81
  ```
81
82
 
83
+ ## CSS and JavaScript Compression
84
+
85
+ By default CSS/JS compression is disabled.
86
+ In order to minify in page javascript and css, you need to supply a compressor in the options hash.
87
+ A compressor can be `:yui` or `:closure` or any object that responds to `:compress`. E.g.: `compressed = compressor.compress(source)`
88
+
89
+ ```ruby
90
+
91
+ class MyCompressor
92
+
93
+ def compress(source)
94
+ return 'minified'
95
+ end
96
+
97
+ end
98
+
99
+ options = {
100
+ :compress_css => true,
101
+ :css_compressor => MyCompressor.new,
102
+ :compress_javascript => true,
103
+ :javascript_compressor => MyCompressor.new
104
+ }
105
+
106
+ ```
107
+
108
+ Please note that in order to use yui or closure compilers you need to manually add them to the Gemfile
109
+
110
+ ```ruby
111
+ gem 'yui-compressor'
112
+
113
+ ...
114
+
115
+ options = {
116
+ :compress_javscript => true,
117
+ :javascript_compressor => :yui,
118
+ :compress_css => true
119
+ :css_compressor => :yui
120
+ }
121
+ ```
122
+
123
+ ```ruby
124
+ gem 'closure-compiler'
125
+
126
+ ...
127
+
128
+ options = {
129
+ :compress_javascript => true,
130
+ :javascript_compressor => :closure
131
+ }
132
+ ```
133
+
82
134
  ## Statistics
83
135
 
84
136
  As of now the statistic framework hasn't been ported. Refer to original [htmlcompressor documentation](http://code.google.com/p/htmlcompressor/) for statistics on minified pages.
data/Rakefile CHANGED
@@ -1,2 +1,20 @@
1
1
  #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+
10
+ require 'rake/testtask'
11
+
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = false
17
+ end
18
+
19
+
20
+ task :default => :test
@@ -8,10 +8,10 @@ Gem::Specification.new do |gem|
8
8
  gem.summary = %q{htmlcompressor provides a class and a rack middleware to minify html pages}
9
9
  gem.homepage = ""
10
10
 
11
- gem.add_dependency 'yui-compressor', '~> 0.9.6'
12
- gem.add_development_dependency 'closure-compiler', '~> 1.1.5'
11
+ gem.add_development_dependency 'yui-compressor', '~> 0.9'
12
+ gem.add_development_dependency 'closure-compiler', '~> 1.1'
13
13
  gem.add_development_dependency 'rake'
14
- gem.add_development_dependency 'minitest'
14
+ gem.add_development_dependency 'minitest', '~> 5.0'
15
15
 
16
16
  gem.files = `git ls-files`.split($\)
17
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -1,4 +1,4 @@
1
- require "yui/compressor"
1
+ require "htmlcompressor/exceptions"
2
2
 
3
3
  module HtmlCompressor
4
4
  class Compressor
@@ -92,6 +92,15 @@ module HtmlCompressor
92
92
  TEMP_SKIP_PATTERN = Regexp.new("%%%~COMPRESS~SKIP~(\\d+?)~%%%")
93
93
  TEMP_LINE_BREAK_PATTERN = Regexp.new("%%%~COMPRESS~LT~(\\d+?)~%%%")
94
94
 
95
+ JAVASCRIPT_COMPRESSORS_OPTIONS = {
96
+ :closure => { :compilation_level => 'ADVANCED_OPTIMIZATIONS' },
97
+ :yui => { :munge => true, :preserve_semicolons => true, :optimize => true, :line_break => nil }
98
+ }
99
+
100
+ CSS_COMPRESSORS_OPTIONS = {
101
+ :yui => { :line_break => -1 }
102
+ }
103
+
95
104
  DEFAULT_OPTIONS = {
96
105
  :enabled => true,
97
106
 
@@ -100,6 +109,8 @@ module HtmlCompressor
100
109
  :remove_multi_spaces => true,
101
110
 
102
111
  # optional settings
112
+ :javascript_compressor => :yui,
113
+ :css_compressor => :yui,
103
114
  :remove_intertag_spaces => false,
104
115
  :remove_quotes => false,
105
116
  :compress_javascript => false,
@@ -126,11 +137,42 @@ module HtmlCompressor
126
137
 
127
138
  @options = DEFAULT_OPTIONS.merge(options)
128
139
 
129
- # YUICompressor settings
130
- @yuiCssLineBreak = -1
131
- @yuiJsNoMunge = false
132
- @yuiJsPreserveAllSemiColons = false
133
- @yuiJsDisableOptimizations = false
140
+ detect_external_compressors
141
+ end
142
+
143
+ def detect_external_compressors
144
+ @javascript_compressors = {}
145
+ @css_compressors = {}
146
+
147
+ # Try Closure.
148
+ begin
149
+ require 'closure-compiler'
150
+ @javascript_compressors[:closure] = Closure::Compiler
151
+ rescue LoadError
152
+ end
153
+
154
+ # Try YUI
155
+ begin
156
+ require 'yui/compressor'
157
+ @javascript_compressors[:yui] = YUI::JavaScriptCompressor
158
+ @css_compressors[:yui] = YUI::CssCompressor
159
+ rescue LoadError
160
+ end
161
+ end
162
+
163
+ def get_javascript_compressor(compressor_name)
164
+
165
+ if @javascript_compressors.has_key? compressor_name
166
+ @javascript_compressors[compressor_name].new JAVASCRIPT_COMPRESSORS_OPTIONS[compressor_name]
167
+ end
168
+
169
+ end
170
+
171
+ def get_css_compressor(compressor_name)
172
+
173
+ if @css_compressors.has_key? compressor_name
174
+ @css_compressors[compressor_name].new CSS_COMPRESSORS_OPTIONS[compressor_name]
175
+ end
134
176
 
135
177
  end
136
178
 
@@ -465,15 +507,14 @@ module HtmlCompressor
465
507
 
466
508
  def compress_javascript(source)
467
509
  # set default javascript compressor
468
- javaScriptCompressor = @options[:javascript_compressor]
510
+ javascript_compressor = @options[:javascript_compressor]
511
+
512
+ if javascript_compressor.is_a?(Symbol)
513
+ javascript_compressor = get_javascript_compressor(javascript_compressor)
514
+ end
469
515
 
470
- if javaScriptCompressor.nil?
471
- javaScriptCompressor = YUI::JavaScriptCompressor.new(
472
- :munge => !@yuiJsNoMunge,
473
- :preserve_semicolons => !@yuiJsDisableOptimizations,
474
- :optimize => !@yuiJsDisableOptimizations,
475
- :line_break => @yuiJsLineBreak
476
- )
516
+ if javascript_compressor.nil?
517
+ raise MissingCompressorError, "No JavaScript Compressor. Please set the :javascript_compressor option"
477
518
  end
478
519
 
479
520
  # detect CDATA wrapper
@@ -483,7 +524,7 @@ module HtmlCompressor
483
524
  source = $1
484
525
  end
485
526
 
486
- result = javaScriptCompressor.compress(source).strip
527
+ result = javascript_compressor.compress(source).strip
487
528
 
488
529
  if cdataWrapper
489
530
  result = "<![CDATA[" + result + "]]>"
@@ -494,10 +535,14 @@ module HtmlCompressor
494
535
 
495
536
  def compress_css_styles(source)
496
537
  # set default css compressor
497
- cssCompressor = @options[:css_compressor]
538
+ css_compressor = @options[:css_compressor]
539
+
540
+ if css_compressor.is_a?(Symbol)
541
+ css_compressor = get_css_compressor(css_compressor)
542
+ end
498
543
 
499
- if cssCompressor.nil?
500
- cssCompressor = YUI::CssCompressor.new(:line_break => @yuiCssLineBreak)
544
+ if css_compressor.nil?
545
+ raise MissingCompressorError, "No CSS Compressor. Please set the :css_compressor option"
501
546
  end
502
547
 
503
548
  # detect CDATA wrapper
@@ -507,7 +552,7 @@ module HtmlCompressor
507
552
  source = $1
508
553
  end
509
554
 
510
- result = cssCompressor.compress(source)
555
+ result = css_compressor.compress(source)
511
556
 
512
557
  if cdataWrapper
513
558
  result = "<![CDATA[" + result + "]]>"
@@ -782,4 +827,4 @@ module HtmlCompressor
782
827
 
783
828
  end
784
829
 
785
- end
830
+ end
@@ -0,0 +1,3 @@
1
+ module HtmlCompressor
2
+ class MissingCompressorError < StandardError; end
3
+ end
@@ -1,3 +1,3 @@
1
1
  module HtmlCompressor
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ module HtmlCompressor
4
+
5
+ class TestCompressor < Minitest::Test
6
+
7
+ def test_compress_java_script_closure
8
+ source = read_resource("testCompressJavaScript.html")
9
+ result = read_resource("testCompressJavaScriptClosureResult.html")
10
+
11
+ compressor = Compressor.new(
12
+ :compress_javascript => true,
13
+ :javascript_compressor => :closure,
14
+ :remove_intertag_spaces => true
15
+ )
16
+
17
+ assert_equal result, compressor.compress(source)
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -1,10 +1,8 @@
1
- require 'htmlcompressor/compressor'
2
- require 'minitest/autorun'
3
- require 'closure-compiler'
1
+ require 'test_helper'
4
2
 
5
3
  module HtmlCompressor
6
4
 
7
- class TestCompressor < MiniTest::Unit::TestCase
5
+ class TestCompressor < Minitest::Test
8
6
 
9
7
  def test_enabled
10
8
  source = read_resource("testEnabled.html")
@@ -80,43 +78,6 @@ module HtmlCompressor
80
78
  assert_equal result, compressor.compress(source)
81
79
  end
82
80
 
83
- def test_compress_javascript_yui
84
- source = read_resource("testCompressJavaScript.html");
85
- result = read_resource("testCompressJavaScriptYuiResult.html");
86
-
87
- compressor = Compressor.new(
88
- :compress_javascript => true,
89
- :remove_intertag_spaces => true
90
- )
91
-
92
- assert_equal result, compressor.compress(source)
93
- end
94
-
95
- def test_compress_java_script_closure
96
- source = read_resource("testCompressJavaScript.html")
97
- result = read_resource("testCompressJavaScriptClosureResult.html")
98
-
99
- compressor = Compressor.new(
100
- :compress_javascript => true,
101
- :javascript_compressor => Closure::Compiler.new(:compilation_level => 'ADVANCED_OPTIMIZATIONS'),
102
- :remove_intertag_spaces => true
103
- )
104
-
105
- assert_equal result, compressor.compress(source)
106
- end
107
-
108
- def test_compress_css
109
- source = read_resource("testCompressCss.html")
110
- result = read_resource("testCompressCssResult.html")
111
-
112
- compressor = Compressor.new(
113
- :compress_css => true,
114
- :remove_intertag_spaces => true
115
- )
116
-
117
- assert_equal result, compressor.compress(source)
118
- end
119
-
120
81
  def test_compress
121
82
  source = read_resource("testCompress.html")
122
83
  result = read_resource("testCompressResult.html")
@@ -246,18 +207,6 @@ module HtmlCompressor
246
207
  assert_equal result, compressor.compress(source)
247
208
  end
248
209
 
249
- private
250
-
251
- def resource_path
252
- File.join File.expand_path(File.dirname(__FILE__)), 'resources', 'html'
253
- end
254
-
255
- def read_resource file
256
- File.open File.join(resource_path, file), 'r' do |f|
257
- return f.readlines.join('')
258
- end
259
- end
260
-
261
210
  end
262
211
 
263
- end
212
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ module HtmlCompressor
4
+
5
+ class TestCompressor < Minitest::Test
6
+
7
+ def test_compress_javascript_yui
8
+ source = read_resource("testCompressJavaScript.html");
9
+ result = read_resource("testCompressJavaScriptYuiResult.html");
10
+
11
+ compressor = Compressor.new(
12
+ :compress_javascript => true,
13
+ :javascript_compressor => :yui,
14
+ :remove_intertag_spaces => true
15
+ )
16
+
17
+ assert_equal result, compressor.compress(source)
18
+ end
19
+
20
+ def test_compress_css
21
+ source = read_resource("testCompressCss.html")
22
+ result = read_resource("testCompressCssResult.html")
23
+
24
+ compressor = Compressor.new(
25
+ :compress_css => true,
26
+ :css_compressor => :yui,
27
+ :remove_intertag_spaces => true
28
+ )
29
+
30
+ assert_equal result, compressor.compress(source)
31
+ end
32
+
33
+ end
34
+
35
+ end
@@ -1,4 +1,4 @@
1
- <script type="text/javascript">var i=0,i=i+1;alert(i);</script><script><![CDATA[var i=0,i=i+1;alert(i);]]></script><pre>
1
+ <script type="text/javascript">var a=0,a=a+1;alert(a);</script><script><![CDATA[var a=0,a=a+1;alert(a);]]></script><pre>
2
2
  <script>
3
3
  var i = 0; //comment
4
4
  i = i + 1;
@@ -0,0 +1,22 @@
1
+ require 'minitest/autorun'
2
+ require 'htmlcompressor/compressor'
3
+
4
+ module HtmlCompressor
5
+
6
+ class TestCompressor < Minitest::Test
7
+
8
+ private
9
+
10
+ def resource_path
11
+ File.join File.expand_path(File.dirname(__FILE__)), 'resources', 'html'
12
+ end
13
+
14
+ def read_resource file
15
+ File.open File.join(resource_path, file), 'r' do |f|
16
+ return f.readlines.join('')
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,80 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmlcompressor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Paolo Chiodi
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-08 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: yui-compressor
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
21
- version: 0.9.6
22
- type: :runtime
19
+ version: '0.9'
20
+ type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
29
- version: 0.9.6
26
+ version: '0.9'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: closure-compiler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
37
- version: 1.1.5
33
+ version: '1.1'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
45
- version: 1.1.5
40
+ version: '1.1'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: minitest
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ~>
68
60
  - !ruby/object:Gem::Version
69
- version: '0'
61
+ version: '5.0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ~>
76
67
  - !ruby/object:Gem::Version
77
- version: '0'
68
+ version: '5.0'
78
69
  description: Put your html on a diet
79
70
  email:
80
71
  - chiodi84@gmail.com
@@ -83,6 +74,7 @@ extensions: []
83
74
  extra_rdoc_files: []
84
75
  files:
85
76
  - .gitignore
77
+ - .travis.yml
86
78
  - CHANGELOG
87
79
  - Gemfile
88
80
  - LICENSE
@@ -91,9 +83,12 @@ files:
91
83
  - htmlcompressor.gemspec
92
84
  - lib/htmlcompressor.rb
93
85
  - lib/htmlcompressor/compressor.rb
86
+ - lib/htmlcompressor/exceptions.rb
94
87
  - lib/htmlcompressor/rack.rb
95
88
  - lib/htmlcompressor/version.rb
89
+ - test/compressor_closure_test.rb
96
90
  - test/compressor_test.rb
91
+ - test/compressor_yui_test.rb
97
92
  - test/resources/html/.svn/all-wcprops
98
93
  - test/resources/html/.svn/entries
99
94
  - test/resources/html/.svn/prop-base/testRemoveHttpProtocol.html.svn-base
@@ -214,32 +209,34 @@ files:
214
209
  - test/resources/xml/testRemoveCommentsResult.xml
215
210
  - test/resources/xml/testRemoveIntertagSpaces.xml
216
211
  - test/resources/xml/testRemoveIntertagSpacesResult.xml
212
+ - test/test_helper.rb
217
213
  homepage: ''
218
214
  licenses: []
215
+ metadata: {}
219
216
  post_install_message:
220
217
  rdoc_options: []
221
218
  require_paths:
222
219
  - lib
223
220
  required_ruby_version: !ruby/object:Gem::Requirement
224
- none: false
225
221
  requirements:
226
- - - ! '>='
222
+ - - '>='
227
223
  - !ruby/object:Gem::Version
228
224
  version: '0'
229
225
  required_rubygems_version: !ruby/object:Gem::Requirement
230
- none: false
231
226
  requirements:
232
- - - ! '>='
227
+ - - '>='
233
228
  - !ruby/object:Gem::Version
234
229
  version: '0'
235
230
  requirements: []
236
231
  rubyforge_project:
237
- rubygems_version: 1.8.23
232
+ rubygems_version: 2.1.11
238
233
  signing_key:
239
- specification_version: 3
234
+ specification_version: 4
240
235
  summary: htmlcompressor provides a class and a rack middleware to minify html pages
241
236
  test_files:
237
+ - test/compressor_closure_test.rb
242
238
  - test/compressor_test.rb
239
+ - test/compressor_yui_test.rb
243
240
  - test/resources/html/.svn/all-wcprops
244
241
  - test/resources/html/.svn/entries
245
242
  - test/resources/html/.svn/prop-base/testRemoveHttpProtocol.html.svn-base
@@ -360,3 +357,4 @@ test_files:
360
357
  - test/resources/xml/testRemoveCommentsResult.xml
361
358
  - test/resources/xml/testRemoveIntertagSpaces.xml
362
359
  - test/resources/xml/testRemoveIntertagSpacesResult.xml
360
+ - test/test_helper.rb