Make :skip_links option apply to pages instead of individual links

pull/22/head
Thibaut 11 years ago
parent 6c8eea1adb
commit 47eb7eec7f

@ -3,10 +3,10 @@ require 'set'
module Docs module Docs
class InternalUrlsFilter < Filter class InternalUrlsFilter < Filter
def call def call
return doc if skip_links?
internal_urls = Set.new if follow_links? internal_urls = Set.new if follow_links?
css('a').each do |link| css('a').each do |link|
next if skip_link?(link)
next unless url = parse_href(link['href']) next unless url = parse_href(link['href'])
next unless subpath = subpath_to(url) next unless subpath = subpath_to(url)
@ -24,12 +24,12 @@ module Docs
doc doc
end end
def follow_links? def skip_links?
!(context[:follow_links] && context[:follow_links].call(self) == false) context[:skip_links] && context[:skip_links].call(self)
end end
def skip_link?(link) def follow_links?
context[:skip_links] && context[:skip_links].call(link) !(context[:follow_links] && context[:follow_links].call(self) == false)
end end
def parse_href(str) def parse_href(str)

@ -10,7 +10,7 @@ module Docs
options[:title] = 'Backbone.js' options[:title] = 'Backbone.js'
options[:container] = '.container' options[:container] = '.container'
options[:skip_links] = -> (_) { true } options[:skip_links] = ->(filter) { true }
options[:attribution] = <<-HTML options[:attribution] = <<-HTML
&copy; 2010&ndash;2013 Jeremy Ashkenas, DocumentCloud<br> &copy; 2010&ndash;2013 Jeremy Ashkenas, DocumentCloud<br>

@ -9,7 +9,7 @@ module Docs
options[:title] = 'CoffeeScript' options[:title] = 'CoffeeScript'
options[:container] = '.container' options[:container] = '.container'
options[:skip_links] = -> (_) { true } options[:skip_links] = ->(filter) { true }
options[:attribution] = <<-HTML options[:attribution] = <<-HTML
&copy; 2009&ndash;2013 Jeremy Ashkenas<br> &copy; 2009&ndash;2013 Jeremy Ashkenas<br>

@ -8,7 +8,7 @@ module Docs
options[:title] = 'LESS' options[:title] = 'LESS'
options[:container] = 'section' options[:container] = 'section'
options[:skip_links] = -> (_) { true } options[:skip_links] = ->(filter) { true }
options[:attribution] = <<-HTML options[:attribution] = <<-HTML
&copy; 2009&ndash;2013 Alexis Sellier &amp; The Core Less Team<br> &copy; 2009&ndash;2013 Alexis Sellier &amp; The Core Less Team<br>

@ -10,7 +10,7 @@ module Docs
options[:title] = 'Lo-Dash' options[:title] = 'Lo-Dash'
options[:container] = 'h1+div+div' options[:container] = 'h1+div+div'
options[:skip_links] = -> (_) { true } options[:skip_links] = ->(filter) { true }
options[:attribution] = <<-HTML options[:attribution] = <<-HTML
&copy; 2012&ndash;2013 The Dojo Foundation<br> &copy; 2012&ndash;2013 The Dojo Foundation<br>

@ -10,7 +10,7 @@ module Docs
options[:title] = 'Underscore.js' options[:title] = 'Underscore.js'
options[:container] = '#documentation' options[:container] = '#documentation'
options[:skip_links] = -> (_) { true } options[:skip_links] = ->(filter) { true }
options[:attribution] = <<-HTML options[:attribution] = <<-HTML
&copy; 2009&ndash;2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters &amp; Editors<br> &copy; 2009&ndash;2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters &amp; Editors<br>

@ -5,16 +5,15 @@ class InternalUrlsFilterTest < MiniTest::Spec
include FilterTestHelper include FilterTestHelper
self.filter_class = Docs::InternalUrlsFilter self.filter_class = Docs::InternalUrlsFilter
describe ":internal_urls" do before do
before do context[:base_url] = context[:root_url] = context[:url] = 'http://example.com/dir'
context[:base_url] = context[:root_url] = 'http://example.com/dir' end
context[:url] = 'http://example.com/dir'
end
let :internal_urls do let :internal_urls do
filter_result[:internal_urls] filter_result[:internal_urls]
end end
describe ":internal_urls" do
it "is an array" do it "is an array" do
assert_instance_of Array, internal_urls assert_instance_of Array, internal_urls
end end
@ -135,31 +134,6 @@ class InternalUrlsFilterTest < MiniTest::Spec
end end
end end
context "when context[:skip_links] is a block" do
let :block do
context[:skip_links] = Proc.new {}
end
it "passes all links to the block" do
@body = link_to 'http://example.com'
context[:skip_links] = ->(arg) { @arg = arg }
internal_urls
assert_equal @body, @arg.to_s
end
it "doesn't include urls from links where the block returns true" do
@body = link_to 'http://example.com/dir/path'
context[:skip_links] = ->(_) { true }
assert_empty internal_urls
end
it "includes urls from links where the block returns false" do
@body = link_to 'http://example.com/dir/path'
context[:skip_links] = ->(_) { false }
assert_equal 1, internal_urls.length
end
end
context "when context[:follow_links] is a block" do context "when context[:follow_links] is a block" do
before do before do
@body = link_to context[:url] @body = link_to context[:url]
@ -305,4 +279,46 @@ class InternalUrlsFilterTest < MiniTest::Spec
end end
end end
end end
context "context[:skip_links]" do
before do
@body = link_to context[:url]
end
context "when it is a block" do
it "calls the block with the filter instance" do
context[:skip_links] = ->(arg) { @arg = arg; nil }
filter.call
assert_equal filter, @arg
end
context "and the block returns true" do
before do
context[:skip_links] = ->(_) { true }
end
it "doesn't set :internal_urls" do
refute internal_urls
end
it "doesn't replace urls" do
assert_equal @body, filter_output_string
end
end
context "and the block returns false" do
before do
context[:skip_links] = ->(_) { false }
end
it "set :internal_urls" do
assert internal_urls
end
it "replaces urls" do
refute_equal @body, filter_output_string
end
end
end
end
end end

Loading…
Cancel
Save