Question

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.

In addition to reply by Heikki, my solution follows: https://gist.github.com/759002

Feel free to use, if you find it useful

Was it helpful?

Solution

1) I'd say yes. (I didn't have luck with command line options either)

2) I got this example working with coffee script filter. File watching is done with fssm gem. It tracks changes to HAML files recursively in input folder and renders them to output folder with .html file extension.

watch.rb:

require 'rubygems'
require 'fssm'
require 'haml'
require 'coffee-haml-filter'
require 'active_support/core_ext/object/blank'

def render(input_dir, output_dir, relative)
  input_path = File.join(input_dir, relative)
  output_path = File.join(output_dir, relative).gsub(/\.haml$/, ".html")
  haml_engine = Haml::Engine.new(File.read(input_path))
  puts "Rendering #{input_path} -> #{output_path}"
  FileUtils.makedirs(File.dirname(output_path))
  File.open(output_path, 'w') do |file|
    file.write(haml_engine.render)
  end
end

input_dir = File.expand_path(ARGV.length > 0 ? ARGV.shift : '.')
output_dir = File.expand_path(ARGV.length > 0 ? ARGV.shift : input_dir)

puts "Input folder:  '#{input_dir}'"
puts "Output folder: '#{output_dir}'"

FSSM.monitor(input_dir, '**/*.haml') do
  create {|base, relative| render(input_dir, output_dir, relative) }
  update {|base, relative| render(input_dir, output_dir, relative) }
  delete {|base, relative|
    output_path = File.join(output_dir, relative).gsub(/\.haml$/, ".html")
    puts "Deleting #{output_path}"
    File.delete(output_path)
  }
end

Usage:

ruby watch.rb input_folder output_folder

OTHER TIPS

Good news: There's a new CoffeeScript Haml filter that's designed to work independently of Rails!

https://github.com/paulnicholson/coffee-filter

Definitely recommended over coffee-haml-filter, which was never really actively maintained.

The --require/-r option should work for loading the CoffeeScript filter. It doesn't in the most recent version, but that's a bug; it will be fixed in the next release.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top