Simplify and fix relative_assets extension to work more consistently. Also, fix a bug in relative link_to inspired by fixing this bug. This fixes #507.

This commit is contained in:
Ben Hollis 2012-06-30 22:02:23 -07:00
parent dad9b33174
commit 4faa98e874
4 changed files with 29 additions and 31 deletions

View file

@ -61,11 +61,11 @@ Feature: link_to helper
""" """
And the Server is running at "indexable-app" And the Server is running at "indexable-app"
When I go to "/link_to/" When I go to "/link_to/"
Then I should see 'absolute: <a href="needs_index/">Needs Index</a>'
Then I should see 'relative: <a href="needs_index/">Relative</a>'
When I go to "/link_to/sub/"
Then I should see 'absolute: <a href="../needs_index/">Needs Index</a>' Then I should see 'absolute: <a href="../needs_index/">Needs Index</a>'
Then I should see 'relative: <a href="../needs_index/">Relative</a>' Then I should see 'relative: <a href="../needs_index/">Relative</a>'
When I go to "/link_to/sub/"
Then I should see 'absolute: <a href="../../needs_index/">Needs Index</a>'
Then I should see 'relative: <a href="../../needs_index/">Relative</a>'
Scenario: link_to can take a Resource Scenario: link_to can take a Resource
Given a fixture app "indexable-app" Given a fixture app "indexable-app"

View file

@ -89,3 +89,14 @@ Feature: Relative Assets
And the Server is running at "fonts-app" And the Server is running at "fonts-app"
When I go to "/stylesheets/fonts.css" When I go to "/stylesheets/fonts.css"
Then I should see "url('../fonts/StMarie-Thin.otf" Then I should see "url('../fonts/StMarie-Thin.otf"
Scenario: Relative assets via image_tag
Given a fixture app "relative-assets-app"
Given "relative_assets" feature is "enabled"
And a file named "source/sub/image_tag.html.erb" with:
"""
<%= image_tag '/img/blank.gif' %>
"""
And the Server is running at "relative-assets-app"
When I go to "/sub/image_tag.html"
Then I should see '<img src="../img/blank.gif" />'

View file

@ -102,9 +102,10 @@ module Middleman
source = source.to_s.gsub(/\s/, '') source = source.to_s.gsub(/\s/, '')
ignore_extension = (kind == :images) # don't append extension ignore_extension = (kind == :images) # don't append extension
source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/ source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/
result_path = source if source =~ %r{^/} # absolute path if source =~ %r{^/} # absolute path
result_path ||= asset_url(source, asset_folder) asset_folder = ""
"#{result_path}" end
asset_url(source, asset_folder)
end end
# Overload the regular link_to to be sitemap-aware - if you # Overload the regular link_to to be sitemap-aware - if you
@ -131,10 +132,11 @@ module Middleman
raise "Can't use the relative option with an external URL" if relative raise "Can't use the relative option with an external URL" if relative
else else
# Handle relative urls # Handle relative urls
current_dir = Pathname('/' + current_resource.path).dirname current_source_dir = Pathname('/' + current_resource.path).dirname
path = Pathname(url) path = Pathname(url)
url = current_dir.join(path).to_s if path.relative? url = current_source_dir.join(path).to_s if path.relative?
resource = sitemap.find_resource_by_path(url) resource = sitemap.find_resource_by_path(url)
@ -148,6 +150,9 @@ module Middleman
if resource if resource
if effective_relative if effective_relative
resource_url = resource.url resource_url = resource.url
# Output urls relative to the destination path, not the source path
current_dir = Pathname('/' + current_resource.destination_path).dirname
new_url = Pathname(resource_url).relative_path_from(current_dir).to_s new_url = Pathname(resource_url).relative_path_from(current_dir).to_s
# Put back the trailing slash to avoid unnecessary Apache redirects # Put back the trailing slash to avoid unnecessary Apache redirects

View file

@ -30,31 +30,13 @@ module Middleman
# @param [String] prefix # @param [String] prefix
# @return [String] # @return [String]
def asset_url(path, prefix="") def asset_url(path, prefix="")
begin path = super(path, prefix)
prefix = images_dir if prefix == http_images_path
rescue
end
if path.include?("://") if path.include?("//")
super(path, prefix)
elsif path[0,1] == "/"
path path
else else
path = File.join(prefix, path) if prefix.length > 0 current_dir = Pathname('/' + current_resource.destination_path).dirname
Pathname(path).relative_path_from(current_dir)
request_path = current_path.dup
request_path << index_file if path.match(%r{/$})
parts = request_path.gsub(%r{^/}, '').split('/')
if parts.length > 1
arry = []
(parts.length - 1).times { arry << ".." }
arry << path
File.join(*arry)
else
path
end
end end
end end
end end