html_press 0.6.6 → 0.6.7

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
2
-
3
- # Specify your gem's dependencies in html_press.gemspec
4
- gemspec
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in html_press.gemspec
4
+ gemspec
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- require "bundler/gem_tasks"
2
-
3
- [:build, :install, :release].each do |task_name|
4
- Rake::Task[task_name].prerequisites << :spec
5
- end
6
-
7
- require "rspec/core/rake_task"
8
- RSpec::Core::RakeTask.new
9
-
10
- task :default => :spec
1
+ require "bundler/gem_tasks"
2
+
3
+ [:build, :install, :release].each do |task_name|
4
+ Rake::Task[task_name].prerequisites << :spec
5
+ end
6
+
7
+ require "rspec/core/rake_task"
8
+ RSpec::Core::RakeTask.new
9
+
10
+ task :default => :spec
data/Readme.md CHANGED
@@ -14,90 +14,57 @@ Remove all whitespace junk. Leave only HTML
14
14
 
15
15
  ## Usage
16
16
 
17
- ### Jekyll
18
-
19
- Gemfile
20
-
21
- ```ruby
22
- gem "jekyll"
23
- gem "html_press"
24
- ```
25
-
26
- _plugins/strip_tag.rb
27
-
17
+ ### Ruby
28
18
  ```ruby
29
- module Jekyll
30
- class StripTag < Liquid::Block
31
- @total_economy = 0
32
- class << self
33
- attr_accessor :total_economy
34
- end
35
-
36
- begin
37
- require 'html_press'
38
- def render(context)
39
- text = super
40
- before = text.bytesize
41
- text = HtmlPress.press text
42
- after = text.bytesize
43
-
44
- self.class.total_economy += before - after
45
- economy = (self.class.total_economy.to_f / 1024).round(2)
46
- p 'totally saved: ' + economy.to_s + ' Kb'
47
- text
48
- end
49
- rescue LoadError => e
50
- p "Unable to load 'html_press'"
51
- end
52
- end
53
- end
54
-
55
- Liquid::Template.register_tag('strip', Jekyll::StripTag)
19
+ require 'html_press'
20
+ compressed_html = HtmlPress.press html
56
21
  ```
57
22
 
58
- In templates
23
+ ### Jekyll
24
+ see [jekyll_press](https://github.com/stereobooster/jekyll_press)
59
25
 
60
- ```liquid
61
- {% strip %}
62
- here goes text...
63
- {% endstrip %}
64
- ```
26
+ ### Rails
27
+ TODO :exclamation:
65
28
 
66
- Run
29
+ ### Sinatra
30
+ TODO :exclamation:
67
31
 
68
- ```
69
- bundle install
70
- bundle exec jekyll
71
- ```
32
+ ### Command line
33
+ TODO :exclamation:
72
34
 
73
35
  ## TODO
74
-
75
36
  - use parser ([鋸](https://github.com/tenderlove/nokogiri)) instead of regexp's
76
- - check if all utf-8 symbols, are smaller than html entities
77
- - ambigious ampersands
78
- - cli
37
+ - add option to convert relative urls to absolute urls (for SEO)
38
+ - [ambigious ampersands](http://mathiasbynens.be/notes/ambiguous-ampersands) for compression?
79
39
  - Support other js/css minifiers (Closure, YUI compressor)
80
40
  - htmlTydi
81
- - add examples of usage with Sinatra and Rails
41
+ - Rack plugin
82
42
 
83
43
  ## Alternatives
84
-
85
- - [html-minifier](https://github.com/kangax/html-minifier) (js), [test suite](https://github.com/kangax/html-minifier/blob/gh-pages/tests/index.html), [ruby wrapper - html_minifier](https://github.com/stereobooster/html_minifier)
44
+ - [html-minifier](https://github.com/kangax/html-minifier) (js), [test suite](https://github.com/kangax/html-minifier/blob/gh-pages/tests/index.html), ruby wrapper - [html_minifier](https://github.com/stereobooster/html_minifier)
86
45
  - [htmlcompressor](http://code.google.com/p/htmlcompressor/) (java), [test suite](http://code.google.com/p/htmlcompressor/source/browse/#svn%2Ftrunk%2Fsrc%2Ftest%2Fresources%2Fhtml%253Fstate%253Dclosed)
87
46
  - PHPTal compress (php), [test suite](https://svn.motion-twin.com/phptal/trunk/tests/CompressTest.php)
88
47
  - [W3 total cache](http://wordpress.org/extend/plugins/w3-total-cache/) - WP plugin from smashingmagazine contains html minifier (php)
89
48
 
90
49
  ## Additional tools
91
-
92
- - https://github.com/gfranco/jeanny
93
- - https://github.com/aanand/deadweight
94
- - https://github.com/aberant/css-spriter
95
- - https://github.com/sstephenson/sprockets
96
- - https://github.com/documentcloud/jammit
97
- - https://github.com/cjohansen/juicer
98
- - https://github.com/alexdunae/w3c_validators
50
+ - [jeanny](https://github.com/gfranco/jeanny) - rename css classes and ids in css and html files
51
+ - make shorter pathes for images in css
52
+ - [deadweight](https://github.com/aanand/deadweight) - remove unused css rules from css files
53
+ - [css-spriter](https://github.com/aberant/css-spriter), [sprite-factory](https://github.com/jakesgordon/sprite-factory) - combine images in sprites
54
+ - resize images by size defined in html and vice versa embed size of images in html
55
+ - [#1](http://habrahabr.ru/post/90761/), [#2](http://ap-project.org/English/Article/View/53/) - inline small images in css
56
+ - [smusher](https://github.com/grosser/smusher), jpegtran, optipng - losslessly minify images
57
+ - [sprockets](https://github.com/sstephenson/sprockets), [jammit](https://github.com/documentcloud/jammit) - asset bundlers
58
+ - [w3c_validators](https://github.com/alexdunae/w3c_validators)
59
+ - [reduce](https://github.com/grosser/reduce)
99
60
 
100
61
  ## Resources
62
+
63
+ ### Minimize HTML
101
64
  - http://perfectionkills.com/experimenting-with-html-minifier
102
65
  - http://perfectionkills.com/optimizing-html
103
- - https://developers.google.com/speed/articles/optimizing-html
66
+ - https://developers.google.com/speed/articles/optimizing-html
67
+
68
+ ### Front-end optimization
69
+ - https://developers.google.com/speed/docs/insights/rules
70
+ - http://developer.yahoo.com/performance/rules.html
data/html_press.gemspec CHANGED
@@ -1,29 +1,29 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "html_press/version"
4
-
5
- Gem::Specification.new do |s|
6
- s.name = "html_press"
7
- s.version = HtmlPress::VERSION
8
- s.authors = ["stereobooster"]
9
- s.email = ["stereobooster@gmail.com"]
10
- s.homepage = "https://github.com/stereobooster/html_press"
11
- s.summary = %q{Compress html}
12
- s.description = %q{Ruby gem for compressing html}
13
-
14
- s.rubyforge_project = "html_press"
15
-
16
- s.files = `git ls-files`.split("\n")
17
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
- s.require_paths = ["lib"]
20
-
21
- # s.add_dependency "nokogiri"
22
-
23
- s.add_development_dependency "rspec"
24
- s.add_development_dependency "rake"
25
-
26
- s.add_runtime_dependency "css_press", ">=0.3.2"
27
- s.add_runtime_dependency "uglifier"
28
- s.add_runtime_dependency "htmlentities"
29
- end
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "html_press/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "html_press"
7
+ s.version = HtmlPress::VERSION
8
+ s.authors = ["stereobooster"]
9
+ s.email = ["stereobooster@gmail.com"]
10
+ s.homepage = "https://github.com/stereobooster/html_press"
11
+ s.summary = %q{Compress html}
12
+ s.description = %q{Ruby gem for compressing html}
13
+
14
+ s.rubyforge_project = "html_press"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # s.add_dependency "nokogiri"
22
+
23
+ s.add_development_dependency "rspec"
24
+ s.add_development_dependency "rake"
25
+
26
+ s.add_runtime_dependency "css_press", ">=0.3.2"
27
+ s.add_runtime_dependency "uglifier"
28
+ s.add_runtime_dependency "htmlentities"
29
+ end
@@ -75,21 +75,17 @@ module HtmlPress
75
75
  ''
76
76
  end
77
77
 
78
- # replace entities
79
- out.gsub! /&lt;|&#60;|&gt;|&#62;|&amp;|&#38;/ do |m|
80
- reserve m
81
- end
82
-
83
- out = HtmlPress.entities_compressor out
84
-
85
78
  # replace PREs with placeholders
86
79
  out.gsub! /\s*(<pre\b[^>]*?>[\s\S]*?<\/pre>)\s*/i do |m|
87
80
  pre = m.gsub(/\s*<pre\b[^>]*?>([\s\S]*?)<\/pre>\s*/i, "\\1")
88
81
  pre_compressed = pre.gsub(/\s+$/, '')
82
+ pre_compressed = HtmlPress.entities_compressor pre_compressed
89
83
  m.gsub!(pre, pre_compressed)
90
84
  reserve m
91
85
  end
92
86
 
87
+ out = HtmlPress.entities_compressor out
88
+
93
89
  # replace TEXTAREAs with placeholders
94
90
  out.gsub! /\s*(<textarea\b[^>]*?>[\s\S]*?<\/textarea>)\s*/i do |m|
95
91
  reserve m
@@ -1,12 +1,37 @@
1
+ require 'htmlentities'
2
+
1
3
  module HtmlPress
2
- begin
3
- require 'htmlentities'
4
- def self.entities_compressor (text)
5
- HTMLEntities.new.decode(text)
4
+ class Entities
5
+ def initialize
6
+ @replacement_hash = 'MINIFYENTITY' + Time.now.to_i.to_s
7
+ @placeholders = []
6
8
  end
7
- rescue LoadError => e
8
- def self.entities_compressor (text)
9
- text
9
+
10
+ def reserve(content)
11
+ @placeholders.push content
12
+ '%' + @replacement_hash + '%' + (@placeholders.size - 1).to_s + '%'
10
13
  end
14
+
15
+ def minify text
16
+ out = text.dup
17
+
18
+ out.gsub! /&lt;|&#60;|&gt;|&#62;|&amp;|&#38;/ do |m|
19
+ reserve m
20
+ end
21
+
22
+ out = HTMLEntities.new.decode(out)
23
+
24
+ re = Regexp.new('%' + @replacement_hash + '%(\d+)%')
25
+ out.gsub! re do |m|
26
+ m.gsub!(re, "\\1")
27
+ @placeholders[m.to_i]
28
+ end
29
+
30
+ out
31
+ end
32
+ end
33
+
34
+ def self.entities_compressor (text)
35
+ Entities.new.minify(text)
11
36
  end
12
37
  end
@@ -1,3 +1,3 @@
1
- module HtmlPress
2
- VERSION = "0.6.6"
3
- end
1
+ module HtmlPress
2
+ VERSION = "0.6.7"
3
+ end
data/lib/html_press.rb CHANGED
@@ -1,17 +1,17 @@
1
- require "html_press/version"
2
- require "html_press/css_press"
3
- require "html_press/uglifier"
4
- require "html_press/html_entities"
5
- require "html_press/html"
6
-
7
- module HtmlPress
8
- def self.press(text, options = {})
9
- HtmlPress::Html.new(options).press text
10
- end
11
-
12
- # for backward compatibility
13
- def self.compress(text, options = {})
14
- HtmlPress::Html.new(options).press text
15
- end
16
-
17
- end
1
+ require "html_press/version"
2
+ require "html_press/css_press"
3
+ require "html_press/uglifier"
4
+ require "html_press/html_entities"
5
+ require "html_press/html"
6
+
7
+ module HtmlPress
8
+ def self.press(text, options = {})
9
+ HtmlPress::Html.new(options).press text
10
+ end
11
+
12
+ # for backward compatibility
13
+ def self.compress(text, options = {})
14
+ HtmlPress::Html.new(options).press text
15
+ end
16
+
17
+ end
@@ -180,6 +180,9 @@ describe HtmlPress do
180
180
  it "should convert html entities to utf-8 symbols" do
181
181
  HtmlPress.press("&lt; &#60; &gt; &#62; &amp; &#38;").should eql "&lt; &#60; &gt; &#62; &amp; &#38;"
182
182
  HtmlPress.press("&eacute;lan").should eql "élan"
183
+ %W{textarea pre}.each do |t|
184
+ HtmlPress.press("<#{t}>&#39;</#{t}>").should eql "<#{t}>'</#{t}>"
185
+ end
183
186
  end
184
187
 
185
188
  it "should remove unnecessary quotes for attribute values" do
metadata CHANGED
@@ -1,105 +1,103 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: html_press
3
- version: !ruby/object:Gem::Version
4
- hash: 11
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.7
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 6
9
- - 6
10
- version: 0.6.6
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - stereobooster
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-08-07 00:00:00 +03:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- type: :development
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
32
- requirement: *id001
33
- prerelease: false
12
+ date: 2012-09-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
34
15
  name: rspec
35
- - !ruby/object:Gem::Dependency
36
- type: :development
37
- version_requirements: &id002 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
38
17
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
46
- requirement: *id002
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
47
23
  prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
48
31
  name: rake
49
- - !ruby/object:Gem::Dependency
50
- type: :runtime
51
- version_requirements: &id003 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
52
33
  none: false
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- hash: 23
57
- segments:
58
- - 0
59
- - 3
60
- - 2
61
- version: 0.3.2
62
- requirement: *id003
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
63
39
  prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
64
47
  name: css_press
65
- - !ruby/object:Gem::Dependency
66
- type: :runtime
67
- version_requirements: &id004 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
68
49
  none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 0
75
- version: "0"
76
- requirement: *id004
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 0.3.2
54
+ type: :runtime
77
55
  prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.2
62
+ - !ruby/object:Gem::Dependency
78
63
  name: uglifier
79
- - !ruby/object:Gem::Dependency
80
- type: :runtime
81
- version_requirements: &id005 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
82
65
  none: false
83
- requirements:
84
- - - ">="
85
- - !ruby/object:Gem::Version
86
- hash: 3
87
- segments:
88
- - 0
89
- version: "0"
90
- requirement: *id005
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
91
71
  prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
92
79
  name: htmlentities
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
93
94
  description: Ruby gem for compressing html
94
- email:
95
+ email:
95
96
  - stereobooster@gmail.com
96
97
  executables: []
97
-
98
98
  extensions: []
99
-
100
99
  extra_rdoc_files: []
101
-
102
- files:
100
+ files:
103
101
  - .gitignore
104
102
  - .travis.yml
105
103
  - Gemfile
@@ -113,39 +111,34 @@ files:
113
111
  - lib/html_press/uglifier.rb
114
112
  - lib/html_press/version.rb
115
113
  - spec/html_press_spec.rb
116
- has_rdoc: true
117
114
  homepage: https://github.com/stereobooster/html_press
118
115
  licenses: []
119
-
120
116
  post_install_message:
121
117
  rdoc_options: []
122
-
123
- require_paths:
118
+ require_paths:
124
119
  - lib
125
- required_ruby_version: !ruby/object:Gem::Requirement
120
+ required_ruby_version: !ruby/object:Gem::Requirement
126
121
  none: false
127
- requirements:
128
- - - ">="
129
- - !ruby/object:Gem::Version
130
- hash: 3
131
- segments:
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
132
127
  - 0
133
- version: "0"
134
- required_rubygems_version: !ruby/object:Gem::Requirement
128
+ hash: 26551057
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
130
  none: false
136
- requirements:
137
- - - ">="
138
- - !ruby/object:Gem::Version
139
- hash: 3
140
- segments:
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ segments:
141
136
  - 0
142
- version: "0"
137
+ hash: 26551057
143
138
  requirements: []
144
-
145
139
  rubyforge_project: html_press
146
- rubygems_version: 1.5.2
140
+ rubygems_version: 1.8.24
147
141
  signing_key:
148
142
  specification_version: 3
149
143
  summary: Compress html
150
144
  test_files: []
151
-