Commits

Steven! Ragnarök committed f7e8410

A markaby-like HTML generator.

Comments (0)

Files changed (1)

+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