Created
August 31, 2011 21:08
-
-
Save nuclearsandwich/1184724 to your computer and use it in GitHub Desktop.
Hacktastic HTML DSL
This file contains 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
# Only working example so far HTML.new { html { title { "Hello" } } } | |
# Soon attributes will be working. | |
class HTML | |
def initialize &content | |
@html = "" | |
instance_eval &content | |
end | |
def method_missing tag, *attrs, &content | |
@html << tag_with_attributes(tag, attrs.first || {}) | |
@html << instance_eval(&content) if content | |
@html << %|</#{tag}>| | |
end | |
def tag_with_attributes tag, attrs=Hash.new | |
%|<#{tag}| << | |
((attrs.empty? and '') or (' ' << | |
attrs.map{|attr, val| %|#{attr}="#{val}"|} * ' ')) << '>' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment