Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adds a HAML filter #63

Closed
wants to merge 1 commit into from

3 participants

@curthasselschwert

This adds a simple HAML filter with the ability to pass options down to the HAML compiler. I needed it for another project and thought it might be useful to other people.

@ahx

You can use the included Web::Filters::TiltFilter to compile Haml. Or just use filter haml.
Don't forget to require 'haml' beforehand.

@curthasselschwert

Great! Thank you, I wasn't aware of that.

@hjr3 hjr3 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 13, 2013
  1. @curthasselschwert

    adds a haml filter

    curthasselschwert authored
This page is out of date. Refresh to see the latest.
View
1  lib/rake-pipeline-web-filters.rb
@@ -27,5 +27,6 @@ module Filters
require "rake-pipeline-web-filters/chained_filter"
require "rake-pipeline-web-filters/less_filter"
require "rake-pipeline-web-filters/handlebars_filter"
+require "rake-pipeline-web-filters/haml_filter"
require "rake-pipeline-web-filters/iife_filter"
require "rake-pipeline-web-filters/helpers"
View
29 lib/rake-pipeline-web-filters/haml_filter.rb
@@ -0,0 +1,29 @@
+require 'rake-pipeline-web-filters/filter_with_dependencies'
+
+module Rake::Pipeline::Web::Filters
+
+ class HamlFilter < Rake::Pipeline::Filter
+ include Rake::Pipeline::Web::Filters::FilterWithDependencies
+
+ attr_reader :options
+
+ def initialize(options={}, &block)
+ block ||= proc { |input| input.sub(/\.haml$/, '.html') }
+ super(&block)
+ @options = options
+ end
+
+ def generate_output(inputs, output)
+ inputs.each do |input|
+ output.write Haml::Engine.new(input.read, options).render
+ end
+ end
+
+ private
+
+ def external_dependencies
+ [ 'haml' ]
+ end
+ end
+
+end
View
1  rake-pipeline-web-filters.gemspec
@@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "rspec"
gem.add_development_dependency "tilt"
gem.add_development_dependency "sass"
+ gem.add_development_dependency "haml"
gem.add_development_dependency "compass"
gem.add_development_dependency "coffee-script"
gem.add_development_dependency "redcarpet", '~> 2.0'
View
84 spec/haml_filter_spec.rb
@@ -0,0 +1,84 @@
+describe "HamlFilter" do
+ HamlFilter ||= Rake::Pipeline::Web::Filters::HamlFilter
+ MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
+ MemoryManifest ||= Rake::Pipeline::SpecHelpers::MemoryManifest
+
+ let(:haml_input) { <<-HAML }
+%h1 Title
+%p body
+HAML
+
+ let(:haml_html5_input) { <<-HAML }
+!!!
+%html
+ %body
+ %h1 Title
+ %p body
+HAML
+
+ let(:expected_html_output) { <<-HAML }
+<h1>Title</h1>
+<p>body</p>
+HAML
+
+ let(:expected_html5_output) { <<-HAML }
+<!DOCTYPE html>
+<html>
+ <body>
+ <h1>Title</h1>
+ <p>body</p>
+ </body>
+</html>
+HAML
+
+ def input_file(name, content)
+ MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content)
+ end
+
+ def output_file(name)
+ MemoryFileWrapper.new("/path/to/output", name, "UTF-8")
+ end
+
+ def setup_filter(filter, input_files=nil)
+ filter.file_wrapper_class = MemoryFileWrapper
+ filter.manifest = MemoryManifest.new
+ filter.last_manifest = MemoryManifest.new
+ filter.input_files = input_files || [input_file("index.haml", haml_input)]
+ filter.output_root = "/path/to/output"
+ filter.rake_application = Rake::Application.new
+ filter
+ end
+
+ it "generates output" do
+ filter = setup_filter HamlFilter.new
+ filter.output_files.should == [output_file("index.html")]
+
+ tasks = filter.generate_rake_tasks
+ tasks.each(&:invoke)
+
+ file = MemoryFileWrapper.files["/path/to/output/index.html"]
+ file.body.should == expected_html_output
+ file.encoding.should == "UTF-8"
+ end
+
+ describe "naming output files" do
+ it "translates .haml extensions to .html by default" do
+ filter = setup_filter HamlFilter.new
+ filter.output_files.first.path.should == "index.html"
+ end
+
+ it "accepts a block to customize output file names" do
+ filter = setup_filter(HamlFilter.new { |input| "octopus" })
+ filter.output_files.first.path.should == "octopus"
+ end
+ end
+
+ it "accepts options to pass to the HAML compiler" do
+ input_files = [input_file("index.haml", haml_html5_input)]
+ filter = setup_filter(HamlFilter.new(:format => :html5), input_files)
+ tasks = filter.generate_rake_tasks
+ tasks.each(&:invoke)
+ file = MemoryFileWrapper.files["/path/to/output/index.html"]
+ file.body.should == expected_html5_output
+ end
+end
Something went wrong with that request. Please try again.