Skip to content

Instantly share code, notes, and snippets.

@duckpuppy
Created October 24, 2011 03:34
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckpuppy/1308318 to your computer and use it in GitHub Desktop.
Save duckpuppy/1308318 to your computer and use it in GitHub Desktop.
Jekyll Haml and Compass extensions
begin
require 'compass'
require 'compass/exec'
rescue LoadError
require 'rubygems'
require 'compass'
require 'compass/exec'
end
module Jekyll
class Site
def compile_compass
begin
cmd_class = Compass::Commands['compile']
cmd = cmd_class.new(Dir.getwd, {})
cmd.execute
if cmd.successful?
cache_dir = File.join('_cache', 'css')
if File.exists?(cache_dir)
entries = Dir.chdir(cache_dir) { Dir['*'] }
entries.each do |f|
static_files << StaticFile.new(self, '_cache', 'css', f)
end
end
end
cmd.successful?
rescue CompassErrorException => e
end
end
end
class CompassErrorException < Exception
end
AOP.after(Site, :generate) do |site_instance, args|
puts "Processing Sass with Compass"
site_instance.compile_compass
end
end
# Original idea came from https://github.com/codegram/haml-jekyll-extension
# and http://rfelix.com/2010/01/19/jekyll-extensions-minus-equal-pain/
#
# This extension allows the use of Haml layout templates in Jekyll. It has
# the benefit of not requiring the use to generate the html files in the
# source folder. Instead, we generate the HTML into the _cache folder and
# then load the Layout objects from those files. This does affect any other
# plugin or extension that directly reads the layout files rather than
# referencing the layout objects from the Jekyll site instance.
begin
require 'haml'
rescue LoadError
require 'rubygems'
require 'haml'
end
module Jekyll
class Site
def compile_haml_layouts
Dir.glob(File.join(self.source, "_layouts", "*.haml")).each do |f|
begin
origin = File.open(f).read
result = Haml::Engine.new(origin).render
raise HamlErrorException.new if result.empty?
puts "Rendering #{f}"
output_file_name = File.join("_cache", "_layouts", File.basename(f, '.haml') + '.html')
FileUtils.mkpath(File.dirname(output_file_name))
File.open(output_file_name,'w') {|f| f.write(result)} if !File.exists?(output_file_name) or (File.exists?(output_file_name) and result != File.read(output_file_name))
rescue HamlErrorException => e
end
end
end
end
class HamlErrorException < Exception
end
AOP.around(Site, :read_layouts) do |site_instance, args, proceed, abort|
site_instance.compile_haml_layouts
old_source = site_instance.source
site_instance.source = '_cache'
result = proceed.call
site_instance.source = old_source
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment