extensionify relative_assets

This commit is contained in:
Thomas Reynolds 2013-04-20 14:33:18 -07:00
parent 1c4e6cb835
commit 1d1f37d764
2 changed files with 23 additions and 41 deletions

View file

@ -64,10 +64,8 @@ module Middleman
# RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL.
Middleman::Extensions.register(:relative_assets) do
require "middleman-more/extensions/relative_assets"
Middleman::Extensions::RelativeAssets
end
require "middleman-more/extensions/relative_assets"
Middleman::Extensions::RelativeAssets.register
# GZIP assets and pages during build
require "middleman-more/extensions/gzip"

View file

@ -1,44 +1,28 @@
# Extension namespace
module Middleman
module Extensions
# Relative Assets extension
class Middleman::Extensions::RelativeAssets < ::Middleman::Extension
# Relative Assets extension
module RelativeAssets
def initialize(app, options_hash={}, &block)
super
# Setup extension
class << self
# After compass is setup, make it use the registered cache buster
app.compass_config do |config|
config.relative_assets = true
end
end
# Once registered
def registered(app)
# Tell compass to use relative assets
app.compass_config do |config|
config.relative_assets = true
end
helpers do
# asset_url override for relative assets
# @param [String] path
# @param [String] prefix
# @return [String]
def asset_url(path, prefix="")
path = super(path, prefix)
# Include instance methods
app.send :include, InstanceMethods
end
alias :included :registered
end
# Relative Assets instance method
module InstanceMethods
# 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?("//") || !current_resource
path
else
current_dir = Pathname('/' + current_resource.destination_path)
Pathname(path).relative_path_from(current_dir.dirname).to_s
end
end
if path.include?("//") || !current_resource
path
else
current_dir = Pathname('/' + current_resource.destination_path)
Pathname(path).relative_path_from(current_dir.dirname).to_s
end
end
end