Unified · Split
| @@ -3,10 +3,10 @@ require 'set' · 2 unmodified lines | |||
| 3 | module Docs | 3 | module Docs |
| 4 | class InternalUrlsFilter < Filter | 4 | class InternalUrlsFilter < Filter |
| 5 | def call | 5 | def call |
| 6 | return doc if skip_links? | ||
| 6 | internal_urls = Set.new if follow_links? | 7 | internal_urls = Set.new if follow_links? |
| 7 | 8 | ||
| 8 | css('a').each do |link| | 9 | css('a').each do |link| |
| 9 | next if skip_link?(link) | ||
| 10 | next unless url = parse_href(link['href']) | 10 | next unless url = parse_href(link['href']) |
| 11 | next unless subpath = subpath_to(url) | 11 | next unless subpath = subpath_to(url) |
| 12 | 12 | ||
| @@ -24,12 +24,12 @@ module Docs · 11 unmodified lines | |||
| 24 | doc | 24 | doc |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | def follow_links? | 27 | def skip_links? |
| 28 | !(context[:follow_links] && context[:follow_links].call(self) == false) | 28 | context[:skip_links] && context[:skip_links].call(self) |
| 29 | end | 29 | end |
| 30 | 30 | ||
| 31 | def skip_link?(link) | 31 | def follow_links? |
| 32 | context[:skip_links] && context[:skip_links].call(link) | 32 | !(context[:follow_links] && context[:follow_links].call(self) == false) |
| 33 | end | 33 | end |
| 34 | 34 | ||
| 35 | def parse_href(str) | 35 | def parse_href(str) |
| @@ -10,7 +10,7 @@ module Docs · 9 unmodified lines | |||
| 10 | 10 | ||
| 11 | options[:title] = 'Backbone.js' | 11 | options[:title] = 'Backbone.js' |
| 12 | options[:container] = '.container' | 12 | options[:container] = '.container' |
| 13 | options[:skip_links] = -> (_) { true } | 13 | options[:skip_links] = ->(filter) { true } |
| 14 | 14 | ||
| 15 | options[:attribution] = <<-HTML | 15 | options[:attribution] = <<-HTML |
| 16 | © 2010–2013 Jeremy Ashkenas, DocumentCloud<br> | 16 | © 2010–2013 Jeremy Ashkenas, DocumentCloud<br> |
| @@ -9,7 +9,7 @@ module Docs · 8 unmodified lines | |||
| 9 | 9 | ||
| 10 | options[:title] = 'CoffeeScript' | 10 | options[:title] = 'CoffeeScript' |
| 11 | options[:container] = '.container' | 11 | options[:container] = '.container' |
| 12 | options[:skip_links] = -> (_) { true } | 12 | options[:skip_links] = ->(filter) { true } |
| 13 | 13 | ||
| 14 | options[:attribution] = <<-HTML | 14 | options[:attribution] = <<-HTML |
| 15 | © 2009–2013 Jeremy Ashkenas<br> | 15 | © 2009–2013 Jeremy Ashkenas<br> |
| @@ -8,7 +8,7 @@ module Docs · 7 unmodified lines | |||
| 8 | 8 | ||
| 9 | options[:title] = 'LESS' | 9 | options[:title] = 'LESS' |
| 10 | options[:container] = 'section' | 10 | options[:container] = 'section' |
| 11 | options[:skip_links] = -> (_) { true } | 11 | options[:skip_links] = ->(filter) { true } |
| 12 | 12 | ||
| 13 | options[:attribution] = <<-HTML | 13 | options[:attribution] = <<-HTML |
| 14 | © 2009–2013 Alexis Sellier & The Core Less Team<br> | 14 | © 2009–2013 Alexis Sellier & The Core Less Team<br> |
| @@ -10,7 +10,7 @@ module Docs · 9 unmodified lines | |||
| 10 | 10 | ||
| 11 | options[:title] = 'Lo-Dash' | 11 | options[:title] = 'Lo-Dash' |
| 12 | options[:container] = 'h1+div+div' | 12 | options[:container] = 'h1+div+div' |
| 13 | options[:skip_links] = -> (_) { true } | 13 | options[:skip_links] = ->(filter) { true } |
| 14 | 14 | ||
| 15 | options[:attribution] = <<-HTML | 15 | options[:attribution] = <<-HTML |
| 16 | © 2012–2013 The Dojo Foundation<br> | 16 | © 2012–2013 The Dojo Foundation<br> |
| @@ -10,7 +10,7 @@ module Docs · 9 unmodified lines | |||
| 10 | 10 | ||
| 11 | options[:title] = 'Underscore.js' | 11 | options[:title] = 'Underscore.js' |
| 12 | options[:container] = '#documentation' | 12 | options[:container] = '#documentation' |
| 13 | options[:skip_links] = -> (_) { true } | 13 | options[:skip_links] = ->(filter) { true } |
| 14 | 14 | ||
| 15 | options[:attribution] = <<-HTML | 15 | options[:attribution] = <<-HTML |
| 16 | © 2009–2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors<br> | 16 | © 2009–2013 Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors<br> |
| @@ -5,16 +5,15 @@ class InternalUrlsFilterTest < MiniTest::Spec · 4 unmodified lines | |||
| 5 | include FilterTestHelper | 5 | include FilterTestHelper |
| 6 | self.filter_class = Docs::InternalUrlsFilter | 6 | self.filter_class = Docs::InternalUrlsFilter |
| 7 | 7 | ||
| 8 | describe ":internal_urls" do | 8 | before do |
| 9 | before do | 9 | context[:base_url] = context[:root_url] = context[:url] = 'http://example.com/dir' |
| 10 | context[:base_url] = context[:root_url] = 'http://example.com/dir' | 10 | end |
| 11 | context[:url] = 'http://example.com/dir' | ||
| 12 | end | ||
| 13 | 11 | ||
| 14 | let :internal_urls do | 12 | let :internal_urls do |
| 15 | filter_result[:internal_urls] | 13 | filter_result[:internal_urls] |
| 16 | end | 14 | end |
| 17 | 15 | ||
| 16 | describe ":internal_urls" do | ||
| 18 | it "is an array" do | 17 | it "is an array" do |
| 19 | assert_instance_of Array, internal_urls | 18 | assert_instance_of Array, internal_urls |
| 20 | end | 19 | end |
| @@ -135,31 +134,6 @@ class InternalUrlsFilterTest < MiniTest::Spec · 114 unmodified lines | |||
| 135 | end | 134 | end |
| 136 | end | 135 | end |
| 137 | 136 | ||
| 138 | context "when context[:skip_links] is a block" do | ||
| 139 | let :block do | ||
| 140 | context[:skip_links] = Proc.new {} | ||
| 141 | end | ||
| 142 | |||
| 143 | it "passes all links to the block" do | ||
| 144 | @body = link_to 'http://example.com' | ||
| 145 | context[:skip_links] = ->(arg) { @arg = arg } | ||
| 146 | internal_urls | ||
| 147 | assert_equal @body, @arg.to_s | ||
| 148 | end | ||
| 149 | |||
| 150 | it "doesn't include urls from links where the block returns true" do | ||
| 151 | @body = link_to 'http://example.com/dir/path' | ||
| 152 | context[:skip_links] = ->(_) { true } | ||
| 153 | assert_empty internal_urls | ||
| 154 | end | ||
| 155 | |||
| 156 | it "includes urls from links where the block returns false" do | ||
| 157 | @body = link_to 'http://example.com/dir/path' | ||
| 158 | context[:skip_links] = ->(_) { false } | ||
| 159 | assert_equal 1, internal_urls.length | ||
| 160 | end | ||
| 161 | end | ||
| 162 | |||
| 163 | context "when context[:follow_links] is a block" do | 137 | context "when context[:follow_links] is a block" do |
| 164 | before do | 138 | before do |
| 165 | @body = link_to context[:url] | 139 | @body = link_to context[:url] |
| @@ -305,4 +279,46 @@ class InternalUrlsFilterTest < MiniTest::Spec · 139 unmodified lines | |||
| 305 | end | 279 | end |
| 306 | end | 280 | end |
| 307 | end | 281 | end |
| 282 | |||
| 283 | context "context[:skip_links]" do | ||
| 284 | before do | ||
| 285 | @body = link_to context[:url] | ||
| 286 | end | ||
| 287 | |||
| 288 | context "when it is a block" do | ||
| 289 | it "calls the block with the filter instance" do | ||
| 290 | context[:skip_links] = ->(arg) { @arg = arg; nil } | ||
| 291 | filter.call | ||
| 292 | assert_equal filter, @arg | ||
| 293 | end | ||
| 294 | |||
| 295 | context "and the block returns true" do | ||
| 296 | before do | ||
| 297 | context[:skip_links] = ->(_) { true } | ||
| 298 | end | ||
| 299 | |||
| 300 | it "doesn't set :internal_urls" do | ||
| 301 | refute internal_urls | ||
| 302 | end | ||
| 303 | |||
| 304 | it "doesn't replace urls" do | ||
| 305 | assert_equal @body, filter_output_string | ||
| 306 | end | ||
| 307 | end | ||
| 308 | |||
| 309 | context "and the block returns false" do | ||
| 310 | before do | ||
| 311 | context[:skip_links] = ->(_) { false } | ||
| 312 | end | ||
| 313 | |||
| 314 | it "set :internal_urls" do | ||
| 315 | assert internal_urls | ||
| 316 | end | ||
| 317 | |||
| 318 | it "replaces urls" do | ||
| 319 | refute_equal @body, filter_output_string | ||
| 320 | end | ||
| 321 | end | ||
| 322 | end | ||
| 323 | end | ||
| 308 | end | 324 | end |