i've been using this
def H _
# Ruby object-literal syntax as HTML constructors
case _
when Hash then
'<'+(_[:_]||:div).to_s+(_.keys-[:_,:c]).map{|a|
' '+a.to_s+'='+"'"+
_[a].to_s.hsub({"'"=>'%27',
'>'=>'%3E',
'<'=>'%3C'})+"'"}.join+'>'+
(_[:c] ? (H _[:c]) : '')+
(_[:_] == :link ? '' : (''))
when Array then
_.map{|n|H n}.join
else
_.to_s if _
end
end
class String
def hsub h; map{|e|h[e]||e} end
end
irb(main):001:0> H ['',
{_: :html,
c: [{_: :head,
c: ['',
{_: :link, href:'/css/misc/favicon.ico', rel: :icon}]},
{_: :body, c: {_: :h1, c: 'hello woyld'}}]}]
=> "hello
woyld"
linebreaks can be nice for view-source (also, long lines tend to slow down
text-editors to a crawl, at least emacs on mid 00s thinkpads)
class Array
def intersperse i
inject([]){|a,b|a << b << i}[0..-2]
end
def cr; intersperse "\n" end
end
irb(main):017:0> puts H %w{dog piglets duck geese galago}.map{|a|{_: :li, c:
a}}.cr
dog
piglets
duck
geese
galago
=> nil
___
Camping-list mailing list
Camping-list@rubyforge.org
http://rubyforge.org/mailman/listinfo/camping-list