Skip to content

Commit

Permalink
refactor: Reorganize more code (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Sep 10, 2024
1 parent 3667505 commit 652fc61
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 49 deletions.
10 changes: 9 additions & 1 deletion spec/blueprint/html/utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ private class DummyPage
b "World"
end

span { plain { "Plain!" } }

i "Hi"
whitespace
plain "User"
Expand All @@ -24,11 +26,17 @@ end

describe "Blueprint::HTML utils" do
describe "#plain" do
it "renders plain text" do
it "renders plain text passed via argument" do
page = DummyPage.new

page.to_html.should contain("<div>Hello<b>World</b></div>")
end

it "renders plain text passed via block" do
page = DummyPage.new

page.to_html.should contain("<span>Plain!</span>")
end
end

describe "#doctype" do
Expand Down
18 changes: 5 additions & 13 deletions src/blueprint/html.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "./html/builder"
require "./html/component_renderer"
require "./html/element_registrar"
require "./html/element_renderer"
require "./html/renderer"
require "./html/standard_elements"
require "./html/svg"
require "./html/utils"
Expand All @@ -16,34 +17,25 @@ module Blueprint::HTML
include Blueprint::HTML::ComponentRenderer
include Blueprint::HTML::ElementRegistrar
include Blueprint::HTML::ElementRenderer
include Blueprint::HTML::Renderer
include Blueprint::HTML::StandardElements
include Blueprint::HTML::SVG
include Blueprint::HTML::Utils

@buffer = String::Builder.new
@buffer : String::Builder = String::Builder.new

def to_html : String
return "" unless render?

envelope { blueprint }
render_to(@buffer)

@buffer.to_s
end

def to_html(&) : String
return "" unless render?

envelope do
blueprint { render_block { yield } }
end
render_to(@buffer) { yield self }

@buffer.to_s
end

private def render? : Bool
true
end

private def envelope(&) : Nil
yield
end
Expand Down
4 changes: 2 additions & 2 deletions src/blueprint/html/component_registrar.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module Blueprint::HTML::ComponentRegistrar
macro register_component(helper_method, component_class, block = true)
{% if block %}
private def {{helper_method.id}}(**args, &block) : Nil
private def {{helper_method.id}}(**args, &) : Nil
render {{component_class}}.new(**args) do |component|
yield component
end
end

{% if block == :optional %}
private def {{helper_method.id}}(**args) : Nil
render({{component_class}}.new(**args)) { nil }
render({{component_class}}.new(**args)) {}
end
{% end %}
{% else %}
Expand Down
14 changes: 0 additions & 14 deletions src/blueprint/html/component_renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,4 @@ module Blueprint::HTML::ComponentRenderer
with self yield blueprint
end
end

protected def render_to(buffer : String::Builder) : Nil
return unless render?

@buffer = buffer
blueprint
end

protected def render_to(buffer : String::Builder, &) : Nil
return unless render?

@buffer = buffer
blueprint { render_block { yield } }
end
end
23 changes: 23 additions & 0 deletions src/blueprint/html/renderer.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 { render_block { yield } }
end
end

private def render? : Bool
true
end
end
8 changes: 4 additions & 4 deletions src/blueprint/html/svg.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "../svg/component"

module Blueprint::HTML::SVG
private def svg(**attributes) : Nil
svg(**attributes) { }
end

private def svg(**attributes, &) : Nil
render Blueprint::SVG::Component.new(**attributes) do |component|
with component yield
end
end

private def svg(**attributes) : Nil
render Blueprint::SVG::Component.new(**attributes)
end
end
18 changes: 10 additions & 8 deletions src/blueprint/html/utils.cr
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
module Blueprint::HTML::Utils
private def plain(content : String) : Nil
::HTML.escape(content, @buffer)
plain { content }
end

private def plain(&) : Nil
::HTML.escape(yield, @buffer)
end

private def doctype : Nil
@buffer << "<!DOCTYPE html>"
end

private def comment(&) : Nil
@buffer << "<!--"
::HTML.escape(yield, @buffer)
@buffer << "-->"
private def comment(content : String) : Nil
comment { content }
end

private def comment(content : String) : Nil
private def comment(&) : Nil
@buffer << "<!--"
::HTML.escape(content, @buffer)
::HTML.escape(yield, @buffer)
@buffer << "-->"
end

Expand All @@ -24,7 +26,7 @@ module Blueprint::HTML::Utils
end

def unsafe_raw(content : String) : Nil
@buffer << content
unsafe_raw { content }
end

def unsafe_raw(&) : Nil
Expand Down
8 changes: 1 addition & 7 deletions src/blueprint/raw_html.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Blueprint::RawHTML
private def render_block(&) : Nil
buffer_size_before_block_evaluation = @buffer.bytesize
content = with self yield
@buffer << content.to_s if buffer_size_before_block_evaluation == @buffer.bytesize
@buffer << content if buffer_size_before_block_evaluation == @buffer.bytesize
end

private def element(_tag_name : String | Symbol, __content__ : String, **attributes) : Nil
Expand All @@ -39,10 +39,4 @@ module Blueprint::RawHTML
@buffer << yield
@buffer << "-->"
end

private def comment(content : String) : Nil
@buffer << "<!--"
@buffer << content
@buffer << "-->"
end
end

0 comments on commit 652fc61

Please sign in to comment.