bbc_standards 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,16 @@
1
+ = bbc_standards
2
+
3
+ * http://github.com/metade/bbc_standards
4
+
5
+ == DESCRIPTION:
6
+
7
+ BBCStandards provides rspec helpers for checking whether documents conform to the BBC FM&T Guidelines and Standards.
8
+
9
+ See http://www.bbc.co.uk/guidelines/futuremedia/technical/ for more details.
10
+
11
+ == SYNOPSIS:
12
+
13
+ == REQUIREMENTS:
14
+
15
+ * depends on markup_validity and raakt
16
+
@@ -0,0 +1,9 @@
1
+ require 'markup_validity'
2
+ require 'raakt'
3
+
4
+ require 'bbc_standards/validator'
5
+ require 'bbc_standards/rspec'
6
+
7
+ module BBCStandards
8
+ VERSION = '0.1.0'
9
+ end
@@ -0,0 +1,17 @@
1
+ module Spec
2
+ module Matchers
3
+ def obey_bbc_standards
4
+ Matcher.new :obey_bbc_standards do
5
+ validator = nil
6
+ match do |xhtml|
7
+ validator = BBCStandards::Validator.new xhtml
8
+ validator.valid?
9
+ end
10
+
11
+ failure_message_for_should do |actual|
12
+ validator.inspect
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,75 @@
1
+ module BBCStandards
2
+ class Validator
3
+ include Spec::Matchers
4
+
5
+ attr_reader :errors
6
+
7
+ def initialize(xml)
8
+ @xml = xml
9
+ @doc = Nokogiri::HTML(xml)
10
+ @errors = []
11
+ validate
12
+ end
13
+
14
+ def valid?
15
+ @errors.empty?
16
+ end
17
+
18
+ def inspect
19
+ lines = @xml.split("\n")
20
+ strings = []
21
+ errors.each do |error|
22
+ if error.line
23
+ start = error.line - [2, error.line].min
24
+ error_line = error.line == 0 ? 1 : error.line
25
+ strings << "Error on line: #{error_line}:"
26
+ strings << error.message.gsub(/\{[^\}]*\}/, '')
27
+ Range.new(start, error.line + 2).each { |number|
28
+ strings << "#{number + 1}: #{lines[number]}"
29
+ }
30
+ strings << ""
31
+ else
32
+ strings << error.message
33
+ end
34
+ end
35
+ strings.join("\n")
36
+ end
37
+
38
+ protected
39
+
40
+ def validate
41
+ test_validation
42
+ test_accessibility
43
+ test_bbc_standards
44
+ end
45
+
46
+ def test_validation
47
+ validator = MarkupValidity::Validator.new(@xml, MarkupValidity::Validator::XHTML1_STRICT)
48
+ @errors += validator.errors
49
+ end
50
+
51
+ def test_accessibility
52
+ rt = Raakt::Test.new(@xml)
53
+ @errors += rt.all.map { |e| Error.new(e.text) }
54
+ end
55
+
56
+ def test_bbc_standards
57
+ @errors << Error.new("Title is not valid") unless @xml =~ /<title>\w+( - )?([^<-]+)?( - )?([^<-]+)?<\/title>/
58
+ @errors << Error.new("Page doesn't have 1 <h1> tag") unless @doc.xpath('//h1').size == 1
59
+ @errors << Error.new("Page doesn't have any <h2> tags") unless @doc.xpath('//h2').size > 0
60
+ @errors << Error.new("Page is using <br> for whitespace") if @xml =~ /<br\/>\s*<br\/>/
61
+ %w(b i font marquee).each do |tag|
62
+ @errors << Error.new("Page is using <#{tag}>") if @doc.xpath("//#{tag}").any?
63
+ end
64
+ end
65
+ end
66
+
67
+ class Error
68
+ attr_accessor :message, :line
69
+
70
+ def initialize(message, line=nil)
71
+ @message = message
72
+ @line = line
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bbc_standards
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Sinclair
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-23 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
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:
26
+ email: metade@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ files:
34
+ - README
35
+ - lib/bbc_standards/rspec.rb
36
+ - lib/bbc_standards/validator.rb
37
+ - lib/bbc_standards.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/metade/bbc_standards
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --main
45
+ - README
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: What this thing does
67
+ test_files: []
68
+