/
sass_template.rb
executable file
·111 lines (94 loc) · 3.24 KB
/
sass_template.rb
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
require 'tilt'
module Sprockets
module Sass
class SassTemplate < Tilt::SassTemplate
self.default_mime_type = 'text/css'
# A reference to the current Sprockets context
attr_reader :context
# Determines if the Sass functions have been initialized.
# They can be 'initialized' without actually being added.
@sass_functions_initialized = false
class << self
attr_accessor :sass_functions_initialized
alias :sass_functions_initialized? :sass_functions_initialized
# Templates are initialized once the functions are added.
def engine_initialized?
super && sass_functions_initialized?
end
end
# Add the Sass functions if they haven't already been added.
def initialize_engine
super unless self.class.superclass.engine_initialized?
if Sass.add_sass_functions != false
begin
require 'sprockets/helpers'
require 'sprockets/sass/functions'
rescue LoadError; end
end
self.class.sass_functions_initialized = true
end
# Define the expected syntax for the template
def syntax
:sass
end
# See `Tilt::Template#prepare`.
def prepare
@context = nil
@output = nil
end
# See `Tilt::Template#evaluate`.
def evaluate(context, locals, &block)
@output ||= begin
@context = context
::Sass::Engine.new(data, sass_options).render
rescue ::Sass::SyntaxError => e
# Annotates exception message with parse line number
context.__LINE__ = e.sass_backtrace.first[:line]
raise e
end
end
protected
# Returns a Sprockets-aware cache store for Sass::Engine.
def cache_store
return nil if context.environment.cache.nil?
if defined?(Sprockets::SassCacheStore)
Sprockets::SassCacheStore.new context.environment
else
CacheStore.new context.environment
end
end
# A reference to the custom Sass importer, `Sprockets::Sass::Importer`.
def importer
Importer.new context
end
# Assemble the options for the `Sass::Engine`
def sass_options
merge_sass_options(default_sass_options, options).merge(
:filename => eval_file,
:line => line,
:syntax => syntax,
:cache_store => cache_store,
:importer => importer,
:custom => { :sprockets_context => context }
)
end
# Get the default, global Sass options. Start with Compass's
# options, if it's available.
def default_sass_options
if defined?(Compass)
merge_sass_options Compass.sass_engine_options.dup, Sprockets::Sass.options
else
Sprockets::Sass.options.dup
end
end
# Merges two sets of `Sass::Engine` options, prepending
# the `:load_paths` instead of clobbering them.
def merge_sass_options(options, other_options)
if (load_paths = options[:load_paths]) && (other_paths = other_options[:load_paths])
other_options[:load_paths] = other_paths + load_paths
end
options.merge other_options
end
end
end
end