Skip to content
Permalink
Browse files

Merge pull request #242 from remko/master

Allow excluding links from the internal_links checker
  • Loading branch information...
ddfreyne committed Mar 10, 2013
2 parents e2458a3 + e8b95f9 commit 7f3928d0d5480b90c95348540de27f127137fd91
@@ -106,6 +106,17 @@ def array_to_yaml(array)
# When to send notifications (using Growl or notify-send).
notify_on_compilation_success: true
notify_on_compilation_failure: true
# Configuration for the “check” command, which run unit tests on the site.
checks:
# Configuration for the “internal_links” checker, which checks whether all
# internal links are valid.
internal_links:
# A list of patterns, specified as regular expressions, to exclude from the check.
# If an internal link matches this pattern, the validity check will be skipped.
# E.g.:
# exclude: ['^/server_status']
exclude: []
EOS

DEFAULT_RULES = <<EOS unless defined? DEFAULT_RULES
@@ -7,6 +7,9 @@ class InternalLinks < ::Nanoc::Extra::Checking::Check

# Starts the validator. The results will be printed to stdout.
#
# Internal links that match a regexp pattern in `@config[:checks][:internal_links][:exclude]` will
# be skipped.
#
# @return [void]
def run
# TODO de-duplicate this (duplicated in external links check)
@@ -30,6 +33,9 @@ def valid?(href, origin)
# FIXME this is ugly and won’t always be correct
return true if href == '.'

# Skip hrefs that are specified in the exclude configuration
return true if self.excluded?(href)

# Remove target
path = href.sub(/#.*$/, '')
return true if path.empty?
@@ -55,6 +61,11 @@ def valid?(href, origin)
return false
end

def excluded?(href)
excludes = @site.config.fetch(:checks, {}).fetch(:internal_links, {}).fetch(:exclude, [])
excludes.any? { |pattern| Regexp.new(pattern).match(href) }
end

end

end
@@ -55,4 +55,17 @@ def test_remove_query_string
end
end

def test_exclude
with_site do |site|
# Create check
check = Nanoc::Extra::Checking::Checks::InternalLinks.new(site)
site.config.update({ :checks => { :internal_links => { :exclude => ['^/excluded\d+'] } } })

# Test
assert check.send(:valid?, '/excluded1', 'output/origin')
assert check.send(:valid?, '/excluded2', 'output/origin')
assert !check.send(:valid?, '/excluded_not', 'output/origin')
end
end

end

0 comments on commit 7f3928d

Please sign in to comment.
You can’t perform that action at this time.