1
module Compass::Core::SassExtensions::Functions::Urls
4
def self.has?(base, instance_method)
5
Sass::Util.has?(:instance_method, base, instance_method)
8
def self.included(base)
9
base.send(:include, StylesheetUrl) unless has?(base, :stylesheet_url)
10
base.send(:include, FontUrl) unless has?(base, :font_url)
11
base.send(:include, ImageUrl) unless has?(base, :image_url)
12
base.send(:include, GeneratedImageUrl) unless has?(base, :generated_image_url)
16
def self.included(base)
17
if base.respond_to?(:declare)
18
base.declare :stylesheet_url, [:path]
19
base.declare :stylesheet_url, [:path, :only_path]
22
def stylesheet_url(path, only_path = bool(false))
23
# Compute the path to the stylesheet, either root relative or stylesheet relative
24
# or nil if the http_images_path is not set in the configuration.
25
http_stylesheets_path = if relative?
26
compute_relative_path(Compass.configuration.css_path)
27
elsif Compass.configuration.http_stylesheets_path
28
Compass.configuration.http_stylesheets_path
30
Compass.configuration.http_root_relative(Compass.configuration.css_dir)
33
path = "#{http_stylesheets_path}/#{path.value}"
35
unquoted_string(clean_path(path))
43
def self.included(base)
44
if base.respond_to?(:declare)
45
base.declare :font_url, [:path]
46
base.declare :font_url, [:path, :only_path]
47
base.declare :font_url, [:path, :only_path, :cache_buster]
50
def font_url(path, only_path = bool(false), cache_buster = bool(true))
51
path = path.value # get to the string value of the literal.
53
# Short curcuit if they have provided an absolute url.
54
if absolute_path?(path)
55
return unquoted_string("url(#{path})")
58
# Compute the path to the font file, either root relative or stylesheet relative
59
# or nil if the http_fonts_path cannot be determined from the configuration.
60
http_fonts_path = if relative?
61
compute_relative_path(Compass.configuration.fonts_path)
63
Compass.configuration.http_fonts_path
66
# Compute the real path to the font on the file system if the fonts_dir is set.
67
real_path = if Compass.configuration.fonts_dir
68
File.join(Compass.configuration.fonts_path, path.gsub(/[?#].*$/,""))
71
# prepend the path to the font if there's one
73
http_fonts_path = "#{http_fonts_path}/" unless http_fonts_path[-1..-1] == "/"
74
path = "#{http_fonts_path}#{path}"
77
# Compute the asset host unless in relative mode.
78
asset_host = if !relative? && Compass.configuration.asset_host
79
Compass.configuration.asset_host.call(path)
82
# Compute and append the cache buster if there is one.
83
if cache_buster.to_bool
84
path, anchor = path.split("#", 2)
85
if cache_buster.is_a?(Sass::Script::Value::String)
86
path += "#{path["?"] ? "&" : "?"}#{cache_buster.value}"
88
path = cache_busted_path(path, real_path)
90
path = "#{path}#{"#" if anchor}#{anchor}"
93
# prepend the asset host if there is one.
94
path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
97
unquoted_string(clean_path(path))
105
def self.included(base)
106
if base.respond_to?(:declare)
107
base.declare :image_url, [:path]
108
base.declare :image_url, [:path, :only_path]
109
base.declare :image_url, [:path, :only_path, :cache_buster]
112
def image_url(path, only_path = bool(false), cache_buster = bool(true))
113
path = path.value # get to the string value of the literal.
115
if path =~ %r{^#{Regexp.escape(Compass.configuration.http_images_path)}/(.*)}
116
# Treat root relative urls (without a protocol) like normal if they start with
119
elsif absolute_path?(path)
120
# Short curcuit if they have provided an absolute url.
121
return unquoted_string("url(#{path})")
124
# Compute the path to the image, either root relative or stylesheet relative
125
# or nil if the http_images_path is not set in the configuration.
126
http_images_path = if relative?
127
compute_relative_path(Compass.configuration.images_path)
128
elsif Compass.configuration.http_images_path
129
Compass.configuration.http_images_path
131
Compass.configuration.http_root_relative(Compass.configuration.images_dir)
134
# Compute the real path to the image on the file stystem if the images_dir is set.
135
real_path = if Compass.configuration.images_path
136
File.join(Compass.configuration.images_path, path.gsub(/#.*$/,""))
138
File.join(Compass.configuration.project_path, path.gsub(/#.*$/,""))
141
# prepend the path to the image if there's one
143
http_images_path = "#{http_images_path}/" unless http_images_path[-1..-1] == "/"
144
path = "#{http_images_path}#{path}"
147
# Compute the asset host unless in relative mode.
148
asset_host = if !relative? && Compass.configuration.asset_host
149
Compass.configuration.asset_host.call(path)
152
# Compute and append the cache buster if there is one.
153
if cache_buster.to_bool
154
path, anchor = path.split("#", 2)
155
if cache_buster.is_a?(Sass::Script::Value::String)
156
path += "#{path["?"] ? "&" : "?"}#{cache_buster.value}"
158
path = cache_busted_path(path, real_path)
160
path = "#{path}#{"#" if anchor}#{anchor}"
163
# prepend the asset host if there is one.
164
path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
167
unquoted_string(clean_path(path))
174
module GeneratedImageUrl
175
def self.included(base)
176
if base.respond_to?(:declare)
177
base.declare :generated_image_url, [:path]
178
base.declare :generated_image_url, [:path, :cache_buster]
181
def generated_image_url(path, cache_buster = bool(false))
182
path = path.value # get to the string value of the literal.
184
if path =~ %r{^#{Regexp.escape(Compass.configuration.http_generated_images_path)}/(.*)}
185
# Treat root relative urls (without a protocol) like normal if they start with
186
# the generated_images path.
188
elsif absolute_path?(path)
189
# Short curcuit if they have provided an absolute url.
190
return unquoted_string("url(#{path})")
193
# Compute the path to the image, either root relative or stylesheet relative
194
# or nil if the http_generated_images_path is not set in the configuration.
195
http_generated_images_path = if relative?
196
compute_relative_path(Compass.configuration.generated_images_path)
197
elsif Compass.configuration.http_generated_images_path
198
Compass.configuration.http_generated_images_path
200
Compass.configuration.http_root_relative(Compass.configuration.generated_images_dir)
203
# Compute the real path to the image on the file stystem if the generated_images_dir is set.
204
real_path = if Compass.configuration.generated_images_path
205
File.join(Compass.configuration.generated_images_path, path.gsub(/#.*$/,""))
207
File.join(Compass.configuration.project_path, path.gsub(/#.*$/,""))
210
# prepend the path to the image if there's one
211
if http_generated_images_path
212
http_generated_images_path = "#{http_generated_images_path}/" unless http_generated_images_path[-1..-1] == "/"
213
path = "#{http_generated_images_path}#{path}"
216
# Compute the asset host unless in relative mode.
217
asset_host = if !relative? && Compass.configuration.asset_host
218
Compass.configuration.asset_host.call(path)
221
# Compute and append the cache buster if there is one.
222
if cache_buster.to_bool
223
path, anchor = path.split("#", 2)
224
if cache_buster.is_a?(Sass::Script::Value::String)
225
path += "#{path["?"] ? "&" : "?"}#{cache_buster.value}"
227
path = cache_busted_path(path, real_path)
229
path = "#{path}#{"#" if anchor}#{anchor}"
232
# prepend the asset host if there is one.
233
path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
241
# Emits a path, taking off any leading "./"
244
url = url[0..1] == "./" ? url[2..-1] : url
247
# Emits a url, taking off any leading "./"
249
unquoted_string("url('#{clean_path(url)}')")
253
Compass.configuration.relative_assets?
256
def absolute_path?(path)
257
path[0..0] == "/" || path[0..3] == "http"
260
def compute_relative_path(path)
261
if (target_css_file = options[:css_filename])
262
target_path = Pathname.new(File.expand_path(path))
263
source_path = Pathname.new(File.dirname(File.expand_path(target_css_file)))
264
target_path.relative_path_from(source_path).to_s
268
def cache_busted_path(path, real_path)
269
cache_buster = compute_cache_buster(path, real_path)
272
elsif cache_buster.is_a?(String)
273
cache_buster = {:query => cache_buster}
275
path = cache_buster[:path] if cache_buster[:path]
278
if cache_buster[:query]
279
"#{path}#{path["?"] ? "&" : "?"}#{cache_buster[:query]}"
285
def compute_cache_buster(path, real_path)
287
if Compass.configuration.asset_cache_buster
289
if Compass.configuration.asset_cache_buster.arity > 1
291
file = File.new(real_path) if real_path
297
Compass.configuration.asset_cache_buster.call(*args)
299
default_cache_buster(path, real_path)
305
def default_cache_buster(path, real_path)
306
if File.readable?(real_path)
307
File.mtime(real_path).to_i.to_s
309
$stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"