Using HAML with custom filters
- by Guard
Hi everybody.
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
haml input.haml output.html.
And, what is great, there exist a project (one of many forks: https://github.com/aussiegeek/coffee-haml-filter) 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.
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:
haml.rb
require 'rubygems'
require 'active_support/core_ext/object/blank'
require 'haml'
require 'haml/filters/coffee'
template = ARGV.length > 0 ? File.read(ARGV.shift) : STDIN.read
haml_engine = Haml::Engine.new(template)
file = ARGV.length > 0 ? File.open(ARGV.shift, 'w') : STDOUT
file.write(haml_engine.render)
file.close
Which is quite straightforward, except of requires in the beginning.
Now, the questions are:
1) should I really use it, or is there another way to have on-demand HAML to HTML compilation with custom filters?
2) What about HAML watch mode? It's great and convenient. I can, of course, create a polling script in python that will watch the directory changes and call this .rb script, but it looks like a dirty solution.