From 3fca2c6961215a2e0b9d00faee6d185c4b93a5d7 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Mon, 22 Jun 2015 10:37:17 -0700 Subject: [PATCH] Add relative argument to stylesheet and javascript helpers. Closes #1539 --- CHANGELOG.md | 1 + .../features/relative_assets.feature | 11 ++++ .../absolute_image_relative_css.html.erb | 9 ++++ .../source/javascripts/app.js | 3 ++ .../source/relative_image.html.erb | 1 + .../relative_image_absolute_css.html.erb | 9 ++++ .../core_extensions/default_helpers.rb | 50 +++++++++++++++++-- .../extensions/relative_assets.rb | 12 ++--- 8 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 middleman-core/fixtures/relative-assets-app/source/absolute_image_relative_css.html.erb create mode 100644 middleman-core/fixtures/relative-assets-app/source/javascripts/app.js create mode 100644 middleman-core/fixtures/relative-assets-app/source/relative_image_absolute_css.html.erb diff --git a/CHANGELOG.md b/CHANGELOG.md index 22573947c..c93787260 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ master * Empower link_to in a i18n context. * Add retina files support on automatic_image_sizes * Fix woff/woff2 confusion in asset hashing. +* Support `relative: false` on `stylesheet_link_tag` and `javascript_include_tag` 3.3.12 === diff --git a/middleman-core/features/relative_assets.feature b/middleman-core/features/relative_assets.feature index 1ebafdfa6..e0c6b16f0 100644 --- a/middleman-core/features/relative_assets.feature +++ b/middleman-core/features/relative_assets.feature @@ -21,6 +21,12 @@ Feature: Relative Assets Given "relative_assets" feature is "disabled" And the Server is running at "relative-assets-app" When I go to "/relative_image.html" + Then I should see '"/stylesheets/relative_assets.css"' + Then I should see '"/javascripts/app.js"' + Then I should see "/images/blank.gif" + When I go to "/absolute_image_relative_css.html" + Then I should see '"stylesheets/relative_assets.css"' + Then I should see '"javascripts/app.js"' Then I should see "/images/blank.gif" Scenario: Rendering css with the feature enabled @@ -54,6 +60,11 @@ Feature: Relative Assets Given "relative_assets" feature is "enabled" And the Server is running at "relative-assets-app" When I go to "/relative_image.html" + Then I should see '"stylesheets/relative_assets.css"' + Then I should see '"javascripts/app.js"' + When I go to "/relative_image_absolute_css.html" + Then I should see '"/stylesheets/relative_assets.css"' + Then I should see '"/javascripts/app.js"' Then I should not see "/images/blank.gif" And I should see "images/blank.gif" diff --git a/middleman-core/fixtures/relative-assets-app/source/absolute_image_relative_css.html.erb b/middleman-core/fixtures/relative-assets-app/source/absolute_image_relative_css.html.erb new file mode 100644 index 000000000..1a763ecf8 --- /dev/null +++ b/middleman-core/fixtures/relative-assets-app/source/absolute_image_relative_css.html.erb @@ -0,0 +1,9 @@ + + + <%= stylesheet_link_tag :relative_assets, relative: true %> + <%= javascript_include_tag :app, relative: true %> + + + <%= image_tag "blank.gif" %> + + \ No newline at end of file diff --git a/middleman-core/fixtures/relative-assets-app/source/javascripts/app.js b/middleman-core/fixtures/relative-assets-app/source/javascripts/app.js new file mode 100644 index 000000000..a9ec7a893 --- /dev/null +++ b/middleman-core/fixtures/relative-assets-app/source/javascripts/app.js @@ -0,0 +1,3 @@ +function hello() { + console.log('world'); +} \ No newline at end of file diff --git a/middleman-core/fixtures/relative-assets-app/source/relative_image.html.erb b/middleman-core/fixtures/relative-assets-app/source/relative_image.html.erb index 4fb9dc0c1..4344f2a27 100644 --- a/middleman-core/fixtures/relative-assets-app/source/relative_image.html.erb +++ b/middleman-core/fixtures/relative-assets-app/source/relative_image.html.erb @@ -1,6 +1,7 @@ <%= stylesheet_link_tag :relative_assets %> + <%= javascript_include_tag :app %> <%= image_tag "blank.gif" %> diff --git a/middleman-core/fixtures/relative-assets-app/source/relative_image_absolute_css.html.erb b/middleman-core/fixtures/relative-assets-app/source/relative_image_absolute_css.html.erb new file mode 100644 index 000000000..4e910c476 --- /dev/null +++ b/middleman-core/fixtures/relative-assets-app/source/relative_image_absolute_css.html.erb @@ -0,0 +1,9 @@ + + + <%= stylesheet_link_tag :relative_assets, relative: false %> + <%= javascript_include_tag :app, relative: false %> + + + <%= image_tag "blank.gif" %> + + \ No newline at end of file diff --git a/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb index 0ac4c1476..cbb947682 100644 --- a/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb @@ -99,6 +99,36 @@ def auto_javascript_include_tag end end + # Override helper to add `relative` opt-out. + def stylesheet_link_tag(*sources) + options = { + :rel => 'stylesheet' + }.update(sources.extract_options!.symbolize_keys) + + path_options = {} + path_options[:relative] = options.delete(:relative) if options.key?(:relative) + + sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source| + all << tag(:link, { + href: asset_path(:css, source, path_options) + }.update(options)) + end + end + + # Override helper to add `relative` opt-out. + def javascript_include_tag(*sources) + options = sources.extract_options!.symbolize_keys + + path_options = {} + path_options[:relative] = options.delete(:relative) if options.key?(:relative) + + sources.flatten.inject(::ActiveSupport::SafeBuffer.new) do |all,source| + all << content_tag(:script, nil, { + src: asset_path(:js, source, path_options) + }.update(options)) + end + end + # Output a stylesheet link tag based on the current path # # @param [Symbol] asset_ext The type of asset @@ -153,8 +183,9 @@ def page_classes(path=current_path.dup, options={}) # # @param [Symbol] kind The type of file # @param [String] source The path to the file + # @param [Hash] options Additional options. # @return [String] - def asset_path(kind, source) + def asset_path(kind, source, options={}) return source if source.to_s.include?('//') || source.to_s.start_with?('data:') asset_folder = case kind when :css @@ -174,29 +205,38 @@ def asset_path(kind, source) source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}") asset_folder = '' if source.start_with?('/') # absolute path - asset_url(source, asset_folder) + asset_url(source, asset_folder, options) end # Get the URL of an asset given a type/prefix # # @param [String] path The path (such as "photo.jpg") # @param [String] prefix The type prefix (such as "images") + # @param [Hash] options Additional options. # @return [String] The fully qualified asset url - def asset_url(path, prefix='') + def asset_url(path, prefix='', options={}) # Don't touch assets which already have a full path - if path.include?('//') || path.start_with?('data:') + if path.include?('//') || path.start_with?('data:') || !current_resource path else # rewrite paths to use their destination path - if resource = sitemap.find_resource_by_destination_path(url_for(path)) + result = if resource = sitemap.find_resource_by_destination_path(url_for(path)) resource.url else path = File.join(prefix, path) + if resource = sitemap.find_resource_by_path(path) resource.url else File.join(config[:http_prefix], path) end end + + if options[:relative] != true + result + else + current_dir = Pathname('/' + current_resource.destination_path) + Pathname(result).relative_path_from(current_dir.dirname).to_s + end end end diff --git a/middleman-core/lib/middleman-more/extensions/relative_assets.rb b/middleman-core/lib/middleman-more/extensions/relative_assets.rb index 130bffb84..95eb291b5 100644 --- a/middleman-core/lib/middleman-more/extensions/relative_assets.rb +++ b/middleman-core/lib/middleman-more/extensions/relative_assets.rb @@ -13,16 +13,12 @@ def initialize(app, options_hash={}, &block) # asset_url override for relative assets # @param [String] path # @param [String] prefix + # @param [Hash] options Additional options. # @return [String] - def asset_url(path, prefix='') - path = super(path, prefix) + def asset_url(path, prefix='', options={}) + options[:relative] = true unless options.key?(:relative) - if path.include?('//') || path.start_with?('data:') || !current_resource - path - else - current_dir = Pathname('/' + current_resource.destination_path) - Pathname(path).relative_path_from(current_dir.dirname).to_s - end + path = super(path, prefix, options) end end end