middleman/middleman-more/lib/middleman-more/extensions/relative_assets.rb

46 lines
1.1 KiB
Ruby
Raw Normal View History

2011-12-31 23:28:17 +01:00
# Extension namespace
2012-04-27 00:59:28 +02:00
module Middleman
module Extensions
2011-12-31 23:28:17 +01:00
2012-04-27 00:59:28 +02:00
# Relative Assets extension
module RelativeAssets
2011-12-31 23:28:17 +01:00
2012-04-27 00:59:28 +02:00
# Setup extension
class << self
2011-12-31 23:28:17 +01:00
2012-04-27 00:59:28 +02:00
# Once registered
def registered(app)
# Tell compass to use relative assets
app.compass_config do |config|
config.relative_assets = true
end
# Include instance methods
app.send :include, InstanceMethods
end
2012-04-27 00:59:28 +02:00
alias :included :registered
end
2012-04-27 00:59:28 +02:00
# Relative Assets instance method
module InstanceMethods
2011-12-31 23:28:17 +01:00
2012-04-27 00:59:28 +02:00
# asset_url override for relative assets
# @param [String] path
# @param [String] prefix
# @return [String]
def asset_url(path, prefix="")
path = super(path, prefix)
if path.include?("//")
path
2012-04-27 00:59:28 +02:00
else
current_dir = Pathname('/' + current_resource.destination_path).dirname
Pathname(path).relative_path_from(current_dir)
end
end
end
end
end
end