-
Notifications
You must be signed in to change notification settings - Fork 83
/
tilt_template.rb
95 lines (83 loc) · 2.29 KB
/
tilt_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
require 'tilt'
require 'erb'
require 'compass'
require 'yaml'
module ::Tilt
class LivingStyleGuideTemplate < Template
self.default_mime_type = 'text/html'
def prepare
end
def evaluate(scope, locals, &block)
@scope = scope
parse_options(data)
generate_sass
render_living_style_guide
end
private
def sass_options
options = Compass.configuration.to_sass_plugin_options
if defined?(Rails)
options[:load_paths] = options[:load_paths] | Rails.application.config.assets.paths
end
options[:template_location].each do |path, short|
options[:load_paths] << ::LivingStyleGuide::Importer.new(path)
end
options[:filename] = eval_file
options[:line] = line
options[:syntax] = @options[:syntax]
options[:importer] = LivingStyleGuide::Importer.new('.')
options[:sprockets] = { context: @scope }
options
end
private
def parse_options(data)
@options = YAML.load(data)
@options.keys.each do |key|
@options[key.gsub('-', '_').to_sym] = @options.delete(key)
end
@options[:syntax] = @options.has_key?(:styleguide_sass) ? :sass : :scss
end
private
def generate_sass
@sass = [
%Q(@import "#{@options[:source]}"),
style_variables,
%Q(@import "livingstyleguide"),
@options[:styleguide_sass] || @options[:styleguide_scss]
].flatten.join(@options[:syntax] == :sass ? "\n" : ';')
end
private
def configure_cache
return unless @scope.respond_to?(:depend_on)
test = /^#{root}/
@engine.files.uniq.each do |file|
if file =~ test
@scope.depend_on file
end
end
end
private
def root
if @scope.respond_to?(:environment)
@scope.environment.root
else
File.dirname(@file)
end
end
private
def style_variables
return unless @options.has_key?(:style)
@options[:style].map do |key, value|
"$livingstyleguide--#{key}: #{value}"
end
end
private
def render_living_style_guide
@engine = ::LivingStyleGuide::Engine.new(@sass, @options, sass_options)
html = @engine.render
configure_cache
html
end
end
register 'lsg', LivingStyleGuideTemplate
end