Skip to content

Instantly share code, notes, and snippets.

@oriolgual
Created April 4, 2011 07:57
Show Gist options
  • Save oriolgual/901265 to your computer and use it in GitHub Desktop.
Save oriolgual/901265 to your computer and use it in GitHub Desktop.
Jekyll extension to easly use HAML instead of plain HTML

HAML Jekyll extension

Auto-generates html and css files for your layouts in HAML/SASS (you can still have your liquid syntax)

This extension uses jekyll_ext, which allows you to extend the Jekyll static blog generator without forking and modifying it’s codebase.
With this code, not only do your extensions live in your blog directory, but they can also be shared and reutilized.

Use this extension with jekyll_ext by just cloning this repo into the extensions dir of your blog: git clone git://github.com/codegram/haml_jekyllextension.git _extensions

More information about jekyll_ext can be found here: Jekyll Extensions -= Pain

haml_folder: **/*.haml
sass_folder: **/*.sass
require 'rubygems'
require 'haml'
require 'sass'
module Jekyll
class Site
def haml2html
haml_folder = self.config['haml_folder'] || '**/*.haml'
compile_haml(["*.haml", haml_folder], /\.haml$/,'.html')
end
def sass2css
sass_folder = self.config['sass_folder'] || '**/*.sass'
compile_sass(["*.sass", sass_folder], /\.sass$/,'.css')
end
private
def compile_haml(files, input_regex, output_extension)
Dir.glob(files).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 = f.gsub!(input_regex,output_extension)
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
def compile_sass(files, input_regex, output_extension)
Dir.glob(files).each do |f|
begin
origin = File.open(f).read
result = Sass::Engine.new(origin).render
raise HamlErrorException.new if result.empty?
puts "Rendering #{f}"
output_file_name = f.gsub!(input_regex,output_extension)
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.before(Site, :render) do |site_instance, result, args|
site_instance.haml2html
end
AOP.before(Site, :render) do |site_instance, result, args|
site_instance.sass2css
end
AOP.around(Site, :filter_entries) do |site_instance, args, proceed, abort|
result = proceed.call
result.reject{ |entry| entry.match(/\.haml$/) || entry.match(/\.sass$/) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment