Sha256: f96f83718f67c0623f013cc37b38ebbaa5bdb31197d7ed330fd3e15be223292d

Contents?: true

Size: 1.64 KB

Versions: 9

Compression:

Stored size: 1.64 KB

Contents

module Distil

  CSS_IMPORT_REGEX = /@import\s+url\("?(.*\.css)"?\)/
  CSS_IMAGE_URL_REGEX= /url\("?([^)]*\.(jpg|png|gif|otf))"?\)/
  
  class CssFile < SourceFile
    include YuiMinifiableFile
    
    extension "css"
    content_type "css"

    def content
      return @content unless @content.nil?
      @content= File.read(full_path)
      # Replace all ' (single quotes) with " (double quotes)
      @content.gsub!(/\'/,'"')
      # Force a newline after a rule terminating ; (semi-colon)
      @content.gsub!(/;(\n|\r)*/, ";\n")
      @content
    end

    def rewrite_content_relative_to_path(path)
      content.gsub(CSS_IMAGE_URL_REGEX) { |match|
        image_file= File.join(dirname, $1)

        if (!File.exists?(image_file))
          match
        else
          asset= project.file_from_path(image_file)
          "url(\"#{asset.relative_path}\")"
        end
      }
    end

    def dependencies
      @dependencies unless @dependencies.nil?

      line_num=0
      content.each_line { |line|
        line_num+=1

        line.scan(CSS_IMPORT_REGEX) { |match|
          css_file= File.join(dirname, $1)

          if (!File.exists?(css_file))
            error "Imported CSS file not found: #{$1}", line_num
          else
            add_dependency(project.file_from_path(css_file))
          end
        }
      
        line.scan(CSS_IMAGE_URL_REGEX) { |match|
          image_file= File.join(dirname, $1)

          if (!File.exists?(image_file))
            warning "Resource not found: #{$1}", line_num
          else
            asset= project.file_from_path(image_file)
            add_asset(asset)
          end
        }
      }
    end
    
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
distil-0.14.5.a lib/distil/source-file/css-file.rb
distil-0.14.4 lib/distil/source-file/css-file.rb
distil-0.14.3 lib/distil/source-file/css-file.rb
distil-0.14.2 lib/distil/source-file/css-file.rb
distil-0.14.2.a lib/distil/source-file/css-file.rb
distil-0.14.1 lib/distil/source-file/css-file.rb
distil-0.14.1.a lib/distil/source-file/css-file.rb
distil-0.14.0 lib/distil/source-file/css-file.rb
distil-0.14.0.i lib/distil/source-file/css-file.rb