Skip to content

Commit ebc888f

Browse files
authored
Allow passing IO to HTML.build (#24)
1 parent 0adcf40 commit ebc888f

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

spec/html_builder_spec.cr

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ require "../src/html/builder"
33

44
describe HTML::Builder do
55
it "builds html" do
6-
str = HTML::Builder.build do
6+
str = HTML.build do
77
doctype
88
html do
99
head do
@@ -20,8 +20,18 @@ describe HTML::Builder do
2020
str.should eq %(<!DOCTYPE html><html><head><title>Crystal Programming Language</title></head><body><a href="http://crystal-lang.org">Crystal rocks!</a><form method="POST"><input name="name"></form></body></html>)
2121
end
2222

23+
it "builds html to IO" do
24+
io = IO::Memory.new
25+
HTML.build(io) do
26+
doctype
27+
html do
28+
end
29+
end
30+
io.to_s.should eq %(<!DOCTYPE html><html></html>)
31+
end
32+
2333
it "builds html with some tag attributes" do
24-
str = HTML::Builder.new.build do
34+
str = HTML.build do
2535
a(href: "http://crystal-lang.org", class: "crystal", id: "main") do
2636
text "Crystal rocks!"
2737
end
@@ -30,7 +40,7 @@ describe HTML::Builder do
3040
end
3141

3242
it "builds html with some tag attributes, using a hash" do
33-
str = HTML::Builder.new.build do
43+
str = HTML.build do
3444
a(href: "http://crystal-lang.org", class: "crystal", id: "main") do
3545
text "Crystal rocks!"
3646
end
@@ -39,7 +49,7 @@ describe HTML::Builder do
3949
end
4050

4151
it "builds html with some tag attributes, using a named tuple" do
42-
str = HTML::Builder.new.build do |builder|
52+
str = HTML.build do |builder|
4353
builder.a({href: "http://crystal-lang.org", class: "crystal", id: "main"}) do
4454
text "Crystal rocks!"
4555
end
@@ -48,28 +58,28 @@ describe HTML::Builder do
4858
end
4959

5060
it "builds html with an provided html string" do
51-
str = HTML::Builder.new.build do
61+
str = HTML.build do
5262
html "<section>Crystal rocks!</section>"
5363
end
5464
str.should eq %(<section>Crystal rocks!</section>)
5565
end
5666

5767
it "builds html with a custom tag with attributes" do
58-
str = HTML::Builder.new.build do
68+
str = HTML.build do
5969
tag("section", class: "crystal") { text "Crystal rocks!" }
6070
end
6171
str.should eq %(<section class="crystal">Crystal rocks!</section>)
6272
end
6373

6474
it "escapes attribute values" do
65-
str = HTML::Builder.new.build do
75+
str = HTML.build do
6676
a(href: "<>") { }
6777
end
6878
str.should eq %(<a href="&lt;&gt;"></a>)
6979
end
7080

7181
it "escapes text" do
72-
str = HTML::Builder.new.build do
82+
str = HTML.build do
7383
a { text "<>" }
7484
end
7585
str.should eq %(<a>&lt;&gt;</a>)

src/html/builder/builder.cr

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,35 @@ require "html"
1818
struct HTML::Builder
1919
# Creates a new HTML::Builder, yields with with `with ... yield`
2020
# and then returns the resulting string.
21-
def self.build
22-
new.build do |builder|
21+
def self.build : String
22+
String.build do |io|
23+
build(io) do |builder|
24+
with builder yield builder
25+
end
26+
end
27+
end
28+
29+
def self.build(io : IO)
30+
new(io).build_impl do |builder|
2331
with builder yield builder
2432
end
2533
end
2634

27-
def initialize
28-
@buffer = String::Builder.new
35+
@buffer : IO
36+
37+
def initialize(@buffer : IO)
38+
end
39+
40+
@[Deprecated("Use HTML.build directly")]
41+
def initialize(@buffer = String::Builder.new)
42+
end
43+
44+
protected def build_impl
45+
with self yield self
2946
end
3047

31-
def build
48+
@[Deprecated("Use HTML.build directly")]
49+
def build(&block)
3250
with self yield self
3351
@buffer.to_s
3452
end
@@ -179,9 +197,16 @@ end
179197

180198
module HTML
181199
# Convenience method which invokes `HTML::Builder#build`.
182-
def self.build
200+
def self.build : String
183201
HTML::Builder.build do |builder|
184202
with builder yield builder
185203
end
186204
end
205+
206+
# Convenience method which invokes `HTML::Builder#build`.
207+
def self.build(io : IO)
208+
HTML::Builder.build(io) do |builder|
209+
with builder yield builder
210+
end
211+
end
187212
end

0 commit comments

Comments
 (0)