Sha256: 9dd17c4e44ee50140a867655366e2335f81871bbd7cdca41325d9b796039900b

Contents?: true

Size: 827 Bytes

Versions: 2

Compression:

Stored size: 827 Bytes

Contents

module Assetify
  class Pathfix

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

    def images
      @images
    end

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

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

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

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

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
assetify-2.0.1 lib/assetify/asset/pathfix.rb
assetify-2.0.0 lib/assetify/asset/pathfix.rb