Skip to content

Instantly share code, notes, and snippets.

@duckpuppy
Created September 26, 2011 15:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckpuppy/1242558 to your computer and use it in GitHub Desktop.
Save duckpuppy/1242558 to your computer and use it in GitHub Desktop.
require 'haml'
require 'rake/clean'
CLEAN.include( '_site' )
CLOBBER.include( '_cache' )
# We create a FileList from the existing Haml layouts in _layouts/haml
HAML = FileList['_layouts/haml/*.haml']
# We use the Haml FileList to create an HTML FileList, modifying the path of each
# entry to be in the _layouts folder instead of _layouts/haml
HTML = HAML.pathmap("_layouts/%n.html")
# Now we create a build_layouts task, using each entry in the HTML list as a dependency
HTML.each do |html|
task :build_layouts => html
end
# Because we built the HTML list from the HAML list, we can just add the HTML list
# to Rake's CLEAN list, and we still have the ability to add plain HTML layouts.
# Rake will only clean .html files which correspond to a .haml file.
CLEAN.include ( HTML )
# This rule will synthesize a task and dependency for each requested .html file
# which will attempt to create it using it's haml source.
rule '.html' => lambda { |html_file| find_haml(html_file) } do |t|
puts "Processing #{File.basename(t.source)}"
File.open(t.name, 'w') do |out|
out << Haml::Engine.new(File.read(t.source)).render
end
end
desc "Launch preview environment"
task :preview => [:build_layouts] do
system "ejekyll --auto --server"
end
desc "Build site"
task :build => [:build_layouts] do |task, args|
system "ejekyll"
end
task :default => :build
private
# This is modified from a bit of magic I found on the web for having different source and output
# folders for a rule.
# It returns the .haml file that corresponds to a given .html file. It's used by the .html rule
def find_haml(html_file)
HAML.find { |s| File.basename(s, ".haml") == File.basename(html_file, ".html") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment