Class: PageValidations::HTMLValidation

Inherits:
Object
  • Object
show all
Defined in:
lib/html_validation/page_validations.rb

Constant Summary collapse

@@default_tidy_flags =
['-quiet', '-indent']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder_for_data = nil, tidy_flags = [], options = {}) ⇒ HTMLValidation

:folder_for_data: Storage folder path to save, and look for, result files. :options: hash passed directly to HTMLValidationResult



67
68
69
70
71
# File 'lib/html_validation/page_validations.rb', line 67

def initialize(folder_for_data = nil,  tidy_flags = [], options={})
  self.data_folder  = folder_for_data || default_result_file_path
  @tidy_flags       = tidy_flags
  @options          = result_attributes_and_values.merge options
end

Class Method Details

.default_tidy_flagsObject

Default command line flags to pass when tidy is executed. all tidy flags flags as an array of strings like [‘–show-warnings false’] Note: Pass the entire string for each setting, NOT a name value pair settings are available from: tidy -h



78
79
80
# File 'lib/html_validation/page_validations.rb', line 78

def self.default_tidy_flags
  @@default_tidy_flags
end

.default_tidy_flags=(val) ⇒ Object



82
83
84
# File 'lib/html_validation/page_validations.rb', line 82

def self.default_tidy_flags=(val)
  @@default_tidy_flags = val
end

.result_attributes(*names) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/html_validation/page_validations.rb', line 24

def self.result_attributes *names
  @@result_attributes = names.each do |name|
    class_eval(%Q{
      def self.#{name}=(obj)
        @@#{name} = obj
      end

      def self.#{name}
        @@#{name}
      end
    })
  end
end

.show_warnings=(val) ⇒ Object

Shortcut to enable/disable whether warnings are checked in Tidy. Note that changing this setting (or any flag) can change how the result files are seen in terms of their acceptance. Meaning, if you have previously accepted a page with warnings either on or off, you will probably need to re-run the ‘html_validation review’ command following your first run with the new setting.



92
93
94
95
96
97
98
# File 'lib/html_validation/page_validations.rb', line 92

def self.show_warnings=(val)
  if val
    @@default_tidy_flags.delete('--show-warnings false') # remove the flag (rely on default: true)
  else
    (@@default_tidy_flags << '--show-warnings false').uniq!
  end
end

Instance Method Details

#data_folder=(path) ⇒ Object



119
120
121
122
# File 'lib/html_validation/page_validations.rb', line 119

def data_folder=(path)
  FileUtils.mkdir_p(path)
  @data_folder = path
end

#default_result_file_pathObject



124
125
126
127
128
129
130
# File 'lib/html_validation/page_validations.rb', line 124

def default_result_file_path
  posix      = RbConfig::CONFIG['host_os'] =~ /(darwin|linux)/
  rootpath   = Rails.root if defined?(Rails)
  rootpath ||= ::PageValidations.data_path if ::PageValidations.data_path
  rootpath ||= posix ? '/tmp/' : "c:\\tmp\\"
  File.join(rootpath, '.validation')
end

#each_exceptionObject

For each stored exception, yield an HTMLValidationResult object to allow the user to call .accept! on the exception if it is OK.



103
104
105
106
107
108
109
110
# File 'lib/html_validation/page_validations.rb', line 103

def each_exception
  Dir.chdir(@data_folder)
  Dir.glob("*.exceptions.txt").each do |file|
    if File.open(File.join(@data_folder, file), 'r').read != ''
      yield HTMLValidationResult.load_from_files(file.gsub('.exceptions.txt',''))
    end
  end
end

#result_attributes_and_valuesObject



38
39
40
41
42
43
44
# File 'lib/html_validation/page_validations.rb', line 38

def result_attributes_and_values
  {}.tap do |res|
    @@result_attributes.each do |attr|
      res[attr] = self.class.send attr
    end
  end
end

#validation(html, resource_name) ⇒ Object

:html: The html to validate :resource: Used to create a name for the result file, nothing more. Usually a URL.



114
115
116
117
# File 'lib/html_validation/page_validations.rb', line 114

def validation(html, resource_name)
  resource_data_path = File.join(@data_folder, filenameize(resource_name))
  HTMLValidationResult.new(resource_name, html, resource_data_path,  @tidy_flags, @options)
end