-
Notifications
You must be signed in to change notification settings - Fork 83
/
engine.rb
83 lines (64 loc) · 1.59 KB
/
engine.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
module LivingStyleGuide
class Engine
attr_accessor :markdown, :files, :options
def initialize(source, options, sass_options)
@source = source
@options = options
@sass_options = sass_options
@markdown = ''
@files = []
end
def render
data = TemplateData.new(self)
template = File.read(File.join(File.dirname(__FILE__), '..', '..', 'templates', 'layouts', 'default.html.erb'))
ERB.new(template).result(data.get_binding)
end
def css
sass_engine.render
end
def html
LivingStyleGuide::RedcarpetTemplate.new{ markdown }.render
end
private
def sass_engine
return @sass_engine if @sass_engine
sass_options = @sass_options.clone
sass_options[:living_style_guide] = self
@sass_engine = ::Sass::Engine.new(@source, sass_options)
end
end
class TemplateData
include ERB::Util
def initialize(engine)
@engine = engine
end
def get_binding
binding
end
def title
@engine.options[:title]
end
def css
@engine.css
end
def html
@engine.html
end
def head
(@engine.options[:javascript_before] || []).map do |src|
%Q(<script src="#{src}"></script>)
end.join("\n")
end
def header
contents = [@engine.options[:header]]
contents.join("\n")
end
def footer
contents = [@engine.options[:footer]]
contents << (@engine.options[:javascript_after] || []).map do |src|
%Q(<script src="#{src}"></script>)
end
contents.join("\n")
end
end
end