[Compass Core] All url helpers now accept a second argument that when true will cause only the path to be emited.

This commit is contained in:
Chris Eppstein 2010-08-05 20:55:48 -07:00
parent d99698f67a
commit ad967f5e6d
2 changed files with 40 additions and 12 deletions

View File

@ -21,24 +21,33 @@ classnames:
#stylesheet-url.helper #stylesheet-url.helper
%h3 %h3
%a(href="#stylesheet-url") %a(href="#stylesheet-url")
stylesheet-url($path) stylesheet-url($path, $only-path: false)
.details .details
%p %p
Generates a path to an asset found relative to the project's css directory. Generates a path to an asset found relative to the project's css directory.
%br
Passing a true value as the second argument will cause the only the path to be returned
instead of a `url()` function
#font-url.helper #font-url.helper
%h3 %h3
%a(href="#font-url") %a(href="#font-url")
font-url($path) font-url($path, $only-path: false)
.details .details
%p %p
Generates a path to an asset found relative to the project's font directory. Generates a path to an asset found relative to the project's font directory.
%br
Passing a true value as the second argument will cause the only the path to be returned
instead of a `url()` function
#image-url.helper #image-url.helper
%h3 %h3
%a(href="#image-url") %a(href="#image-url")
image-url($path) image-url($path, $only-path: false)
.details .details
%p %p
Generates a path to an asset found relative to the project's images directory. Generates a path to an asset found relative to the project's images directory.
%br
Passing a true value as the second argument will cause the only the path to be returned
instead of a `url()` function

View File

@ -1,6 +1,6 @@
module Compass::SassExtensions::Functions::Urls module Compass::SassExtensions::Functions::Urls
def stylesheet_url(path) def stylesheet_url(path, only_path = Sass::Script::Bool.new(false))
# Compute the path to the stylesheet, either root relative or stylesheet relative # 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. # or nil if the http_images_path is not set in the configuration.
http_stylesheets_path = if relative? http_stylesheets_path = if relative?
@ -11,10 +11,15 @@ module Compass::SassExtensions::Functions::Urls
Compass.configuration.http_root_relative(Compass.configuration.css_dir) Compass.configuration.http_root_relative(Compass.configuration.css_dir)
end end
clean_url("#{http_stylesheets_path}/#{path}") path = "#{http_stylesheets_path}/#{path}"
if only_path.to_bool
Sass::Script::String.new(clean_path(path))
else
clean_url(path)
end
end end
def font_url(path) def font_url(path, only_path = Sass::Script::Bool.new(false))
path = path.value # get to the string value of the literal. path = path.value # get to the string value of the literal.
# Short curcuit if they have provided an absolute url. # Short curcuit if they have provided an absolute url.
@ -30,10 +35,16 @@ module Compass::SassExtensions::Functions::Urls
Compass.configuration.http_fonts_path Compass.configuration.http_fonts_path
end end
clean_url("#{http_fonts_path}/#{path}") path = "#{http_fonts_path}/#{path}"
if only_path.to_bool
Sass::Script::String.new(clean_path(path))
else
clean_url(path)
end
end end
def image_url(path) def image_url(path, only_path = Sass::Script::Bool.new(false))
path = path.value # get to the string value of the literal. path = path.value # get to the string value of the literal.
if path =~ %r{^#{Regexp.escape(Compass.configuration.http_images_path)}/(.*)} if path =~ %r{^#{Regexp.escape(Compass.configuration.http_images_path)}/(.*)}
@ -79,16 +90,24 @@ module Compass::SassExtensions::Functions::Urls
# prepend the asset host if there is one. # prepend the asset host if there is one.
path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
if only_path.to_bool
Sass::Script::String.new(clean_path(path))
else
clean_url(path) clean_url(path)
end end
end
private private
# Emits a url, taking off any leading "./" # Emits a path, taking off any leading "./"
def clean_url(url) def clean_path(url)
url = url.to_s url = url.to_s
url = url[0..1] == "./" ? url[2..-1] : url url = url[0..1] == "./" ? url[2..-1] : url
Sass::Script::String.new("url('#{url}')") end
# Emits a url, taking off any leading "./"
def clean_url(url)
Sass::Script::String.new("url('#{clean_path(url)}')")
end end
def relative? def relative?