Skip to content

Instantly share code, notes, and snippets.

@gcorreaq
Created February 23, 2012 19:11
Show Gist options
  • Save gcorreaq/1894431 to your computer and use it in GitHub Desktop.
Save gcorreaq/1894431 to your computer and use it in GitHub Desktop.
Watch and compile all haml files on a directory, like sass --watch
#!/usr/bin/env ruby
# Script to watch a directory for any changes to a haml file
# and compile it.
#
# USAGE: ruby haml_watch.rb <directory_to_watch> [output_directory]
#
# Blake Smith / http://blakesmith.github.com/2010/09/05/haml-directory-watcher.html
#
# Fork by Gonzalo Correa (https://github.com/gcorreaq)
#
require 'rubygems'
require 'listen'
# By default, the input and output folders are
# the working directory
from_path = ARGV[0] || "."
to_path = ARGV[1] || "."
puts "Watching #{from_path}"
Listen.to(from_path, :filter => /\.haml$/) do |modified, added, removed|
modified.each do |input|
output = File.join(to_path, File.basename(input).gsub('.haml', '.html'))
command = "haml #{input} #{output} --format html5 --double-quote-attributes --no-escape-attrs"
%x{#{command}}
puts <<-eos
HTML generated @ #{Time.now.strftime("%F %T")}:
- from: #{input}
- to: #{output}
eos
end
end
@gcorreaq
Copy link
Author

gcorreaq commented Oct 2, 2012

Now the script uses Listen (https://github.com/guard/listen) for watching for changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment