Convert HTML to Haml within Textmate
Posted November 13th, 2009; 3 comments.
I have been integrating a lot of well built HTML templates into Rails applications this week. I’ve found Haml to be the best templating language, and I like to convert preexisting templates to use it as I add them to a project.
The Haml gem installs a command line executable that performs this conversion, html2haml. The program takes whatever is passed as standard input, performs the conversion, and writes the result to standard out. For a while I was using command this in combination with the pbcopy/pbpaste commands (I work on a Mac) to apply this to the content of the clipboard:
pbpaste | html2haml | pbcopy
This worked, but the extra copying and pasting was annoying. So I made a TextMate command to eliminate the tedium:

The actual command is this (it’s a bit hard to see above):
echo $TM_SELECTED_TEXT | html2haml
You could add a key binding as well, but so far I haven’t bothered to.
Great tip! But actually I think you need to append –stdin, and –rhtml if you’re using RHTML files:
echo $TMSELECTEDTEXT | html2haml –stdin –rhtml
I have this problem after conversion:
undefined method inject for {“class”=>”footer”}:Hpricot::Attributes
Why? Thanks :D
Great stuff - Thanks!