~ubuntu-branches/ubuntu/vivid/ruby-compass/vivid-proposed

« back to all changes in this revision

Viewing changes to core/lib/compass/core/sass_extensions/functions/urls.rb

  • Committer: Package Import Robot
  • Author(s): Jonas Smedegaard
  • Date: 2014-10-23 01:41:15 UTC
  • mfrom: (12.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20141023014115-bfhklnxtb876j1k4
Tags: 1.0.1~dfsg-3
* Fix tighten dependencies on ruby-sass and ruby-chunky-png.
* Adjust watch file to track only stable releases.
* Update Vcs-Browser URL to use cgit web frontend.
* Declare compliance with Debian Policy 3.9.6.
* Add NEWS entry about Blueprint now packaged separately.
* Stop build-depend explicitly on ruby.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
module Compass::Core::SassExtensions::Functions::Urls
 
2
 
 
3
 
 
4
  def self.has?(base, instance_method)
 
5
    Sass::Util.has?(:instance_method, base, instance_method)
 
6
  end
 
7
 
 
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)
 
13
  end
 
14
 
 
15
  module StylesheetUrl
 
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]
 
20
      end
 
21
    end
 
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
 
29
      else
 
30
        Compass.configuration.http_root_relative(Compass.configuration.css_dir)
 
31
      end
 
32
 
 
33
      path = "#{http_stylesheets_path}/#{path.value}"
 
34
      if only_path.to_bool
 
35
        unquoted_string(clean_path(path))
 
36
      else
 
37
        clean_url(path)
 
38
      end
 
39
    end
 
40
  end
 
41
 
 
42
  module FontUrl
 
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]
 
48
      end
 
49
    end
 
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.
 
52
 
 
53
      # Short curcuit if they have provided an absolute url.
 
54
      if absolute_path?(path)
 
55
        return unquoted_string("url(#{path})")
 
56
      end
 
57
 
 
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)
 
62
                        else
 
63
                          Compass.configuration.http_fonts_path
 
64
                        end
 
65
 
 
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(/[?#].*$/,""))
 
69
      end
 
70
 
 
71
      # prepend the path to the font if there's one
 
72
      if http_fonts_path
 
73
        http_fonts_path = "#{http_fonts_path}/" unless http_fonts_path[-1..-1] == "/"
 
74
        path = "#{http_fonts_path}#{path}"
 
75
      end
 
76
 
 
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)
 
80
      end
 
81
 
 
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}"
 
87
        else
 
88
          path = cache_busted_path(path, real_path)
 
89
        end
 
90
        path = "#{path}#{"#" if anchor}#{anchor}"
 
91
      end
 
92
 
 
93
      # prepend the asset host if there is one.
 
94
      path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
 
95
 
 
96
      if only_path.to_bool
 
97
        unquoted_string(clean_path(path))
 
98
      else
 
99
        clean_url(path)
 
100
      end
 
101
    end
 
102
  end
 
103
 
 
104
  module ImageUrl
 
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]
 
110
      end
 
111
    end
 
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.
 
114
 
 
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
 
117
        # the images path.
 
118
        path = $1
 
119
      elsif absolute_path?(path)
 
120
        # Short curcuit if they have provided an absolute url.
 
121
        return unquoted_string("url(#{path})")
 
122
      end
 
123
 
 
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
 
130
      else
 
131
        Compass.configuration.http_root_relative(Compass.configuration.images_dir)
 
132
      end
 
133
 
 
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(/#.*$/,""))
 
137
      else
 
138
        File.join(Compass.configuration.project_path, path.gsub(/#.*$/,""))
 
139
      end
 
140
 
 
141
      # prepend the path to the image if there's one
 
142
      if http_images_path
 
143
        http_images_path = "#{http_images_path}/" unless http_images_path[-1..-1] == "/"
 
144
        path = "#{http_images_path}#{path}"
 
145
      end
 
146
 
 
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)
 
150
      end
 
151
 
 
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}"
 
157
        else
 
158
          path = cache_busted_path(path, real_path)
 
159
        end
 
160
        path = "#{path}#{"#" if anchor}#{anchor}"
 
161
      end
 
162
 
 
163
      # prepend the asset host if there is one.
 
164
      path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
 
165
 
 
166
      if only_path.to_bool
 
167
        unquoted_string(clean_path(path))
 
168
      else
 
169
        clean_url(path)
 
170
      end
 
171
    end
 
172
  end
 
173
 
 
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]
 
179
      end
 
180
    end
 
181
    def generated_image_url(path, cache_buster = bool(false))
 
182
      path = path.value # get to the string value of the literal.
 
183
 
 
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.
 
187
        path = $1
 
188
      elsif absolute_path?(path)
 
189
        # Short curcuit if they have provided an absolute url.
 
190
        return unquoted_string("url(#{path})")
 
191
      end
 
192
 
 
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
 
199
      else
 
200
        Compass.configuration.http_root_relative(Compass.configuration.generated_images_dir)
 
201
      end
 
202
 
 
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(/#.*$/,""))
 
206
      else
 
207
        File.join(Compass.configuration.project_path, path.gsub(/#.*$/,""))
 
208
      end
 
209
 
 
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}"
 
214
      end
 
215
 
 
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)
 
219
      end
 
220
 
 
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}"
 
226
        else
 
227
          path = cache_busted_path(path, real_path)
 
228
        end
 
229
        path = "#{path}#{"#" if anchor}#{anchor}"
 
230
      end
 
231
 
 
232
      # prepend the asset host if there is one.
 
233
      path = "#{asset_host}#{'/' unless path[0..0] == "/"}#{path}" if asset_host
 
234
 
 
235
      clean_url(path)
 
236
    end
 
237
  end
 
238
 
 
239
  private
 
240
 
 
241
  # Emits a path, taking off any leading "./"
 
242
  def clean_path(url)
 
243
    url = url.to_s
 
244
    url = url[0..1] == "./" ? url[2..-1] : url
 
245
  end
 
246
 
 
247
  # Emits a url, taking off any leading "./"
 
248
  def clean_url(url)
 
249
    unquoted_string("url('#{clean_path(url)}')")
 
250
  end
 
251
 
 
252
  def relative?
 
253
    Compass.configuration.relative_assets?
 
254
  end
 
255
 
 
256
  def absolute_path?(path)
 
257
    path[0..0] == "/" || path[0..3] == "http"
 
258
  end
 
259
 
 
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
 
265
    end
 
266
  end
 
267
 
 
268
  def cache_busted_path(path, real_path)
 
269
    cache_buster = compute_cache_buster(path, real_path)
 
270
    if cache_buster.nil?
 
271
      return path
 
272
    elsif cache_buster.is_a?(String)
 
273
      cache_buster = {:query => cache_buster}
 
274
    else
 
275
      path = cache_buster[:path] if cache_buster[:path]
 
276
    end
 
277
    
 
278
    if cache_buster[:query]
 
279
      "#{path}#{path["?"] ? "&" : "?"}#{cache_buster[:query]}"
 
280
    else
 
281
      path
 
282
    end
 
283
  end
 
284
 
 
285
  def compute_cache_buster(path, real_path)
 
286
    file = nil
 
287
    if Compass.configuration.asset_cache_buster
 
288
      args = [path]
 
289
      if Compass.configuration.asset_cache_buster.arity > 1
 
290
        begin
 
291
          file = File.new(real_path) if real_path
 
292
        rescue Errno::ENOENT
 
293
          # pass
 
294
        end
 
295
        args << file
 
296
      end
 
297
      Compass.configuration.asset_cache_buster.call(*args)
 
298
    elsif real_path
 
299
      default_cache_buster(path, real_path)
 
300
    end
 
301
  ensure
 
302
    file.close if file
 
303
  end
 
304
 
 
305
  def default_cache_buster(path, real_path)
 
306
    if File.readable?(real_path)
 
307
      File.mtime(real_path).to_i.to_s
 
308
    else
 
309
      $stderr.puts "WARNING: '#{File.basename(path)}' was not found (or cannot be read) in #{File.dirname(real_path)}"
 
310
    end
 
311
  end
 
312
 
 
313
end