-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
131 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require "../../spec_helper" | ||
|
||
private class Component | ||
include Blueprint::HTML | ||
|
||
private def blueprint(&) | ||
span { yield } | ||
end | ||
end | ||
|
||
describe "renderer" do | ||
it "renders Blueprint::HTML classes" do | ||
actual_html = Blueprint::HTML.build do | ||
render Component do | ||
"Hello" | ||
end | ||
end | ||
|
||
expected_html = "<span>Hello</span>" | ||
|
||
actual_html.should eq expected_html | ||
end | ||
|
||
it "renders Blueprint::HTML instances" do | ||
actual_html = Blueprint::HTML.build do | ||
render Component.new do | ||
"Hello" | ||
end | ||
end | ||
|
||
expected_html = "<span>Hello</span>" | ||
|
||
actual_html.should eq expected_html | ||
end | ||
|
||
it "renders strings" do | ||
actual_html = Blueprint::HTML.build do | ||
render "Hello" | ||
end | ||
|
||
expected_html = "Hello" | ||
|
||
actual_html.should eq expected_html | ||
end | ||
|
||
it "renders blocks" do | ||
actual_html = Blueprint::HTML.build do | ||
render ->{ "He" + "llo" } | ||
end | ||
|
||
expected_html = "Hello" | ||
|
||
actual_html.should eq expected_html | ||
end | ||
|
||
it "renders nothing when renderable is nil" do | ||
actual_html = Blueprint::HTML.build do | ||
render nil | ||
end | ||
|
||
expected_html = "" | ||
|
||
actual_html.should eq expected_html | ||
end | ||
|
||
it "renders objects that respond `to_s`" do | ||
actual_html = Blueprint::HTML.build do | ||
render MarkdownLink.new("Example", "example.com") | ||
end | ||
|
||
expected_html = "[Example](example.com)" | ||
|
||
actual_html.should eq expected_html | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
module Blueprint::HTML::BlockRenderer | ||
private def capture_content(&) : Nil | ||
private def __capture_content__(&) : Nil | ||
buffer_size_before_block_evaluation = @buffer.bytesize | ||
content = yield | ||
return if buffer_size_before_block_evaluation != @buffer.bytesize # return if something was written to buffer | ||
|
||
append_to_buffer(content) | ||
__append_to_buffer__(content) | ||
end | ||
|
||
private def __capture_content__(block : Proc) : Nil | ||
buffer_size_before_block_evaluation = @buffer.bytesize | ||
content = block.call | ||
return if buffer_size_before_block_evaluation != @buffer.bytesize # return if something was written to buffer | ||
|
||
__append_to_buffer__(content) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
module Blueprint::HTML::BufferAppender | ||
private def append_to_buffer(content : String) | ||
escape(content, @buffer) | ||
private def __append_to_buffer__(content : String) | ||
__escape__(content, @buffer) | ||
end | ||
|
||
private def append_to_buffer(content : SafeObject) | ||
private def __append_to_buffer__(content : Proc) | ||
__capture_content__(content) | ||
end | ||
|
||
private def __append_to_buffer__(content : SafeObject) | ||
content.to_s(@buffer) | ||
end | ||
|
||
private def append_to_buffer(content : Nil) | ||
private def __append_to_buffer__(content : Nil) | ||
end | ||
|
||
private def append_to_buffer(content) | ||
escape(content.to_s, @buffer) | ||
private def __append_to_buffer__(content) | ||
__escape__(content.to_s, @buffer) | ||
end | ||
|
||
private def escape(value : String, io : IO) | ||
private def __escape__(value : String, io : IO) | ||
::HTML.escape(value, io) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Blueprint::HTML::Renderer | ||
private def render(renderable : Blueprint::HTML) : Nil | ||
renderable.to_s(@buffer) | ||
end | ||
|
||
private def render(renderable : Blueprint::HTML.class) : Nil | ||
renderable.new.to_s(@buffer) | ||
end | ||
|
||
private def render(renderable : Blueprint::HTML, &) : Nil | ||
renderable.to_s(@buffer) do | ||
yield renderable | ||
end | ||
end | ||
|
||
private def render(renderable : Blueprint::HTML.class, &) : Nil | ||
renderable.new.to_s(@buffer) do | ||
yield renderable | ||
end | ||
end | ||
|
||
private def render(renderable) : Nil | ||
__append_to_buffer__(renderable) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters