-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
htmlproofer
executable file
·112 lines (94 loc) · 7.35 KB
/
htmlproofer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env ruby
# frozen_string_literal: true
STDOUT.sync = true
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'html-proofer'
require 'mercenary'
Mercenary.program(:htmlproofer) do |p|
p.version HTMLProofer::VERSION
p.description %(Test your rendered HTML files to make sure they're accurate.)
p.syntax 'htmlproofer PATH [options]'
p.description 'Runs the HTML-Proofer suite on the files in PATH. For more details, see the README.'
p.option 'allow_missing_href', '--allow-missing-href', 'If `true`, does not flag `a` tags missing `href` (this is the default for HTML5).'
p.option 'allow_hash_href', '--allow-hash-href', 'If `true`, ignores the `href="#"`'
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, 'A comma-separated list of Strings or RegExps containing `img`s whose missing `alt` tags are safe to ignore'
p.option 'assume_extension', '--assume-extension', 'Automatically add extension (e.g. `.html`) to file paths, to allow extensionless URLs (as supported by Jekyll 3 and GitHub Pages) (default: `false`).'
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 webpage 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 'check_img_http', '--check-img-http', 'Fails an image if it\'s marked as `http` (default: `false`).'
p.option 'check_opengraph', '--check-opengraph', 'Enables the Open Graph checker (default: `false`).'
p.option 'check_sri', '--check-sri', 'Check that `<link>` and `<script>` external resources do use SRI (default: `false`).'
p.option 'directory_index_file', '--directory-index-file <filename>', String, 'Sets the file to look for when a link refers to a directory. (default: `index.html`)'
p.option 'disable_external', '--disable-external', 'If `true`, does not run the external link checker, which can take a lot of time (default: `false`)'
p.option 'empty_alt_ignore', '--empty-alt-ignore', 'If `true`, ignores images with empty alt tags'
p.option 'error_sort', '--error-sort <sort>', String, 'Defines the sort order for error output. Can be `:path`, `:desc`, or `:status` (default: `:path`).'
p.option 'enforce_https', '--enforce-https', 'Fails a link if it\'s not marked as `https` (default: `false`).'
p.option 'extension', '--extension <ext>', String, 'The extension of your HTML files including the dot. (default: `.html`)'
p.option 'external_only', '--external_only', 'Only checks problems with external references'
p.option 'file_ignore', '--file-ignore file1,[file2,...]', Array, 'A comma-separated list of Strings or RegExps containing file paths that are safe to ignore'
p.option 'http_status_ignore', '--http-status-ignore 123,[xxx, ...]', Array, 'A comma-separated list of numbers representing status codes to ignore.'
p.option 'internal_domains', '--internal-domains domain1,[domain2,...]', Array, 'A comma-separated list of Strings containing domains that will be treated as internal urls.'
p.option 'report_invalid_tags', '--report-invalid-tags', 'Ignore `check_html` errors associated with unknown markup (default: `false`)'
p.option 'report_missing_names', '--report-missing-names', 'Ignore `check_html` errors associated with missing entities (default: `false`)'
p.option 'report_script_embeds', '--report-script-embeds', 'Ignore `check_html` errors associated with `script`s (default: `false`)'
p.option 'log_level', '--log-level <level>', String, 'Sets the logging level, as determined by Yell. One of `:debug`, `:info`, `:warn`, `:error`, or `:fatal`. (default: `:info`)'
p.option 'only_4xx', '--only-4xx', 'Only reports errors for links that fall within the 4xx status code range'
p.option 'storage_dir', '--storage-dir PATH', String, 'Directory where to store the cache log (default: "tmp/.htmlproofer")'
p.option 'timeframe', '--timeframe <time>', String, 'A string representing the caching timeframe.'
p.option 'typhoeus_config', '--typhoeus-config CONFIG', String, 'JSON-formatted string of Typhoeus config. Will override the html-proofer defaults.'
p.option 'url_ignore', '--url-ignore link1,[link2,...]', Array, 'A comma-separated list of Strings or RegExps containing URLs that are safe to ignore. It affects all HTML attributes. Note that non-HTTP(S) URIs are always ignored'
p.option 'url_swap', '--url-swap re:string,[re:string,...]', Array, 'A comma-separated list containing key-value pairs of `RegExp => String`. It transforms URLs that match `RegExp` into `String` via `gsub`. The escape sequences `\\:` should be used to produce literal `:`s.'
p.option 'root_dir', '--root-folder PATH', String, 'The absolute path to the directory serving your html-files. Used when running html-proofer on a file, rather than a directory.'
p.action do |args, opts|
args = ['.'] if args.empty?
path = args.first
options = {}
# prepare everything to go to proofer
p.options.reject { |o| opts[o.config_key].nil? }.each do |option|
if opts[option.config_key].is_a?(Array)
opts[option.config_key] = opts[option.config_key].map { |i| HTMLProofer::Configuration.to_regex?(i) }
end
options[option.config_key.to_sym] = opts[option.config_key]
end
# some minor manipulation of a special option
unless opts['url_swap'].nil?
options[:url_swap] = {}
opts['url_swap'].each do |s|
splt = s.split(/(?<!\\):/, 2)
re = splt[0].gsub(/\\:/, ':')
string = splt[1].gsub(/\\:/, ':')
options[:url_swap][Regexp.new(re)] = string
end
end
options[:error_sort] = opts['error-sort'].to_sym unless opts['error-sort'].nil?
options[:log_level] = opts['log_level'].to_sym unless opts['log_level'].nil?
options[:validation] = HTMLProofer::Configuration::VALIDATION_DEFAULTS.dup
options[:validation][:report_script_embeds] = opts['report_script_embeds'] unless opts['report_script_embeds'].nil?
options[:validation][:report_missing_names] = opts['report_missing_names'] unless opts['report_missing_names'].nil?
options[:validation][:report_invalid_tags] = opts['report_invalid_tags'] unless opts['report_invalid_tags'].nil?
unless opts['typhoeus_config'].nil?
options[:typhoeus] = HTMLProofer::Configuration.parse_json_option('typhoeus_config', opts['typhoeus_config'])
end
unless opts['timeframe'].nil?
options[:cache] ||= {}
options[:cache][:timeframe] = opts['timeframe'] unless opts['timeframe'].nil?
end
unless opts['storage_dir'].nil?
options[:cache] ||= {}
options[:cache][:storage_dir] = opts['storage_dir'] unless opts['storage_dir'].nil?
end
options[:http_status_ignore] = Array(options[:http_status_ignore]).map(&:to_i)
paths = path.split(',')
if opts['as_links']
links = path.delete(' ').split(',')
HTMLProofer.check_links(links, options).run
elsif File.directory?(paths.first)
HTMLProofer.check_directories(paths, options).run
else
HTMLProofer.check_file(path, options).run
end
end
end