From f2f1fe58ef51f1fbc6a31ef8cf65226cfd6d2087 Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Sat, 14 Sep 2024 14:26:03 -0300 Subject: [PATCH] feat: Remove Blueprint::RawHTML module (#72) --- benchmark/benchmark.cr | 7 +- benchmark/blueprint_raw_html.cr | 65 --------------- spec/blueprint/raw_html_spec.cr | 138 -------------------------------- src/blueprint/html/helpers.cr | 1 + src/blueprint/raw_html.cr | 43 ---------- 5 files changed, 3 insertions(+), 251 deletions(-) delete mode 100644 benchmark/blueprint_raw_html.cr delete mode 100644 spec/blueprint/raw_html_spec.cr delete mode 100644 src/blueprint/raw_html.cr diff --git a/benchmark/benchmark.cr b/benchmark/benchmark.cr index 8f6baca..a2af7a6 100644 --- a/benchmark/benchmark.cr +++ b/benchmark/benchmark.cr @@ -1,19 +1,16 @@ require "benchmark" require "./blueprint_html" -require "./blueprint_raw_html" require "./ecr" require "../src/blueprint/version" 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 +ecr = ECR::Page.new.to_s.chomp # ECR is here just to have a base value to compare -raise "Different results" if blueprint_html != blueprint_raw_html && blueprint_html != ecr +raise "Different results" if blueprint_html != ecr Benchmark.ips do |x| 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/benchmark/blueprint_raw_html.cr b/benchmark/blueprint_raw_html.cr deleted file mode 100644 index 18bd228..0000000 --- a/benchmark/blueprint_raw_html.cr +++ /dev/null @@ -1,65 +0,0 @@ -require "../src/blueprint/raw_html" - -class BlueprintRawHTML::LayoutComponent - include Blueprint::RawHTML - - def initialize(@title = "Example"); end - - def blueprint(&) - html do - head do - title @title - meta name: "viewport", content: "width=device-width,initial-scale=1" - link href: "/assets/tailwind.css", rel: "stylesheet" - end - - body class: "bg-zinc-100" do - nav class: "p-5", id: "main_nav" do - ul do - li(class: "p-5") { a("Home", href: "/") } - li(class: "p-5") { a("About", href: "/about") } - li(class: "p-5") { a("Contact", href: "/contact") } - end - end - - div class: "container mx-auto p-5" do - yield - end - end - end - end -end - -class BlueprintRawHTML::Page - include Blueprint::RawHTML - - def blueprint - render BlueprintRawHTML::LayoutComponent.new do - h1 "Hi" - - table id: "test", class: "a b c d e f g" do - tr do - td id: "test", class: "a b c d e f g" do - span "Hi" - end - - td id: "test", class: "a b c d e f g" do - span "Hi" - end - - td id: "test", class: "a b c d e f g" do - span "Hi" - end - - td id: "test", class: "a b c d e f g" do - span "Hi" - end - - td id: "test", class: "a b c d e f g" do - span "Hi" - end - end - end - end - end -end diff --git a/spec/blueprint/raw_html_spec.cr b/spec/blueprint/raw_html_spec.cr deleted file mode 100644 index 232e7d7..0000000 --- a/spec/blueprint/raw_html_spec.cr +++ /dev/null @@ -1,138 +0,0 @@ -require "../spec_helper" -require "../../src/blueprint/raw_html" - -private class BaseLayout - include Blueprint::RawHTML - - private def blueprint(&) - doctype - - html lang: "en" do - head do - title "Test page" - - meta charset: "utf-8" - meta name: "viewport", content: "width=device-width,initial-scale=1" - - link href: "app.css", rel: "stylesheet" - script type: "text/javascript", src: "app.js" - end - - body do - yield - end - end - end -end - -private class NavbarComponent - include Blueprint::RawHTML - - private def blueprint - nav do - ul do - li { a("Home", href: "/home") } - li { a("About", href: "/about") } - li { a("Contact", href: "/contact") } - end - end - end -end - -private class ArticleComponent - include Blueprint::RawHTML - - def initialize(@title : String); end - - private def blueprint(&) - div class: "flex flex-col gap-2 bg-white border shadow" do - title - yield - end - end - - def title - div @title, class: "p-2 text-lg font-bold" - end - - def body(&) - div class: "p-4" do - yield - end - end -end - -private class ExamplePage - include Blueprint::RawHTML - - private def blueprint - render BaseLayout.new do - render NavbarComponent.new - - div do - article("Hello World", "Welcome to blueprint") - article("Blueprint", "Blueprint is an Html builder") - div(data: {hello: "world"}, checked: true, class: ["a", "b", "c"]) { "" } - a "Click me!", class: "bg-red\" onclick=\"alert('danger!')" - end - end - end - - private def article(title : String, content : String) - render ArticleComponent.new(title) do |article| - article.body { content } - end - end -end - -describe Blueprint::RawHTML do - describe "#to_s" do - it "renders html without escaping" do - page = ExamplePage.new - expected_html = normalize_html <<-HTML - - - - Test page - - - - - - - - - - - -
-
-
Hello World
-
Welcome to blueprint
-
- -
-
Blueprint
-
Blueprint is an Html builder
-
- -
- - Click me! -
- - - HTML - - html = page.to_s - - html.should eq expected_html - end - end -end diff --git a/src/blueprint/html/helpers.cr b/src/blueprint/html/helpers.cr index 90ecf84..a9f563f 100644 --- a/src/blueprint/html/helpers.cr +++ b/src/blueprint/html/helpers.cr @@ -3,6 +3,7 @@ module Blueprint::HTML::Helpers Blueprint::SafeValue.new(value) end + @[Experimental] macro tokens(**conditions) String.build do |io| {% for key, value in conditions %} diff --git a/src/blueprint/raw_html.cr b/src/blueprint/raw_html.cr deleted file mode 100644 index 21d72f9..0000000 --- a/src/blueprint/raw_html.cr +++ /dev/null @@ -1,43 +0,0 @@ -require "html" - -require "./html" - -@[Experimental] -module Blueprint::RawHTML - include Blueprint::HTML - - private def append_attribute(attribute_name, attribute_value) : Nil - @buffer << " " - @buffer << attribute_name.to_s - @buffer << %(=") - @buffer << attribute_value.to_s - @buffer << %(") - end - - private def capture_content(&) : Nil - buffer_size_before_block_evaluation = @buffer.bytesize - content = yield - @buffer << content if buffer_size_before_block_evaluation == @buffer.bytesize - end - - private def element(_tag_name : String | Symbol, __content__ : String, **attributes) : Nil - @buffer << "<" - @buffer << _tag_name - append_attributes(attributes) - @buffer << ">" - @buffer << __content__ - @buffer << "" - end - - private def plain(content : String) : Nil - @buffer << content - end - - private def comment(&) : Nil - @buffer << "" - end -end