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

63 lines
1.5 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="")
begin
prefix = images_dir if prefix == http_images_path
rescue
end
2012-04-27 00:59:28 +02:00
if path.include?("://")
super(path, prefix)
elsif path[0,1] == "/"
path
2012-04-27 00:59:28 +02:00
else
path = File.join(prefix, path) if prefix.length > 0
request_path = @request_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