-
-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from benbalter/refactor
Refactor
- Loading branch information
Showing
9 changed files
with
191 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module HTML | ||
class Proofer | ||
class Checkable | ||
|
||
def initialize(obj, check) | ||
@src = obj['src'] | ||
@href = obj['href'] | ||
@alt = obj['alt'] | ||
@name = obj['name'] | ||
@id = obj['id'] | ||
@check = check | ||
|
||
if @href && @check.options[:href_swap] | ||
@options[:href_swap].each do |link, replace| | ||
@href = @href.gsub(link, replace) | ||
end | ||
end | ||
|
||
end | ||
|
||
def url | ||
@src || @href || "" | ||
end | ||
|
||
def valid? | ||
begin | ||
URI.parse url | ||
rescue | ||
false | ||
end | ||
end | ||
|
||
def parts | ||
URI.parse url | ||
end | ||
|
||
def path | ||
parts.path | ||
end | ||
|
||
def hash | ||
parts.fragment | ||
end | ||
|
||
# path is to an external server | ||
def remote? | ||
uri = URI.parse url | ||
%w( http https ).include?(uri.scheme) | ||
rescue URI::BadURIError | ||
false | ||
rescue URI::InvalidURIError | ||
false | ||
end | ||
|
||
def ignore? | ||
uri = URI.parse url | ||
%w( mailto ).include?(uri.scheme) || @check.additional_href_ignores.include?(href) | ||
rescue URI::BadURIError | ||
false | ||
rescue URI::InvalidURIError | ||
false | ||
end | ||
|
||
# path is external to the file | ||
def external? | ||
!internal? | ||
end | ||
|
||
# path is an anchor | ||
def internal? | ||
url[0] == "#" | ||
end | ||
|
||
def file_path | ||
|
||
return if path.nil? | ||
|
||
if path =~ /^\// #path relative to root | ||
base = @check.src | ||
elsif File.exist? File.expand_path path, @check.src #relative links, path is a file | ||
base = File.dirname @check.path | ||
else #relative link, path is a directory | ||
base = @check.path | ||
end | ||
|
||
file = File.join base, path | ||
|
||
# implicit /index.html support, with support for tailing slashes | ||
file = File.join path, "index.html" if File.directory? File.expand_path file, @check.src | ||
|
||
file | ||
end | ||
|
||
# checks if a file exists relative to the current pwd | ||
def exists? | ||
File.exist? absolute_path | ||
end | ||
|
||
def absolute_path | ||
path = file_path || @check.path | ||
File.expand_path path, Dir.pwd | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,50 @@ | ||
# encoding: utf-8 | ||
|
||
class Image < ::HTML::Proofer::Checkable | ||
|
||
SCREEN_SHOT_REGEX = /Screen(?: |%20)Shot(?: |%20)\d+-\d+-\d+(?: |%20)at(?: |%20)\d+.\d+.\d+/ | ||
|
||
def valid_alt_tag? | ||
@alt and !@alt.empty? | ||
end | ||
|
||
def terrible_filename? | ||
@src =~ SCREEN_SHOT_REGEX | ||
end | ||
|
||
def src | ||
@src unless @src.nil? || @src.empty? | ||
end | ||
|
||
def missing_src? | ||
!src | ||
end | ||
|
||
end | ||
|
||
class Images < ::HTML::Proofer::Checks::Check | ||
|
||
def run | ||
@html.css('img').each do |img| | ||
src = img['src'] | ||
|
||
# check image sources | ||
if src && src.length > 0 | ||
if !external_href?(src) | ||
self.add_issue("#{@path}".blue + ": internal image #{src} does not exist") unless file? src | ||
else | ||
validate_url(src, "#{@path}".blue + ": external image #{src} does not exist") | ||
end | ||
|
||
img = Image.new img, self | ||
|
||
# screenshot filenames, return because invalid URL | ||
return self.add_issue "image has a terrible filename (#{img.src})" if img.terrible_filename? | ||
|
||
# does the image exist? | ||
if img.missing_src? | ||
self.add_issue "image has no src attribute" | ||
elsif img.remote? | ||
validate_url img.src, "external image #{img.src} does not exist" | ||
else | ||
self.add_issue("#{@path}".blue + ": image has no src attribute") | ||
self.add_issue("internal image #{img.src} does not exist") unless img.exists? | ||
end | ||
|
||
# check alt tag | ||
self.add_issue("#{@path}".blue + ": image #{src} does not have an alt attribute") unless img['alt'] and !img['alt'].empty? | ||
self.add_issue "image #{img.src} does not have an alt attribute" unless img.valid_alt_tag? | ||
|
||
screenShotRegExp = /Screen(?: |%20)Shot(?: |%20)\d+-\d+-\d+(?: |%20)at(?: |%20)\d+.\d+.\d+/ | ||
|
||
if src =~ screenShotRegExp | ||
self.add_issue("#{@path}".blue + ": image has a terrible filename (#{src})") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.