rack-validate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ed Schmalzle
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = rack-validate
2
+
3
+ Simple Rack middleware to validate HTML with w3c validator and present response in the page. Used by passing rack-validate=true in the request.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Ed Schmalzle. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-validate"
8
+ gem.summary = %Q{Rack middleware that validates HTML with w3c validator and displays result in page.}
9
+ gem.description = %Q{Rack middleware that validates HTML with w3c validator and displays result in page.}
10
+ gem.email = "Jonas714@gmail.com"
11
+ gem.homepage = "http://github.com/nerdEd/rack-validate"
12
+ gem.authors = ["Ed Schmalzle"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "rack-validate #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,34 @@
1
+ require 'rack'
2
+ require 'rack-validate/validator'
3
+
4
+ module Rack
5
+
6
+ # A rack middleware for validating HTML via w3c validator
7
+ class Validate
8
+
9
+ def initialize( app )
10
+ @app = app
11
+ end
12
+
13
+ def call( env )
14
+ status, headers, response = @app.call( env )
15
+
16
+ request = Rack::Request.new( env )
17
+ if !request.params['rack-validate'].blank?
18
+ if headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
19
+ body = response.body
20
+
21
+ issues = Validator.validate( body )
22
+
23
+ body.insert( 0, Validator.generate_report( issues ) )
24
+
25
+ headers["Content-Length"] = body.length.to_s
26
+ response = [body]
27
+ end
28
+ end
29
+
30
+ [status, headers, response]
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,131 @@
1
+ require 'rack'
2
+ require 'w3c_validators'
3
+
4
+ module Rack
5
+ class Validate
6
+ class Validator
7
+ include W3CValidators
8
+
9
+ def self.validate( response )
10
+ validator = MarkupValidator.new
11
+ validator.validate_text( response )
12
+ end
13
+
14
+ def self.generate_report( issues )
15
+ report = ""
16
+ report << STYLES
17
+ report << SCRIPT
18
+
19
+ report << "<div id='message_toolbar'>"
20
+ report << '<div id="controls">'
21
+ summary = "<span>There were " + issues.errors.size.to_s + " errors and " + issues.warnings.size.to_s + " warnings</span><br/><br/>"
22
+ report << summary
23
+ report << LINKS
24
+ report << '</div>'
25
+
26
+ if !issues.errors.empty?
27
+ report << "<table id='errors_table' style='display:none;'>"
28
+ report << "<tr><td colspan='2' class='header_column'>Errors</td></tr>"
29
+ issues.errors.each do |item|
30
+ report << "<tr><td class='line_number_column'>Line #{item.line}</td><td class='message_column'>#{html_escape( item.message )}</td></tr>"
31
+ end
32
+ report << "</table>"
33
+ end
34
+
35
+ if !issues.warnings.empty?
36
+ report << "<table id='warnings_table' style='display:none;'>"
37
+ report << "<tr><td colspan='2' class='header_column'>Warnings</td></tr>"
38
+ issues.warnings.each do |item|
39
+ report << "<tr><td class='line_number_column'>--</td><td class='message_column'>#{html_escape( item.message )}</td></tr>"
40
+ end
41
+ report << "</table>"
42
+ end
43
+ report << "</div>"
44
+ end
45
+
46
+ private
47
+
48
+ # Stealing HTML escape method from rails
49
+ def self.html_escape( string )
50
+ string.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
51
+ end
52
+
53
+ # Stealing HTML escape constant from rails
54
+ HTML_ESCAPE = { '&' => '&amp;', '>' => '&gt;', '<' => '&lt;', '"' => '&quot;' }
55
+
56
+ LINKS = <<-HERE
57
+ <a href="#" onclick="$( '#errors_table' ).toggle();updateText( this, 'Show Errors', 'Hide Errors' );">Show Errors</a>
58
+ <a href="#" onclick="$( '#warnings_table' ).toggle();updateText( this, 'Show Warnings', 'Hide Warnings' );">Show Warnings</a>
59
+ <a href="#" onclick="$( '#message_toolbar' ).toggle();">Close Toolbar</a>
60
+ HERE
61
+
62
+ SCRIPT = <<-HERE
63
+ <script src="http://www.google.com/jsapi"></script>
64
+ <script type="text/javascript">
65
+ google.load("jquery", "1.3.2");
66
+ google.load("jqueryui", "1.7.2");
67
+
68
+ function updateText( element, textOne, textTwo ) {
69
+ if( element.innerHTML == textOne ) {
70
+ element.innerHTML = textTwo;
71
+ }
72
+ else {
73
+ element.innerHTML = textOne;
74
+ }
75
+ }
76
+ </script>
77
+ HERE
78
+
79
+ STYLES = <<-HERE
80
+ <style type='text/css'>
81
+ #message_toolbar {
82
+ width: 100%;
83
+ min-height: 50px;
84
+ background: black;
85
+ position: absolute;
86
+ top:0;
87
+ left:0;
88
+ -moz-opacity:.80;
89
+ filter:alpha(opacity=80);
90
+ opacity:.80;
91
+ font-size: smaller;
92
+ font-family: "Courier New";
93
+ color: white;
94
+ text-align: center;
95
+ }
96
+ #message_toolbar a{
97
+ color: white;
98
+ font-weight: bold;
99
+ }
100
+ .line_number_column {
101
+ text-align: left;
102
+ width: 100px;
103
+ }
104
+ .error_message_column {
105
+ text-align: left;
106
+ }
107
+ #errors_table,
108
+ #warnings_table {
109
+ width: 75%;
110
+ margin: 0 auto;
111
+ border: none;
112
+ color: white;
113
+ margin-top: 10px;
114
+ }
115
+ #errors_table .header_column,
116
+ #warnings_table .header_column {
117
+ text-align: center;
118
+ border-bottom: 2px dashed;
119
+ }
120
+ #errors_table .header_column {
121
+ border-color: red;
122
+ }
123
+ #warnings_table .header_column {
124
+ border-color: yellow;
125
+ }
126
+ </style>
127
+
128
+ HERE
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack-validate}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ed Schmalzle"]
12
+ s.date = %q{2009-11-16}
13
+ s.description = %q{Rack middleware that validates HTML with w3c validator and displays result in page.}
14
+ s.email = %q{Jonas714@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rack-validate.rb",
27
+ "lib/rack-validate/validator.rb",
28
+ "rack-validate.gemspec",
29
+ "test/rack-validate_test.rb",
30
+ "test/test_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/nerdEd/rack-validate}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Rack middleware that validates HTML with w3c validator and displays result in page.}
37
+ s.test_files = [
38
+ "test/rack-validate_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
48
+ # s.add_development_dependency(%q<w3c_validator>, [">= 0.9.3]"])
49
+ else
50
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ # s.add_dependency(%q<w3c_validator>, [">= 0.9.3]"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ # s.add_dependency(%q<w3c_validator>, [">= 0.9.3]"])
56
+ end
57
+ end
58
+
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class RackValidateTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'rack-validate'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-validate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ed Schmalzle
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-16 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Rack middleware that validates HTML with w3c validator and displays result in page.
26
+ email: Jonas714@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/rack-validate.rb
42
+ - lib/rack-validate/validator.rb
43
+ - rack-validate.gemspec
44
+ - test/rack-validate_test.rb
45
+ - test/test_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/nerdEd/rack-validate
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 3
73
+ summary: Rack middleware that validates HTML with w3c validator and displays result in page.
74
+ test_files:
75
+ - test/rack-validate_test.rb
76
+ - test/test_helper.rb