/usr/lib/ruby/vendor_ruby/sass/importers/filesystem.rb is in ruby-sass 3.1.15-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | require 'pathname'
module Sass
module Importers
# The default importer, used for any strings found in the load path.
# Simply loads Sass files from the filesystem using the default logic.
class Filesystem < Base
attr_accessor :root
# Creates a new filesystem importer that imports files relative to a given path.
#
# @param root [String] The root path.
# This importer will import files relative to this path.
def initialize(root)
@root = File.expand_path(root)
end
# @see Base#find_relative
def find_relative(name, base, options)
_find(File.dirname(base), name, options)
end
# @see Base#find
def find(name, options)
_find(@root, name, options)
end
# @see Base#mtime
def mtime(name, options)
file, s = find_real_file(@root, name)
File.mtime(file) if file
rescue Errno::ENOENT
nil
end
# @see Base#key
def key(name, options)
[self.class.name + ":" + File.dirname(File.expand_path(name)),
File.basename(name)]
end
# @see Base#to_s
def to_s
@root
end
def hash
@root.hash
end
def eql?(other)
root.eql?(other.root)
end
protected
# If a full uri is passed, this removes the root from it
# otherwise returns the name unchanged
def remove_root(name)
if name.index(@root + "/") == 0
name[(@root.length + 1)..-1]
else
name
end
end
# A hash from file extensions to the syntaxes for those extensions.
# The syntaxes must be `:sass` or `:scss`.
#
# This can be overridden by subclasses that want normal filesystem importing
# with unusual extensions.
#
# @return [{String => Symbol}]
def extensions
{'sass' => :sass, 'scss' => :scss}
end
# Given an `@import`ed path, returns an array of possible
# on-disk filenames and their corresponding syntaxes for that path.
#
# @param name [String] The filename.
# @return [Array(String, Symbol)] An array of pairs.
# The first element of each pair is a filename to look for;
# the second element is the syntax that file would be in (`:sass` or `:scss`).
def possible_files(name)
name = escape_glob_characters(name)
dirname, basename, extname = split(name)
sorted_exts = extensions.sort
syntax = extensions[extname]
return [["#{dirname}/{_,}#{basename}.#{extensions.invert[syntax]}", syntax]] if syntax
sorted_exts.map {|ext, syn| ["#{dirname}/{_,}#{basename}.#{ext}", syn]}
end
def escape_glob_characters(name)
name.gsub(/[\*\[\]\{\}\?]/) do |char|
"\\#{char}"
end
end
REDUNDANT_DIRECTORY = %r{#{Regexp.escape(File::SEPARATOR)}\.#{Regexp.escape(File::SEPARATOR)}}
# Given a base directory and an `@import`ed name,
# finds an existant file that matches the name.
#
# @param dir [String] The directory relative to which to search.
# @param name [String] The filename to search for.
# @return [(String, Symbol)] A filename-syntax pair.
def find_real_file(dir, name)
for (f,s) in possible_files(remove_root(name))
path = (dir == "." || Pathname.new(f).absolute?) ? f : "#{dir}/#{f}"
if full_path = Dir[path].first
full_path.gsub!(REDUNDANT_DIRECTORY,File::SEPARATOR)
return full_path, s
end
end
nil
end
# Splits a filename into three parts, a directory part, a basename, and an extension
# Only the known extensions returned from the extensions method will be recognized as such.
def split(name)
extension = nil
dirname, basename = File.dirname(name), File.basename(name)
if basename =~ /^(.*)\.(#{extensions.keys.map{|e| Regexp.escape(e)}.join('|')})$/
basename = $1
extension = $2
end
[dirname, basename, extension]
end
private
def _find(dir, name, options)
full_filename, syntax = find_real_file(dir, name)
return unless full_filename && File.readable?(full_filename)
options[:syntax] = syntax
options[:filename] = full_filename
options[:importer] = self
Sass::Engine.new(File.read(full_filename), options)
end
def join(base, path)
Pathname.new(base).join(path).to_s
end
end
end
end
|