Class: WebAssets::StylesheetProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/web_assets/stylesheet_processor.rb

Constant Summary collapse

RE_EXTENSION =
/\.(css|scss|sass)\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStylesheetProcessor

Returns a new instance of StylesheetProcessor.



15
16
17
# File 'lib/web_assets/stylesheet_processor.rb', line 15

def initialize
  Sass.load_paths.clear
end

Instance Attribute Details

#source_pathObject (readonly)

Returns the value of attribute source_path.



13
14
15
# File 'lib/web_assets/stylesheet_processor.rb', line 13

def source_path
  @source_path
end

Instance Method Details

#add_load_path(path) ⇒ Object



26
27
28
29
30
31
# File 'lib/web_assets/stylesheet_processor.rb', line 26

def add_load_path path
  return [:error, "#{path} isn't an existing directory."] unless Dir.exists? path
  WebAssets.logger.debug "StylesheetProcessor#add_load_path #{path.inspect}"
  Sass.load_paths << path
  :ok
end

#content(filename, options) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/web_assets/stylesheet_processor.rb', line 37

def content filename, options
  filepath = full_path filename.sub(RE_EXTENSION, '')
  WebAssets.logger.debug "StylesheetProcessor#content #{filepath.inspect}"
  content = case
  when File.exists?("#{filepath}.css")
    WebAssets.logger.debug "StylesheetProcessor#content: File.read"
    File.read "#{filepath}.css"
  when File.exists?("#{filepath}.scss")
    WebAssets.logger.debug "StylesheetProcessor#content: #render_sass_file/scss"
    render_sass_file filepath, :scss, render_options(options)
  when File.exists?("#{filepath}.sass")
    WebAssets.logger.debug "StylesheetProcessor#content: #render_sass_file/sass"
    render_sass_file filepath, :sass, render_options(options)
  else
    ""
  end
  options[:gzip] ? Gzipper.compress(content) : content
end

#digest_filename(filename) ⇒ Object



56
57
58
59
60
61
# File 'lib/web_assets/stylesheet_processor.rb', line 56

def digest_filename filename
  minified = content filename, minify: true
  return "" if minified.empty?
  hexdigest = digest.hexdigest minified
  "#{File.basename filename, ".*"}-#{hexdigest}#{File.extname filename}"
end

#pathsObject



33
34
35
# File 'lib/web_assets/stylesheet_processor.rb', line 33

def paths
  Sass.load_paths.dup
end

#set_path(path) ⇒ Object



19
20
21
22
23
24
# File 'lib/web_assets/stylesheet_processor.rb', line 19

def set_path path
  return [:error, "#{path} isn't an existing directory."] unless Dir.exists? path
  WebAssets.logger.debug "StylesheetProcessor#set_path #{path.inspect}"
  @source_path = path
  :ok
end