[Compass Core] A new helper function stylesheet_url(path) can now be used to refer to assets that are relative to the css directory.

This commit is contained in:
Chris Eppstein 2009-07-03 22:00:18 -07:00
parent 21cfce33db
commit ff5c850014
4 changed files with 38 additions and 13 deletions

View File

@ -143,8 +143,9 @@ module Compass
end
end
def mk_http_path(path)
hp = http_path[0..-2] if http_path[-1..-1] == "/"
def root_relative(path)
hp = http_path || default_http_path
hp = hp[0..-2] if hp[-1..-1] == "/"
"#{hp}/#{path}"
end

View File

@ -1,14 +1,14 @@
module Compass::SassExtensions::Functions
end
['selectors', 'enumerate', 'image_url', 'display', 'inline_image'].each do |func|
['selectors', 'enumerate', 'urls', 'display', 'inline_image'].each do |func|
require File.join(File.dirname(__FILE__), 'functions', func)
end
module Sass::Script::Functions
include Compass::SassExtensions::Functions::Selectors
include Compass::SassExtensions::Functions::Enumerate
include Compass::SassExtensions::Functions::ImageUrl
include Compass::SassExtensions::Functions::Urls
include Compass::SassExtensions::Functions::Display
include Compass::SassExtensions::Functions::InlineImage
end

View File

@ -1,4 +1,19 @@
module Compass::SassExtensions::Functions::ImageUrl
module Compass::SassExtensions::Functions::Urls
def stylesheet_url(path)
# Compute the path to the stylesheet, either root relative or stylesheet relative
# or nil if the http_images_path is not set in the configuration.
http_stylesheets_path = if relative?
compute_relative_path(Compass.configuration.css_dir)
elsif Compass.configuration.http_stylesheets_path
Compass.configuration.http_stylesheets_path
else
Compass.configuration.root_relative(Compass.configuration.css_dir)
end
url("#{http_stylesheets_path}/#{path}")
end
def image_url(path)
path = path.value # get to the string value of the literal.
# Short curcuit if they have provided an absolute url.
@ -10,9 +25,11 @@ module Compass::SassExtensions::Functions::ImageUrl
# Compute the path to the image, either root relative or stylesheet relative
# or nil if the http_images_path is not set in the configuration.
http_images_path = if relative?
compute_relative_path
else
compute_relative_path(Compass.configuration.images_dir)
elsif Compass.configuration.http_stylesheets_path
Compass.configuration.http_images_path
else
Compass.configuration.root_relative(Compass.configuration.images_dir)
end
# Compute the real path to the image on the file stystem if the images_dir is set.
@ -37,23 +54,30 @@ module Compass::SassExtensions::Functions::ImageUrl
# prepend the asset host if there is one.
path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
Sass::Script::String.new("url(#{path})")
url(path)
end
# Emits a url, taking off any leading "./"
def url(url)
url = url.to_s
url = url[0..1] == "./" ? url[2..-1] : url
Sass::Script::String.new("url('#{url}')")
end
private
def relative?
Compass.configuration.http_images_path == :relative
Compass.configuration.relative_assets?
end
def absolute_path?(path)
path[0..0] == "/" || path[0..3] == "http"
end
def compute_relative_path
def compute_relative_path(dir)
if (target_css_file = options[:css_filename])
images_path = File.join(Compass.configuration.project_path, Compass.configuration.images_dir)
Pathname.new(images_path).relative_path_from(Pathname.new(File.dirname(target_css_file))).to_s
path = File.join(Compass.configuration.project_path, dir)
Pathname.new(path).relative_path_from(Pathname.new(File.dirname(target_css_file))).to_s
end
end

View File

@ -1,3 +1,3 @@
.showgrid { background-image: url(http://assets2.example.com/images/grid.png?busted=true); }
.showgrid { background-image: url('http://assets2.example.com/images/grid.png?busted=true'); }
.inlinegrid { background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAUEAYAAACv1qP4AAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAZ0lEQVRYw+3QwQ2AIBAFUTEUwI3+uzN7gDscsIgxEuO8An52J11X73OudfxMraXkzHfO3Y98nQEhA0IGhAwIGRAyIGRAyICQASEDQgaEDAgZEDIgZEDIgJABoZzSGK3tPuN9ERFP7Nw4fg+c5g8V1wAAAABJRU5ErkJggg=='); }