Skip to content

Commit

Permalink
Refactor SVG
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Jan 24, 2025
1 parent 4043a3e commit 89e282f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,17 @@ private ELEMENTS = %w(a animate animateMotion animateTransform circle clipPath d
text textPath title tspan use view
)

private class ExamplePage
include Blueprint::HTML

private def blueprint
svg width: 30, height: 10 do
g fill: :red do
rect x: 0, y: 0, width: 10, height: 10
rect x: 20, y: 0, width: 10, height: 10
describe "SVG" do
it "allows SVG rendering" do
actual_html = Blueprint::HTML.build do
svg width: 30, height: 10 do
g fill: :red do
rect x: 0, y: 0, width: 10, height: 10
rect x: 20, y: 0, width: 10, height: 10
end
end
end
end
end

private class CompleteExamplePage
include Blueprint::HTML

private def blueprint
svg do
{% for element in ELEMENTS %}
{{element.id}}
{{element.id}}(attribute: "test")
{{element.id}} { "content" }
{{element.id}}(attribute: "test") { "content" }
{% end %}
end
end
end

describe "SVG rendering" do
it "allows SVG rendering" do
example = ExamplePage.new
expected_html = normalize_html <<-HTML
<svg width="30" height="10">
<g fill="red">
Expand All @@ -48,11 +28,21 @@ describe "SVG rendering" do
</svg>
HTML

example.to_s.should eq expected_html
actual_html.should eq expected_html
end

it "defines all SVG element helper methods" do
page = CompleteExamplePage.new
actual_html = Blueprint::HTML.build do
svg do
{% for element in ELEMENTS %}
{{element.id}}
{{element.id}}(attribute: "test")
{{element.id}} { "content" }
{{element.id}}(attribute: "test") { "content" }
{% end %}
end
end

expected_html = String.build do |io|
io << "<svg>"
ELEMENTS.each do |tag|
Expand All @@ -64,6 +54,6 @@ describe "SVG rendering" do
io << "</svg>"
end

page.to_s.should eq expected_html
actual_html.should eq expected_html
end
end
11 changes: 10 additions & 1 deletion src/blueprint/html.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module Blueprint::HTML
include Blueprint::HTML::ElementRegistrar
include Blueprint::HTML::OutputHelpers
include Blueprint::HTML::StandardElements
include Blueprint::HTML::SVG
include Blueprint::HTML::ValueHelpers

@buffer : String::Builder = String::Builder.new
Expand Down Expand Up @@ -97,4 +96,14 @@ module Blueprint::HTML
def render(renderable) : Nil
BufferRenderer.render(renderable, to: @buffer)
end

private def svg(**attributes) : Nil
svg(**attributes) { }
end

private def svg(**attributes, &) : Nil
render SVG.new(**attributes) do |svg|
with svg yield
end
end
end
44 changes: 13 additions & 31 deletions src/blueprint/html/svg.cr
Original file line number Diff line number Diff line change
@@ -1,38 +1,20 @@
module Blueprint::HTML::SVG
private def svg(**attributes) : Nil
svg(**attributes) { }
end

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

private struct SVGComponent(T)
include Blueprint::HTML
struct Blueprint::HTML::SVG(T)
include Blueprint::HTML

@attributes : T
@attributes : T

def self.new(**kwargs) : SVGComponent
new kwargs
end

macro finished
{% for tag in %w(a animate animateMotion animateTransform circle clipPath defs desc discard ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter foreignObject g image line linearGradient marker mask metadata mpath path pattern polygon polyline radialGradient rect script set stop style svg switch symbol text textPath title tspan use view) %}
register_element {{tag}}
{% end %}
end
macro finished
{% for tag in %w(a animate animateMotion animateTransform circle clipPath defs desc discard ellipse feBlend feColorMatrix feComponentTransfer feComposite feConvolveMatrix feDiffuseLighting feDisplacementMap feDistantLight feDropShadow feFlood feFuncA feFuncB feFuncG feFuncR feGaussianBlur feImage feMerge feMergeNode feMorphology feOffset fePointLight feSpecularLighting feSpotLight feTile feTurbulence filter foreignObject g image line linearGradient marker mask metadata mpath path pattern polygon polyline radialGradient rect script set stop style svg switch symbol text textPath title tspan use view) %}
register_element {{tag}}
{% end %}
end

def initialize(attributes : T)
{% T.raise "Expected T be NamedTuple, but got #{T}." unless T <= NamedTuple %}
@attributes = attributes
end
def initialize(**@attributes : **T)
end

private def blueprint(&) : Nil
element :svg, **@attributes do
yield
end
private def blueprint(&) : Nil
element :svg, **@attributes do
yield
end
end
end

0 comments on commit 89e282f

Please sign in to comment.