Skip to content

Commit

Permalink
Merge pull request #818 from gjtorikian/color-in-non-tty
Browse files Browse the repository at this point in the history
Let Rainbow handle color management
  • Loading branch information
gjtorikian committed May 6, 2024
2 parents 071fa22 + 61c6658 commit da1dd16
Show file tree
Hide file tree
Showing 31 changed files with 106 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.2.1
3.3.0
24 changes: 19 additions & 5 deletions lib/html_proofer/attribute/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def unknown_extension?

def ignore?
return true if /^javascript:/.match?(@url)
return true if ignores_pattern?(@runner.options[:ignore_urls])

true if ignores_pattern?(@runner.options[:ignore_urls])
end

def valid?
Expand Down Expand Up @@ -220,11 +221,24 @@ def without_hash
@url.to_s.sub(/##{hash}/, "")
end

# catch any obvious issues, like strings in port numbers
# catch any obvious issues
private def clean_url!
return if @url =~ /^([!#{Regexp.last_match(0)}-;=?-\[\]_a-z~]|%[0-9a-fA-F]{2})+$/

@url = Addressable::URI.parse(@url).normalize.to_s
parsed_url = Addressable::URI.parse(@url)
url = if parsed_url.scheme.nil?
parsed_url
else
parsed_url.normalize
end.to_s

# normalize strips this off, which causes issues with cache
@url = if @url.end_with?("/") && !url.end_with?("/")
"#{url}/"
elsif !@url.end_with?("/") && url.end_with?("/")
url.chop
else
url
end
rescue Addressable::URI::InvalidURIError # rubocop:disable Lint/SuppressedException; error will be reported at check time
end

private def swap_urls!
Expand Down
7 changes: 4 additions & 3 deletions lib/html_proofer/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def initialize(runner, options)
end

def parsed_timeframe(timeframe)
return nil if timeframe.nil?
return if timeframe.nil?

time, date = timeframe.match(/(\d+)(\D)/).captures
time = time.to_i
Expand Down Expand Up @@ -252,7 +252,7 @@ def size(type)
SECONDS_PER_HOUR = 3600
SECONDS_PER_DAY = 86400
SECONDS_PER_WEEK = 604800
SECONDS_PER_MONTH = 2629746 # 1/12 of a gregorian year
SECONDS_PER_MONTH = 2629746 # 1/12 of a gregorian year

private def time_ago(measurement, unit)
case unit
Expand All @@ -269,7 +269,8 @@ def size(type)

private def url_matches_type?(url, type)
return true if type == :internal && url !~ URI_REGEXP
return true if type == :external && url =~ URI_REGEXP

true if type == :external && url =~ URI_REGEXP
end

private def cleaned_url(url)
Expand Down
4 changes: 2 additions & 2 deletions lib/html_proofer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ def parse_cli_options(args)
arg.split(",").each_with_object({}) do |s, hsh|
split = s.split(/(?<!\\):/, 2)

re = split[0].gsub(/\\:/, ":")
string = split[1].gsub(/\\:/, ":")
re = split[0].gsub("\\:", ":")
string = split[1].gsub("\\:", ":")
hsh[Regexp.new(re)] = string
end
end
Expand Down
14 changes: 7 additions & 7 deletions lib/html_proofer/element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def link_attribute
end

def meta_content
return nil unless meta_tag?
return unless meta_tag?

@node["content"]
end
Expand All @@ -37,7 +37,7 @@ def meta_tag?
end

def src
return nil if !img_tag? && !script_tag? && !source_tag?
return if !img_tag? && !script_tag? && !source_tag?

@node["src"]
end
Expand All @@ -51,7 +51,7 @@ def script_tag?
end

def srcset
return nil if !img_tag? && !source_tag?
return if !img_tag? && !source_tag?

@node["srcset"]
end
Expand All @@ -61,7 +61,7 @@ def source_tag?
end

def href
return nil if !a_tag? && !link_tag?
return if !a_tag? && !link_tag?

@node["href"]
end
Expand Down Expand Up @@ -96,7 +96,7 @@ def multiple_srcsets?
IMAGE_CANDIDATE_REGEX = /\s*([^,]\S*[^,](?:\s+[^,]+)?)\s*(?:,|$)/

def srcsets
return nil if blank?(srcset)
return if blank?(srcset)

srcset.split(IMAGE_CANDIDATE_REGEX).select.with_index do |_part, idx|
idx.odd?
Expand All @@ -112,7 +112,7 @@ def multiple_sizes?
end

def srcsets_wo_sizes
return nil if blank?(srcsets)
return if blank?(srcsets)

srcsets.map do |srcset|
srcset.split(" ").first
Expand All @@ -133,7 +133,7 @@ def ignore?

attrs = @runner.options[:swap_attributes][@node.name]

return true unless blank?(attrs)
true unless blank?(attrs)
end

private def swap_attributes!
Expand Down
4 changes: 2 additions & 2 deletions lib/html_proofer/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def colorize(level, message)
:red
end

if (STDOUT_LEVELS.include?(level) && $stdout.isatty) ||
(STDERR_LEVELS.include?(level) && $stderr.isatty)
if STDOUT_LEVELS.include?(level) ||
STDERR_LEVELS.include?(level)
Rainbow(message).send(color)
else
message
Expand Down
4 changes: 2 additions & 2 deletions lib/html_proofer/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def load_external_cache
end

private def load_cache(type)
ivar = instance_variable_get("@#{type}_urls")
ivar = instance_variable_get(:"@#{type}_urls")

existing_urls_count = @cache.size(type)
cache_text = pluralize(existing_urls_count, "#{type} link", "#{type} links")
Expand All @@ -249,7 +249,7 @@ def load_external_cache

private def format_checks_list(checks)
checks.map do |check|
check.sub(/HTMLProofer::Check::/, "")
check.sub("HTMLProofer::Check::", "")
end.sort.join(", ")
end
end
Expand Down
4 changes: 1 addition & 3 deletions lib/html_proofer/url_validator/internal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ def run_internal_link_checker(links)
decoded_href_hash = Addressable::URI.unescape(href_hash)
fragment_ids = [href_hash, decoded_href_hash]
# https://www.w3.org/TR/html5/single-page.html#scroll-to-fragid
return true if fragment_ids.include?("top")

nil
true if fragment_ids.include?("top")
end

private def hash_exists_in_html?(href_hash, html)
Expand Down
3 changes: 2 additions & 1 deletion spec/html-proofer/attribute/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

require "spec_helper"

describe HTMLProofer::Attribute::Url do
describe "Attribute::Url" do
let(:runner) { HTMLProofer::Runner.new("") }
let(:described_class) { HTMLProofer::Attribute::Url }

describe "#ignores_pattern_check" do
it "works for regex patterns" do
Expand Down
4 changes: 3 additions & 1 deletion spec/html-proofer/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

require "spec_helper"

describe HTMLProofer::Cache do
describe "Cache" do
let(:cache_fixture_dir) { File.join(FIXTURES_DIR, "cache") }

let(:default_cache_options) { { storage_dir: cache_fixture_dir } }

let(:file_runner) { HTMLProofer.check_file(test_file, runner_options) }
let(:link_runner) { HTMLProofer.check_links(links, runner_options) }

let(:described_class) { HTMLProofer::Cache }

def read_cache(cache_filename)
JSON.parse(File.read(File.join(cache_fixture_dir, cache_filename)))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe HTMLProofer::Check::Favicon do
describe "Check::Favicon" do
it "ignores for absent favicon by default" do
absent = File.join(FIXTURES_DIR, "favicon", "favicon_absent.html")
expect(run_proofer(absent, :file).failed_checks).to(eq([]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe HTMLProofer::Check::Images do
describe "Check::Images" do
it "passes for existing external images" do
external_image_filepath = File.join(FIXTURES_DIR, "images", "existing_image_external.html")
proofer = run_proofer(external_image_filepath, :file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe HTMLProofer::Check::Links do
describe "Check::Links" do
it "fails for broken internal hash (even if the file exists)" do
broken_hash_external_filepath = File.join(FIXTURES_DIR, "links", "broken_hash_external_file.html")
proofer = run_proofer(broken_hash_external_filepath, :file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe HTMLProofer::Check::OpenGraph do
describe "Check::OpenGraph" do
it "passes for existing external url" do
url_valid = File.join(FIXTURES_DIR, "opengraph", "url-valid.html")
proofer = run_proofer(url_valid, :file, checks: ["OpenGraph"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe HTMLProofer::Check::Scripts do
describe "Check::Scripts" do
it "fails for src missing the protocol" do
file = File.join(FIXTURES_DIR, "scripts", "script_missing_protocol.html")
proofer = run_proofer(file, :file)
Expand Down
4 changes: 2 additions & 2 deletions spec/html-proofer/check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def run
end
end

describe HTMLProofer::Reporter do
it "supports a custom reporter" do
describe "Check" do
it "supports a custom check" do
file = File.join(FIXTURES_DIR, "links", "mailto_octocat.html")
cassette_name = make_cassette_name(file, {})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "spec_helper"

describe HTMLProofer::CLI do
describe "CLI" do
it "works with allow-hash-href" do
broken = File.join(FIXTURES_DIR, "links", "hash_href.html")
output = make_bin("--allow-hash-href #{broken}")
Expand Down Expand Up @@ -215,14 +215,14 @@

it "has only one UA" do
http = make_bin(%|--typhoeus='{"verbose":true,"headers":{"User-Agent":"Mozilla/5.0 (Macintosh; My New User-Agent)"}}' --as-links https://linkedin.com|)
expect(http.scan(/User-Agent: Typhoeus/).count).to(eq(0))
expect(http.scan("User-Agent: Typhoeus").count).to(eq(0))
expect(http.scan(%r{User-Agent: Mozilla/5.0 \(Macintosh; My New User-Agent\)}i).count).to(eq(2))
end

it "supports hydra" do
http = make_bin(%(--hydra '{"max_concurrency": 5}' http://www.github.com --as-links))
expect(http.scan(/max_concurrency is invalid/).count).to(eq(0))
expect(http.scan(/successfully/).count).to(eq(1))
expect(http.scan("max_concurrency is invalid").count).to(eq(0))
expect(http.scan("successfully").count).to(eq(1))
end
end

Expand All @@ -231,7 +231,7 @@
new_time = Time.local(2022, 1, 6, 12, 0, 0)
Timecop.freeze(new_time) do
http = make_bin(%(--cache '{"timeframe": { "external": "1d"}}' http://www.github.com --as-links))
expect(http.scan(/successfully/).count).to(eq(1))
expect(http.scan("successfully").count).to(eq(1))
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
require "spec_helper"
require "html-proofer"

describe HTMLProofer::Configuration do
describe "Configuration" do
let(:described_class) { HTMLProofer::Configuration }

it "Throws an error when the option name is not a string" do
expect do
described_class.new.parse_json_option(
Expand Down
3 changes: 2 additions & 1 deletion spec/html-proofer/element_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

require "spec_helper"

describe HTMLProofer::Element do
describe "Element" do
let(:runner) { HTMLProofer::Runner.new("") }
let(:described_class) { HTMLProofer::Element }

describe "#initialize" do
it "accepts the xmlns attribute" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{"/somewhere.html":{"time":"2022-01-06 12:00:00 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":7}]}}}
{"version":2,"internal":{"/somewhere.html":{"time":"2022-01-06 12:00:00 -0700","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":7}]}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/external_example.html","line":7}]}}}
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/external_example.html","line":7}]}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian":{"time":"2022-01-06 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/some_link.html","line":7}]}}}
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian":{"time":"2022-01-06 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/some_link.html","line":7}]}}}
2 changes: 1 addition & 1 deletion spec/html-proofer/fixtures/cache/version_2/.runner.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{},"external":{"https://www.github.com":{"time":"2022-02-17 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/links/_site/folder.html/index.html","line":4}]}}}
{"version":2,"internal":{},"external":{"https://www.github.com":{"time":"2022-02-17 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/links/_site/folder.html/index.html","line":4}]}}}
2 changes: 1 addition & 1 deletion spec/html-proofer/fixtures/links/unicode_domain.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

<body>

<p><a href="https://αναπτυξηεφαρμογων.gr">A real link</a></p>
<p><a href="https://granö.fi">A real link</a></p>

</html>

0 comments on commit da1dd16

Please sign in to comment.