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
3module Docs3module Docs
4 class InternalUrlsFilter < Filter4 class InternalUrlsFilter < Filter
5 def call5 def call
6 internal_urls = Set.new6 internal_urls = Set.new if follow_links?
7 7
8 css('a').each do |link|8 css('a').each do |link|
9 next if skip_link?(link)9 next if skip_link?(link)
@@ -17,13 +17,17 @@ module Docs · 7 unmodified lines
17 normalize_internal_url(url, subpath)17 normalize_internal_url(url, subpath)
18 18
19 link['href'] = internal_path_to(url)19 link['href'] = internal_path_to(url)
20 internal_urls << url.merge!(fragment: nil).to_s20 internal_urls << url.merge!(fragment: nil).to_s if internal_urls
21 end21 end
22 22
23 result[:internal_urls] = internal_urls.to_a23 result[:internal_urls] = (internal_urls || []).to_a
24 doc24 doc
25 end25 end
26 26
27 def follow_links?
28 !(context[:follow_links] && context[:follow_links].call(self) == false)
29 end
30
27 def skip_link?(link)31 def skip_link?(link)
28 context[:skip_links] && context[:skip_links].call(link)32 context[:skip_links] && context[:skip_links].call(link)
29 end33 end
test/lib/docs/filters/core/internal_urls_test.rb+22−0
@@ -159,6 +159,28 @@ class InternalUrlsFilterTest < MiniTest::Spec · 158 unmodified lines
159 assert_equal 1, internal_urls.length159 assert_equal 1, internal_urls.length
160 end160 end
161 end161 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
162 end184 end
163 185
164 context "when the base url is 'example.com'" do186 context "when the base url is 'example.com'" do