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 # RelativeAssets allow any asset path in dynamic templates to be either
# relative to the root of the project or use an absolute URL. # relative to the root of the project or use an absolute URL.
Middleman::Extensions.register(:relative_assets) do require "middleman-more/extensions/relative_assets"
require "middleman-more/extensions/relative_assets" Middleman::Extensions::RelativeAssets.register
Middleman::Extensions::RelativeAssets
end
# GZIP assets and pages during build # GZIP assets and pages during build
require "middleman-more/extensions/gzip" require "middleman-more/extensions/gzip"

View file

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