amrita2 1.9.6 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +112 -0
- data/init.rb +6 -0
- data/lib/amrita2/gettext.rb +116 -0
- data/lib/amrita2/macro.rb +153 -0
- data/lib/amrita2/rails_bridge.rb +172 -26
- data/lib/amrita2/template.rb +2634 -234
- data/lib/amrita2/testsupport.rb +171 -0
- data/lib/amrita2/version.rb +3 -3
- data/lib/amrita2.rb +1 -0
- data/sample/depot/app/controllers/admin_controller.rb +59 -0
- data/sample/depot/app/controllers/application.rb +20 -0
- data/sample/depot/app/controllers/info_controller.rb +19 -0
- data/sample/depot/app/controllers/login_controller.rb +85 -0
- data/sample/depot/app/controllers/store_controller.rb +68 -0
- data/sample/depot/app/helpers/admin_helper.rb +7 -0
- data/sample/depot/app/helpers/application_helper.rb +10 -0
- data/sample/depot/app/helpers/ar_form.rb +169 -0
- data/sample/depot/app/helpers/form_tag.rb +24 -0
- data/sample/depot/app/helpers/info_helper.rb +7 -0
- data/sample/depot/app/helpers/standard_form.rb +73 -0
- data/sample/depot/app/helpers/store_helper.rb +14 -0
- data/sample/depot/app/models/cart.rb +36 -0
- data/sample/depot/app/models/cart_item.rb +26 -0
- data/sample/depot/app/models/line_item.rb +34 -0
- data/sample/depot/app/models/order.rb +57 -0
- data/sample/depot/app/models/product.rb +41 -0
- data/sample/depot/app/models/user.rb +83 -0
- data/sample/depot/config/boot.rb +49 -0
- data/sample/depot/config/environment.rb +83 -0
- data/sample/depot/config/environments/development.rb +24 -0
- data/sample/depot/config/environments/production.rb +24 -0
- data/sample/depot/config/environments/test.rb +24 -0
- data/sample/depot/config/routes.rb +10 -0
- data/sample/depot/db/migrate/001_create_products.rb +18 -0
- data/sample/depot/db/migrate/002_add_price.rb +14 -0
- data/sample/depot/db/migrate/003_add_test_data.rb +68 -0
- data/sample/depot/db/migrate/004_add_sessions.rb +20 -0
- data/sample/depot/db/migrate/005_create_orders.rb +21 -0
- data/sample/depot/db/migrate/006_create_line_items.rb +27 -0
- data/sample/depot/db/migrate/007_create_users.rb +18 -0
- data/sample/depot/db/schema.rb +45 -0
- data/sample/depot/public/dispatch.rb +15 -0
- data/sample/depot/test/functional/admin_controller_test.rb +54 -0
- data/sample/depot/test/functional/info_controller_test.rb +23 -0
- data/sample/depot/test/functional/login_controller_test.rb +74 -0
- data/sample/depot/test/functional/store_controller_test.rb +57 -0
- data/sample/depot/test/integration/dsl_user_stories_test.rb +126 -0
- data/sample/depot/test/integration/user_stories_test.rb +70 -0
- data/sample/depot/test/performance/order_speed_test.rb +58 -0
- data/sample/depot/test/test_helper.rb +16 -0
- data/sample/depot/test/unit/cart_test.rb +39 -0
- data/sample/depot/test/unit/cart_test1.rb +31 -0
- data/sample/depot/test/unit/line_item_test.rb +15 -0
- data/sample/depot/test/unit/order_test.rb +15 -0
- data/sample/depot/test/unit/product_test.rb +98 -0
- data/sample/depot/vendor/plugins/amrita2/init.rb +6 -0
- data/sample/hello/hello.rb +22 -0
- data/sample/login_engine/app/controllers/application.rb +16 -0
- data/sample/login_engine/app/controllers/user_controller.rb +265 -0
- data/sample/login_engine/app/helpers/application_helper.rb +3 -0
- data/sample/login_engine/app/helpers/form_tag.rb +16 -0
- data/sample/login_engine/app/helpers/two_columns.rb +24 -0
- data/sample/login_engine/app/helpers/two_columns_form.rb +47 -0
- data/sample/login_engine/app/helpers/user_helper.rb +88 -0
- data/sample/login_engine/app/models/user.rb +7 -0
- data/sample/login_engine/app/models/user_notify.rb +75 -0
- data/sample/login_engine/config/boot.rb +45 -0
- data/sample/login_engine/config/environment.rb +140 -0
- data/sample/login_engine/config/environments/development.rb +21 -0
- data/sample/login_engine/config/environments/production.rb +18 -0
- data/sample/login_engine/config/environments/test.rb +19 -0
- data/sample/login_engine/config/routes.rb +23 -0
- data/sample/login_engine/db/migrate/001_create_users.rb +25 -0
- data/sample/login_engine/db/schema.rb +25 -0
- data/sample/login_engine/lib/config.rb +113 -0
- data/sample/login_engine/lib/hpricot_test_extension.rb +80 -0
- data/sample/login_engine/lib/login_engine/authenticated_system.rb +113 -0
- data/sample/login_engine/lib/login_engine/authenticated_user.rb +155 -0
- data/sample/login_engine/lib/login_engine.rb +62 -0
- data/sample/login_engine/public/dispatch.rb +10 -0
- data/sample/login_engine/test/functional/amrita2_test.rb +267 -0
- data/sample/login_engine/test/functional/user_controller_test.rb +544 -0
- data/sample/login_engine/test/mocks/mail.rb +14 -0
- data/sample/login_engine/test/mocks/time.rb +19 -0
- data/sample/login_engine/test/test_helper.rb +31 -0
- data/sample/login_engine/test/unit/user_test.rb +116 -0
- data/sample/login_engine/vendor/plugins/amrita2/init.rb +6 -0
- data/specs/attribute.rb +201 -0
- data/specs/datatypes.rb +231 -0
- data/specs/dictionary.rb +68 -0
- data/specs/erb_cdata.rb +187 -0
- data/specs/filters.rb +513 -0
- data/specs/gettext/erb_gettext.rb +42 -0
- data/specs/gettext/gettext_util.rb +65 -0
- data/specs/gettext/static_text.rb +103 -0
- data/specs/impl/code_generator.rb +87 -0
- data/specs/impl/dynamic_element.rb +92 -0
- data/specs/impl/hash_delegator.rb +57 -0
- data/specs/impl/parse_opt.rb +34 -0
- data/specs/impl/preprocess.rb +823 -0
- data/specs/impl/testsupport.rb +89 -0
- data/specs/inlineruby.rb +429 -0
- data/specs/intro.rb +654 -0
- data/specs/loop.rb +203 -0
- data/specs/macro.rb +532 -0
- data/specs/sample.rb +34 -0
- data/specs/sanitize.rb +110 -0
- data/specs/template.rb +189 -0
- data/specs/trace.rb +97 -0
- metadata +138 -19
- data/lib/amrita2/core.rb +0 -1897
- data/lib/amrita2/rd.rb +0 -314
data/specs/intro.rb
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
|
|
2
|
+
require 'rexml/document'
|
|
3
|
+
require 'amrita2'
|
|
4
|
+
require 'amrita2/testsupport'
|
|
5
|
+
|
|
6
|
+
context "Simple Template" do
|
|
7
|
+
include Amrita2
|
|
8
|
+
|
|
9
|
+
specify "XML template" do
|
|
10
|
+
#
|
|
11
|
+
# Template for Amrita2 is basically well-formed xml/xhtml file
|
|
12
|
+
# with am:src attributes specifying dynamic elements.
|
|
13
|
+
#
|
|
14
|
+
t = Amrita2::Template.new <<-END
|
|
15
|
+
<html>
|
|
16
|
+
<head>
|
|
17
|
+
<title am:src="page_title" />
|
|
18
|
+
</head>
|
|
19
|
+
<body>
|
|
20
|
+
<h1 am:src="header_title" />
|
|
21
|
+
<p class="text" am:src="text">
|
|
22
|
+
<span am:src="template" /> is a html template library for <span am:src="lang" />
|
|
23
|
+
</p>
|
|
24
|
+
</body>
|
|
25
|
+
</html>
|
|
26
|
+
END
|
|
27
|
+
|
|
28
|
+
#
|
|
29
|
+
# A Ruby Hash or almost any data in Ruby can be model data for rendering dynamic element.
|
|
30
|
+
#
|
|
31
|
+
data = {
|
|
32
|
+
:page_title=>'Amrita2',
|
|
33
|
+
:header_title=>'Hello, Amrita2',
|
|
34
|
+
:text=>{
|
|
35
|
+
:template => 'Amrita2',
|
|
36
|
+
:lang => 'Ruby'
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
# This is output
|
|
41
|
+
expected = <<-END
|
|
42
|
+
<html>
|
|
43
|
+
<head>
|
|
44
|
+
<title>Amrita2</title>
|
|
45
|
+
</head>
|
|
46
|
+
<body>
|
|
47
|
+
<h1>Hello, Amrita2</h1>
|
|
48
|
+
<p class="text">Amrita2 is a html template library for Ruby</p>
|
|
49
|
+
</body>
|
|
50
|
+
</html>
|
|
51
|
+
END
|
|
52
|
+
|
|
53
|
+
t.render_with(data).should_be_samexml_as(expected)
|
|
54
|
+
|
|
55
|
+
#
|
|
56
|
+
# Amrita2 has an alternate indent based format for template: AMXML
|
|
57
|
+
#
|
|
58
|
+
t2 = Amrita2::Template.new <<-END
|
|
59
|
+
<<html<
|
|
60
|
+
<<head<
|
|
61
|
+
<<title:page_title>>
|
|
62
|
+
<<body<
|
|
63
|
+
<<h1 :header_title>>
|
|
64
|
+
<<p.text :text<
|
|
65
|
+
<<:template>> is a html template library for <<:lang>>
|
|
66
|
+
END
|
|
67
|
+
|
|
68
|
+
# Same result
|
|
69
|
+
t2.render_with(data).should_be_samexml_as(expected)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
context "erb" do
|
|
74
|
+
include Amrita2::Runtime
|
|
75
|
+
include Amrita2
|
|
76
|
+
|
|
77
|
+
specify "simple erb" do
|
|
78
|
+
# You can mix erb elements in Amrita2 templates
|
|
79
|
+
t = Amrita2::Template.new <<-'END'
|
|
80
|
+
<%= 1 + 2 %>
|
|
81
|
+
END
|
|
82
|
+
t.render_with(binding).should_be_samexml_as " 3 "
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
specify "all CDATA will be evaluated as erb" do
|
|
86
|
+
t = Amrita2::Template.new <<-'END'
|
|
87
|
+
<![CDATA[
|
|
88
|
+
<%
|
|
89
|
+
cls = 'xxx'
|
|
90
|
+
a = 1
|
|
91
|
+
%>
|
|
92
|
+
<div class="<%= cls %>">
|
|
93
|
+
1 + 2 = <%= a + 2 %>
|
|
94
|
+
</div>
|
|
95
|
+
]]>
|
|
96
|
+
END
|
|
97
|
+
t.render_with(binding).should_be_samexml_as "<div class='xxx'>1 + 2 = 3</div>"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
specify "erb using context data" do
|
|
101
|
+
#
|
|
102
|
+
# In ERb elements in Amrita2, the context data will be passed
|
|
103
|
+
# as $_ variable.
|
|
104
|
+
#
|
|
105
|
+
t = Amrita2::Template.new <<-'END'
|
|
106
|
+
<<ul<
|
|
107
|
+
<<:list<
|
|
108
|
+
<li><%= $_ * 10 %></li>
|
|
109
|
+
END
|
|
110
|
+
list = [1..3]
|
|
111
|
+
t.render_with(binding).should_be_samexml_as <<-END
|
|
112
|
+
<ul>
|
|
113
|
+
<li>10</li>
|
|
114
|
+
<li>20</li>
|
|
115
|
+
<li>30</li>
|
|
116
|
+
</ul>
|
|
117
|
+
END
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
specify "erb providing context data" do
|
|
121
|
+
|
|
122
|
+
# If the context data is binding,
|
|
123
|
+
# you can set a context data to a variables for Amrita2 rendering engine
|
|
124
|
+
t = Amrita2::Template.new <<-'END'
|
|
125
|
+
<<ul<
|
|
126
|
+
# Line start with # is a comment line.
|
|
127
|
+
# Line start with % is an ERb element line.
|
|
128
|
+
% list1 = (1..3).collect { |n| n*10 }
|
|
129
|
+
<<li:list1>>
|
|
130
|
+
END
|
|
131
|
+
t.render_with(binding).should_be_samexml_as <<-END
|
|
132
|
+
<ul>
|
|
133
|
+
<li>10</li>
|
|
134
|
+
<li>20</li>
|
|
135
|
+
<li>30</li>
|
|
136
|
+
</ul>
|
|
137
|
+
END
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
specify "erb filtering model data" do
|
|
141
|
+
t = Amrita2::Template.new <<-'END'
|
|
142
|
+
<<ul<
|
|
143
|
+
<<li:list<
|
|
144
|
+
% $_[:no_times10] = $_[:no] * 10
|
|
145
|
+
<<:no>> * 10 = <<:no_times10>>
|
|
146
|
+
END
|
|
147
|
+
|
|
148
|
+
list = (1..3).collect do |n|
|
|
149
|
+
{ :no => n }
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
t.render_with(binding).should_be_samexml_as <<-END
|
|
153
|
+
<ul>
|
|
154
|
+
<li>1 * 10 = 10</li>
|
|
155
|
+
<li>2 * 10 = 20</li>
|
|
156
|
+
<li>3 * 10 = 30</li>
|
|
157
|
+
</ul>
|
|
158
|
+
END
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
HashDelegator = Amrita2::HashDelegator
|
|
162
|
+
|
|
163
|
+
specify "erb filtering model data with amxml" do
|
|
164
|
+
t = Amrita2::Template.new <<-'END'
|
|
165
|
+
<<ul<
|
|
166
|
+
<<:list <
|
|
167
|
+
<<li ?[($_[:no]%2) != 0 ] <
|
|
168
|
+
# "<< { ... } <" adds members for context data
|
|
169
|
+
<<{
|
|
170
|
+
:no => $_[:no],
|
|
171
|
+
:no_times10 => $_[:no] * 10
|
|
172
|
+
} <
|
|
173
|
+
<<:no>> * 10 = <<:no_times10>>
|
|
174
|
+
END
|
|
175
|
+
|
|
176
|
+
list = (1..3).collect do |n|
|
|
177
|
+
{ :no => n }
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
t.render_with(binding).should_be_samexml_as <<-END
|
|
181
|
+
<ul>
|
|
182
|
+
<li>1 * 10 = 10</li>
|
|
183
|
+
<li>3 * 10 = 30</li>
|
|
184
|
+
</ul>
|
|
185
|
+
END
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
context "filters" do
|
|
190
|
+
include Amrita2
|
|
191
|
+
include Amrita2::Runtime
|
|
192
|
+
|
|
193
|
+
# You can set Filters in dynamic elements.
|
|
194
|
+
specify "Format" do
|
|
195
|
+
t = Amrita2::Template.new <<-END
|
|
196
|
+
<<:a | Format['(%-4.2f)'] >>
|
|
197
|
+
END
|
|
198
|
+
|
|
199
|
+
t.render_with(:a=>1234.56).should_be_samexml_as('(1234.56)')
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
specify "Default and Format" do
|
|
203
|
+
t = Amrita2::Template.new <<-END
|
|
204
|
+
<<div<
|
|
205
|
+
<<span.number :a | Default['(0.00)'] | Format['(%-4.2f)'] >>
|
|
206
|
+
END
|
|
207
|
+
t.render_with(:a=>[1234.56,nil,-7890]).should_be_samexml_as <<-END
|
|
208
|
+
<div>
|
|
209
|
+
<span class='number'>(1234.56)</span>
|
|
210
|
+
<span class='number'>(0.00)</span>
|
|
211
|
+
<span class='number'>(-7890.00)</span>
|
|
212
|
+
</div>
|
|
213
|
+
END
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
context "Attribute" do
|
|
218
|
+
include Amrita2
|
|
219
|
+
include Amrita2::Runtime
|
|
220
|
+
|
|
221
|
+
setup do
|
|
222
|
+
@data = {
|
|
223
|
+
:mail=>{
|
|
224
|
+
:no=>142990,
|
|
225
|
+
:title=>"[ANN] Amrita2 1.9.5"
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
@expected = <<-EOF
|
|
230
|
+
<a href='http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/142990'>[ruby-talk:142990][ANN] Amrita2 1.9.5</a>
|
|
231
|
+
EOF
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
specify "NVar" do
|
|
235
|
+
# NVar filter replaces $1, $2, ... by context data.
|
|
236
|
+
t = Amrita2::Template.new <<-END
|
|
237
|
+
<<a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/$1"
|
|
238
|
+
:mail | NVar[:no,:title] <
|
|
239
|
+
[ruby-talk:$1]$2
|
|
240
|
+
END
|
|
241
|
+
|
|
242
|
+
t.render_with(@data).should_be_samexml_as(@expected)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
specify "Attr with erb" do
|
|
246
|
+
#
|
|
247
|
+
# Attr filter replaces attributes by context data.
|
|
248
|
+
#
|
|
249
|
+
t = Amrita2::Template.new <<-'END'
|
|
250
|
+
<%
|
|
251
|
+
mail = @data[:mail]
|
|
252
|
+
mail[:href] = "http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/#{mail[:no]}"
|
|
253
|
+
mail[:body] = "[ruby-talk:#{@data[:mail][:no]}]#{mail[:title]}"
|
|
254
|
+
%>
|
|
255
|
+
<<a :mail | Attr[:href, :body] >>
|
|
256
|
+
END
|
|
257
|
+
#t.set_trace(STDOUT)
|
|
258
|
+
t.render_with(binding).should_be_samexml_as(@expected)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
specify "Attr" do
|
|
262
|
+
t = Amrita2::Template.new <<-'END'
|
|
263
|
+
<<{
|
|
264
|
+
:mm => {
|
|
265
|
+
:href=>"http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/#{$_[:no]}",
|
|
266
|
+
:body=>"[ruby-talk:#{$_[:no]}]#{$_[:title]}"
|
|
267
|
+
}
|
|
268
|
+
} <
|
|
269
|
+
<<a :mm | Attr[:href, :body] >>
|
|
270
|
+
END
|
|
271
|
+
|
|
272
|
+
t.render_with(@data[:mail]).should_be_samexml_as(@expected)
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
specify "Hook" do
|
|
276
|
+
|
|
277
|
+
# Using Hook
|
|
278
|
+
t = Amrita2::Template.new <<-'END'
|
|
279
|
+
<<:mail_hook | AcceptData[:hook] <
|
|
280
|
+
<<a :m|Attr[:href, :body]>>
|
|
281
|
+
END
|
|
282
|
+
|
|
283
|
+
mail = @data[:mail]
|
|
284
|
+
|
|
285
|
+
# Hook object can control rendering
|
|
286
|
+
hook1 = Amrita2::Core::Hook.new do
|
|
287
|
+
href = "http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/#{mail[:no]}"
|
|
288
|
+
body = "[ruby-talk:#{mail[:no]}]#{mail[:title]}"
|
|
289
|
+
render_child(:m, :href=>href, :body=>body)
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
#t.set_trace(STDOUT)
|
|
293
|
+
t.render_with(:mail_hook=>hook1).should_be_samexml_as(@expected)
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# http://www.cheetahtemplate.org/examples.html
|
|
298
|
+
context "Cheetah's sample" do
|
|
299
|
+
include Amrita2
|
|
300
|
+
|
|
301
|
+
setup do
|
|
302
|
+
@t = Amrita2::Template.new <<EOF
|
|
303
|
+
<html>
|
|
304
|
+
<head><title am:src="title" /></head>
|
|
305
|
+
<body>
|
|
306
|
+
<table>
|
|
307
|
+
<tr am:src="clients|NVar[:surname, :firstname, :email]">
|
|
308
|
+
<td>$1, $2</td>
|
|
309
|
+
<td><a href="mailto:$3">$3</a></td>
|
|
310
|
+
</tr>
|
|
311
|
+
</table>
|
|
312
|
+
</body>
|
|
313
|
+
</html>
|
|
314
|
+
EOF
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
specify "" do
|
|
318
|
+
client = Struct.new(:surname, :firstname, :email)
|
|
319
|
+
data = {
|
|
320
|
+
:title => "Amrita2 sample",
|
|
321
|
+
:clients => [
|
|
322
|
+
client.new("Matsumoto", "Yukihiro", "matz@netlab.co.jp"),
|
|
323
|
+
client.new("Nakajima", "Taku", "tnakajima@brain-tokyo.jp"),
|
|
324
|
+
]
|
|
325
|
+
}
|
|
326
|
+
@t.render_with(data).should_be_samexml_as <<-EOF
|
|
327
|
+
<html>
|
|
328
|
+
<head><title>Amrita2 sample</title></head>
|
|
329
|
+
<body>
|
|
330
|
+
<table>
|
|
331
|
+
<tr>
|
|
332
|
+
<td>Matsumoto, Yukihiro</td>
|
|
333
|
+
<td><a href='mailto:matz@netlab.co.jp'>matz@netlab.co.jp</a></td>
|
|
334
|
+
</tr><tr>
|
|
335
|
+
<td>Nakajima, Taku</td>
|
|
336
|
+
<td><a href='mailto:tnakajima@brain-tokyo.jp'>tnakajima@brain-tokyo.jp</a></td>
|
|
337
|
+
</tr>
|
|
338
|
+
</table>
|
|
339
|
+
</body>
|
|
340
|
+
</html>
|
|
341
|
+
EOF
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
context "table" do
|
|
346
|
+
#Lang = Struct.new(:name, :author, :website)
|
|
347
|
+
WebSite = Struct.new(:name, :url)
|
|
348
|
+
|
|
349
|
+
class Lang
|
|
350
|
+
include Amrita2::DictionaryData
|
|
351
|
+
attr_reader :name, :author, :website
|
|
352
|
+
def initialize(n, a, w)
|
|
353
|
+
@name, @author, @website = n, a, w
|
|
354
|
+
end
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
setup do
|
|
358
|
+
@data = {
|
|
359
|
+
:lang_list =>[
|
|
360
|
+
Lang.new("Ruby", "matz", WebSite.new('Ruby Home Page', 'http://www.ruby-lang.org/')),
|
|
361
|
+
Lang.new("Perl", "Larry Wall", WebSite.new('The Source for Perl', 'http://perl.com/')),
|
|
362
|
+
Lang.new("Python","Guido van Rossum", WebSite.new('Python Programing Language', 'http://www.python.org/'))
|
|
363
|
+
]
|
|
364
|
+
}
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
specify "simple" do
|
|
368
|
+
# AMXML preprocessor generates TD/TH from a line start with '|'.
|
|
369
|
+
t = Amrita2::Template.new <<-END
|
|
370
|
+
<<table border='1'<
|
|
371
|
+
<<tr:lang_list <
|
|
372
|
+
||| <<:name>> | <<:author>>|
|
|
373
|
+
END
|
|
374
|
+
|
|
375
|
+
t.render_with(@data).should_be_samexml_as <<-END
|
|
376
|
+
<table border = "1">
|
|
377
|
+
<tr><td>Ruby</td><td>matz</td></tr>
|
|
378
|
+
<tr><td>Perl</td><td>Larry Wall</td></tr>
|
|
379
|
+
<tr><td>Python</td><td>Guido van Rossum</td></tr>
|
|
380
|
+
</table>
|
|
381
|
+
END
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
specify "with title and attributes" do
|
|
385
|
+
t = Amrita2::Template.new <<-END
|
|
386
|
+
<<table border='1'<
|
|
387
|
+
<<tr<------------------------------------------------------------------
|
|
388
|
+
| || name ||author ||cite ||
|
|
389
|
+
|class || h_name ||h_author ||h_cite ||
|
|
390
|
+
----------------------------------------------------------------------
|
|
391
|
+
<<tr:lang_list
|
|
392
|
+
|ToHash[:name=>:name, :author=>:author, :site=>:website]
|
|
393
|
+
|Each[:class=>["odd", "even"]]
|
|
394
|
+
|Attr[:class]
|
|
395
|
+
<---------------------------------------------------------------------
|
|
396
|
+
| || <<:name>> | <<:author>>|<a am:src='site' |
|
|
397
|
+
| || | | am:filter='Attr[ |
|
|
398
|
+
| || | | :href=>:url, :body=>:name]'/>|
|
|
399
|
+
|class || name | author |site |
|
|
400
|
+
----------------------------------------------------------------------
|
|
401
|
+
END
|
|
402
|
+
|
|
403
|
+
#t.set_trace(STDOUT)
|
|
404
|
+
t.render_with(@data).should_be_samexml_as <<-END
|
|
405
|
+
<table border='1'>
|
|
406
|
+
<tr>
|
|
407
|
+
<th class='h_name'>name</th>
|
|
408
|
+
<th class='h_author'>author</th>
|
|
409
|
+
<th class='h_cite'>cite</th>
|
|
410
|
+
</tr>
|
|
411
|
+
<tr class='odd'>
|
|
412
|
+
<td class='name'>Ruby</td>
|
|
413
|
+
<td class='author'>matz</td>
|
|
414
|
+
<td class='site'>
|
|
415
|
+
<a href='http://www.ruby-lang.org/'>Ruby Home Page</a>
|
|
416
|
+
</td>
|
|
417
|
+
</tr>
|
|
418
|
+
<tr class='even'>
|
|
419
|
+
<td class='name'>Perl</td>
|
|
420
|
+
<td class='author'>Larry Wall</td>
|
|
421
|
+
<td class='site'>
|
|
422
|
+
<a href='http://perl.com/'>The Source for Perl</a>
|
|
423
|
+
</td>
|
|
424
|
+
</tr>
|
|
425
|
+
<tr class='odd'>
|
|
426
|
+
<td class='name'>Python</td>
|
|
427
|
+
<td class='author'>Guido van Rossum</td>
|
|
428
|
+
<td class='site'>
|
|
429
|
+
<a href='http://www.python.org/'>Python Programing Language</a>
|
|
430
|
+
</td>
|
|
431
|
+
</tr>
|
|
432
|
+
</table>
|
|
433
|
+
END
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
specify 'rowspan colspan' do
|
|
437
|
+
# from http://www.y-adagio.com/public/standards/tr_html4/struct/tables.html
|
|
438
|
+
=begin
|
|
439
|
+
A test table with merged cells
|
|
440
|
+
/-----------------------------------------\
|
|
441
|
+
| | Average | Red |
|
|
442
|
+
| |-------------------| eyes |
|
|
443
|
+
| | height | weight | |
|
|
444
|
+
|-----------------------------------------|
|
|
445
|
+
| Males | 1.9 | 0.003 | 40% |
|
|
446
|
+
|-----------------------------------------|
|
|
447
|
+
| Females | 1.7 | 0.002 | 43% |
|
|
448
|
+
\-----------------------------------------/
|
|
449
|
+
=end
|
|
450
|
+
expected = <<-END
|
|
451
|
+
<table border="1"
|
|
452
|
+
summary="This table gives some statistics about fruit
|
|
453
|
+
flies: average height and weight, and percentage
|
|
454
|
+
with red eyes (for both males and females).">
|
|
455
|
+
<caption><em>A test table with merged cells</em></caption>
|
|
456
|
+
<tr>
|
|
457
|
+
<th rowspan="2"></th>
|
|
458
|
+
<th colspan="2">Average</th>
|
|
459
|
+
<th rowspan="2">Red<br/>eyes</th>
|
|
460
|
+
</tr>
|
|
461
|
+
<tr>
|
|
462
|
+
<th>height</th>
|
|
463
|
+
<th>weight</th>
|
|
464
|
+
</tr>
|
|
465
|
+
<tr>
|
|
466
|
+
<th>Males</th>
|
|
467
|
+
<td>1.9</td>
|
|
468
|
+
<td>0.003</td>
|
|
469
|
+
<td>40%</td>
|
|
470
|
+
</tr>
|
|
471
|
+
<tr>
|
|
472
|
+
<th>Females</th>
|
|
473
|
+
<td>1.7</td>
|
|
474
|
+
<td>0.002</td>
|
|
475
|
+
<td>43%</td>
|
|
476
|
+
</tr>
|
|
477
|
+
</table>
|
|
478
|
+
END
|
|
479
|
+
t = Amrita2::Template.new <<-END
|
|
480
|
+
<<table border='1':|Attr[:summary]<
|
|
481
|
+
<<caption<
|
|
482
|
+
<<em:caption>>
|
|
483
|
+
<<<-------------------------------------------------------------------------
|
|
484
|
+
| rowspan || 2 || || 2 ||
|
|
485
|
+
| colspan || || 2 || ||
|
|
486
|
+
| || || Average ||Red<br />eyes ||
|
|
487
|
+
<<<---------------------------------------------------------------------------
|
|
488
|
+
| || || ||
|
|
489
|
+
| || height ||weight ||
|
|
490
|
+
| || || ||
|
|
491
|
+
<<tr:data<
|
|
492
|
+
|am:src || sex || height | weight | percent |
|
|
493
|
+
|am:filter || || | Format['%1.3f'] |Format['%d%%'] |
|
|
494
|
+
END
|
|
495
|
+
|
|
496
|
+
data = {
|
|
497
|
+
:summary=>"This table gives some statistics about fruit
|
|
498
|
+
flies: average height and weight, and percentage
|
|
499
|
+
with red eyes (for both males and females).",
|
|
500
|
+
:caption=>'A test table with merged cells',
|
|
501
|
+
:data => [
|
|
502
|
+
{ :sex=>'Males' , :height=>1.9, :weight=>0.003, :percent=>40 },
|
|
503
|
+
{ :sex=>'Females' , :height=>1.7, :weight=>0.002, :percent=>43 }
|
|
504
|
+
]
|
|
505
|
+
}
|
|
506
|
+
#puts t.render_with(data)
|
|
507
|
+
t.render_with(data).should_be_samexml_as(expected)
|
|
508
|
+
IO.popen("w3m -T text/html", "r+") do |io|
|
|
509
|
+
io.puts t.render_with(data)
|
|
510
|
+
io.close_write
|
|
511
|
+
#print io.read
|
|
512
|
+
end
|
|
513
|
+
end
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
context "Cheetah's sample with amxml" do
|
|
518
|
+
include Amrita2
|
|
519
|
+
|
|
520
|
+
setup do
|
|
521
|
+
@t = Amrita2::Template.new <<EOF
|
|
522
|
+
<<html<
|
|
523
|
+
<<head<
|
|
524
|
+
<<title:title>>
|
|
525
|
+
<<body<
|
|
526
|
+
<<table border='1'<
|
|
527
|
+
<<tr:clients|NVar[:surname, :firstname, :email]<
|
|
528
|
+
#-----------------------------------------
|
|
529
|
+
|class||cell1 |cell2 |
|
|
530
|
+
#-----------------------------------------
|
|
531
|
+
| ||$1, $2||<a href="mailto:$3">$3</a>|
|
|
532
|
+
#-----------------------------------------
|
|
533
|
+
EOF
|
|
534
|
+
@t = Amrita2::Template.new <<'EOF'
|
|
535
|
+
<<html<
|
|
536
|
+
<<head<
|
|
537
|
+
<<title:title>>
|
|
538
|
+
<<body<
|
|
539
|
+
<<table border='1'<
|
|
540
|
+
<<tr:clients <
|
|
541
|
+
<< {
|
|
542
|
+
:name=>"#{$_[:surname]}, #{$_[:firstname] }",
|
|
543
|
+
:url => "mailto:#{$_[:email]}",
|
|
544
|
+
:mailtext => $_[:email]
|
|
545
|
+
} <
|
|
546
|
+
#-----------------------------------------
|
|
547
|
+
|class||cell1 | cell2 |
|
|
548
|
+
| ||<<:name>>||<<a:\|Attr[:href=>:url, :body=>:mailtext]>>|
|
|
549
|
+
EOF
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
specify "" do
|
|
553
|
+
client = Struct.new(:surname, :firstname, :email)
|
|
554
|
+
data = {
|
|
555
|
+
:title => "Amrita2 sample",
|
|
556
|
+
:clients => [
|
|
557
|
+
client.new("Matsumoto", "Yukihiro", "matz@netlab.co.jp"),
|
|
558
|
+
client.new("Nakajima", "Taku", "tnakajima@brain-tokyo.jp"),
|
|
559
|
+
]
|
|
560
|
+
}
|
|
561
|
+
#puts @t.render_with(data)
|
|
562
|
+
@t.render_with(data).should_be_samexml_as <<-EOF
|
|
563
|
+
<html>
|
|
564
|
+
<head><title>Amrita2 sample</title></head>
|
|
565
|
+
<body>
|
|
566
|
+
<table border='1'>
|
|
567
|
+
<tr>
|
|
568
|
+
<th class='cell1'>Matsumoto, Yukihiro</th>
|
|
569
|
+
<td class='cell2'><a href='mailto:matz@netlab.co.jp'>matz@netlab.co.jp</a></td>
|
|
570
|
+
</tr><tr>
|
|
571
|
+
<th class='cell1'>Nakajima, Taku</th>
|
|
572
|
+
<td class='cell2'><a href='mailto:tnakajima@brain-tokyo.jp'>tnakajima@brain-tokyo.jp</a></td>
|
|
573
|
+
</tr>
|
|
574
|
+
</table>
|
|
575
|
+
</body>
|
|
576
|
+
</html>
|
|
577
|
+
EOF
|
|
578
|
+
|
|
579
|
+
=begin
|
|
580
|
+
IO.popen("w3m -dump -T text/html", "r+") do |io|
|
|
581
|
+
io.puts @t.render_with(data)
|
|
582
|
+
io.close_write
|
|
583
|
+
print io.read
|
|
584
|
+
end
|
|
585
|
+
=end
|
|
586
|
+
end
|
|
587
|
+
end
|
|
588
|
+
|
|
589
|
+
# http://d.hatena.ne.jp/tokuhirom/20070110/1168416921
|
|
590
|
+
context "TT's sample" do
|
|
591
|
+
include Amrita2
|
|
592
|
+
include Amrita2::Runtime
|
|
593
|
+
setup do
|
|
594
|
+
@t = Amrita2::Template.new <<EOF
|
|
595
|
+
<table border="0">
|
|
596
|
+
<tr
|
|
597
|
+
am:for="colors.keys.sort"
|
|
598
|
+
am:v="{ :name => $_ , :color => colors[$_] }"
|
|
599
|
+
>
|
|
600
|
+
<td
|
|
601
|
+
am:v='{:bgcolor => colors["white"], :body=> $_}'
|
|
602
|
+
am:filter="Attr[:bgcolor, :body]"
|
|
603
|
+
>
|
|
604
|
+
<b am:src='body'><span am:src='name'/></b>
|
|
605
|
+
</td>
|
|
606
|
+
<td
|
|
607
|
+
am:v='{:bgcolor => $_[:color] }'
|
|
608
|
+
am:filter="Attr[:bgcolor, :body]"
|
|
609
|
+
>
|
|
610
|
+
<span
|
|
611
|
+
am:v='" "'
|
|
612
|
+
am:filter="NoSanitize | Repeat[5]"
|
|
613
|
+
/>
|
|
614
|
+
</td>
|
|
615
|
+
<td
|
|
616
|
+
am:v='{:bgcolor => colors["white"] , :body=> $_[:color]}'
|
|
617
|
+
am:filter="Attr[:bgcolor, :body]"
|
|
618
|
+
>
|
|
619
|
+
</td>
|
|
620
|
+
</tr>
|
|
621
|
+
</table>
|
|
622
|
+
EOF
|
|
623
|
+
end
|
|
624
|
+
|
|
625
|
+
specify "" do
|
|
626
|
+
@t.inline_ruby = true
|
|
627
|
+
colors = {
|
|
628
|
+
'white' => '#FFFFFF',
|
|
629
|
+
'black' => '#000000',
|
|
630
|
+
}
|
|
631
|
+
@t.render_with(binding).should_be_samexml_as <<-EOF
|
|
632
|
+
<table border = "0">
|
|
633
|
+
<tr>
|
|
634
|
+
<td bgcolor='#FFFFFF'>
|
|
635
|
+
<b>black</b>
|
|
636
|
+
</td>
|
|
637
|
+
<td bgcolor='#000000'>
|
|
638
|
+
     
|
|
639
|
+
</td>
|
|
640
|
+
<td bgcolor='#FFFFFF'>#000000</td>
|
|
641
|
+
</tr><tr>
|
|
642
|
+
<td bgcolor='#FFFFFF'>
|
|
643
|
+
<b>white</b>
|
|
644
|
+
</td>
|
|
645
|
+
<td bgcolor='#FFFFFF'>
|
|
646
|
+
     
|
|
647
|
+
</td>
|
|
648
|
+
<td bgcolor='#FFFFFF'>#FFFFFF</td>
|
|
649
|
+
</tr>
|
|
650
|
+
</table>
|
|
651
|
+
EOF
|
|
652
|
+
end
|
|
653
|
+
end
|
|
654
|
+
|