Sha256: 03cb5c7db3913f45c0e09b758e84e946a5079cc3412378597838cc2bf8ec4cb8

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'pathname'
require 'singleton'

class Sass::Globbing::Importer < Sass::Importers::Filesystem

  include Singleton

  GLOB = /\*/

  SASS_EXTENSIONS = {
    ".sass" => :sass,
    ".scss" => :scss
  }

  def initialize
    super(".")
  end

  def sass_file?(filename)
    SASS_EXTENSIONS.has_key?(File.extname(filename.to_s))
  end
  
  def syntax(filename)
    SASS_EXTENSIONS[File.extname(filename.to_s)]
  end
  
  def find_relative(name, base, options)
    if name =~ GLOB
      contents = ""
      base_pathname = Pathname.new(base)
      each_globbed_file(name, base_pathname, options) do |filename|
        contents << "@import #{Pathname.new(filename).relative_path_from(base_pathname.dirname).to_s.inspect};\n"
      end
      return nil if contents.empty?
      Sass::Engine.new(contents, options.merge(
        :filename => base_pathname.to_s,
        :importer => self,
        :syntax => :scss
      ))
    else
      super
    end
  end
  
  def find(name, options)
    if options[:filename] # globs must be relative
      find_relative(name, options[:filename], options)
    end
  end
  
  def each_globbed_file(glob, base_pathname, options)
    Dir["#{base_pathname.dirname}/#{glob}"].sort.each do |filename|
      next if filename == options[:filename]
      yield filename if sass_file?(filename)
    end
  end
  
  def mtime(name, options)
    if name =~ GLOB && options[:filename]
      mtime = nil
      each_globbed_file(name, Pathname.new(options[:filename]), options) do |p|
        if mtime.nil?
          mtime = File.mtime(p)
        else
          mtime = [mtime, File.mtime(p)].max
        end
      end
      mtime
    end
  end
  
  def key(name, options)
    ["Glob:" + File.dirname(File.expand_path(name)), File.basename(name)]
  end
  
  def to_s
    "Sass::Globbing::Importer"
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sass-globbing-1.0.0 lib/sass/globbing/importer.rb
sass-globbing-1.0.0.rc.2 lib/sass/globbing/importer.rb