Sha256: 75379ca92bfdcb7dced1fc8168c5552702b2f57643f423c7f6ae1d6979847de9

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

require "#{$script_dir}/filters/file-reference-filter"

$jsl_import_regex= /\/\*jsl:import\s+([^\*]*)\*\//

class CssDependencyFilter < FileReferenceFilter
  
  def handles_file(file)
    return [".css"].include?(file.extension)
  end
  
  def preprocess_content(file, content)
    # Replace all ' (single quotes) with " (double quotes) in
    # order to fix a problem with the background url regexp
    content.gsub!(/\'/,'"')
    # Force a newline after a rule terminating ; (semi-colon) 
    # in order to fix a problem with the background url regexp
    content.gsub!(/;(\n|\r)*/, ";\n")

    # Rewrites the 'url("...")' rules to a relative path 
    # based on the location of the new concatenated CSS file.
    line_num=0

    lines= content.split("\n")
    
    lines.each { |line|
    
      line_num+=1

      line.gsub!(/@import\s+url\("?(.*\.css)"?\)/) { |match|
        css_file= File.join(file.parent_folder, $1)
        
        if (!File.exists?(css_file))
          file.error "imported CSS file not found: #{$1}", line_num
          # leave the @import rule in place
          match
        else
          file.add_dependency(SourceFile.from_path(css_file))
        end
      }
      
      line.gsub!(/url\("?(.*\.(jpg|png|gif))"?\)/) { |match|
        image_file= File.join(file.parent_folder, $1)

        if (!File.exists?(image_file))
          file.warning "resource not found: #{$1} (#{image_file})", line_num
          "url(\"#{$1}\")"
        else
          asset= SourceFile.from_path(image_file)
          file.add_asset(asset)
          "url(\"#{file_reference(asset)}\")"
        end
      }
    }
    
    lines.join("\n")
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
distil-0.8.4 lib/filters/css-filter.rb
distil-0.8.2 lib/filters/css-filter.rb
distil-0.8.1 lib/filters/css-filter.rb