-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtemplate.rb
More file actions
222 lines (198 loc) · 5.85 KB
/
template.rb
File metadata and controls
222 lines (198 loc) · 5.85 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
require 'temple'
require 'temple/generators/string_buffer'
require 'slim'
require 'slim/parser'
require 'slim/filter'
require 'slim/embedded'
require 'slim/interpolation'
require 'pp'
module UI
module Template
# This module allows to build user interfaces
# using slim templates (http://www.slim-lang.org)
#
# @see UI#slim
module Slim
# @visibility private
module IdGenerator
def self.generate(name)
@table ||= {}
@table[name] ||= 0
@table[name] += 1
num = @table[name]
return "#{name}_#{num}"
end
end
# @visibility private
class Generator < Temple::Generator
define_options :freeze_static => false
def call(exp)
compile(exp)
end
def concat(str)
str
end
def on_static(text)
text
end
def on_multi(*exps)
exps.map{|e| compile(e)}.join(" ")
end
def on_code(code)
code + "\n"
end
end
# @visibility private
class Evaluator < ::Slim::Filter
def on_slim_output (*args)
ret = options[:context].instance_eval(args[1])
[:static,ret.to_s]
end
# @visibility private
def on_multi(*exps)
#remove all newlines it has no sense for us ( maybe in future for some edit box we place exception here :)
exps.delete_if { |type,arg| type == :newline }
result = [:multi]
text = nil
exps.each do |exp|
exp = compile(exp)
case exp.first
when :static
if text
text << exp.last
else
text = exp.last.dup
result << [:static, text]
end
when :multi
result.concat(exp[1..-1])
text = nil
else
result << exp
text = nil
end
end
result
end
end
# Filter class that merge statics together, evaluate outputs and clean newlines
# @visibility private
class Cleaner < ::Slim::Filter
def on_multi(*exps)
return compile(exps.first) if exps.size == 1
result = [:multi]
text = nil
exps.each do |exp|
exp = compile(exp)
case exp.first
when :static
if text
text << exp.last
else
text = exp.last.dup
result << [:static, text]
end
when :multi
result.concat(exp[1..-1])
text = nil
else
result << exp
text = nil
end
end
result
end
end
# Convert html tags to dynamic ruby code
# @visibility private
class ToRuby < ::Slim::Filter
def on_static(body)
[:static, "\""+compile(body)+"\""]
end
def on_escape(_flag, body)
[:multi,
# enclose it into "" to allow string append
[:static, "\"\#{"],
compile(body),
[:static, "}\""]
]
end
def parse_attributes attrs
return {} unless attrs.is_a? Array
puts attrs.inspect
attributes = attrs[2..-1].reduce("") do |acc,el|
acc << ", " unless acc.empty?
if el[0] == :slim #slim attrs
acc << ":#{el[2]} => #{el[4]}"
elsif el[0] == :html #match html attrs with static attr
el2 = el[3]
case el2[0]
when :slim
acc << ":#{el[2]} => #{el2[3]}"
when :escape
acc << ":#{el[2]} => \"#{el2[2][1]}\""
else
raise "unknown attr #{el.inspect}"
end
else
raise "Unknown attribute #{el.inspect}"
end
acc
end
end
def on_slim_tag(name, attrs, body)
attrs_str = parse_attributes attrs
if Builder::LEAF_ELEMENTS.include?(name.to_sym)
attrs_str.prepend ", " unless attrs_str.empty?
body = compile(body)
if body.empty?
body = [:static, "\"\""] # ensure default empty value for leaf elements
end
[:multi,
[:static, "#{name} "],
body,
[:dynamic, attrs_str + "\n"]
]
elsif (Builder::CONTAINER_ELEMENTS+Builder::TOPLEVEL_ELEMENTS).include?(name.to_sym)
attrs_str = "("+attrs_str+")" unless attrs_str.empty?
[ :multi,
[ :dynamic, "#{name}#{ attrs_str } {\n"],
compile(body),
[ :static, "}\n" ]
]
else
raise "unknown element #{name}"
end
end
alias_method :on_html_tag, :on_slim_tag
end
# @visibility private
class Engine < Temple::Engine
use ::Slim::Parser
use ::Slim::Interpolation
use ::Slim::DoInserter
use ::Slim::EndInserter
use ::Slim::Controls
use Evaluator
use Cleaner
use ToRuby
use Generator
end
end
# Builds a widget/dialog using a slim template
# mandatory part of options is either :file or :text key that
# specify template
#
# {include:file:examples/slim_template.rb}
def render(options={})
raise "specify template to render by file: or text: key" if !options[:file] && !options[:text]
io = options.delete(:text) || File.read(options.delete(:file))
options[:context] ||= self
code = UI::Template::Slim::Engine.new(options).call(io)
File.write("/tmp/ui.log", code)
puts options.inspect
options[:context].extend UI::Builder
options[:context].instance_eval code
end
end
end