Skip to content

Commit

Permalink
refactor: Reorganize code in modules (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv authored Sep 9, 2024
1 parent b051430 commit b2ce823
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 186 deletions.
18 changes: 14 additions & 4 deletions src/blueprint/html.cr
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
require "html"

require "./html/attributes_parser"
require "./html/block_renderer"
require "./html/builder"
require "./html/content_capture"
require "./html/component_renderer"
require "./html/element_registrar"
require "./html/elements"
require "./html/renderer"
require "./html/element_renderer"
require "./html/standard_elements"
require "./html/svg"
require "./html/utils"

module Blueprint::HTML
include Blueprint::HTML::AttributesParser
include Blueprint::HTML::BlockRenderer
include Blueprint::HTML::ComponentRenderer
include Blueprint::HTML::ElementRegistrar
include Blueprint::HTML::ElementRenderer
include Blueprint::HTML::StandardElements
include Blueprint::HTML::SVG
include Blueprint::HTML::Utils

@buffer = String::Builder.new

def to_html : String
Expand All @@ -24,7 +34,7 @@ module Blueprint::HTML
return "" unless render?

envelope do
blueprint { capture_content { yield } }
blueprint { render_block { yield } }
end

@buffer.to_s
Expand Down
46 changes: 22 additions & 24 deletions src/blueprint/html/attributes_parser.cr
Original file line number Diff line number Diff line change
@@ -1,49 +1,47 @@
module Blueprint::HTML
private def parse_attributes(attributes : NamedTuple) : String
String.build do |io|
attributes.each do |name, value|
append_attribute(io, name, value)
end
module Blueprint::HTML::AttributesParser
private def parse_attributes(attributes : NamedTuple)
attributes.each do |name, value|
append_attribute(name, value)
end
end

private def append_attribute(io : String::Builder, attribute_name, attribute_value) : Nil
private def append_attribute(attribute_name, attribute_value) : Nil
case attribute_value
when Nil, false
# does nothing
when true
append_boolean_attribute(io, attribute_name)
append_boolean_attribute(attribute_name)
when NamedTuple
process_named_tuple_attribute(io, attribute_name, attribute_value)
process_named_tuple_attribute(attribute_name, attribute_value)
when Array
append_array_attribute(io, attribute_name, attribute_value)
append_array_attribute(attribute_name, attribute_value)
else
append_normal_attribute(io, attribute_name, attribute_value)
append_normal_attribute(attribute_name, attribute_value)
end
end

private def append_normal_attribute(io : String::Builder, attribute_name, attribute_value) : Nil
io << " "
io << parse_attribute_name(attribute_name)
io << %(=")
::HTML.escape(attribute_value.to_s, io)
io << %(")
private def append_normal_attribute(attribute_name, attribute_value) : Nil
@buffer << " "
@buffer << parse_attribute_name(attribute_name)
@buffer << %(=")
::HTML.escape(attribute_value.to_s, @buffer)
@buffer << %(")
end

private def append_boolean_attribute(io : String::Builder, attribute_name) : Nil
io << " "
io << parse_attribute_name(attribute_name)
private def append_boolean_attribute(attribute_name) : Nil
@buffer << " "
@buffer << parse_attribute_name(attribute_name)
end

private def append_array_attribute(io : String::Builder, attribute_name, attribute_value : Array) : Nil
append_normal_attribute(io, attribute_name, attribute_value.flatten.compact.join(" "))
private def append_array_attribute(attribute_name, attribute_value : Array) : Nil
append_normal_attribute(attribute_name, attribute_value.flatten.compact.join(" "))
end

private def process_named_tuple_attribute(io : String::Builder, attribute_name, attribute_value : NamedTuple) : Nil
private def process_named_tuple_attribute(attribute_name, attribute_value : NamedTuple) : Nil
attribute_name_prefix = parse_attribute_name(attribute_name)

attribute_value.each do |name, value|
append_attribute(io, "#{attribute_name_prefix}-#{parse_attribute_name(name)}", value)
append_attribute("#{attribute_name_prefix}-#{parse_attribute_name(name)}", value)
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Blueprint::HTML
private def capture_content(&) : Nil
module Blueprint::HTML::BlockRenderer
private def render_block(&) : Nil
buffer_size_before_block_evaluation = @buffer.bytesize
content = with self yield
if buffer_size_before_block_evaluation == @buffer.bytesize
Expand Down
5 changes: 3 additions & 2 deletions src/blueprint/html/builder.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Blueprint::HTML
# EXPERIMENTAL
@[Experimental]
def self.build(&) : String
Builder.build { |builder| with builder yield }
end

private struct Builder
@[Experimental]
struct Builder
include Blueprint::HTML

def self.build(&) : String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# :nodoc:
module Blueprint::HTML
module Blueprint::HTML::ComponentRenderer
private def render(blueprint : Blueprint::HTML) : Nil
blueprint.render_to(@buffer)
end
Expand All @@ -21,6 +21,6 @@ module Blueprint::HTML
return unless render?

@buffer = buffer
blueprint { capture_content { yield } }
blueprint { render_block { yield } }
end
end
31 changes: 1 addition & 30 deletions src/blueprint/html/element_registrar.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Blueprint::HTML
module Blueprint::HTML::ElementRegistrar
macro register_element(method_name, tag = nil)
{% tag ||= method_name.tr("_", "-") %}

Expand Down Expand Up @@ -30,33 +30,4 @@ module Blueprint::HTML
void_element({{tag}}, **attributes)
end
end

private def element(_tag_name : String | Symbol, **attributes, &block) : Nil
@buffer << "<"
@buffer << _tag_name
@buffer << parse_attributes(attributes)
@buffer << ">"
capture_content { with self yield }
@buffer << "</"
@buffer << _tag_name
@buffer << ">"
end

private def element(_tag_name : String | Symbol, __content__ : String, **attributes) : Nil
@buffer << "<"
@buffer << _tag_name
@buffer << parse_attributes(attributes)
@buffer << ">"
::HTML.escape(__content__, @buffer)
@buffer << "</"
@buffer << _tag_name
@buffer << ">"
end

private def void_element(_tag_name : String | Symbol, **attributes) : Nil
@buffer << "<"
@buffer << _tag_name
@buffer << parse_attributes(attributes)
@buffer << ">"
end
end
30 changes: 30 additions & 0 deletions src/blueprint/html/element_renderer.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Blueprint::HTML::ElementRenderer
private def element(_tag_name : String | Symbol, **attributes, &) : Nil
@buffer << "<"
@buffer << _tag_name
parse_attributes(attributes)
@buffer << ">"
render_block { with self yield }
@buffer << "</"
@buffer << _tag_name
@buffer << ">"
end

private def element(_tag_name : String | Symbol, __content__ : String, **attributes) : Nil
@buffer << "<"
@buffer << _tag_name
parse_attributes(attributes)
@buffer << ">"
::HTML.escape(__content__, @buffer)
@buffer << "</"
@buffer << _tag_name
@buffer << ">"
end

private def void_element(_tag_name : String | Symbol, **attributes) : Nil
@buffer << "<"
@buffer << _tag_name
parse_attributes(attributes)
@buffer << ">"
end
end
116 changes: 0 additions & 116 deletions src/blueprint/html/elements.cr

This file was deleted.

Loading

0 comments on commit b2ce823

Please sign in to comment.