alexdunae-w3c_validators 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ = CHANGELOG
2
+
3
+ == Version 0.9.0
4
+ * initial version
5
+
6
+ == Version 0.9.1
7
+ * fixed include path bug
8
+
9
+ == Version 0.9.2 (the Ryan King edition)
10
+ * added check for HTML validator being unable to parse a file
11
+
12
+ == Version 0.9.3 (the Ryan King/Jonathan Julian/Sylvain LaFleur edition)
13
+ * MarkupValidator::validate_file now returns file path as Results.uri
14
+ * All validate_file methods now allow a file path or an IO object (e.g. File)
15
+
16
+ == Version 1.0.0 (the GitHub edition)
17
+ * Gemspec is now generated by a rake task to allow dynamic file lists
18
+ * Updated docs
19
+
data/LICENSE ADDED
@@ -0,0 +1,60 @@
1
+ = W3C Validators License
2
+
3
+ W3C Validators is copyrighted free software by Alex Dunae (http://dunae.ca/).
4
+ You can redistribute it and/or modify it under either the terms of the GPL
5
+ or the conditions below:
6
+
7
+ 1. You may make and give away verbatim copies of the source form of the
8
+ software without restriction, provided that you duplicate all of the
9
+ original copyright notices and associated disclaimers.
10
+
11
+ 2. You may modify your copy of the software in any way, provided that
12
+ you do at least ONE of the following:
13
+
14
+ a) place your modifications in the Public Domain or otherwise
15
+ make them Freely Available, such as by posting said
16
+ modifications to Usenet or an equivalent medium, or by allowing
17
+ the author to include your modifications in the software.
18
+
19
+ b) use the modified software only within your corporation or
20
+ organization.
21
+
22
+ c) rename any non-standard executables so the names do not conflict
23
+ with standard executables, which must also be provided.
24
+
25
+ d) make other distribution arrangements with the author.
26
+
27
+ 3. You may distribute the software in object code or executable
28
+ form, provided that you do at least ONE of the following:
29
+
30
+ a) distribute the executables and library files of the software,
31
+ together with instructions (in the manual page or equivalent)
32
+ on where to get the original distribution.
33
+
34
+ b) accompany the distribution with the machine-readable source of
35
+ the software.
36
+
37
+ c) give non-standard executables non-standard names, with
38
+ instructions on where to get the original software distribution.
39
+
40
+ d) make other distribution arrangements with the author.
41
+
42
+ 4. You may modify and include the part of the software into any other
43
+ software (possibly commercial). But some files in the distribution
44
+ are not written by the author, so that they are not under this terms.
45
+
46
+ They are gc.c(partly), utils.c(partly), regex.[ch], st.[ch] and some
47
+ files under the ./missing directory. See each file for the copying
48
+ condition.
49
+
50
+ 5. The scripts and library files supplied as input to or produced as
51
+ output from the software do not automatically fall under the
52
+ copyright of the software, but belong to whomever generated them,
53
+ and may be sold commercially, and may be aggregated with this
54
+ software.
55
+
56
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
57
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
58
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59
+ PURPOSE.
60
+
@@ -0,0 +1,122 @@
1
+ = W3C Validators Gem README
2
+
3
+ W3C Validators is a Ruby wrapper for the World Wide Web Consortium's online
4
+ validation services.
5
+
6
+ It supports the markup validator, the feed validator and the CSS validator.
7
+
8
+ === Installation
9
+
10
+ gem sources -a http://gems.github.com
11
+ gem install alexdunae-w3c_validators
12
+
13
+ === Usage
14
+
15
+ There are three main validator classes available, the W3CValidators::MarkupValidator
16
+ (used for HTML), the W3CValidators::FeedValidator and the
17
+ W3CValidators::CSSValidator.
18
+
19
+ Each validator has offers three different validation methods.
20
+
21
+ * +validate_text+ methods take a string
22
+ * +validate_file+ methods take a path to a file or an IO object
23
+ * +validate_uri+ methods take a published URL
24
+
25
+ In addition, the W3CValidators::MarkupValidator has a validate_uri_quickly method, which
26
+ performs a HEAD request against the markup validation service. The Results
27
+ of this call give an error count but no error details.
28
+
29
+ ==== Using a local validator
30
+
31
+ Each of the three validators allows you to specify a custom path to the
32
+ validator. You can set your own validator like this:
33
+
34
+ validator = MarkupValidator.new(:validator_uri => 'http://localhost/check')
35
+
36
+ === Examples
37
+
38
+ ==== Example #1: Markup validator, local file
39
+
40
+ require 'w3c_validators'
41
+
42
+ include W3CValidators
43
+
44
+ @validator = MarkupValidator.new
45
+
46
+ # override the DOCTYPE
47
+ @validator.set_doctype!(:html32)
48
+
49
+ # turn on debugging messages
50
+ @validator.set_debug!(true)
51
+
52
+ file = File.dirname(__FILE__) + '/fixtures/markup.html'
53
+ results = @validator.validate_file(fp)
54
+
55
+ if results.errors.length > 0
56
+ results.errors.each do |err|
57
+ puts err.to_s
58
+ end
59
+ else
60
+ puts 'Valid!'
61
+ end
62
+
63
+ puts 'Debugging messages'
64
+
65
+ results.debug_messages.each do |key, value|
66
+ puts "#{key}: #{value}"
67
+ end
68
+
69
+
70
+ ==== Example #2: Feed validator, remote file
71
+
72
+ require 'w3c_validators'
73
+
74
+ include W3CValidators
75
+
76
+ @validator = FeedValidator.new
77
+
78
+ results = @validator.validate_uri('http://example.com/feed.xml')
79
+
80
+ if results.errors.length > 0
81
+ results.errors.each do |err|
82
+ puts err.to_s
83
+ end
84
+ else
85
+ puts 'Valid!'
86
+ end
87
+
88
+ ==== Example #3: CSS validator, text fragment
89
+
90
+ require 'w3c_validators'
91
+
92
+ include W3CValidators
93
+
94
+ @validator = CSSValidator.new
95
+
96
+ results = @validator.validate_text('body { margin: 0px; }')
97
+
98
+ if results.errors.length > 0
99
+ results.errors.each do |err|
100
+ puts err.to_s
101
+ end
102
+ else
103
+ puts 'Valid!'
104
+ end
105
+
106
+ === Tests
107
+
108
+ Run unit tests using <tt>rake test</tt>. Note that there is a one second delay
109
+ between each call to the W3C's validators per their request.
110
+
111
+
112
+ === Credits and code
113
+
114
+ Documentation is available at {http://code.dunae.ca/w3c_validators}[http://code.dunae.ca/w3c_validators].
115
+
116
+ Source is available on {GitHub}[https://github.com/alexdunae/w3c-validators]
117
+
118
+ Written by Alex Dunae ({dunae.ca}[http://dunae.ca/], e-mail 'code' at the same domain), 2007.
119
+
120
+ Thanks to {Ryan King}[http://theryanking.com/] for creating the 0.9.2 update.
121
+
122
+ Thanks to {Ryan King}[http://theryanking.com/], {Jonathan Julian}[http://jonathanjulian.org/] and Sylvain LaFleur for creating the 0.9.3 update.
@@ -0,0 +1,6 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'w3c_validators/validator'
3
+ require 'w3c_validators/markup_validator'
4
+ require 'w3c_validators/nu_validator'
5
+ require 'w3c_validators/feed_validator'
6
+ require 'w3c_validators/css_validator'
@@ -0,0 +1,80 @@
1
+ module W3CValidators
2
+ CSS_PROFILES = { :css1 => 'CSS 1',
3
+ :css2 => 'CSS 2.0',
4
+ :css21 => 'CSS 1.0',
5
+ :css3 => 'CSS 3.0',
6
+ :svg => 'SVG',
7
+ :svgbasic => 'SVG Basic',
8
+ :svgtiny => 'SVG Tiny',
9
+ :mobile => 'Mobile',
10
+ :atsc_tv => 'ATSC TV',
11
+ :tv => 'TV'
12
+ }
13
+
14
+ DOCTYPES = { :xhtml10_strict => 'XHTML 1.0 Strict',
15
+ :xhtml10_transitional => 'XHTML 1.0 Transitional',
16
+ :xhtml10_frameset => 'XHTML 1.0 Frameset',
17
+ :xhtml401_strict => 'HTML 4.01 Strict',
18
+ :xhtml401_transitional => 'HTML 4.01 Transitional',
19
+ :xhtml401_framest => 'HTML 4.01 Frameset',
20
+ :html5 => 'HTML 5',
21
+ :html32 => 'HTML 3.2',
22
+ :html20 => 'HTML 2.0',
23
+ :iso_html => 'ISO/IEC 15445:2000 (&quot;ISO HTML&quot;)',
24
+ :xhtml11 => 'XHTML 1.1',
25
+ :xhtml_basic10 => 'XHTML Basic 1.0',
26
+ :xhtml_print10 => 'XHTML-Print 1.0',
27
+ :xhtml11_plus_mathml20 => 'XHTML 1.1 plus MathML 2.0',
28
+ :xhtml11_plus_mathml20_plus_svg11 => 'XHTML 1.1 plus MathML 2.0 plus SVG 1.1',
29
+ :mathml20=> 'MathML 2.0',
30
+ :svg10 => 'SVG 1.0',
31
+ :svg11 => 'SVG 1.1',
32
+ :svg11tiny => 'SVG 1.1 Tiny',
33
+ :svg11_basic => 'SVG 1.1 Basic',
34
+ :smil10 => 'SMIL 1.0',
35
+ :smil20 => 'SMIL 2.0'
36
+ }
37
+
38
+ CHARSETS = { :utf_8 => 'utf-8',
39
+ :utf_16 => 'utf-16',
40
+ :iso_8859_1 => 'iso-8859-1',
41
+ :iso_8859_2 => 'iso-8859-2',
42
+ :iso_8859_3 => 'iso-8859-3',
43
+ :iso_8859_4 => 'iso-8859-4',
44
+ :iso_8859_5 => 'iso-8859-5',
45
+ :iso_8859_6i => 'iso-8859-6-i',
46
+ :iso_8859_7 => 'iso-8859-7',
47
+ :iso_8859_8 => 'iso-8859-8',
48
+ :iso_8859_8i => 'iso-8859-8-i',
49
+ :iso_8859_9 => 'iso-8859-9',
50
+ :iso_8859_10 => 'iso-8859-10',
51
+ :iso_8859_11 => 'iso-8859-11',
52
+ :iso_8859_13 => 'iso-8859-13',
53
+ :iso_8859_14 => 'iso-8859-14',
54
+ :iso_8859_15 => 'iso-8859-15',
55
+ :iso_8859_16 => 'iso-8859-16',
56
+ :us_ascci => 'us-ascii',
57
+ :euc_jp => 'euc-jp',
58
+ :shift_jis => 'shift_jis',
59
+ :iso_2022_hp => 'iso-2022-jp',
60
+ :euc_jr => 'euc-kr',
61
+ :ksk_5601 => 'ksc_5601',
62
+ :gb_2312 => 'gb2312',
63
+ :gb_18030 => 'gb18030',
64
+ :big5 => 'big5',
65
+ :big5_hkscs => 'big5-HKSCS',
66
+ :tis_620 => 'tis-620',
67
+ :koi8_r => 'koi8-r',
68
+ :koi8_u => 'koi8-u',
69
+ :iso_ir_111 => 'iso-ir-111',
70
+ :macintosh => 'macintosh',
71
+ :windows_1250 => 'windows-1250',
72
+ :windows_1251 => 'windows-1251',
73
+ :windows_1252 => 'windows-1252',
74
+ :windows_1253 => 'windows-1253',
75
+ :windows_1254 => 'windows-1254',
76
+ :windows_1255 => 'windows-1255',
77
+ :windows_1256 => 'windows-1256',
78
+ :windows_1257 => 'windows-1257'
79
+ }
80
+ end
@@ -0,0 +1,149 @@
1
+ module W3CValidators
2
+ class CSSValidator < Validator
3
+ CSS_VALIDATOR_URI = 'http://jigsaw.w3.org/css-validator/validator'
4
+
5
+ # Create a new instance of the CSSValidator.
6
+ #
7
+ # ==== Options
8
+ # You can pass in your own validator's URI (i.e.
9
+ # <tt>CSSValidator.new(:validator_uri => 'http://localhost/check')</tt>).
10
+ def initialize(options = {})
11
+ if options[:validator_uri]
12
+ @validator_uri = URI.parse(options[:validator_uri])
13
+ options.delete(options[:validator_uri])
14
+ else
15
+ @validator_uri = URI.parse(CSS_VALIDATOR_URI)
16
+ end
17
+ super(options)
18
+ end
19
+
20
+ # The CSS profile used for the validation.
21
+ #
22
+ # +charset+ can be a string or a symbl from the W3CValidators::CSS_PROFILES hash.
23
+ #
24
+ # ==== Example
25
+ # set_profile!('css1')
26
+ # set_profile!(:css1)
27
+ def set_profile!(profile)
28
+ if profile.kind_of?(Symbol)
29
+ if CSS_PROFILES.has_key?(profile)
30
+ profile = profile.to_s
31
+ else
32
+ return
33
+ end
34
+ end
35
+ @options[:profile] = profile
36
+ end
37
+
38
+ # The warning level, no for no warnings, 0 for less warnings, 1or 2 for more warnings
39
+ def set_warn_level!(level = 2)
40
+ warn_levels = ['0','1','2','no']
41
+ return unless warn_levels.include?(level.to_s.downcase)
42
+
43
+ @options[:warning] = level
44
+ end
45
+
46
+ # The language used for the response.
47
+ def set_language!(lang = 'en')
48
+ @options[:lang] = lang
49
+ end
50
+
51
+ # Validate the CSS of an URI.
52
+ #
53
+ # Returns W3CValidators::Results.
54
+ def validate_uri(uri)
55
+ return validate({:uri => uri})
56
+ end
57
+
58
+ # Validate the CSS of a string.
59
+ #
60
+ # Returns W3CValidators::Results.
61
+ def validate_text(text)
62
+ return validate({:text => text})
63
+ end
64
+
65
+ # Validate the CSS of a local file.
66
+ #
67
+ # +file_path+ may be either the fully-expanded path to the file or
68
+ # an IO object (like File).
69
+ #
70
+ # Returns W3CValidators::Results.
71
+ def validate_file(file_path)
72
+ if file_path.respond_to? :read
73
+ src = file_path.read
74
+ else
75
+ src = read_local_file(file_path)
76
+ end
77
+ return validate_text(src)
78
+ end
79
+
80
+
81
+ protected
82
+ def validate(options) # :nodoc:
83
+ options = get_request_options(options)
84
+ response = send_request(options, :get)
85
+ @results = parse_soap_response(response.body)
86
+ @results
87
+ end
88
+
89
+ # Perform sanity checks on request params
90
+ def get_request_options(options) # :nodoc:
91
+ options = @options.merge(options)
92
+
93
+ options[:output] = SOAP_OUTPUT_PARAM
94
+
95
+ unless options[:uri] or options[:text]
96
+ raise ArgumentError, "an uri or text is required."
97
+ end
98
+
99
+ # URI should be a string. If it is a URI object, .to_s will
100
+ # be seamless; if it is not an exception will be raised.
101
+ if options[:uri] and not options[:uri].kind_of?(String)
102
+ options[:uri] = options[:uri].to_s
103
+ end
104
+
105
+ options
106
+ end
107
+
108
+
109
+ def parse_soap_response(response) # :nodoc:
110
+ doc = REXML::Document.new(response)
111
+
112
+ result_params = {}
113
+
114
+ {:uri => 'uri', :checked_by => 'checkedby', :validity => 'validity', :css_level => 'csslevel'}.each do |local_key, remote_key|
115
+ if val = doc.elements["//*[local-name()='cssvalidationresponse']/*[local-name()='#{remote_key.to_s}']"]
116
+ result_params[local_key] = val.text
117
+ end
118
+ end
119
+
120
+ results = Results.new(result_params)
121
+
122
+ ['warninglist', 'errorlist'].each do |list_type|
123
+ doc.elements.each("//*[local-name()='#{list_type.to_s}']") do |message_list|
124
+
125
+ if uri_node = message_list.elements["*[local-name()='uri']"]
126
+ uri = uri_node.text
127
+ end
128
+
129
+ [:warning, :error].each do |msg_type|
130
+ message_list.elements.each("*[local-name()='#{msg_type.to_s}']") do |message|
131
+ message_params = {}
132
+ message.each_element_with_text do |el|
133
+ message_params[el.name.to_sym] = el.text
134
+ end
135
+ message_params[:uri] = uri
136
+ results.add_message(msg_type, message_params)
137
+ end
138
+ end
139
+ end
140
+ end
141
+ return results
142
+
143
+ rescue Exception => e
144
+ handle_exception e
145
+ end
146
+
147
+
148
+ end
149
+ end
@@ -0,0 +1,4 @@
1
+ module W3CValidators
2
+ class ValidatorUnavailable < StandardError; end
3
+ class ParsingError < StandardError; end
4
+ end