class PageValidations::HTMLValidation

Public Class Methods

default_tidy_flags() click to toggle source

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

# File lib/html_validation/page_validations.rb, line 78
def self.default_tidy_flags
  @@default_tidy_flags
end
default_tidy_flags=(val) click to toggle source
# File lib/html_validation/page_validations.rb, line 82
def self.default_tidy_flags=(val)
  @@default_tidy_flags = val
end
new(folder_for_data = nil, tidy_flags = [], options={}) click to toggle source

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

# 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
result_attributes(*names) click to toggle source
# 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) click to toggle source

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.

# 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

Public Instance Methods

data_folder=(path) click to toggle source
# File lib/html_validation/page_validations.rb, line 119
def data_folder=(path)
  FileUtils.mkdir_p(path)
  @data_folder = path
end
default_result_file_path() click to toggle source
# File lib/html_validation/page_validations.rb, line 124
def default_result_file_path
  posix      = RbConfig::CONFIG['host_os'] =~ /(darwin|linux)/
  rootpath   = ::PageValidations.data_path if ::PageValidations.data_path
  rootpath ||= Rails.root if defined?(Rails)
  rootpath ||= posix ? '/tmp/' : "c:\\tmp\\"
  File.join(rootpath, '.validation')
end
each_exception() { |load_from_files(gsub('.txt',''))| ... } click to toggle source

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

# 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_values() click to toggle source
# 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) click to toggle source

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

# 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

Private Instance Methods

filenameize(path) click to toggle source

Takes a url or filepath qne trims and sanitizes it for use as a filename.

# File lib/html_validation/page_validations.rb, line 135
def filenameize(path)
  new_path = path.gsub(/www.|^(http:\/\/|\/|C:\\)/, '')
  new_path.gsub(/[^0-9A-Za-z.]/, '_')
end