class Sprockets::Sass::Importer

Constants

GLOB

Public Instance Methods

find(path, options) click to toggle source

@see Sass::Importers::Base#find

# File lib/sprockets/sass/importer.rb, line 19
def find(path, options)
  engine_from_path(path, nil, options)
end
find_relative(path, base_path, options) click to toggle source

@see Sass::Importers::Base#find_relative

# File lib/sprockets/sass/importer.rb, line 10
def find_relative(path, base_path, options)
  if path =~ GLOB
    engine_from_glob(path, base_path, options)
  else
    engine_from_path(path, base_path, options)
  end
end
key(path, options) click to toggle source

@see Sass::Importers::Base#key

# File lib/sprockets/sass/importer.rb, line 33
def key(path, options)
  path = Pathname.new(path)
  ["#{self.class.name}:#{path.dirname.expand_path}", path.basename]
end
mtime(path, options) click to toggle source

@see Sass::Importers::Base#mtime

# File lib/sprockets/sass/importer.rb, line 24
def mtime(path, options)
  if pathname = resolve(path)
    pathname.mtime
  end
rescue Errno::ENOENT
  nil
end
to_s() click to toggle source

@see Sass::Importers::Base#to_s

# File lib/sprockets/sass/importer.rb, line 39
def to_s
  "#{self.class.name}:#{context.pathname}"
end

Protected Instance Methods

engine_from_glob(glob, base_path, options) click to toggle source

Create a Sass::Engine that will handle importing a glob of files.

# File lib/sprockets/sass/importer.rb, line 59
def engine_from_glob(glob, base_path, options)
  context = options[:custom][:sprockets_context]
  imports = resolve_glob(context, glob, base_path).inject('') do |imports, path|
    context.depend_on path
    relative_path = path.relative_path_from Pathname.new(base_path).dirname
    imports << %Q(@import "#{relative_path}";\n)
  end
  return nil if imports.empty?
  ::Sass::Engine.new imports, options.merge(
    :filename => base_path.to_s,
    :syntax   => :scss,
    :importer => self
  )
end
engine_from_path(path, base_path, options) click to toggle source

Create a Sass::Engine from the given path.

# File lib/sprockets/sass/importer.rb, line 46
def engine_from_path(path, base_path, options)
  context = options[:custom][:sprockets_context]
  pathname = resolve(context, path, base_path) or return nil
  context.depend_on pathname
  ::Sass::Engine.new evaluate(context, pathname), options.merge(
    :filename => pathname.to_s,
    :syntax   => syntax(pathname),
    :importer => self
  )
end
evaluate(context, path) click to toggle source

Returns the string to be passed to the Sass engine. We use Sprockets to process the file, but we remove any Sass processors because we need to let the Sass::Engine handle that.

# File lib/sprockets/sass/importer.rb, line 134
def evaluate(context, path)
  attributes = context.environment.attributes_for(path)
  processors = context.environment.preprocessors(attributes.content_type) + attributes.engines.reverse
  processors.delete_if { |processor| processor < Tilt::SassTemplate }
  context.evaluate(path, :processors => processors)
end
partialize_path(path) click to toggle source

Returns the partialized version of the given path. Returns nil if the path is already to a partial.

# File lib/sprockets/sass/importer.rb, line 120
def partialize_path(path)
  if path.basename.to_s !~ /\A_/
    Pathname.new path.to_s.sub(/([^\/]+)\Z/, '_\1')
  end
end
possible_files(context, path, base_path) click to toggle source

Returns all of the possible paths (including partial variations) to attempt to resolve with the given path.

# File lib/sprockets/sass/importer.rb, line 97
def possible_files(context, path, base_path)
  path      = Pathname.new(path)
  base_path = Pathname.new(base_path).dirname
  paths     = [ path, partialize_path(path) ]

  # Find base_path's root
  env_root_paths = context.environment.paths.map {|p| Pathname.new(p) }
  root_path = env_root_paths.detect do |env_root_path|
    base_path.to_s.start_with?(env_root_path.to_s)
  end
  root_path ||= Pathname.new(context.root_path)

  # Add the relative path from the root, if necessary
  if path.relative? && base_path != root_path
    relative_path = base_path.relative_path_from(root_path).join path
    paths.unshift(relative_path, partialize_path(relative_path))
  end

  paths.compact
end
resolve(context, path, base_path) click to toggle source

Finds an asset from the given path. This is where we make Sprockets behave like Sass, and import partial style paths.

# File lib/sprockets/sass/importer.rb, line 77
def resolve(context, path, base_path)
  possible_files(context, path, base_path).each do |file|
    context.resolve(file) { |found| return found if context.asset_requirable?(found) }
  end

  nil
end
resolve_glob(context, glob, base_path) click to toggle source

Finds all of the assets using the given glob.

# File lib/sprockets/sass/importer.rb, line 86
def resolve_glob(context, glob, base_path)
  base_path      = Pathname.new(base_path)
  path_with_glob = base_path.dirname.join(glob).to_s

  Pathname.glob(path_with_glob).sort.select do |path|
    path != context.pathname && context.asset_requirable?(path)
  end
end
syntax(path) click to toggle source

Returns the Sass syntax of the given path.

# File lib/sprockets/sass/importer.rb, line 127
def syntax(path)
  path.to_s.include?('.sass') ? :sass : :scss
end