-
Notifications
You must be signed in to change notification settings - Fork 0
/
laydown.rb
73 lines (55 loc) · 1.39 KB
/
laydown.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
require 'tilt'
require 'temple/utils'
module Laydown
RAW_TEMPLATE = File.read(File.join(
File.dirname(__FILE__), 'template.rb'
))
DEFAULT_TEMPLATE = {
:charset => 'utf-8',
:title => nil,
:description => nil,
:favicon => nil,
:keywords => [],
:css => [],
:js => [],
:inline_js => [],
:head => [],
:body_class => [],
:body => :yield,
:ga_code => :@ga_code
}
ARRAY_PROPS = [:keywords, :css, :js, :inline_js, :head, :body_class]
def self.compile(template={})
template = DEFAULT_TEMPLATE.merge(template)
ARRAY_PROPS.each do |k|
template[k] = Array(template[k]).flatten.compact
end
RAW_TEMPLATE.gsub(/data\[:([a-zA-Z0-9_]+)\]/) do |m|
literalize template[:"#{$1}"]
end
end
def self.literalize(obj)
case obj
when String then obj.inspect.gsub(/\\#\{/, '#{') + "+''"
when Symbol then obj.to_s
when nil then 'nil'
when Array
'[' + obj.map {|o| literalize(o) }.join(', ') + ']'
else obj.to_s.inspect
end
end
class Template < Tilt::Template
def initialize(hsh)
super(nil, 1, hsh) { '' } # tilt hack
end
def prepare
@src = Laydown.compile(options)
end
def precompiled_template(locals)
@src
end
end
def self.new(hsh={})
Template.new(hsh)
end
end