Created
March 19, 2014 14:40
-
-
Save asterite/9643064 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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