Module: Compass::SassExtensions::Functions::Urls::GeneratedImageUrl

Defined in:
lib/compass/sass_extensions/functions/urls.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



142
143
144
145
146
147
# File 'lib/compass/sass_extensions/functions/urls.rb', line 142

def self.included(base)
  if base.respond_to?(:declare)
    base.declare :generated_image_url, [:path]
    base.declare :generated_image_url, [:path, :cache_buster]
  end
end

Instance Method Details

#generated_image_url(path, cache_buster = Sass::Script::Bool.new(false)) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/compass/sass_extensions/functions/urls.rb', line 148

def generated_image_url(path, cache_buster = Sass::Script::Bool.new(false))
  path = path.value # get to the string value of the literal.

  if path =~ %r{^#{Regexp.escape(Compass.configuration.http_generated_images_path)}/(.*)}
    # Treat root relative urls (without a protocol) like normal if they start with
    # the generated_images path.
    path = $1
  elsif absolute_path?(path)
    # Short curcuit if they have provided an absolute url.
    return Sass::Script::String.new("url(#{path})")
  end

  # Compute the path to the image, either root relative or stylesheet relative
  # or nil if the http_generated_images_path is not set in the configuration.
  http_generated_images_path = if relative?
    compute_relative_path(Compass.configuration.generated_images_path)
  elsif Compass.configuration.http_generated_images_path
    Compass.configuration.http_generated_images_path
  else
    Compass.configuration.http_root_relative(Compass.configuration.generated_images_dir)
  end

  # Compute the real path to the image on the file stystem if the generated_images_dir is set.
  real_path = if Compass.configuration.generated_images_dir
    File.join(Compass.configuration.project_path, Compass.configuration.generated_images_dir, path)
  end

  # prepend the path to the image if there's one
  if http_generated_images_path
    http_generated_images_path = "#{http_generated_images_path}/" unless http_generated_images_path[-1..-1] == "/"
    path = "#{http_generated_images_path}#{path}"
  end

  # Compute the asset host unless in relative mode.
  asset_host = if !relative? && Compass.configuration.asset_host
    Compass.configuration.asset_host.call(path)
  end

  # Compute and append the cache buster if there is one.
  if cache_buster.to_bool
    if cache_buster.is_a?(Sass::Script::String)
      path += "?#{cache_buster.value}"
    else
      path = cache_busted_path(path, real_path)
    end
  end

  # prepend the asset host if there is one.
  path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host

  clean_url(path)
end