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:
parent
2a18e075c6
commit
bed5fe3458
@ -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
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user