Hydrogen Templates
Par Carmen Lotek le vendredi 30 juillet 2010, 00:09 - Lien permanent
def H _
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]) : '')+
'</'+(_[:_]||:div).to_s+'>'
when Array then
_.map{|n|H n}.join\
else
_.to_s
end
end
this is sort of an anti-template-language. some allow you to write pretty code - but after a few rounds of learning all the syntactic subtleties of another language, scrounge around for tricks to do some subset of control structures, trying to get your editor add-on properly highlighting the embedded code-within-code, inventing caching schemes to avoid running an almost-turing-complete language-implemented-in-slow-scripting-language-of-choice, familiarizing yourself with said mountain of code so you can create a patch when something breaks, i wrote this
the example here is rewriting BBC News' content to avoid scanning all over the page with a nonsensical hodpodge of random squares & whitespace, missing article descriptions, massive-icon sidebars and so on. i used element to do on-the-fly feedparsing and model generation..
Hash literals are elements, if you don't provide a tagname, it defaults to divs, attributes work as expected Arrays can contain Arrays, Hashes, or strings.. Strings allow you to drop down to retro HTML if youre in the mood, i did here:

not quite as pretty as Haml, but definitely blazing fast and trivial to port to new languages (with at least Record/Hash/Table and Array constructors) in a few minutes

