Module Sass::Files
In: lib/sass/files.rb
Haml::Util Engine Color SyntaxError UnitConversionError StandardError Node Operation Literal UnaryOperation Funcall Variable Number String Bool EvaluationContext Node\n[lib/sass/css.rb\nlib/sass/tree/node.rb] DebugNode IfNode CommentNode ForNode MixinNode VariableNode ImportNode WhileNode MixinDefNode Repl CSS Environment Lexer Parser PropNode\n[lib/sass/css.rb\nlib/sass/tree/prop_node.rb] DirectiveNode\n[lib/sass/css.rb\nlib/sass/tree/directive_node.rb] RuleNode\n[lib/sass/css.rb\nlib/sass/tree/rule_node.rb] Rack lib/sass/repl.rb lib/sass/css.rb lib/sass/environment.rb lib/sass/error.rb lib/sass/engine.rb lib/sass/script/lexer.rb lib/sass/script/color.rb lib/sass/script/string.rb lib/sass/script/unary_operation.rb lib/sass/script/variable.rb lib/sass/script/funcall.rb lib/sass/script/operation.rb lib/sass/script/bool.rb lib/sass/script/parser.rb lib/sass/script/literal.rb lib/sass/script/node.rb lib/sass/script/number.rb lib/sass/script/functions.rb Functions Script Files lib/sass/tree/while_node.rb lib/sass/tree/if_node.rb lib/sass/tree/mixin_def_node.rb lib/sass/tree/debug_node.rb lib/sass/tree/for_node.rb lib/sass/tree/import_node.rb lib/sass/tree/prop_node.rb lib/sass/tree/node.rb lib/sass/tree/comment_node.rb lib/sass/tree/mixin_node.rb lib/sass/tree/directive_node.rb lib/sass/tree/rule_node.rb lib/sass/tree/variable_node.rb Tree lib/sass/plugin/rack.rb Plugin Sass dot/m_54_0.png

This module contains various bits of functionality related to finding and caching Sass files.

Methods

Public Instance methods

Find the full filename of a Sass or CSS file to import. This follows Sass‘s import rules: if the filename given ends in `".sass"` or `".css"`, it will try to find that type of file; otherwise, it will try to find the corresponding Sass file and fall back on CSS if it‘s not available.

Any Sass filename returned will correspond to an actual Sass file on the filesystem. CSS filenames, however, may not; they‘re expected to be put through directly to the stylesheet as CSS `@import` statements.

@param filename [String] The filename to search for @param load_paths [Array<String>] The set of filesystem paths

  to search for Sass files.

@return [String] The filename of the imported file.

  This is an absolute path if the file is a `".sass"` file.

@raise [Sass::SyntaxError] if `filename` ends in ``".sass"``

  and no corresponding Sass file could be found.

[Source]

    # File lib/sass/files.rb, line 66
66:     def find_file_to_import(filename, load_paths)
67:       was_sass = false
68:       original_filename = filename
69: 
70:       if filename[-5..-1] == ".sass"
71:         filename = filename[0...-5]
72:         was_sass = true
73:       elsif filename[-4..-1] == ".css"
74:         return filename
75:       end
76: 
77:       new_filename = find_full_path("#{filename}.sass", load_paths)
78: 
79:       return new_filename if new_filename
80:       unless was_sass
81:         warn "WARNING: \#{filename}.sass not found. Using \#{filename}.css instead.\nThis behavior is deprecated and will be removed in a future version.\nIf you really need \#{filename}.css, import it explicitly.\n"
82:         return filename + '.css'
83:       end
84:       raise SyntaxError.new("File to import not found or unreadable: #{original_filename}.", @line)
85:     end

Returns the {Sass::Tree} for the given file, reading it from the Sass cache if possible.

@param filename [String] The path to the Sass file @param options [{Symbol => Object}] The options hash.

  Only the {file:SASS_REFERENCE.md#cache-option `:cache_location`} option is used

@raise [Sass::SyntaxError] if there‘s an error in the document

[Source]

    # File lib/sass/files.rb, line 18
18:     def tree_for(filename, options)
19:       options = Sass::Engine::DEFAULT_OPTIONS.merge(options)
20:       text = File.read(filename)
21: 
22:       if options[:cache]
23:         compiled_filename = sassc_filename(filename, options)
24:         sha = Digest::SHA1.hexdigest(text)
25: 
26:         if root = try_to_read_sassc(filename, compiled_filename, sha)
27:           root.options = options.merge(:filename => filename)
28:           return root
29:         end
30:       end
31: 
32:       engine = Sass::Engine.new(text, options.merge(:filename => filename))
33: 
34:       begin
35:         root = engine.to_tree
36:       rescue Sass::SyntaxError => err
37:         err.add_backtrace_entry(filename)
38:         raise err
39:       end
40: 
41:       try_to_write_sassc(root, compiled_filename, sha, options) if options[:cache]
42: 
43:       root
44:     end

Private Instance methods

[Source]

     # File lib/sass/files.rb, line 127
127:     def find_full_path(filename, load_paths)
128:       partial_name = File.join(File.dirname(filename), "_#{File.basename(filename)}")
129: 
130:       if Pathname.new(filename).absolute?
131:         [partial_name, filename].each do |name|
132:           return name if File.readable?(name)
133:         end
134:         return nil
135:       end
136: 
137:       load_paths.each do |path|
138:         [partial_name, filename].each do |name|
139:           full_path = File.join(path, name)
140:           if File.readable?(full_path)
141:             return full_path
142:           end
143:         end
144:       end
145:       nil
146:     end

[Source]

    # File lib/sass/files.rb, line 94
94:     def sassc_filename(filename, options)
95:       File.join(options[:cache_location],
96:         Digest::SHA1.hexdigest(File.dirname(File.expand_path(filename))),
97:         File.basename(filename) + 'c')
98:     end

[Source]

     # File lib/sass/files.rb, line 100
100:     def try_to_read_sassc(filename, compiled_filename, sha)
101:       return unless File.readable?(compiled_filename)
102: 
103:       File.open(compiled_filename, "rb") do |f|
104:         return unless f.readline("\n").strip == Sass::VERSION
105:         return unless f.readline("\n").strip == sha
106:         return Marshal.load(f.read)
107:       end
108:     rescue TypeError, ArgumentError => e
109:       warn "Warning. Error encountered while reading cache #{compiled_filename}: #{e}"
110:     end

[Source]

     # File lib/sass/files.rb, line 112
112:     def try_to_write_sassc(root, compiled_filename, sha, options)
113:       return unless File.writable?(File.dirname(options[:cache_location]))
114:       return if File.exists?(options[:cache_location]) && !File.writable?(options[:cache_location])
115:       return if File.exists?(File.dirname(compiled_filename)) && !File.writable?(File.dirname(compiled_filename))
116:       return if File.exists?(compiled_filename) && !File.writable?(compiled_filename)
117:       FileUtils.mkdir_p(File.dirname(compiled_filename))
118:       File.open(compiled_filename, "wb") do |f|
119:         f.write(Sass::VERSION)
120:         f.write("\n")
121:         f.write(sha)
122:         f.write("\n")
123:         f.write(Marshal.dump(root))
124:       end
125:     end

[Validate]