1. 场景分析
我们希望把
<r:box icon="happyface" title="Spiffy box title">
This is where the main content of our box will go. Lots
of HTML or radius tags can go here.
</r:box>
解析成
<div class="box">
<h2>
<img src="/images/icons/happyface.png" />
Spiffy box title
</h2>
<div class="content">
This is where the main content of our box will go. Lots
of HTML or radius tags can go here.
</div>
</div>
<r:box /> 是没有定义的tag,需要我们自己定义。
2. 解决方案
1. 生成扩展extension -----custom_tags
./script/generate extension custom_tags
2. 测试先行-----编写specs
新建Lib目录,并在_custom_tags/spec/lib/custom_tags_spec.rb_
require File.dirname(__FILE__) + '/../spec_helper'
describe 'CustomTags' do
dataset :pages
describe '<r:box>' do
it 'should render the correct HTML' do
tag = '<r:box icon="happyface" title="Test Title">Content</r:box>'
expected = %{<div class="box">
<h2>
<img src="/images/icons/happyface.png" />
Test Title
</h2>
<div class="content">
Content
</div>
</div>}
pages(:home).should render(tag).as(expected)
end
end
end
3. 修改custom_tags_extension.rb文件,在active方法中加入
Page.send :include, CustomTags
4. 定义Radius Tag
vendor/extensions/custom_tags/lib/custom_tags.rb
module CustomTags
include Radiant::Taggable
desc "Creates an HTML box with a title, icon and body content"
tag "box" do |tag|
""
end
end
5. 运行spec测试
cd vendor/extensions/custom_tags
rake spec
6. 结果如下
.
Finished in 3.312051 seconds
1 example, 0 failures