From 4faa98e874b26193038aabf8ac05b60f3e6cf996 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Sat, 30 Jun 2012 22:02:23 -0700 Subject: [PATCH] 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. --- .../features/helpers_link_to.feature | 6 ++--- .../features/relative_assets.feature | 13 +++++++++- .../core_extensions/default_helpers.rb | 15 +++++++---- .../extensions/relative_assets.rb | 26 +++---------------- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/middleman-more/features/helpers_link_to.feature b/middleman-more/features/helpers_link_to.feature index ff8e59b5..990e7e8a 100644 --- a/middleman-more/features/helpers_link_to.feature +++ b/middleman-more/features/helpers_link_to.feature @@ -61,11 +61,11 @@ Feature: link_to helper """ And the Server is running at "indexable-app" When I go to "/link_to/" - Then I should see 'absolute: Needs Index' - Then I should see 'relative: Relative' - When I go to "/link_to/sub/" Then I should see 'absolute: Needs Index' Then I should see 'relative: Relative' + When I go to "/link_to/sub/" + Then I should see 'absolute: Needs Index' + Then I should see 'relative: Relative' Scenario: link_to can take a Resource Given a fixture app "indexable-app" diff --git a/middleman-more/features/relative_assets.feature b/middleman-more/features/relative_assets.feature index 7fef1c57..1a18d905 100644 --- a/middleman-more/features/relative_assets.feature +++ b/middleman-more/features/relative_assets.feature @@ -88,4 +88,15 @@ Feature: Relative Assets Given "relative_assets" feature is "enabled" And the Server is running at "fonts-app" When I go to "/stylesheets/fonts.css" - Then I should see "url('../fonts/StMarie-Thin.otf" \ No newline at end of file + 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 '' \ No newline at end of file diff --git a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb index cd2adcc7..05b2f197 100644 --- a/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-more/lib/middleman-more/core_extensions/default_helpers.rb @@ -102,9 +102,10 @@ module Middleman source = source.to_s.gsub(/\s/, '') ignore_extension = (kind == :images) # don't append extension source << ".#{kind}" unless ignore_extension or source =~ /\.#{kind}/ - result_path = source if source =~ %r{^/} # absolute path - result_path ||= asset_url(source, asset_folder) - "#{result_path}" + if source =~ %r{^/} # absolute path + asset_folder = "" + end + asset_url(source, asset_folder) end # 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 else # Handle relative urls - current_dir = Pathname('/' + current_resource.path).dirname + current_source_dir = Pathname('/' + current_resource.path).dirname + 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) @@ -148,6 +150,9 @@ module Middleman if resource if effective_relative 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 # Put back the trailing slash to avoid unnecessary Apache redirects diff --git a/middleman-more/lib/middleman-more/extensions/relative_assets.rb b/middleman-more/lib/middleman-more/extensions/relative_assets.rb index 12cb2663..612f41d5 100755 --- a/middleman-more/lib/middleman-more/extensions/relative_assets.rb +++ b/middleman-more/lib/middleman-more/extensions/relative_assets.rb @@ -30,31 +30,13 @@ module Middleman # @param [String] prefix # @return [String] def asset_url(path, prefix="") - begin - prefix = images_dir if prefix == http_images_path - rescue - end + path = super(path, prefix) - if path.include?("://") - super(path, prefix) - elsif path[0,1] == "/" + if path.include?("//") path else - path = File.join(prefix, path) if prefix.length > 0 - - 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 + current_dir = Pathname('/' + current_resource.destination_path).dirname + Pathname(path).relative_path_from(current_dir) end end end