title: Using HAML with custom filters
body: <p>I feel quite excited about HAML and CoffeeScript and am working on tutorial showing how to use them in non-Rails environment.
  So, haml has easy to use command-line utility</p>
  
  <pre><code>haml input.haml output.html.
  </code></pre>
  
  <p>And, what is great, there exist a project (one of many forks: <a href="https://github.com/aussiegeek/coffee-haml-filter" rel="nofollow">https://github.com/aussiegeek/coffee-haml-filter</a>) aimed at providing custom filter that converts CoffeeScript into JS inside of HAML files.
  Unfortunately (or am I missing something?) haml doesn't allow specifying custom filters on the command line or with some configuration file. </p>
  
  <p>I (not being a Ruby fan or even knowing it enough) managed to solve it (based on some clever suggestion somewhere on SO) with this helper script:
  <code>haml.rb</code></p>
  
  <pre><code>require 'rubygems'
  require 'active_support/core_ext/object/blank'
  require 'haml'
  require 'haml/filters/coffee'
  
  template = ARGV.length &gt; 0 ? File.read(ARGV.shift) : STDIN.read
  haml_engine = Haml::Engine.new(template)
  file = ARGV.length &gt; 0 ? File.open(ARGV.shift, 'w') : STDOUT
  file.write(haml_engine.render)
  file.close
  </code></pre>
  
  <p>Which is quite straightforward, except of requires in the beginning.</p>
  
  <p>Now, the questions are:</p>
  
  <p>1) should I really use it, or is there another way to have on-demand HAML to HTML compilation with custom filters?</p>
  
  <p>2) What about HAML watch mode? It's great and convenient. I can, of course, create a polling script in <code>python</code> that will watch the directory changes and call this <code>.rb</code> script, but it looks like a dirty solution.</p>
  
  <p><strong>In addition to reply by Heikki</strong>, my solution follows:
  <a href="https://gist.github.com/759002" rel="nofollow">https://gist.github.com/759002</a></p>
  
  <p>Feel free to use, if you find it useful</p>