Skip to content

Instantly share code, notes, and snippets.

@intinig
Created August 8, 2012 14:25
Show Gist options
  • Save intinig/3295427 to your computer and use it in GitHub Desktop.
Save intinig/3295427 to your computer and use it in GitHub Desktop.
HamlWatcher
namespace :haml do
desc "Watch the site's HAML templates and recompile them when they change"
task :watch do
require File.join(File.dirname(__FILE__), 'lib', 'haml_watcher')
HamlWatcher.watch ENV['SOURCE'], ENV['DEST']
end
end
require 'rubygems'
require 'listen'
require 'haml'
class HamlWatcher
class << self
def watch(source, dest)
@source = source
@dest = dest
refresh
puts ">>> HamlWatcher is watching for changes. Press Ctrl-C to Stop."
Listen.to(source, :filter => /\.haml$/) do |modified, added, removed|
modified.each do |m|
puts "M #{m}"
HamlWatcher.compile(m)
end
added.each do |a|
puts "A #{a}"
HamlWatcher.compile(a)
end
removed.each do |r|
puts "R #{r}"
HamlWatcher.remove(r)
end
end
end
def output_file(filename)
File.join(@dest, File.basename(filename).gsub(/\.haml$/,'.html'))
end
def remove(file)
output = output_file(file)
begin
File.delete output
puts "\033[0;31m remove\033[0m #{output}"
rescue
puts "\033[0;31m error\033[0m #{output}"
end
end
def compile(file)
output_file_name = output_file(file)
origin = File.open(file).read
result = Haml::Engine.new(origin).render
if result.empty?
puts "\033[0;31m error\033[0m #{file}"
else
# Write rendered HTML to file
color, action = File.exist?(output_file_name) ? [33, 'overwrite'] : [32, ' create']
puts "\033[0;#{color}m#{action}\033[0m #{output_file_name}"
File.open(output_file_name,'w') {|f| f.write(result)}
end
end
# Check that all haml templates have been rendered.
def refresh
Dir.glob('haml/**/*.haml').each do |file|
file.gsub!(/^haml\//, '')
compile(file) unless File.exist?(output_file(file))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment