Added a new Sass function called image_url() that can be used instead of url() to link to an image relative to the images directory according to the compass project configuration.

This commit is contained in:
Chris Eppstein 2009-04-05 03:13:42 -07:00
parent 2a18e075c6
commit bed5fe3458
2 changed files with 30 additions and 1 deletions

View File

@ -5,13 +5,15 @@ module Compass
include Singleton include Singleton
ATTRIBUTES = [ ATTRIBUTES = [
:project_type,
:project_path, :project_path,
:css_dir, :css_dir,
:sass_dir, :sass_dir,
:images_dir, :images_dir,
:javascripts_dir, :javascripts_dir,
:output_style, :output_style,
:environment :environment,
:http_images_path
] ]
attr_accessor *ATTRIBUTES attr_accessor *ATTRIBUTES
@ -77,6 +79,14 @@ module Compass
"images" "images"
end end
def default_http_images_path
"/#{images_dir}"
end
def comment_for_http_images_path
"# To enable relative image paths using the images_url() function:\n# http_images_path = :relative\n"
end
def default_output_style def default_output_style
if environment == :development if environment == :development
:expanded :expanded
@ -98,6 +108,9 @@ module Compass
contents << "\n" if required_libraries.any? contents << "\n" if required_libraries.any?
ATTRIBUTES.each do |prop| ATTRIBUTES.each do |prop|
value = send(prop) value = send(prop)
if respond_to?("comment_for_#{prop}")
contents << send("comment_for_#{prop}")
end
unless value.nil? unless value.nil?
contents << %Q(#{prop} = #{value.inspect}\n) contents << %Q(#{prop} = #{value.inspect}\n)
end end

View File

@ -2,6 +2,7 @@ require 'sass'
module Sass::Script::Functions module Sass::Script::Functions
COMMA_SEPARATOR = /\s*,\s*/ COMMA_SEPARATOR = /\s*,\s*/
def nest(*arguments) def nest(*arguments)
nested = arguments.map{|a| a.value}.inject do |memo,arg| nested = arguments.map{|a| a.value}.inject do |memo,arg|
ancestors = memo.split(COMMA_SEPARATOR) ancestors = memo.split(COMMA_SEPARATOR)
@ -10,4 +11,19 @@ module Sass::Script::Functions
end end
Sass::Script::String.new(nested) Sass::Script::String.new(nested)
end end
def image_url(path)
http_images_path = if Compass.configuration.http_images_path == :relative
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
else
nil
end
else
Compass.configuration.http_images_path
end
path = "#{http_images_path}/#{path}" if http_images_path
Sass::Script::String.new("url(#{path})")
end
end end