html_mini 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/LICENSE +25 -0
  2. data/README.md +53 -0
  3. data/lib/html_mini.rb +18 -0
  4. data/lib/rack/minify.rb +67 -0
  5. metadata +67 -0
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2012, Jerome Touffe-Blin
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ * Redistributions in binary form must reproduce the above copyright notice
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ * Neither the name of Jerome Touffe-Blin nor the names of its contributors
13
+ may be used to endorse or promote products derived from this software
14
+ without specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # HtmlMini
2
+
3
+ The *html-mini* gem includes a simple module and a middleware that adds HTML minification
4
+ for Rack applications. HtmlMini is optimised for speed over syntax preservation.
5
+
6
+ ## Disclaimer
7
+
8
+ This is an early release that has not been tested in production. I wanted to try out creating a rack middleware and a gem.
9
+ The minify logic is **minimal** therefore unexpected issues can occur. **Use at your own risk**
10
+
11
+ If you have inline Javascript in your Html, make sure all lines are ended by semi colons, and that you only use multi or full line comments, no comments at tend of line.
12
+
13
+ ## Using with Rack application
14
+
15
+ *HtmlMini* can be used with any Rack application,
16
+ for example with a **Sinatra** application.
17
+ If your application includes a rackup file
18
+ or uses *Rack::Builder* to construct the application pipeline,
19
+ simply require and use as follows:
20
+
21
+ require 'rack/minify'
22
+ use Rack::Minify
23
+ run app
24
+
25
+ ## Using with Rails 3
26
+
27
+ In order to use, include the following in a Rails application
28
+ *Gemfile* file:
29
+
30
+ gem 'html_mini'
31
+
32
+ *config/application.rb* file:
33
+
34
+ require 'rack/minify'
35
+ config.middleware.insert_before Rack::ETag, Rack::Minify
36
+
37
+ Check the Rack configuration:
38
+
39
+ rake middleware
40
+
41
+ ## Credits
42
+
43
+ * [rack-htmltidy](https://github.com/wbzyl/rack-htmltidy): An Outdated Tidy Rack middleware
44
+ * [railscast.com](http://railscasts.com/episodes/151-rack-middleware): A screencast on middleware (a bit old)
45
+
46
+ ## Author
47
+
48
+ Jerome Touffe-Blin, [@jtblin](https://twitter.com/jtlbin), [http://www.linkedin.com/in/jtblin](http://www.linkedin.com/in/jtblin)
49
+
50
+ ## License
51
+
52
+ Html-Mini is copyright 2012 Jerome Touffe-Blin and contributors. It is licensed under the BSD license. See the include LICENSE file for details.
53
+
data/lib/html_mini.rb ADDED
@@ -0,0 +1,18 @@
1
+ module HtmlMini
2
+
3
+ VERSION = "0.0.1"
4
+
5
+ def self.minify(html)
6
+ # 1. Remove all comments: gsub(/(<!--(\w|\s|:|!|#|<|>|'|"|=|;|,|\.|\?)*-->|\/\*[^\*]*\*\/|^(\t|\s)*\/\/.*)/, '')
7
+ # 1.1 html comments without special characters: <!--(\w|\s|:|!|#|<|>|'|"|=|;|,|\.|\?)*-->
8
+ # 1.2. Remove javascript comments e.g. /* */ and // \/\*[^\*]*\*\/ and ^(\t|\s)*\/\/.*
9
+ # 3. Replace all carrier return and all tabs by a single space gsub(/(\n|\t)/, ' ').
10
+ # 4. Replace any consecutive spaces by a single space gsub(/\s{2,}/, ' ')
11
+ # 5. Remove space between tags gsub(/>\s+</, '><').strip.
12
+ html.gsub(/(<!--(\w|\s|:|!|#|<|>|'|"|=|;|,|\.|\?)*-->|\/\*[^\*]*\*\/|^(\t|\s)*\/\/.*)/, '').
13
+ gsub(/(\n|\t)/, ' ').
14
+ gsub(/\s{2,}/, ' ').
15
+ gsub(/>\s+</, '><').strip
16
+ end
17
+
18
+ end
@@ -0,0 +1,67 @@
1
+ require 'html_mini'
2
+ require 'rack/utils'
3
+ require 'rack/logger'
4
+
5
+ module Rack
6
+ class Minify
7
+ include Rack::Utils
8
+
9
+ FORMAT = %{[HtmlMini #{HtmlMini::VERSION}] %s - %s - [%s] %s "%s%s %s"\n}
10
+
11
+ def initialize(app, opts={})
12
+ @app = app
13
+ @bench = opts[:bench] || true
14
+ @logger = opts[:logger]
15
+ end
16
+
17
+ def call(env)
18
+ dup._call(env)
19
+ end
20
+
21
+ def _call(env)
22
+ status, headers, response = @app.call(env)
23
+
24
+ headers = HeaderHash.new(headers)
25
+
26
+ if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
27
+ !headers['transfer-encoding'] &&
28
+ headers['content-type'] &&
29
+ headers['content-type'].include?("text/html")
30
+
31
+ body = ""
32
+ response.each { |part| body += part }
33
+
34
+ start, size_before = Time.now, body.size if @bench
35
+
36
+ html = HtmlMini.minify(body) rescue nil
37
+
38
+ stop = Time.now and log(env, (stop-start).seconds, size_before, html.size) if @bench
39
+
40
+ headers['Content-Length'] = html.size.to_s
41
+ headers['Html-Mini'] = "version #{HtmlMini::VERSION}, time #{(stop-start).seconds}s, size #{html.size-size_before} bytes" if @bench
42
+
43
+ response = [html] unless html.nil?
44
+ end
45
+
46
+ [status, headers, response]
47
+ end
48
+
49
+ private
50
+
51
+ def log(env, time, size_before, size)
52
+ now = Time.now
53
+ logger = @logger || env['rack.errors']
54
+
55
+ logger.write FORMAT % [
56
+ "Time: #{time}s",
57
+ "Gain: #{size_before-size}/#{size_before.to_s}",
58
+ now.strftime("%d-%b-%Y %H:%M:%S"),
59
+ env["REQUEST_METHOD"],
60
+ env["PATH_INFO"],
61
+ env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
62
+ env["HTTP_VERSION"]
63
+ ]
64
+ end
65
+
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_mini
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jerome Touffe-Blin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.4.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.0
30
+ description: Simple, fast HTML minification for Rack applications
31
+ email: jtblin@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files:
35
+ - LICENSE
36
+ - README.md
37
+ files:
38
+ - lib/html_mini.rb
39
+ - lib/rack/minify.rb
40
+ - LICENSE
41
+ - README.md
42
+ homepage: http://github.com/jtblin/html-mini
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 1.3.6
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.24
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: HTML Mini
67
+ test_files: []