forked from middleman/middleman-sprockets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sass.rb
77 lines (59 loc) · 2.28 KB
/
sass.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
require "sprockets-sass"
module Middleman
module Sprockets
# Sass renderer
module Sass
# Setup extension
class << self
# Once registered
def registered(app)
# Stick with Compass' asset functions
::Sprockets::Sass.add_sass_functions = false
# Tell Sprockets to use our custom Sass template
::Sprockets.register_engine ".sass", SassPlusCSSFilenameTemplate
# Tell Tilt to use it as well (for inline sass blocks)
::Tilt.register 'sass', SassPlusCSSFilenameTemplate
::Tilt.prefer(SassPlusCSSFilenameTemplate)
# Tell Sprockets to use our custom Scss template
::Sprockets.register_engine ".scss", ScssPlusCSSFilenameTemplate
# Tell Tilt to use it as well (for inline scss blocks)
::Tilt.register 'scss', ScssPlusCSSFilenameTemplate
::Tilt.prefer(ScssPlusCSSFilenameTemplate)
end
alias :included :registered
end
# A SassTemplate for Sprockets/Tilt which outputs debug messages
class SassPlusCSSFilenameTemplate < ::Sprockets::Sass::SassTemplate
# Add exception messaging
# @param [Class] context
# @param [Hash] locals
# @return [String]
def evaluate(context, locals, &block)
begin
super
rescue Sass::SyntaxError => e
Sass::SyntaxError.exception_to_css(e, :full_exception => true)
end
end
protected
# Change Sass path, for url functions, to the build folder if we're building
# @return [Hash]
def sass_options
location_of_sass_file = File.expand_path(@context.source, @context.root)
parts = basename.split('.')
parts.pop
css_filename = File.join(location_of_sass_file, @context.css_dir, parts.join("."))
super.merge(:css_filename => css_filename)
end
end
# SCSS version of the above template
class ScssPlusCSSFilenameTemplate < SassPlusCSSFilenameTemplate
# Define the expected syntax for the template
# @return [Symbol]
def syntax
:scss
end
end
end
end
end