From bed5fe345853732beb2d4ad72b96065b0cf490f9 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Sun, 5 Apr 2009 03:13:42 -0700 Subject: [PATCH] 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. --- lib/compass/configuration.rb | 15 ++++++++++++++- lib/sass_extensions.rb | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/compass/configuration.rb b/lib/compass/configuration.rb index 31b867c0..d48a4a5d 100644 --- a/lib/compass/configuration.rb +++ b/lib/compass/configuration.rb @@ -5,13 +5,15 @@ module Compass include Singleton ATTRIBUTES = [ + :project_type, :project_path, :css_dir, :sass_dir, :images_dir, :javascripts_dir, :output_style, - :environment + :environment, + :http_images_path ] attr_accessor *ATTRIBUTES @@ -77,6 +79,14 @@ module Compass "images" 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 if environment == :development :expanded @@ -98,6 +108,9 @@ module Compass contents << "\n" if required_libraries.any? ATTRIBUTES.each do |prop| value = send(prop) + if respond_to?("comment_for_#{prop}") + contents << send("comment_for_#{prop}") + end unless value.nil? contents << %Q(#{prop} = #{value.inspect}\n) end diff --git a/lib/sass_extensions.rb b/lib/sass_extensions.rb index d432d818..dd3feafb 100644 --- a/lib/sass_extensions.rb +++ b/lib/sass_extensions.rb @@ -2,6 +2,7 @@ require 'sass' module Sass::Script::Functions COMMA_SEPARATOR = /\s*,\s*/ + def nest(*arguments) nested = arguments.map{|a| a.value}.inject do |memo,arg| ancestors = memo.split(COMMA_SEPARATOR) @@ -10,4 +11,19 @@ module Sass::Script::Functions end Sass::Script::String.new(nested) 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 \ No newline at end of file