$ sass -r ./file_exists.rb scss/foo.scss css/foo.css
require "./file_exists.rb"
.foo {
background: if(file-exists("../images/foo.png"), url("/images/foo.png"), url("/images/generic.png"));
}
module Sass::Script::Functions | |
def file_exists(path) | |
return bool(false) unless options[:filename] # not all sass has a file name (e.g. std input) | |
current_directory = File.dirname(options[:filename]) rescue nil # a sass filename from an importer may not be processable by File.dirname | |
return bool(false) unless current_directory && File.exist?(current_directory) # not all sass files are served from the filesystem | |
full_path = File.expand_path(path.value, current_directory) # a relative path will be expanded against the current_directory of the sass file, symlinks will be followed, absolute paths will be left alone. | |
return bool(File.exist?(full_path)) | |
end | |
end |