rack-htmltidy 0.1.0

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