require 'rexml/document'
require 'amrita2'
require 'amrita2/testsupport'
context "Simple Template" do
include Amrita2
specify "XML template" do
#
# Template for Amrita2 is basically well-formed xml/xhtml file
# with am:src attributes specifying dynamic elements.
#
t = Amrita2::Template.new <<-END
is a html template library for
END
#
# A Ruby Hash or almost any data in Ruby can be model data for rendering dynamic element.
#
data = {
:page_title=>'Amrita2',
:header_title=>'Hello, Amrita2',
:text=>{
:template => 'Amrita2',
:lang => 'Ruby'
}
}
# This is output
expected = <<-END
Amrita2
Hello, Amrita2
Amrita2 is a html template library for Ruby
END
t.render_with(data).should_be_samexml_as(expected)
#
# Amrita2 has an alternate indent based format for template: AMXML
#
t2 = Amrita2::Template.new <<-END
<>
<>
<> is a html template library for <<:lang>>
END
# Same result
t2.render_with(data).should_be_samexml_as(expected)
end
end
context "erb" do
include Amrita2::Runtime
include Amrita2
specify "simple erb" do
# You can mix erb elements in Amrita2 templates
t = Amrita2::Template.new <<-'END'
<%= 1 + 2 %>
END
t.render_with(binding).should_be_samexml_as " 3 "
end
specify "all CDATA will be evaluated as erb" do
t = Amrita2::Template.new <<-'END'
1 + 2 = <%= a + 2 %>
]]>
END
t.render_with(binding).should_be_samexml_as "1 + 2 = 3
"
end
specify "erb using context data" do
#
# In ERb elements in Amrita2, the context data will be passed
# as $_ variable.
#
t = Amrita2::Template.new <<-'END'
<<%= $_ * 10 %>
END
list = [1..3]
t.render_with(binding).should_be_samexml_as <<-END
END
end
specify "erb providing context data" do
# If the context data is binding,
# you can set a context data to a variables for Amrita2 rendering engine
t = Amrita2::Template.new <<-'END'
<>
END
t.render_with(binding).should_be_samexml_as <<-END
END
end
specify "erb filtering model data" do
t = Amrita2::Template.new <<-'END'
<> * 10 = <<:no_times10>>
END
list = (1..3).collect do |n|
{ :no => n }
end
t.render_with(binding).should_be_samexml_as <<-END
- 1 * 10 = 10
- 2 * 10 = 20
- 3 * 10 = 30
END
end
HashDelegator = Amrita2::HashDelegator
specify "erb filtering model data with amxml" do
t = Amrita2::Template.new <<-'END'
< $_[:no],
:no_times10 => $_[:no] * 10
} <
<<:no>> * 10 = <<:no_times10>>
END
list = (1..3).collect do |n|
{ :no => n }
end
t.render_with(binding).should_be_samexml_as <<-END
END
end
end
context "filters" do
include Amrita2
include Amrita2::Runtime
# You can set Filters in dynamic elements.
specify "Format" do
t = Amrita2::Template.new <<-END
<<:a | Format['(%-4.2f)'] >>
END
t.render_with(:a=>1234.56).should_be_samexml_as('(1234.56)')
end
specify "Default and Format" do
t = Amrita2::Template.new <<-END
<>
END
t.render_with(:a=>[1234.56,nil,-7890]).should_be_samexml_as <<-END
(1234.56)
(0.00)
(-7890.00)
END
end
end
context "Attribute" do
include Amrita2
include Amrita2::Runtime
setup do
@data = {
:mail=>{
:no=>142990,
:title=>"[ANN] Amrita2 1.9.5"
}
}
@expected = <<-EOF
[ruby-talk:142990][ANN] Amrita2 1.9.5
EOF
end
specify "NVar" do
# NVar filter replaces $1, $2, ... by context data.
t = Amrita2::Template.new <<-END
<
<>
END
#t.set_trace(STDOUT)
t.render_with(binding).should_be_samexml_as(@expected)
end
specify "Attr" do
t = Amrita2::Template.new <<-'END'
<<{
:mm => {
:href=>"http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/#{$_[:no]}",
:body=>"[ruby-talk:#{$_[:no]}]#{$_[:title]}"
}
} <
<>
END
t.render_with(@data[:mail]).should_be_samexml_as(@expected)
end
specify "Hook" do
# Using Hook
t = Amrita2::Template.new <<-'END'
<<:mail_hook | AcceptData[:hook] <
<>
END
mail = @data[:mail]
# Hook object can control rendering
hook1 = Amrita2::Core::Hook.new do
href = "http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/#{mail[:no]}"
body = "[ruby-talk:#{mail[:no]}]#{mail[:title]}"
render_child(:m, :href=>href, :body=>body)
end
#t.set_trace(STDOUT)
t.render_with(:mail_hook=>hook1).should_be_samexml_as(@expected)
end
end
# http://www.cheetahtemplate.org/examples.html
context "Cheetah's sample" do
include Amrita2
setup do
@t = Amrita2::Template.new <
EOF
end
specify "" do
client = Struct.new(:surname, :firstname, :email)
data = {
:title => "Amrita2 sample",
:clients => [
client.new("Matsumoto", "Yukihiro", "matz@netlab.co.jp"),
client.new("Nakajima", "Taku", "tnakajima@brain-tokyo.jp"),
]
}
@t.render_with(data).should_be_samexml_as <<-EOF
Amrita2 sample
EOF
end
end
context "table" do
#Lang = Struct.new(:name, :author, :website)
WebSite = Struct.new(:name, :url)
class Lang
include Amrita2::DictionaryData
attr_reader :name, :author, :website
def initialize(n, a, w)
@name, @author, @website = n, a, w
end
end
setup do
@data = {
:lang_list =>[
Lang.new("Ruby", "matz", WebSite.new('Ruby Home Page', 'http://www.ruby-lang.org/')),
Lang.new("Perl", "Larry Wall", WebSite.new('The Source for Perl', 'http://perl.com/')),
Lang.new("Python","Guido van Rossum", WebSite.new('Python Programing Language', 'http://www.python.org/'))
]
}
end
specify "simple" do
# AMXML preprocessor generates TD/TH from a line start with '|'.
t = Amrita2::Template.new <<-END
<> | <<:author>>|
END
t.render_with(@data).should_be_samexml_as <<-END
Ruby | matz |
Perl | Larry Wall |
Python | Guido van Rossum |
END
end
specify "with title and attributes" do
t = Amrita2::Template.new <<-END
<:name, :author=>:author, :site=>:website]
|Each[:class=>["odd", "even"]]
|Attr[:class]
<---------------------------------------------------------------------
| || <<:name>> | <<:author>>||
|class || name | author |site |
----------------------------------------------------------------------
END
#t.set_trace(STDOUT)
t.render_with(@data).should_be_samexml_as <<-END
END
end
specify 'rowspan colspan' do
# from http://www.y-adagio.com/public/standards/tr_html4/struct/tables.html
=begin
A test table with merged cells
/-----------------------------------------\
| | Average | Red |
| |-------------------| eyes |
| | height | weight | |
|-----------------------------------------|
| Males | 1.9 | 0.003 | 40% |
|-----------------------------------------|
| Females | 1.7 | 0.002 | 43% |
\-----------------------------------------/
=end
expected = <<-END
A test table with merged cells
|
Average |
Red eyes |
height |
weight |
Males |
1.9 |
0.003 |
40% |
Females |
1.7 |
0.002 |
43% |
END
t = Amrita2::Template.new <<-END
<>
<<<-------------------------------------------------------------------------
| rowspan || 2 || || 2 ||
| colspan || || 2 || ||
| || || Average ||Red
eyes ||
<<<---------------------------------------------------------------------------
| || || ||
| || height ||weight ||
| || || ||
<"This table gives some statistics about fruit
flies: average height and weight, and percentage
with red eyes (for both males and females).",
:caption=>'A test table with merged cells',
:data => [
{ :sex=>'Males' , :height=>1.9, :weight=>0.003, :percent=>40 },
{ :sex=>'Females' , :height=>1.7, :weight=>0.002, :percent=>43 }
]
}
#puts t.render_with(data)
t.render_with(data).should_be_samexml_as(expected)
IO.popen("w3m -T text/html", "r+") do |io|
io.puts t.render_with(data)
io.close_write
#print io.read
end
end
end
context "Cheetah's sample with amxml" do
include Amrita2
setup do
@t = Amrita2::Template.new <>
<$3|
#-----------------------------------------
EOF
@t = Amrita2::Template.new <<'EOF'
<>
<"#{$_[:surname]}, #{$_[:firstname] }",
:url => "mailto:#{$_[:email]}",
:mailtext => $_[:email]
} <
#-----------------------------------------
|class||cell1 | cell2 |
| ||<<:name>>||<:url, :body=>:mailtext]>>|
EOF
end
specify "" do
client = Struct.new(:surname, :firstname, :email)
data = {
:title => "Amrita2 sample",
:clients => [
client.new("Matsumoto", "Yukihiro", "matz@netlab.co.jp"),
client.new("Nakajima", "Taku", "tnakajima@brain-tokyo.jp"),
]
}
#puts @t.render_with(data)
@t.render_with(data).should_be_samexml_as <<-EOF
Amrita2 sample
EOF
=begin
IO.popen("w3m -dump -T text/html", "r+") do |io|
io.puts @t.render_with(data)
io.close_write
print io.read
end
=end
end
end
# http://d.hatena.ne.jp/tokuhirom/20070110/1168416921
context "TT's sample" do
include Amrita2
include Amrita2::Runtime
setup do
@t = Amrita2::Template.new <
|
|
|
EOF
end
specify "" do
@t.inline_ruby = true
colors = {
'white' => '#FFFFFF',
'black' => '#000000',
}
@t.render_with(binding).should_be_samexml_as <<-EOF
black
|
     
|
#000000 |
white
|
     
|
#FFFFFF |
EOF
end
end