Class: Distil::CssFile

Inherits:
SourceFile show all
Includes:
YuiMinifiableFile
Defined in:
lib/distil/source-file/css-file.rb

Instance Attribute Summary

Attributes inherited from SourceFile

#full_path, #is_asset, #language, #project

Instance Method Summary collapse

Methods included from YuiMinifiableFile

#minified_content

Methods inherited from SourceFile

#add_asset, #add_dependency, #assets, #basename, #content_type, #copy_to, #dirname, #error, #extension, #initialize, #last_modified, #minified_content, #output_path, #path_relative_to, #path_relative_to_folder, #relative_path, #to_s, #to_str, #warning

Methods included from ErrorReporter

#error, error, #has_errors?, #has_warnings?, #ignore_warnings, #ignore_warnings=, #report, #total_error_count, #total_warning_count, warning, #warning

Constructor Details

This class inherits a constructor from Distil::SourceFile

Instance Method Details

#contentObject



12
13
14
15
16
17
18
19
20
# File 'lib/distil/source-file/css-file.rb', line 12

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

#dependenciesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/distil/source-file/css-file.rb', line 35

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

#rewrite_content_relative_to_path(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/distil/source-file/css-file.rb', line 22

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