Skip to content

Instantly share code, notes, and snippets.

@cahrehn
Forked from fin/haml_watch.rb
Created October 17, 2012 19:18
Show Gist options
  • Save cahrehn/3907526 to your computer and use it in GitHub Desktop.
Save cahrehn/3907526 to your computer and use it in GitHub Desktop.
haml directory watcher
#!/usr/bin/ruby1.8
# Script to watch a directory for any changes to a haml file and compile it.
#
# Changes to a file named like _partial.haml will compile any haml files that
# reference the partial i.e. any that contain
# =Haml::Engine.new(File.read("/path/to/_partial.haml")).render
#
# USAGE: ruby haml_watch.rb <directory_to_watch>
#
# Original by Blake Smith / http://blakesmith.github.com/2010/09/05/haml-directory-watcher.html
# Modifications by fin / http://fin.io
# Support for partials workflow by cahrehn
#
require 'rubygems'
require 'fssm'
require 'haml'
def rebuild_haml(base, relative)
input_file = File.join(base, relative)
input = File.read(input_file)
engine = Haml::Engine.new(input)
output_file = File.join(base, relative.gsub('.haml', '.html'))
output = open(output_file, 'w')
output.write(engine.render)
output.close
puts "Rendered #{relative} to #{relative.gsub('.haml', '.html')}"
end
def rebuild_partials(base, relative)
Dir.foreach(base) do |item|
next if item == '.' or item == '..'
if item.match(/haml$/) and
File.readlines("#{base}/#{item}").grep(/#{relative}/).size > 0 then
rebuild_haml(base, item)
end
end
end
directory = ARGV.first
FSSM.monitor(directory, '**/*.haml') do
update do |base, relative|
if relative.match(/^_/) then
rebuild_haml(base, relative)
rebuild_partials(base, relative)
else
rebuild_haml(base, relative)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment