middleman/middleman-core/lib/middleman-core/core_extensions/assets.rb

40 lines
1 KiB
Ruby
Raw Normal View History

2011-11-30 08:43:01 +01:00
# Base helper to manipulate asset paths
2011-07-06 19:40:17 +02:00
module Middleman::CoreExtensions::Assets
2011-11-30 08:43:01 +01:00
# Extension registered
2011-07-06 19:40:17 +02:00
class << self
2011-11-30 08:43:01 +01:00
# @private
2011-07-06 19:40:17 +02:00
def registered(app)
2011-11-21 06:21:19 +01:00
# Disable Padrino cache buster
app.set :asset_stamp, false
2011-11-30 08:43:01 +01:00
# Include helpers
app.send :include, InstanceMethod
2011-07-06 19:40:17 +02:00
end
alias :included :registered
end
2011-11-30 08:43:01 +01:00
# Methods to be mixed-in to Middleman::Base
module InstanceMethod
2011-11-30 08:43:01 +01:00
# 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")
2011-12-18 05:12:13 +01:00
# @return [String] The fully qualified asset url
2011-07-06 19:40:17 +02:00
def asset_url(path, prefix="")
2011-11-30 08:43:01 +01:00
# Don't touch assets which already have a full path
if path.include?("//")
path
else # rewrite paths to use their destination path
path = File.join(prefix, path)
2012-04-04 19:26:07 +02:00
if resource = sitemap.find_resource_by_path(path)
path = resource.path
end
File.join(http_prefix, path)
end
2011-07-06 19:40:17 +02:00
end
end
end