Fold assets.rb into default_helpers.rb

This commit is contained in:
Ben Hollis 2013-04-09 20:21:21 -07:00
parent fe6a18fbe6
commit fb9a322b6e
3 changed files with 29 additions and 54 deletions

View file

@ -23,10 +23,6 @@ module Middleman
require "middleman-more/core_extensions/default_helpers"
Middleman::Application.register Middleman::CoreExtensions::DefaultHelpers
# Setup asset path pipeline
require "middleman-more/core_extensions/assets"
Middleman::Application.register Middleman::CoreExtensions::Assets
# i18n
require "i18n"
app.after_configuration do

View file

@ -1,43 +0,0 @@
module Middleman
module CoreExtensions
# Base helper to manipulate asset paths
module Assets
# Extension registered
class << self
def registered(app)
# Disable Padrino cache buster
app.set :asset_stamp, false
# Include helpers
app.send :include, InstanceMethod
end
alias :included :registered
end
# Methods to be mixed-in to Middleman::Application
module InstanceMethod
# 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")
# @return [String] The fully qualified asset url
def asset_url(path, prefix="")
# 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)
if resource = sitemap.find_resource_by_path(path)
resource.url
else
File.join(config[:http_prefix], path)
end
end
end
end
end
end
end

View file

@ -22,6 +22,9 @@ module Middleman
app.helpers Helpers
app.config.define_setting :relative_links, false, 'Whether to generate relative links instead of absolute ones'
# Disable Padrino cache buster
app.set :asset_stamp, false
end
alias :included :registered
end
@ -104,6 +107,25 @@ module Middleman
asset_url(source, asset_folder)
end
# 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")
# @return [String] The fully qualified asset url
def asset_url(path, prefix="")
# 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)
if resource = sitemap.find_resource_by_path(path)
resource.url
else
File.join(config[:http_prefix], path)
end
end
end
# Given a source path (referenced either absolutely or relatively)
# or a Resource, this will produce the nice URL configured for that
# path, respecting :relative_links, directory indexes, etc.