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:

Screenshot of the TextMate bundle editor

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.

3 Responses to “Convert HTML to Haml within Textmate”

  1. Frederik replied on December 19th, 2009 at 11:45 AM:

    Great tip! But actually I think you need to append –stdin, and –rhtml if you’re using RHTML files:

    echo $TMSELECTEDTEXT | html2haml –stdin –rhtml

  2. Smash replied on January 22nd, 2010 at 03:56 AM:

    I have this problem after conversion:

    undefined method inject for {“class”=>”footer”}:Hpricot::Attributes

    Why? Thanks :D

  3. Great stuff - Thanks!

Leave a Reply