Skip to content

Commit

Permalink
Import markaby template from markaby gem v0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
smtlaissezfaire committed Aug 16, 2010
1 parent 50cb223 commit 088f200
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 0 deletions.
43 changes: 43 additions & 0 deletions lib/tilt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,47 @@ def evaluate(scope, locals, &block)
end
end
register 'radius', RadiusTemplate


# Markaby
# http://github.com/markaby/markaby
class MarkabyTemplate < Template
def self.builder_class
@builder_class ||= Class.new(Markaby::Builder) do
def __capture_markaby_tilt__(&block)
__run_markaby_tilt__ do
text capture(&block)
end
end
end
end

def initialize_engine
return if defined? ::Markaby
require_template_library 'markaby'
end

def prepare
end

def evaluate(scope, locals, &block)
builder = self.class.builder_class.new({}, scope)
builder.locals = locals

if block
builder.instance_eval <<-CODE, __FILE__, __LINE__
def __run_markaby_tilt__
#{data}
end
CODE

builder.__capture_markaby_tilt__(&block)
else
builder.instance_eval(data, __FILE__, __LINE__)
end

builder.to_s
end
end
register 'mab', MarkabyTemplate
end
1 change: 1 addition & 0 deletions test/markaby/locals.mab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
li foo
1 change: 1 addition & 0 deletions test/markaby/markaby.mab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text "hello from markaby!"
1 change: 1 addition & 0 deletions test/markaby/markaby_other_static.mab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text "_why?"
1 change: 1 addition & 0 deletions test/markaby/render_twice.mab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text "foo"
1 change: 1 addition & 0 deletions test/markaby/scope.mab
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
li foo
2 changes: 2 additions & 0 deletions test/markaby/yielding.mab
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
text("Hey ")
yield
73 changes: 73 additions & 0 deletions test/tilt_markaby_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'contest'
require 'tilt'

begin
require 'markaby'

class MarkabyTiltTest < Test::Unit::TestCase
def setup
@block = lambda do |t|
File.read(File.dirname(__FILE__) + "/#{t.file}")
end
end

test "should be able to render a markaby template with static html" do
tilt = Tilt::MarkabyTemplate.new("markaby/markaby.mab", &@block)
assert_equal "hello from markaby!", tilt.render
end

test "should use the contents of the template" do
tilt = ::Tilt::MarkabyTemplate.new("markaby/markaby_other_static.mab", &@block)
assert_equal "_why?", tilt.render
end

test "should render from a string (given as data)" do
tilt = ::Tilt::MarkabyTemplate.new { "html do; end" }
assert_equal "<html></html>", tilt.render
end

test "should evaluate a template file in the scope given" do
scope = Object.new
def scope.foo
"bar"
end

tilt = ::Tilt::MarkabyTemplate.new("markaby/scope.mab", &@block)
assert_equal "<li>bar</li>", tilt.render(scope)
end

test "should pass locals to the template" do
tilt = ::Tilt::MarkabyTemplate.new("markaby/locals.mab", &@block)
assert_equal "<li>bar</li>", tilt.render(Object.new, { :foo => "bar" })
end

test "should yield to the block given" do
tilt = ::Tilt::MarkabyTemplate.new("markaby/yielding.mab", &@block)
eval_scope = Markaby::Builder.new

output = tilt.render(Object.new, {}) do
text("Joe")
end

assert_equal "Hey Joe", output
end

test "should be able to render two templates in a row" do
tilt = ::Tilt::MarkabyTemplate.new("markaby/render_twice.mab", &@block)

assert_equal "foo", tilt.render
assert_equal "foo", tilt.render
end

test "should retrieve a Tilt::MarkabyTemplate when calling Tilt['hello.mab']" do
assert_equal Tilt::MarkabyTemplate, ::Tilt['./markaby/markaby.mab']
end

test "should return a new instance of the implementation class (when calling Tilt.new)" do
assert ::Tilt.new(File.dirname(__FILE__) + "/markaby/markaby.mab").kind_of?(Tilt::MarkabyTemplate)
end
end

rescue LoadError => boom
warn "Tilt::MarkabyTemplate (disabled)\n"
end

0 comments on commit 088f200

Please sign in to comment.