فهرست منبع

Make :skip_links option apply to pages instead of individual links

Thibaut 12 سال پیش
والد
کامیت
47eb7eec7f

+ 5 - 5
lib/docs/filters/core/internal_urls.rb

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

+ 1 - 1
lib/docs/scrapers/backbone.rb

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

+ 1 - 1
lib/docs/scrapers/coffeescript.rb

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

+ 1 - 1
lib/docs/scrapers/less.rb

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

+ 1 - 1
lib/docs/scrapers/lodash.rb

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

+ 1 - 1
lib/docs/scrapers/underscore.rb

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

+ 49 - 33
test/lib/docs/filters/core/internal_urls_test.rb

@@ -5,16 +5,15 @@ class InternalUrlsFilterTest < MiniTest::Spec
   include FilterTestHelper
   self.filter_class = Docs::InternalUrlsFilter
 
-  describe ":internal_urls" do
-    before do
-      context[:base_url] = context[:root_url] = 'http://example.com/dir'
-      context[:url] = 'http://example.com/dir'
-    end
+  before do
+    context[:base_url] = context[:root_url] = context[:url] = 'http://example.com/dir'
+  end
 
-    let :internal_urls do
-      filter_result[:internal_urls]
-    end
+  let :internal_urls do
+    filter_result[:internal_urls]
+  end
 
+  describe ":internal_urls" do
     it "is an array" do
       assert_instance_of Array, internal_urls
     end
@@ -135,31 +134,6 @@ class InternalUrlsFilterTest < MiniTest::Spec
       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
       before do
         @body = link_to context[:url]
@@ -305,4 +279,46 @@ class InternalUrlsFilterTest < MiniTest::Spec
       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