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 ? '' : ('</'+(_[:_]||:div).to_s+'>'))
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 ['<!DOCTYPE html>',
{_: :html,
c: [{_: :head,
c: ['<meta charset="utf-8" />',
{_: :link, href:'/css/misc/favicon.ico', rel: :icon}]},
{_: :body, c: {_: :h1, c: 'hello woyld'}}]}]
=> "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><link
href='/css/misc/favicon.ico' rel='icon'></head><body><h1>hello
woyld</h1></body></html>"
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
<li>dog</li>
<li>piglets</li>
<li>duck</li>
<li>geese</li>
<li>galago</li>
=> nil
_______________________________________________
Camping-list mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/camping-list