Upgrade url_for to search for resources relative to their destination paths as well as their source paths. This would fix #818.

This commit is contained in:
Ben Hollis 2013-09-22 14:36:00 -07:00
parent 8f4057736a
commit 22dace72df
2 changed files with 15 additions and 7 deletions

View file

@ -27,12 +27,12 @@ Feature: Directory Index
And the file "regular/index.html" should contain "Regular"
And the file "evil spaces/index.html" should contain "Spaces"
Scenario: Preview normal file
Given the Server is running at "indexable-app"
When I go to "/needs_index/"
Then I should see "Indexable"
Scenario: Preview normal file with spaces in filename
Given the Server is running at "indexable-app"
When I go to "/evil%20spaces/"
@ -42,7 +42,7 @@ Feature: Directory Index
Given the Server is running at "indexable-app"
When I go to "/a_folder/needs_index/"
Then I should see "Indexable"
Scenario: Preview ignored file
Given the Server is running at "indexable-app"
When I go to "/leave_me_alone/"
@ -69,14 +69,13 @@ Feature: Directory Index
And the Server is running at "indexable-app"
When I go to "/link_to/"
Then I should see 'link_to: <a href="/needs_index/">Needs Index</a>'
Then I should see 'explicit_link_to: <a href="/needs_index/index.html">Explicit</a>'
Then I should see 'explicit_link_to: <a href="/needs_index/">Explicit</a>'
Then I should see 'unknown_link_to: <a href="/unknown.html">Unknown</a>'
Then I should see 'relative_link_to: <a href="/needs_index/">Relative</a>'
Then I should see 'link_to_with_spaces: <a href="/evil%20spaces/">Spaces</a>'
When I go to "/link_to/sub/"
Then I should see 'link_to: <a href="/needs_index/">Needs Index</a>'
Then I should see 'explicit_link_to: <a href="/needs_index/index.html">Explicit</a>'
Then I should see 'explicit_link_to: <a href="/needs_index/">Explicit</a>'
Then I should see 'unknown_link_to: <a href="/unknown.html">Unknown</a>'
Then I should see 'relative_link_to: <a href="/needs_index/">Relative</a>'
Then I should see 'link_to_with_spaces: <a href="/evil%20spaces/">Spaces</a>'

View file

@ -247,7 +247,16 @@ module Middleman
current_source_dir = Pathname('/' + this_resource.path).dirname
url_path = current_source_dir.join(url_path) if url_path.relative?
resource = app.sitemap.find_resource_by_path(url_path.to_s)
resource_url = resource.url if resource
if resource
resource_url = resource.url
else
# Try to find a resource relative to destination paths
url_path = Pathname(uri.path)
current_source_dir = Pathname('/' + this_resource.destination_path).dirname
url_path = current_source_dir.join(url_path) if url_path.relative?
resource = app.sitemap.find_resource_by_destination_path(url_path.to_s)
resource_url = resource.url if resource
end
elsif options[:find_resource] && uri.path
resource = app.sitemap.find_resource_by_path(uri.path)
resource_url = resource.url if resource