#!/usr/bin/env ruby STDOUT.sync = true $LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w( .. lib )) require 'html/proofer' require 'mercenary' require 'rubygems' def to_regex?(item) if item.start_with? '/' and item.end_with? '/' Regexp.new item[1...-1] else item end end Mercenary.program(:htmlproof) do |p| p.version Gem::Specification.load(File.join(File.dirname(__FILE__), '..', 'html-proofer.gemspec')).version p.description %(Test your rendered HTML files to make sure they're accurate.) p.syntax 'htmlproof PATH [options]' p.description 'Runs the HTML-Proofer suite on the files in PATH. For more details, see the README.' p.option 'as_links', '--as-links', 'Assumes that `PATH` is a comma-separated array of links to check.' p.option 'alt_ignore', '--alt-ignore image1,[image2,...]', Array, 'Comma-separated list of Strings or RegExps containing `img`s whose missing `alt` tags are safe to ignore' p.option 'empty_alt_ignore', '--empty-alt-ignore', 'Ignores images with empty alt tags.' p.option 'checks_to_ignore', '--checks-to-ignore check1,[check2,...]', Array, ' An array of Strings indicating which checks you\'d like to not perform.' p.option 'check_external_hash', '--check-external-hash', 'Checks whether external hashes exist (even if the website exists). This slows the checker down (default: `false`).' p.option 'check_favicon', '--check-favicon', 'Enables the favicon checker (default: `false`).' p.option 'check_html', '--check-html', 'Enables HTML validation errors from Nokogiri (default: `false`).' p.option 'directory_index_file', '--directory-index-file', String, 'Sets the file to look for when a link refers to a directory. (default: `index.html`)' p.option 'disable_external', '--disable-external', 'Disables the external link checker (default: `false`)' p.option 'error_sort', '--error-sort SORT', 'Defines the sort order for error output. Can be `path`, `desc`, or `status` (default: `path`).' p.option 'ext', '--ext EXT', String, 'The extension of your HTML files (default: `.html`)' p.option 'file_ignore', '--file-ignore file1,[file2,...]', Array, 'Comma-separated list of Strings or RegExps containing file paths that are safe to ignore' p.option 'href_ignore', '--href-ignore link1,[link2,...]', Array, 'Comma-separated list of Strings or RegExps containing `href`s that are safe to ignore.' p.option 'href_swap', '--href-swap re:string,[re:string,...]', Array, 'Comma-separated list of key-value pairs of `RegExp:String`. Transforms links matching `RegExp` into `String`' p.option 'ignore_script_errors', '--ignore-script-errors', 'Ignore `check_html` errors associated with `script`s (default: `false`)' p.option 'only_4xx', '--only-4xx', 'Only reports errors for links that fall within the 4x status code range.' p.option 'url_ignore', '--url-ignore link1,[link2,...]', Array, 'Comma-separated list of Strings or RegExps containing URLs that are safe to ignore.' p.option 'verbose', '--verbose', 'Enables more verbose logging.' p.action do |args, opts| args = ['.'] if args.empty? path = args.first options = {} # prepare every to go to proofer p.options.select { |o| !opts[o.config_key].nil? }.each do |option| if option.return_type.to_s == 'Array' # TODO: is_a? doesn't work here? opts[option.config_key] = opts[option.config_key].map { |i| to_regex?(i) } end options[option.config_key.to_sym] = opts[option.config_key] end # some minor manipulation of a special option unless opts['href_swap'].nil? options[:href_swap] = {} opts['href_swap'].each do |s| pair = s.split(':', 2) options[:href_swap][Regexp.new(pair[0])] = pair[1] end end options[:error_sort] = opts['error-sort'].to_sym unless opts['error-sort'].nil? path = path.delete(' ').split(',') if opts['as_links'] HTML::Proofer.new(path, options).run end end