-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
htmlproofer
executable file
·95 lines (80 loc) · 6.01 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
#!/usr/bin/env ruby
STDOUT.sync = true
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w( .. 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_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 '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', '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', '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 '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'
p.option 'only_4xx', '--only-4xx', 'Only reports errors for links that fall within the 4xx status code range'
p.option 'timeframe', '--timeframe <time>', String, 'A string representing the caching timeframe.'
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 'internal_domains', '--internal-domains domain1,[domain2,...]', Array, 'A comma-separated list of Strings containing domains that will be treated as internal urls.'
p.action do |args, opts|
args = ['.'] if args.empty?
path = args.first
options = {}
# prepare everything to go to proofer
p.options.select { |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?
# FIXME: this is gross
options[:validation] = {}
options[:validation][:report_script_embeds] = opts['report_script_embeds']
options[:validation][:report_missing_names] = opts['report_missing_names']
options[:validation][:report_invalid_tags] = opts['report_invalid_tags']
options[:cache] = {}
options[:cache][:timeframe] = opts['timeframe'] unless opts['timeframe'].nil?
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