History

Add :follow_links option to not follow links on select pages

Thibaut committed 12 years ago

Commit 6c8eea11 parent 6ccfb392 files · +29 −3Browse files

Unified · Split

2 files
  1. lib/docs/filters/core/internal_urls.rb +7 −3
  2. test/lib/docs/filters/core/internal_urls_test.rb +22 −0
lib/docs/filters/core/internal_urls.rb+7−3
@@ -3,7 +3,7 @@ require 'set' · 2 unmodified lines
33 module Docs
44 class InternalUrlsFilter < Filter
55 def call
6− internal_urls = Set.new
6+ internal_urls = Set.new if follow_links?
77
88 css('a').each do |link|
99 next if skip_link?(link)
@@ -17,13 +17,17 @@ module Docs · 7 unmodified lines
1717 normalize_internal_url(url, subpath)
1818
1919 link['href'] = internal_path_to(url)
20− internal_urls << url.merge!(fragment: nil).to_s
20+ internal_urls << url.merge!(fragment: nil).to_s if internal_urls
2121 end
2222
23− result[:internal_urls] = internal_urls.to_a
23+ result[:internal_urls] = (internal_urls || []).to_a
2424 doc
2525 end
2626
27+ def follow_links?
28+ !(context[:follow_links] && context[:follow_links].call(self) == false)
29+ end
30+
2731 def skip_link?(link)
2832 context[:skip_links] && context[:skip_links].call(link)
2933 end
test/lib/docs/filters/core/internal_urls_test.rb+22−0
@@ -159,6 +159,28 @@ class InternalUrlsFilterTest < MiniTest::Spec · 158 unmodified lines
159159 assert_equal 1, internal_urls.length
160160 end
161161 end
162+
163+ context "when context[:follow_links] is a block" do
164+ before do
165+ @body = link_to context[:url]
166+ end
167+
168+ it "calls the block with the filter instance" do
169+ context[:follow_links] = ->(arg) { @arg = arg; nil }
170+ filter.call
171+ assert_equal filter, @arg
172+ end
173+
174+ it "is empty when the block returns false" do
175+ context[:follow_links] = ->(_) { false }
176+ assert_empty internal_urls
177+ end
178+
179+ it "is the default when the block returns true" do
180+ context[:follow_links] = ->(_) { true }
181+ refute_empty internal_urls
182+ end
183+ end
162184 end
163185
164186 context "when the base url is 'example.com'" do