Class: Assetify::Pathfix

Inherits:
Object
  • Object
show all
Defined in:
lib/assetify/asset/pathfix.rb

Instance Method Summary collapse

Constructor Details

#initialize(chunk, as = :erb, ns = nil) ⇒ Pathfix

Returns a new instance of Pathfix.



4
5
6
7
# File 'lib/assetify/asset/pathfix.rb', line 4

def initialize chunk, as = :erb, ns = nil
  @chunk, @as, @ns = chunk, as, ns
  @images = scan_images
end

Instance Method Details

#fixObject



26
27
28
29
30
31
# File 'lib/assetify/asset/pathfix.rb', line 26

def fix
  @images.each do |path|
    @chunk["url(#{path})"] = replace path.split("/").last
  end
  @as != :erb ? tmpl_chunk : @chunk
end

#imagesObject



9
10
11
# File 'lib/assetify/asset/pathfix.rb', line 9

def images
  @images
end

#replace(src) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/assetify/asset/pathfix.rb', line 17

def replace src
  fpath = @ns ? "#{@ns}/#{src}" : src
  if @as == :erb
    "url('<%= image_path('#{fpath}') %>')"
  else
    "image-url('#{fpath}')"
  end
end

#scan_imagesObject



13
14
15
# File 'lib/assetify/asset/pathfix.rb', line 13

def scan_images
  @chunk.scan(/url\(([a-zA-Z0-9\/\_\-\.]*\.\w+)\)/xo).flatten
end

#tmpl_chunkObject



33
34
35
36
37
38
39
40
# File 'lib/assetify/asset/pathfix.rb', line 33

def tmpl_chunk
  begin
    require 'sass/css'
    Sass::CSS.new(@chunk).render(@as)
  rescue Sass::SyntaxError => e
    @error = e
  end
end