Skip to content

Instantly share code, notes, and snippets.

@asterite
Created March 19, 2014 14:40
Show Gist options
  • Select an option

  • Save asterite/9643064 to your computer and use it in GitHub Desktop.

Select an option

Save asterite/9643064 to your computer and use it in GitHub Desktop.
class HtmlBuilder
def initialize
@str = StringBuilder.new
end
def build
self.yield
@str.to_s
end
macro self.tag(name)
"
def #{name}
@str << \"<#{name}>\"
self.yield
@str << \"</#{name}>\"
end
"
end
tag :html
tag :head
tag :body
tag :title
tag :b
def title(title)
title do
text(title)
end
end
def text(text)
@str << text
end
end
str = HtmlBuilder.new.build do
html do
head do
title "Crystal Programming Language"
end
body do
b { text "Crystal rocks!" }
end
end
end
puts str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment