DylanFM-pleasevalidate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-03-09
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2009 Dylan Fogarty-MacDonald
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ RakeFile
6
+ bin/pleasevalidate
7
+ cucumber.yml
8
+ features/cli_validation.feature
9
+ features/steps/cli_validation_steps.rb
10
+ features/steps/helper.rb
11
+ features/steps/validate_steps.rb
12
+ features/validate.feature
13
+ lib/please_validate.rb
14
+ lib/please_validate/cli.rb
15
+ lib/please_validate/validator.rb
data/README.rdoc ADDED
@@ -0,0 +1,74 @@
1
+ = Please Validate
2
+
3
+ http://github.com/DylanFM/please_validate/tree
4
+
5
+ == DESCRIPTION:
6
+
7
+ A little markup validator.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Validate (X)HTML files.
12
+
13
+ == SYNOPSIS:
14
+
15
+ === Valid file
16
+
17
+ $ pleasevalidate examples/valid.html
18
+ Valid: examples/valid.html
19
+
20
+ === Invalid file
21
+
22
+ $ pleasevalidate examples/invalid.html
23
+ Invalid: examples/invalid.html
24
+ 11 errors:
25
+ Line 8, Column 19: end tag for element "itle" which is not open
26
+ Line 10, Column 6: end tag for "title" omitted, but OMITTAG NO was specified
27
+ Line 8, Column 1: start tag was here
28
+ Line 16, Column 5: document type does not allow element "ul" here; missing one of "object", "ins", "del", "map", "button" start-tag
29
+ Line 20, Column 0: unclosed end-tag requires SHORTTAG YES
30
+ Line 20, Column 0: end tag for "li" omitted, but OMITTAG NO was specified
31
+ Line 17, Column 4: start tag was here
32
+ Line 20, Column 0: end tag for "ul" omitted, but OMITTAG NO was specified
33
+ Line 16, Column 2: start tag was here
34
+ Line 20, Column 0: end tag for "p" omitted, but OMITTAG NO was specified
35
+ Line 15, Column 2: start tag was here
36
+ Line 8, Column 20: XML Parsing Error: Opening and ending tag mismatch: title line 8 and itle
37
+ Line 20, Column 7: XML Parsing Error: expected '>'
38
+ Line 20, Column 7: XML Parsing Error: Premature end of data in tag p line 15
39
+ Line 20, Column 7: XML Parsing Error: Premature end of data in tag body line 12
40
+ Line 20, Column 7: XML Parsing Error: Premature end of data in tag html line 4
41
+
42
+ == REQUIREMENTS:
43
+
44
+ * nokogiri
45
+ * colored
46
+
47
+ == INSTALL:
48
+
49
+ rake install_gem
50
+
51
+ == LICENSE:
52
+
53
+ (The MIT License)
54
+
55
+ Copyright (c) 2009 Dylan Fogarty-MacDonald
56
+
57
+ Permission is hereby granted, free of charge, to any person obtaining
58
+ a copy of this software and associated documentation files (the
59
+ 'Software'), to deal in the Software without restriction, including
60
+ without limitation the rights to use, copy, modify, merge, publish,
61
+ distribute, sublicense, and/or sell copies of the Software, and to
62
+ permit persons to whom the Software is furnished to do so, subject to
63
+ the following conditions:
64
+
65
+ The above copyright notice and this permission notice shall be
66
+ included in all copies or substantial portions of the Software.
67
+
68
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
69
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
70
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
71
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
72
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
73
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
74
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/RakeFile ADDED
@@ -0,0 +1,31 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/please_validate'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('pleasevalidate', PleaseValidate::VERSION) do |p|
7
+ p.developer('Dylan Fogarty-MacDonald', 'dylan.fm@gmail.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt'
10
+ p.rubyforge_name = p.name
11
+ p.extra_deps = [
12
+ ['nokogiri'],
13
+ ['colored']
14
+ ]
15
+ p.extra_dev_deps = [
16
+ ['newgem', ">= #{::Newgem::VERSION}"]
17
+ ]
18
+
19
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
20
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
21
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
22
+ p.rsync_args = '-av --delete --ignore-errors'
23
+ end
24
+
25
+ require 'newgem/tasks' # load /tasks/*.rake
26
+ Dir['tasks/**/*.rake'].each { |t| load t }
27
+
28
+ require 'cucumber/rake/task'
29
+
30
+ Cucumber::Rake::Task.new
31
+
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created on 2009-3-9.
4
+ # Copyright (c) 2009. All rights reserved.
5
+
6
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/please_validate")
7
+
8
+ PleaseValidate::CLI.execute(STDOUT, ARGV)
data/cucumber.yml ADDED
@@ -0,0 +1 @@
1
+ default: features
@@ -0,0 +1,9 @@
1
+ Feature: Validate a file
2
+ In order to product valid markup
3
+ As a web developer
4
+ I want to validate a file
5
+
6
+ Scenario: Validate a file
7
+ Given there is a valid html file examples/valid.html
8
+ When the file is validated and the output is captured
9
+ Then the result should be displayed saying it's valid
@@ -0,0 +1,14 @@
1
+ Given /^there is a valid html file ([~\w\/.]+)$/ do |file|
2
+ @file = file
3
+ File.exist?(@file).should be_true
4
+ end
5
+
6
+ When /^the file is validated and the output is captured$/ do
7
+ PleaseValidate::CLI.execute(@stdout_io = StringIO.new, [@file])
8
+ @stdout_io.rewind
9
+ @stdout = @stdout_io.read
10
+ end
11
+
12
+ Then /^the result should be displayed saying it's valid$/ do
13
+ @stdout.should =~ /Valid: #{@file}/
14
+ end
@@ -0,0 +1,3 @@
1
+ require 'spec'
2
+ require 'stringio'
3
+ require File.dirname(__FILE__) + '/../../lib/please_validate'
@@ -0,0 +1,37 @@
1
+ Given /^there is a valid XHTML file$/ do
2
+ @file = 'examples/valid.html'
3
+ File.exist?(@file).should be_true
4
+ end
5
+
6
+ When /^the file is validated$/ do
7
+ @result = PleaseValidate::Validator.file(@file)
8
+ end
9
+
10
+ Then /^the response should not show errors$/ do
11
+ @result[:error_count].should == 0
12
+ @result[:errors].length.should == 0
13
+ end
14
+
15
+ Then /^the file should be valid$/ do
16
+ @result[:status].should == :valid
17
+ end
18
+
19
+ Given /^there is an invalid XHTML file$/ do
20
+ @file = 'examples/invalid.html'
21
+ File.exist?(@file).should be_true
22
+ end
23
+
24
+ Then /^the file should be invalid$/ do
25
+ @result[:status].should == :invalid
26
+ end
27
+
28
+ Then /^the response should show errors$/ do
29
+ @result[:error_count].should > 0
30
+ @result[:errors].length.should > 0
31
+ @result[:errors].each do |error|
32
+ error[:message].should_not be_empty
33
+ error[:line].should be_an_instance_of(Fixnum)
34
+ error[:col].should be_an_instance_of(Fixnum)
35
+ end
36
+ end
37
+
@@ -0,0 +1,17 @@
1
+ Feature: Validate markup
2
+ In order to be standards compliant
3
+ As a web developer
4
+ I want to validate my markup
5
+
6
+ Scenario: Validate valid markup
7
+ Given there is a valid XHTML file
8
+ When the file is validated
9
+ Then the response should not show errors
10
+ And the file should be valid
11
+
12
+ Scenario: Validate invalid markup
13
+ Given there is an invalid XHTML file
14
+ When the file is validated
15
+ Then the file should be invalid
16
+ And the response should show errors
17
+
@@ -0,0 +1,71 @@
1
+ require 'optparse'
2
+ require 'colored'
3
+
4
+ module PleaseValidate
5
+ class CLI
6
+ class << self
7
+ # Execute the CLI
8
+ def execute(stdout, arguments=[])
9
+ options = {}
10
+ mandatory_options = %w( )
11
+
12
+ parser = OptionParser.new do |opts|
13
+ opts.banner = <<-BANNER.gsub(/^ /,'')
14
+ Please Validate does some lovely markup validation of your (X)HTML files.
15
+
16
+ Usage: #{File.basename($0)} /path/to/file [options]
17
+
18
+ Options are:
19
+ BANNER
20
+ opts.separator ""
21
+ # opts.on("-p", "--path=PATH", String,
22
+ # "This is a sample message.",
23
+ # "For multiple lines, add more strings.",
24
+ # "Default: ~") { |arg| options[:path] = arg }
25
+ opts.on("-h", "--help",
26
+ "Show this help message.") { stdout.puts opts; exit }
27
+ opts.parse!(arguments)
28
+
29
+ if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
30
+ stdout.puts opts; exit
31
+ end
32
+ end
33
+ validation = new(arguments)
34
+ stdout.puts validation.msg
35
+ end
36
+ end
37
+
38
+ # Takes the requested file, passes it to validate for validation and displays the result with the display method
39
+ def initialize(arguments)
40
+ begin
41
+ @file = arguments[0]
42
+ @result = validate
43
+ @msg = display
44
+ rescue Exception => e
45
+ @msg = "Validation failed"
46
+ end
47
+ end
48
+
49
+ # Calls the validator class's file method for the requested file
50
+ def validate
51
+ PleaseValidate::Validator.file(@file)
52
+ end
53
+
54
+ # Displays the file validation's results
55
+ def display
56
+ msg = "#{@result[:status].to_s.capitalize}: #{@file}".send(@result[:status] == :valid ? :on_green : :on_red)
57
+ if @result[:status] == :invalid
58
+ msg += "\n#{@result[:error_count]} error#{@result[:error_count] == 1 ? nil:'s'}:"
59
+ @result[:errors].each do |error|
60
+ msg += "\nLine #{error[:line]}, Column #{error[:col]}".red + ": #{error[:message]}"
61
+ end
62
+ end
63
+ msg
64
+ end
65
+
66
+ #Displays the message for the command line result
67
+ def msg
68
+ @msg
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,55 @@
1
+ module PleaseValidate
2
+
3
+ class Validator
4
+
5
+ class << self
6
+ # Read requested file's contents and send to the w3c validator api then call the parse_response method to sort the response out.
7
+ def file(file_path)
8
+ begin
9
+ raise "Please specify a file to validate" unless file_path
10
+ raise "The specified file doesn't exist" unless File.exist?(file_path)
11
+ response = File.open(file_path, 'r') do |f|
12
+ Net::HTTP.start('validator.w3.org').post(
13
+ '/check',
14
+ "fragment=#{CGI.escape(f.read)}&output=xml",
15
+ {'Content-Type' => 'application/x-www-form-urlencoded'}
16
+ )
17
+ end
18
+ parse_response response
19
+ rescue Exception => e
20
+ puts e
21
+ raise
22
+ end
23
+
24
+ end
25
+
26
+ # Takes an XML response from the file method's call to the w3c and parses it into a nice little hash
27
+ def parse_response(response)
28
+ # Use Nokogiri to parse the xml
29
+ response_data = Nokogiri::XML.parse(response.body)
30
+ # Begin building the return hash
31
+ result = {
32
+ :status => response['x-w3c-validator-status'].downcase.to_sym,
33
+ :error_count => response['x-w3c-validator-errors'].to_i,
34
+ :errors => Array.new
35
+ }
36
+ # Get meta elements like encoding and doctype
37
+ response_data.css('result > meta *').each do |meta|
38
+ next unless %w{encoding doctype}.include? meta.name
39
+ result[meta.name.to_sym] = meta.content
40
+ end
41
+ # Get errors
42
+ response_data.css('result messages msg').each do |error|
43
+ result[:errors] << {
44
+ :message => error.content,
45
+ :line => error['line'].to_i,
46
+ :col => error['col'].to_i
47
+ }
48
+ end
49
+ result
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'cgi'
4
+ require 'nokogiri'
5
+
6
+ Dir.glob(File.join(File.dirname(__FILE__), "please_validate", "*.rb")).each { |f| require f }
7
+
8
+ module PleaseValidate
9
+ VERSION = '0.0.1'
10
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: DylanFM-pleasevalidate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dylan Fogarty-MacDonald
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-10 00:00:00 -07:00
13
+ default_executable: pleasevalidate
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: nokogiri
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: colored
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: newgem
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.2.3
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: hoe
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.0
54
+ version:
55
+ description: A little markup validator.
56
+ email:
57
+ - dylan.fm@gmail.com
58
+ executables:
59
+ - pleasevalidate
60
+ extensions: []
61
+
62
+ extra_rdoc_files:
63
+ - History.txt
64
+ - Manifest.txt
65
+ - README.rdoc
66
+ files:
67
+ - History.txt
68
+ - LICENSE
69
+ - Manifest.txt
70
+ - README.rdoc
71
+ - RakeFile
72
+ - bin/pleasevalidate
73
+ - cucumber.yml
74
+ - features/cli_validation.feature
75
+ - features/steps/cli_validation_steps.rb
76
+ - features/steps/helper.rb
77
+ - features/steps/validate_steps.rb
78
+ - features/validate.feature
79
+ - lib/please_validate.rb
80
+ - lib/please_validate/cli.rb
81
+ - lib/please_validate/validator.rb
82
+ has_rdoc: true
83
+ homepage: http://github.com/DylanFM/please_validate/tree
84
+ post_install_message: PostInstall.txt
85
+ rdoc_options:
86
+ - --main
87
+ - README.rdoc
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: "0"
101
+ version:
102
+ requirements: []
103
+
104
+ rubyforge_project: pleasevalidate
105
+ rubygems_version: 1.2.0
106
+ signing_key:
107
+ specification_version: 2
108
+ summary: A little markup validator.
109
+ test_files: []
110
+