wbzyl-rack-htmltidy 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
File without changes
data/README.markdown ADDED
@@ -0,0 +1,70 @@
1
+ # Rack::HTMLTidy
2
+
3
+ *Rack::HTMLTidy* is a middleware that adds HTML validation
4
+ for Rack applications. It uses Dave's Raggett HTML Tidy
5
+ to check HTML pages. The results are written to log.
6
+ The middleware uses the
7
+ [*TidyLib* C library](http://tidy.sourceforge.net/)
8
+ and the
9
+ [*tidy*](http://github.com/ak47/tidy/) gem.
10
+
11
+ The idea of using middleware to validate HTML belongs to
12
+ Marcin Kulik, [Rack middleware using HTML
13
+ Tidy](http://sickill.net/blog/2009/05/10/rack-middleware-using-html-tidy.html).
14
+
15
+ **Limitations of [TidyLib](http://tidy.sourceforge.net/libintro.html)**:
16
+ Currently, all character encoding support is hard wired into the
17
+ library. This means we do a poor job of supporting many popular
18
+ encodings such as GB2312, euc-kr, eastern European languages,
19
+ cyrillic, etc. Any of these languages must first be transcoded into
20
+ ISO-10646/Unicode before Tidy can work with it.
21
+
22
+ ## Using with Rack application
23
+
24
+ *Rack::HTMLTidy* can be used with any Rack application,
25
+ for example with a **Sinatra** application.
26
+ If your application includes a rackup file
27
+ or uses *Rack::Builder* to construct the application pipeline,
28
+ simply require and use as follows:
29
+
30
+ gem 'wbzyl-rack-htmltidy'
31
+ require 'rack/htmltidy'
32
+
33
+ use Rack::HTMLTidy, :errors => true, :diagnostics => true, :path => "/usr/lib/libtidy-0.99.so.0"
34
+
35
+ run app
36
+
37
+ Remember to update the `:path` option to the location of *TidyLib* on your system.
38
+
39
+ ## Using with Rails 2.3.2
40
+
41
+ In order to use include the following in a Rails application
42
+ *config/environment.rb* file:
43
+
44
+ require 'rack/htmltidy'
45
+
46
+ Rails::Initializer.run do |config|
47
+ config.gem "rack-htmltidy"
48
+ config.middleware.use(Rack::HTMLTidy,
49
+ :errors => true,
50
+ :diagnostics => true,
51
+ :path => "/usr/lib/libtidy-0.99.so.0")
52
+ end
53
+
54
+ Check the Rack configuration:
55
+
56
+ rake middleware
57
+
58
+ Remember to update the `:path` option to the location of *TidyLib* on your system.
59
+
60
+
61
+ ## Miscellaneous stuff
62
+
63
+ To install *TidyLib* on Fedora 9 and above:
64
+
65
+ yum install libtidy libtidy-devel
66
+
67
+ To fix this bug: `tidybuf.rb:40: [BUG] Segmentation fault`,
68
+ clone, build and install the *tidy* gem from here:
69
+
70
+ git://github.com/ak47/tidy.git
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require "rake"
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gemspec|
6
+ gemspec.name = "rack-htmltidy"
7
+ gemspec.summary = "HTML Tidy."
8
+ gemspec.email = "matwb@univ.gda.pl"
9
+ gemspec.homepage = "http://github.com/wbzyl/rack-tidy"
10
+ gemspec.authors = ["Wlodek Bzyl"]
11
+ gemspec.description = gemspec.summary
12
+
13
+ gemspec.files = %w[LICENSE Rakefile VERSION.yml] + FileList['lib/**/*.rb']
14
+
15
+ gemspec.add_runtime_dependency 'rack', '>=1.0.0'
16
+ gemspec.add_runtime_dependency 'tidy', '>=1.1.2' # patched
17
+
18
+ gemspec.add_development_dependency 'rack-test', '>=0.3.0'
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler not available."
22
+ puts "Install it with:"
23
+ puts " sudo gem install technicalpickles-jeweler -s http://gems.github.com"
24
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 6
3
+ :major: 0
4
+ :minor: 0
@@ -0,0 +1,63 @@
1
+ gem 'rack'
2
+ require 'rack/utils'
3
+
4
+ gem 'tidy'
5
+ require 'tidy'
6
+
7
+ module Rack
8
+ class HTMLTidy
9
+ include Rack::Utils
10
+
11
+ FORMAT = %{|| Tidy: %s - [%s] "%s %s%s %s"\n%s}
12
+
13
+ def initialize(app, opts={})
14
+ @app = app
15
+ @errors = opts[:errors] || false
16
+ @diagnostics = opts[:diagnostics] || false
17
+ @logger = opts[:logger]
18
+
19
+ @path = opts[:path] || "/usr/lib/libtidy-0.99.so.0"
20
+ ::Tidy.path = @path
21
+ end
22
+
23
+ def call(env)
24
+ status, headers, response = @app.call(env)
25
+
26
+ headers = HeaderHash.new(headers)
27
+
28
+ if !STATUS_WITH_NO_ENTITY_BODY.include?(status) &&
29
+ !headers['transfer-encoding'] &&
30
+ headers['content-type'] &&
31
+ headers['content-type'].include?("text/html")
32
+
33
+ ::Tidy.open(:show_warnings => true, "char-encoding" => "utf8") do |tidy|
34
+ html = ""
35
+ response.each { |part| html += part }
36
+ tidy.clean(html)
37
+ log(env, tidy, "errors") if @errors && tidy.errors.length > 0
38
+ log(env, tidy, "diagnostics") if @diagnostics && tidy.diagnostics.length > 0
39
+ end
40
+ end
41
+
42
+ [status, headers, response]
43
+ end
44
+
45
+ private
46
+
47
+ def log(env, tidy, what)
48
+ now = Time.now
49
+ logger = @logger || env['rack.errors']
50
+
51
+ logger.write FORMAT % [
52
+ what,
53
+ now.strftime("%d/%b/%Y %H:%M:%S"),
54
+ env["REQUEST_METHOD"],
55
+ env["PATH_INFO"],
56
+ env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
57
+ env["HTTP_VERSION"],
58
+ tidy.send(what)
59
+ ]
60
+ end
61
+
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wbzyl-rack-htmltidy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Wlodek Bzyl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-05 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: tidy
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.1.2
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rack-test
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.3.0
44
+ version:
45
+ description: HTML Tidy.
46
+ email: matwb@univ.gda.pl
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.markdown
54
+ files:
55
+ - LICENSE
56
+ - Rakefile
57
+ - VERSION.yml
58
+ - lib/rack/htmltidy.rb
59
+ - README.markdown
60
+ has_rdoc: false
61
+ homepage: http://github.com/wbzyl/rack-tidy
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --charset=UTF-8
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ version:
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ version:
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.2.0
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: HTML Tidy.
86
+ test_files: []
87
+