History

Add :skip_link option for ignoring certain links in scrapers

Thibaut committed 12 years ago

Commit c122caa1 parent f4b736e2 files · +41 −0Browse files

Unified · Split

2 files
  1. lib/docs/filters/core/internal_urls.rb +1 −0
  2. test/lib/docs/filters/core/internal_urls_test.rb +40 −0
lib/docs/filters/core/internal_urls.rb+1−0
@@ -9,6 +9,7 @@ module Docs · 8 unmodified lines
99
1010 def update_links
1111 css('a').each do |link|
12+ next if context[:skip_link].is_a?(Proc) && context[:skip_link].call(link)
1213 next unless url = to_internal_url(link['href'])
1314 link['href'] = internal_path_to(url)
1415 yield url if block_given?
test/lib/docs/filters/core/internal_urls_test.rb+40−0
@@ -366,4 +366,44 @@ class InternalUrlsFilterTest < MiniTest::Spec · 365 unmodified lines
366366 end
367367 end
368368 end
369+
370+ context "context[:skip_link] is a block" do
371+ before do
372+ @body = link_to context[:url]
373+ end
374+
375+ it "calls the block with each link" do
376+ context[:skip_link] = ->(arg) { @arg = arg.try(:to_html); nil }
377+ filter.call
378+ assert_equal @body, @arg
379+ end
380+
381+ context "and the block returns true" do
382+ before do
383+ context[:skip_link] = ->(_) { true }
384+ end
385+
386+ it "doesn't include the link's url in :internal_urls" do
387+ assert internal_urls.empty?
388+ end
389+
390+ it "doesn't replace the link's url" do
391+ assert_equal @body, filter_output_string
392+ end
393+ end
394+
395+ context "and the block returns false" do
396+ before do
397+ context[:skip_link] = ->(_) { false }
398+ end
399+
400+ it "includes the link's url in :internal_urls" do
401+ refute internal_urls.empty?
402+ end
403+
404+ it "replaces the link's url" do
405+ refute_equal @body, filter_output_string
406+ end
407+ end
408+ end
369409 end