class PageValidations::HTMLValidation
Public Class Methods
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
# File lib/html_validation/page_validations.rb, line 82 def self.default_tidy_flags=(val) @@default_tidy_flags = val end
: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
# 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
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
# File lib/html_validation/page_validations.rb, line 119 def data_folder=(path) FileUtils.mkdir_p(path) @data_folder = path end
# 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
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
# 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
: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
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