Class: Tipsy::Handler::SassHandler::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/tipsy/handler/sass/importer.rb

Constant Summary collapse

GLOB =
/\*|\[.+\]/
PARTIAL =
/^_/
HAS_EXTENSION =
/\.css(.s[ac]ss)?$/
SASS_EXTENSIONS =
{
      ".css.sass" => :sass,
      ".css.scss" => :scss,
      ".sass" => :sass,
      ".scss" => :scss
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Importer

Returns a new instance of Importer.



23
24
25
26
# File 'lib/tipsy/handler/sass/importer.rb', line 23

def initialize(context)
  @context = context
  @resolver = Resolver.new(context)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



21
22
23
# File 'lib/tipsy/handler/sass/importer.rb', line 21

def context
  @context
end

Instance Method Details

#find(name, options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tipsy/handler/sass/importer.rb', line 67

def find(name, options)
  if pathname = resolve(name)
    context.depend_on(pathname)
    if sass_file?(pathname)
      render_with_engine(pathname.read, pathname)
    else
      render_with_engine(@resolver.process(pathname), pathname)
    end
  else
    nil
  end
end

#find_relative(name, base, options) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tipsy/handler/sass/importer.rb', line 53

def find_relative(name, base, options)
  base_pathname = Pathname.new(base)
  if pathname = resolve(name, base_pathname.dirname)
    context.depend_on(pathname)
    if sass_file?(pathname)
      render_with_engine(pathname.read, pathname)
    else
      render_with_engine(@resolver.process(pathname), pathname)
    end
  else
    nil
  end
end

#key(name, options) ⇒ Object



86
87
88
# File 'lib/tipsy/handler/sass/importer.rb', line 86

def key(name, options)
  ["Sprockets:" + File.dirname(File.expand_path(name)), File.basename(name)]
end

#mtime(name, options) ⇒ Object



80
81
82
83
84
# File 'lib/tipsy/handler/sass/importer.rb', line 80

def mtime(name, options)
  if pathname = resolve_sass_path(name, options)
    pathname.mtime
  end
end

#render_with_engine(data, pathname, options = {}) ⇒ Object



39
40
41
# File 'lib/tipsy/handler/sass/importer.rb', line 39

def render_with_engine(data, pathname, options = {})
  ::Sass::Engine.new(data, options.merge(:filename => pathname.to_s, :importer => self, :syntax => syntax(pathname)))
end

#resolve(name, base_pathname = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/tipsy/handler/sass/importer.rb', line 43

def resolve(name, base_pathname = nil)
  name = Pathname.new(name)
  if base_pathname && base_pathname.to_s.size > 0
    root = Pathname.new(context.root_path)
    name = base_pathname.relative_path_from(root).join(name)
  end
  partial_name = name.dirname.join("_#{name.basename}")
  @resolver.resolve(name) || @resolver.resolve(partial_name)
end

#resolve_sass_path(name, options = {}, relative = false) ⇒ Object



90
91
92
93
# File 'lib/tipsy/handler/sass/importer.rb', line 90

def resolve_sass_path(name, options = {}, relative = false)
  prefix = ( relative === false ? "" : "./" )
  (context.resolve("#{prefix}#{name}", options) || context.resolve("#{prefix}_#{name}", options))
end

#sass_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
# File 'lib/tipsy/handler/sass/importer.rb', line 28

def sass_file?(filename)
  filename = filename.to_s
  SASS_EXTENSIONS.keys.any?{|ext| filename[ext]}
end

#syntax(filename) ⇒ Object



33
34
35
36
37
# File 'lib/tipsy/handler/sass/importer.rb', line 33

def syntax(filename)
  filename = filename.to_s
  SASS_EXTENSIONS.each {|ext, syntax| return syntax if filename[(ext.size+2)..-1][ext]}
  nil
end