diff --git a/README.md b/README.md index 1787520..45d8a2b 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ class Alert end end -Alert.new.to_html +Alert.new.to_s ``` Output: diff --git a/benchmark/benchmark.cr b/benchmark/benchmark.cr index 736ef7d..8f6baca 100644 --- a/benchmark/benchmark.cr +++ b/benchmark/benchmark.cr @@ -6,14 +6,14 @@ require "./ecr" require "../src/blueprint/version" -blueprint_html = BlueprintHTML::Page.new.to_html -blueprint_raw_html = BlueprintRawHTML::Page.new.to_html +blueprint_html = BlueprintHTML::Page.new.to_s +blueprint_raw_html = BlueprintRawHTML::Page.new.to_s ecr = ECR::Page.new.to_s # ECR is here just to have a base value to compare raise "Different results" if blueprint_html != blueprint_raw_html && blueprint_html != ecr Benchmark.ips do |x| - x.report("Blueprint::HTML #{Blueprint::VERSION}") { BlueprintHTML::Page.new.to_html } - x.report("Blueprint::RawHTML #{Blueprint::VERSION}") { BlueprintRawHTML::Page.new.to_html } + x.report("Blueprint::HTML #{Blueprint::VERSION}") { BlueprintHTML::Page.new.to_s } + x.report("Blueprint::RawHTML #{Blueprint::VERSION}") { BlueprintRawHTML::Page.new.to_s } x.report("ECR") { ECR::Page.new.to_s } end diff --git a/spec/blueprint/html/attributes_handling_spec.cr b/spec/blueprint/html/attributes_handling_spec.cr index 9d4af2b..74b22db 100644 --- a/spec/blueprint/html/attributes_handling_spec.cr +++ b/spec/blueprint/html/attributes_handling_spec.cr @@ -20,7 +20,7 @@ describe "attributes handling" do
Normal attributes
HTML - page.to_html.should contain(div) + page.to_s.should contain(div) end it "converts attribute values to string" do @@ -29,7 +29,7 @@ describe "attributes handling" do Non-string attribute values HTML - page.to_html.should contain(span) + page.to_s.should contain(span) end it "replaces `_` by `-` on attribute names" do @@ -38,7 +38,7 @@ describe "attributes handling" do
Transform attribute name
HTML - page.to_html.should contain(section) + page.to_s.should contain(section) end it "accepts boolean attributes" do @@ -47,7 +47,7 @@ describe "attributes handling" do HTML - page.to_html.should contain(input) + page.to_s.should contain(input) end it "expands nested attributes" do @@ -56,7 +56,7 @@ describe "attributes handling" do HTML - page.to_html.should contain(nav) + page.to_s.should contain(nav) end it "flattens, compacts and joins array attributes" do @@ -65,6 +65,6 @@ describe "attributes handling" do
Array attributes
HTML - page.to_html.should contain(nav) + page.to_s.should contain(nav) end end diff --git a/spec/blueprint/html/building_style_spec.cr b/spec/blueprint/html/building_style_spec.cr index ed51a39..686f56c 100644 --- a/spec/blueprint/html/building_style_spec.cr +++ b/spec/blueprint/html/building_style_spec.cr @@ -102,7 +102,7 @@ describe "style building" do end it "allows to build style inside components" do - actual_html = ButtonComponent.new.to_html { "Build Style!" } + actual_html = ButtonComponent.new.to_s { "Build Style!" } expected_html = normalize_html <<-HTML Build Style! HTML diff --git a/spec/blueprint/html/component_rendering_spec.cr b/spec/blueprint/html/component_rendering_spec.cr index aad5798..beb1b1c 100644 --- a/spec/blueprint/html/component_rendering_spec.cr +++ b/spec/blueprint/html/component_rendering_spec.cr @@ -73,7 +73,7 @@ describe "component rendering" do
Basic component
HTML - page.to_html.should contain(basic_component) + page.to_s.should contain(basic_component) end it "can provide content to another blueprints" do @@ -86,7 +86,7 @@ describe "component rendering" do HTML - page.to_html.should contain(content_component) + page.to_s.should contain(content_component) end it "can use another blueprint methods" do @@ -99,6 +99,6 @@ describe "component rendering" do HTML - page.to_html.should contain(complex_component) + page.to_s.should contain(complex_component) end end diff --git a/spec/blueprint/html/components_registration_spec.cr b/spec/blueprint/html/components_registration_spec.cr index 4db1c7f..4e13c04 100644 --- a/spec/blueprint/html/components_registration_spec.cr +++ b/spec/blueprint/html/components_registration_spec.cr @@ -60,7 +60,7 @@ describe "components registration" do
Component with required block
HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end it "allows component helper definition without required block" do @@ -69,7 +69,7 @@ describe "components registration" do

Component without block

HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end it "allows component helper definition with optional block" do @@ -79,6 +79,6 @@ describe "components registration" do
HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end end diff --git a/spec/blueprint/html/conditional_rendering_spec.cr b/spec/blueprint/html/conditional_rendering_spec.cr index dadb932..5e49b46 100644 --- a/spec/blueprint/html/conditional_rendering_spec.cr +++ b/spec/blueprint/html/conditional_rendering_spec.cr @@ -51,17 +51,17 @@ describe "conditional rendering" do
HTML - page.to_html.should eq expected_html + page.to_s.should eq expected_html end end context "when blueprint `#render?` returns false" do it "doesn't render the blueprint" do page = NoRenderPage.new - page.to_html.should eq "" + page.to_s.should eq "" page = NoRenderPage.new - html = page.to_html { "This page will not be rendered" } + html = page.to_s { "This page will not be rendered" } html.should eq "" end end diff --git a/spec/blueprint/html/custom_elements_registration_spec.cr b/spec/blueprint/html/custom_elements_registration_spec.cr index bcb94f2..8fde51c 100644 --- a/spec/blueprint/html/custom_elements_registration_spec.cr +++ b/spec/blueprint/html/custom_elements_registration_spec.cr @@ -23,7 +23,7 @@ describe "custom elements registration" do Home HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end it "allows passing content as first argument" do @@ -32,7 +32,7 @@ describe "custom elements registration" do Contact HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end it "allows empty custom elements" do @@ -41,7 +41,7 @@ describe "custom elements registration" do HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end it "allows defining custom tags" do @@ -50,6 +50,6 @@ describe "custom elements registration" do HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end end diff --git a/spec/blueprint/html/enveloping_spec.cr b/spec/blueprint/html/enveloping_spec.cr index 3d305b0..8caf05b 100644 --- a/spec/blueprint/html/enveloping_spec.cr +++ b/spec/blueprint/html/enveloping_spec.cr @@ -56,6 +56,6 @@ describe "enveloping" do HTML - page.to_html.should eq expected_html + page.to_s.should eq expected_html end end diff --git a/spec/blueprint/html/helpers_spec.cr b/spec/blueprint/html/helpers_spec.cr index b1221ec..f0517d3 100644 --- a/spec/blueprint/html/helpers_spec.cr +++ b/spec/blueprint/html/helpers_spec.cr @@ -28,7 +28,7 @@ describe "helpers" do
Tokens
HTML - page.to_html.should contain expected_html + page.to_s.should contain expected_html end end end diff --git a/spec/blueprint/html/safety_spec.cr b/spec/blueprint/html/safety_spec.cr index d9c4313..32f0372 100644 --- a/spec/blueprint/html/safety_spec.cr +++ b/spec/blueprint/html/safety_spec.cr @@ -33,7 +33,7 @@ describe "safety" do <script>alert('hello')</script> HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes content passed to tags via argument" do @@ -42,7 +42,7 @@ describe "safety" do <script>alert('content')</script> HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes plain text" do @@ -51,7 +51,7 @@ describe "safety" do <script>alert('Plain Text')</script> HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes content passed to blueprints" do @@ -60,7 +60,7 @@ describe "safety" do <script>alert('ExampleComponent')</script> HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes attribute values" do @@ -69,7 +69,7 @@ describe "safety" do
HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes comment content passed via block" do @@ -78,7 +78,7 @@ describe "safety" do HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes comment content passed via argument" do @@ -87,7 +87,7 @@ describe "safety" do HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes custom tag content passed via argument" do @@ -96,7 +96,7 @@ describe "safety" do <script>alert('content')</script> HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end it "escapes custom tag content passed via block" do @@ -105,6 +105,6 @@ describe "safety" do <script>alert('hello')</script> HTML - page.to_html.should contain(expected_html) + page.to_s.should contain(expected_html) end end diff --git a/spec/blueprint/html/standard_elements_spec.cr b/spec/blueprint/html/standard_elements_spec.cr index dca4fdb..57a64df 100644 --- a/spec/blueprint/html/standard_elements_spec.cr +++ b/spec/blueprint/html/standard_elements_spec.cr @@ -73,6 +73,6 @@ describe "standard HTML elements" do io << "" end - page.to_html.should eq expected_html + page.to_s.should eq expected_html end end diff --git a/spec/blueprint/html/svg_rendering_spec.cr b/spec/blueprint/html/svg_rendering_spec.cr index a6d096a..d2aa7a4 100644 --- a/spec/blueprint/html/svg_rendering_spec.cr +++ b/spec/blueprint/html/svg_rendering_spec.cr @@ -48,7 +48,7 @@ describe "SVG rendering" do HTML - example.to_html.should eq expected_html + example.to_s.should eq expected_html end it "defines all SVG element helper methods" do @@ -64,6 +64,6 @@ describe "SVG rendering" do io << "" end - page.to_html.should eq expected_html + page.to_s.should eq expected_html end end diff --git a/spec/blueprint/html/utils_spec.cr b/spec/blueprint/html/utils_spec.cr index be38df1..0bdb675 100644 --- a/spec/blueprint/html/utils_spec.cr +++ b/spec/blueprint/html/utils_spec.cr @@ -29,13 +29,13 @@ describe "utils" do it "renders plain text passed via argument" do page = ExamplePage.new - page.to_html.should contain("
HelloWorld
") + page.to_s.should contain("
HelloWorld
") end it "renders plain text passed via block" do page = ExamplePage.new - page.to_html.should contain("Plain!") + page.to_s.should contain("Plain!") end end @@ -43,7 +43,7 @@ describe "utils" do it "renders HTML 5 doctype declaration" do page = ExamplePage.new - page.to_html.should contain("") + page.to_s.should contain("") end end @@ -51,13 +51,13 @@ describe "utils" do it "renders an html comment passed via block" do page = ExamplePage.new - page.to_html.should contain("") + page.to_s.should contain("") end it "renders an html comment passed via argument" do page = ExamplePage.new - page.to_html.should contain("") + page.to_s.should contain("") end end @@ -65,7 +65,7 @@ describe "utils" do it "renders an whitespace" do page = ExamplePage.new - page.to_html.should contain("Hi User") + page.to_s.should contain("Hi User") end end @@ -73,13 +73,13 @@ describe "utils" do it "renders content passed via argument without escaping" do page = ExamplePage.new - page.to_html.should contain("") + page.to_s.should contain("") end it "renders content passed via block without escaping" do page = ExamplePage.new - page.to_html.should contain("
") + page.to_s.should contain("
") end end end diff --git a/spec/blueprint/html_spec.cr b/spec/blueprint/html_spec.cr index 51d98b6..fdfaa82 100644 --- a/spec/blueprint/html_spec.cr +++ b/spec/blueprint/html_spec.cr @@ -83,7 +83,7 @@ private class ExamplePage end describe Blueprint::HTML do - describe "#to_html" do + describe "#to_s" do it "renders html" do page = ExamplePage.new expected_html = normalize_html <<-HTML @@ -123,7 +123,7 @@ describe Blueprint::HTML do HTML - html = page.to_html + html = page.to_s html.should eq expected_html end diff --git a/spec/blueprint/raw_html_spec.cr b/spec/blueprint/raw_html_spec.cr index 4d331ce..232e7d7 100644 --- a/spec/blueprint/raw_html_spec.cr +++ b/spec/blueprint/raw_html_spec.cr @@ -86,7 +86,7 @@ private class ExamplePage end describe Blueprint::RawHTML do - describe "#to_html" do + describe "#to_s" do it "renders html without escaping" do page = ExamplePage.new expected_html = normalize_html <<-HTML @@ -130,7 +130,7 @@ describe Blueprint::RawHTML do HTML - html = page.to_html + html = page.to_s html.should eq expected_html end diff --git a/src/blueprint/html.cr b/src/blueprint/html.cr index 31f01cf..695d7cc 100644 --- a/src/blueprint/html.cr +++ b/src/blueprint/html.cr @@ -7,7 +7,6 @@ require "./html/component_renderer" require "./html/element_registrar" require "./html/element_renderer" require "./html/helpers" -require "./html/renderer" require "./html/standard_elements" require "./html/style_builder" require "./html/svg" @@ -20,7 +19,6 @@ module Blueprint::HTML include Blueprint::HTML::ElementRegistrar include Blueprint::HTML::ElementRenderer include Blueprint::HTML::Helpers - include Blueprint::HTML::Renderer include Blueprint::HTML::StandardElements include Blueprint::HTML::StyleBuilder include Blueprint::HTML::SVG @@ -28,18 +26,40 @@ module Blueprint::HTML @buffer : String::Builder = String::Builder.new - def to_html : String - render_to(@buffer) + def to_s : String + to_s(@buffer) @buffer.to_s end - def to_html(&) : String - render_to(@buffer) { yield } + def to_s(&) : String + to_s(@buffer) { yield } @buffer.to_s end + def to_s(buffer : String::Builder) : Nil + return unless render? + + @buffer = buffer + + envelope { blueprint } + end + + def to_s(buffer : String::Builder, &) : Nil + return unless render? + + @buffer = buffer + + envelope do + blueprint { capture_content { yield } } + end + end + + private def render? : Bool + true + end + private def envelope(&) : Nil yield end diff --git a/src/blueprint/html/builder.cr b/src/blueprint/html/builder.cr index 85ddb57..293da0f 100644 --- a/src/blueprint/html/builder.cr +++ b/src/blueprint/html/builder.cr @@ -14,7 +14,7 @@ module Blueprint::HTML end def build(&) : String - to_html { yield } + to_s { yield } end private def blueprint(&) : Nil diff --git a/src/blueprint/html/component_renderer.cr b/src/blueprint/html/component_renderer.cr index a18d647..9f1e5c8 100644 --- a/src/blueprint/html/component_renderer.cr +++ b/src/blueprint/html/component_renderer.cr @@ -1,10 +1,10 @@ module Blueprint::HTML::ComponentRenderer private def render(blueprint : Blueprint::HTML) : Nil - blueprint.render_to(@buffer) + blueprint.to_s(@buffer) end private def render(blueprint : Blueprint::HTML, &) : Nil - blueprint.render_to(@buffer) do + blueprint.to_s(@buffer) do yield blueprint end end diff --git a/src/blueprint/html/renderer.cr b/src/blueprint/html/renderer.cr deleted file mode 100644 index dba9966..0000000 --- a/src/blueprint/html/renderer.cr +++ /dev/null @@ -1,23 +0,0 @@ -module Blueprint::HTML::Renderer - def render_to(buffer : String::Builder) : Nil - return unless render? - - @buffer = buffer - - envelope { blueprint } - end - - def render_to(buffer : String::Builder, &) : Nil - return unless render? - - @buffer = buffer - - envelope do - blueprint { capture_content { yield } } - end - end - - private def render? : Bool - true - end -end