Skip to content

Commit

Permalink
Install rubocop and use autocorrect
Browse files Browse the repository at this point in the history
rubocop.yml ignores some issues. also internal snake_case fixes
  • Loading branch information
stephengroat committed May 18, 2017
1 parent d6f140b commit 101b381
Show file tree
Hide file tree
Showing 87 changed files with 492 additions and 481 deletions.
14 changes: 14 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Style/LineLength:
Enabled: false
Style/MethodLength:
Enabled: false
Style/ClassLength:
Enabled: false
Style/BlockLength:
Enabled: false
Style/CyclomaticComplexity:
Enabled: false
Metrics/AbcSize:
Enabled: false
Metrics/PerceivedComplexity:
Enabled: false
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
language: ruby
rvm:
- 2.3.1
- 2.3.2
- 2.3.3
- 2.3.4
- 2.4.0
- 2.4.1
env:
global:
- NOKOGIRI_USE_SYSTEM_LIBRARIES=true

sudo: false
cache: bundler

git:
depth: 10
matrix:
include:
- script: bundle exec rake rubocop
rvm: 2.3.1
allow_failures:
- script: bundle exec rake rubocop
10 changes: 7 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => [:spec, :proof_readme]
require 'rubocop/rake_task'

RuboCop::RakeTask.new(:rubocop)

task default: %i[spec proof_readme]

task :proof_readme do
require 'html-proofer'
require 'redcarpet'

renderer = Redcarpet::Render::HTML.new \
with_toc_data: true
with_toc_data: true
redcarpet = Redcarpet::Markdown.new(renderer)
html = redcarpet.render File.read('README.md')

mkdir_p 'out'
File.write('out/README.html', html)

opts = { :url_ignore => [/badge.fury.io/] }
opts = { url_ignore: [/badge.fury.io/] }
HTMLProofer.check_directory('./out', opts).run
end
4 changes: 2 additions & 2 deletions bin/htmlproofer
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
STDOUT.sync = true

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w( .. lib ))
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')

require 'html-proofer'
require 'mercenary'
Expand Down Expand Up @@ -51,7 +51,7 @@ Mercenary.program(:htmlproofer) do |p|
options = {}

# prepare everything to go to proofer
p.options.select { |o| !opts[o.config_key].nil? }.each do |option|
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
Expand Down
1 change: 1 addition & 0 deletions html-proofer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'activesupport', '>= 4.2', '< 6.0'

gem.add_development_dependency 'redcarpet'
gem.add_development_dependency 'rubocop'
gem.add_development_dependency 'rspec', '~> 3.1'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'pry', '~> 0.10.0'
Expand Down
10 changes: 4 additions & 6 deletions lib/html-proofer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,30 @@ def require_all(path)
require 'awesome_print'
require 'pry'
rescue LoadError; end

module HTMLProofer

def check_file(file, options = {})
fail ArgumentError unless file.is_a?(String)
raise ArgumentError unless file.is_a?(String)
options[:type] = :file
HTMLProofer::Runner.new(file, options)
end
module_function :check_file

def check_directory(directory, options = {})
fail ArgumentError unless directory.is_a?(String)
raise ArgumentError unless directory.is_a?(String)
options[:type] = :directory
HTMLProofer::Runner.new([directory], options)
end
module_function :check_directory

def check_directories(directories, options = {})
fail ArgumentError unless directories.is_a?(Array)
raise ArgumentError unless directories.is_a?(Array)
options[:type] = :directory
HTMLProofer::Runner.new(directories, options)
end
module_function :check_directories

def check_links(links, options = {})
fail ArgumentError unless links.is_a?(Array)
raise ArgumentError unless links.is_a?(Array)
options[:type] = :links
HTMLProofer::Runner.new(links, options)
end
Expand Down
45 changes: 21 additions & 24 deletions lib/html-proofer/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Cache
include HTMLProofer::Utils

DEFAULT_STORAGE_DIR = File.join('tmp', '.htmlproofer')
DEFAULT_CACHE_FILE_NAME = "cache.log"
DEFAULT_CACHE_FILE_NAME = 'cache.log'.freeze

attr_reader :exists, :cache_log, :storage_dir, :cache_file

Expand Down Expand Up @@ -54,17 +54,17 @@ def parsed_timeframe(timeframe)
when 'h'
time.hours.ago
else
fail ArgumentError, "#{date} is not a valid timeframe!"
raise ArgumentError, "#{date} is not a valid timeframe!"
end
end

def add(url, filenames, status, msg = '')
data = {
:time => @cache_time,
:filenames => filenames,
:status => status,
:message => msg
}
time: @cache_time,
filenames: filenames,
status: status,
message: msg
}

@cache_log[clean_url(url)] = data
end
Expand Down Expand Up @@ -148,28 +148,25 @@ def clean_url(url)
end

def setup_cache!(options)
if options[:storage_dir]
@storage_dir = options[:storage_dir]
else
@storage_dir = DEFAULT_STORAGE_DIR
end
@storage_dir = if options[:storage_dir]
options[:storage_dir]
else
DEFAULT_STORAGE_DIR
end

if !Dir.exist?(storage_dir)
FileUtils.mkdir_p(storage_dir)
end
FileUtils.mkdir_p(storage_dir) unless Dir.exist?(storage_dir)

if options[:cache_file]
cache_file_name = options[:cache_file]
else
cache_file_name = DEFAULT_CACHE_FILE_NAME
end
cache_file_name = if options[:cache_file]
options[:cache_file]
else
DEFAULT_CACHE_FILE_NAME
end

@cache_file = File.join(storage_dir, cache_file_name)

if File.exist?(cache_file)
contents = File.read(cache_file)
@cache_log = contents.empty? ? {} : JSON.parse(contents)
end
return unless File.exist?(cache_file)
contents = File.read(cache_file)
@cache_log = contents.empty? ? {} : JSON.parse(contents)
end
end
end
10 changes: 4 additions & 6 deletions lib/html-proofer/check/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ def run
# does the image exist?
if missing_src?
add_issue('image has no src or srcset attribute', line: line, content: content)
else
if @img.remote?
add_to_external_urls(@img.url)
elsif !@img.exists?
add_issue("internal image #{@img.url} does not exist", line: line, content: content)
end
elsif @img.remote?
add_to_external_urls(@img.url)
elsif !@img.exists?
add_issue("internal image #{@img.url} does not exist", line: line, content: content)
end

if !@img.ignore_alt? && (@img.alt.nil? || (empty_alt_tag? && !@img.ignore_empty_alt?))
Expand Down
4 changes: 2 additions & 2 deletions lib/html-proofer/check/links.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def external_link_check(link, line, content)
def hash_check(html, href_hash)
decoded_href_hash = URI.decode(href_hash)
fragment_ids = [href_hash, decoded_href_hash]
fragment_ids.include?('top') || find_fragments(html, fragment_ids).length > 0
fragment_ids.include?('top') || !find_fragments(html, fragment_ids).empty?
end

def find_fragments(html, fragment_ids)
Expand Down Expand Up @@ -137,7 +137,7 @@ def check_sri(line, content)

class XpathFunctions
def case_insensitive_equals(node_set, str_to_match)
node_set.find_all {|node| node.to_s.downcase == str_to_match.to_s.downcase }
node_set.find_all { |node| node.to_s.casecmp(str_to_match.to_s.downcase).zero? }
end
end
end
10 changes: 5 additions & 5 deletions lib/html-proofer/check/opengraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

class OpenGraphElement < ::HTMLProofer::Element
attr_reader :src

def initialize(obj, check)
super(obj, check)
# Fake up src from the content attribute
instance_variable_set("@src", @content)
instance_variable_set('@src', @content)

@src.insert 0, 'http:' if @src =~ %r{^//}
end
end
Expand All @@ -16,11 +16,11 @@ class OpenGraphCheck < ::HTMLProofer::Check
def missing_src?
!@opengraph.src
end

def empty_src?
blank?(@opengraph.src)
end

def run
@html.css('meta[property="og:url"], meta[property="og:image"]').each do |m|
@opengraph = OpenGraphElement.new(m, self)
Expand Down
2 changes: 1 addition & 1 deletion lib/html-proofer/check/scripts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def run
end

def check_sri(line, content)
if !defined? @script.integrity and !defined? @script.crossorigin
if !defined? @script.integrity && !defined? @script.crossorigin
add_issue("SRI and CORS not provided in: #{@script.src}", line: line, content: content)
elsif !defined? @script.integrity
add_issue("Integrity is missing in: #{@script.src}", line: line, content: content)
Expand Down
76 changes: 38 additions & 38 deletions lib/html-proofer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,54 @@ module Configuration
require_relative 'version'

PROOFER_DEFAULTS = {
:allow_hash_href => false,
:alt_ignore => [],
:assume_extension => false,
:check_external_hash => false,
:check_favicon => false,
:check_html => false,
:check_img_http => false,
:check_opengraph => false,
:checks_to_ignore => [],
:check_sri => false,
:directory_index_file => 'index.html',
:disable_external => false,
:empty_alt_ignore => false,
:enforce_https => false,
:error_sort => :path,
:extension => '.html',
:external_only => false,
:file_ignore => [],
:http_status_ignore => [],
:internal_domains => [],
:log_level => :info,
:only_4xx => false,
:url_ignore => [],
:url_swap => {}
}
allow_hash_href: false,
alt_ignore: [],
assume_extension: false,
check_external_hash: false,
check_favicon: false,
check_html: false,
check_img_http: false,
check_opengraph: false,
checks_to_ignore: [],
check_sri: false,
directory_index_file: 'index.html',
disable_external: false,
empty_alt_ignore: false,
enforce_https: false,
error_sort: :path,
extension: '.html',
external_only: false,
file_ignore: [],
http_status_ignore: [],
internal_domains: [],
log_level: :info,
only_4xx: false,
url_ignore: [],
url_swap: {}
}.freeze

TYPHOEUS_DEFAULTS = {
:followlocation => true,
:headers => {
followlocation: true,
headers: {
'User-Agent' => "Mozilla/5.0 (compatible; HTML Proofer/#{HTMLProofer::VERSION}; +https://github.com/gjtorikian/html-proofer)"
},
:connecttimeout => 10,
:timeout => 30
}
connecttimeout: 10,
timeout: 30
}.freeze

HYDRA_DEFAULTS = {
:max_concurrency => 50
}
max_concurrency: 50
}.freeze

PARALLEL_DEFAULTS = {}
PARALLEL_DEFAULTS = {}.freeze

VALIDATION_DEFAULTS = {
:report_script_embeds => false,
:report_missing_names => false,
:report_invalid_tags => false
}
report_script_embeds: false,
report_missing_names: false,
report_invalid_tags: false
}.freeze

CACHE_DEFAULTS = {}
CACHE_DEFAULTS = {}.freeze

def self.to_regex?(item)
if item.start_with?('/') && item.end_with?('/')
Expand Down
Loading

0 comments on commit 101b381

Please sign in to comment.