Permalink
Browse files

Merge pull request #49 from kelonye/master

Jade filter
  • Loading branch information...
2 parents f28f580 + 682270d commit 1e8aede170297ad08b364b07955b43355718df19 @wycats committed Oct 17, 2012
@@ -17,6 +17,7 @@ module Filters
require "rake-pipeline-web-filters/neuter_filter"
require "rake-pipeline-web-filters/sass_filter"
require "rake-pipeline-web-filters/stylus_filter"
+require "rake-pipeline-web-filters/jade_filter"
require "rake-pipeline-web-filters/tilt_filter"
require "rake-pipeline-web-filters/coffee_script_filter"
require "rake-pipeline-web-filters/yui_javascript_filter"
@@ -33,6 +33,12 @@ def sass(*args, &block)
end
alias_method :scss, :sass
+ # Add a new {JadeFilter} to the pipeline.
+ # @see JadeFilter#initialize
+ def jade(*args, &block)
+ filter(Rake::Pipeline::Web::Filters::JadeFilter, *args, &block)
+ end
+
# Add a new {StylusFilter} to the pipeline.
# @see StylusFilter#initialize
def stylus(*args, &block)
@@ -0,0 +1,59 @@
+require 'rake-pipeline-web-filters/filter_with_dependencies'
+
+module Rake::Pipeline::Web::Filters
+ # A filter that compiles input files written in Jade
+ # to HTML using the Jade compiler
+ #
+ # Requires node and https://npmjs.org/package/jade by
+ # [sudo] npm install jade -g
+ #
+ # @example
+ # !!!ruby
+ # Rake::Pipeline.build do
+ # input "app/assets", "**/*.jade"
+ # output "public"
+ #
+ # # Compile each Jade file under the app/assets
+ # # directory.
+ # filter Rake::Pipeline::Web::Filters::JadeFilter
+ # end
+ # !!!ruby
+ # Rake::Pipeline.build do
+ # input "app/assets", "**/*.jade"
+ # output "public"
+ #
+ # jade :pretty
+ # end
+ class JadeFilter < Rake::Pipeline::Filter
+ include Rake::Pipeline::Web::Filters::FilterWithDependencies
+
+ # @return [Hash] a hash of options to pass to Less
+ # when compiling.
+ # @option option :prettify output
+ attr_reader :options
+
+ def initialize(options={}, &block)
+ block ||= proc { |input| input.sub(/\.(jade)$/, '.html') }
+ super(&block)
+ @options = options
+ end
+
+ def generate_output(inputs, output)
+
+ inputs.each do |input|
+ if options[:pretty]
+ output.write `echo "#{input.read}" | jade -P`
+ else
+ output.write `echo "#{input.read}" | jade`
+ end
+ end
+ end
+
+ private
+
+ def external_dependencies
+ [ ]
+ end
+
+ end
+end
@@ -30,4 +30,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "json"
gem.add_development_dependency "therubyracer"
gem.add_development_dependency "stylus"
+ gem.add_development_dependency "jade"
end
@@ -33,6 +33,13 @@ def filter
end
end
+ describe "#jade" do
+ it "creates a JadeCompiler" do
+ dsl.jade
+ filter.should be_kind_of(Rake::Pipeline::Web::Filters::JadeFilter)
+ end
+ end
+
describe "#tilt" do
it "creates a TiltFilter" do
dsl.tilt
@@ -0,0 +1,83 @@
+describe "JadeFilter" do
+ JadeFilter ||= Rake::Pipeline::Web::Filters::JadeFilter
+ MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
+
+ let(:jade_input) {"""
+!!! 5
+html
+ head
+ title Hello
+ body
+ h1 Hello
+"""
+}
+
+ let(:expected_html_output) {"""<!DOCTYPE html><html><head><title>Hello</title></head><body><h1>Hello</h1></body></html>"""
+}
+
+let(:expected_prettified_html_output) {"""\
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Hello</title>
+ </head>
+ <body>
+ <h1>Hello</h1>
+ </body>
+</html>"""
+}
+
+ 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=jade_input)
+ filter.file_wrapper_class = MemoryFileWrapper
+ filter.input_files = [input_file("index.jade", input)]
+ filter.output_root = "/path/to/output"
+ filter.rake_application = Rake::Application.new
+ filter
+ end
+
+ it "generates output" do
+ filter = setup_filter JadeFilter.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
+
+ it "prettifies output" do
+ filter = setup_filter JadeFilter.new :pretty => true
+
+ 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_prettified_html_output
+ file.encoding.should == "UTF-8"
+ end
+
+ describe "naming output files" do
+ it "translates .jade extensions to .html by default" do
+ filter = setup_filter JadeFilter.new
+ filter.output_files.first.path.should == "index.html"
+ end
+
+ it "accepts a block to customize output file names" do
+ filter = setup_filter(JadeFilter.new { |input| "hbs" })
+ filter.output_files.first.path.should == "hbs"
+ end
+ end
+end

0 comments on commit 1e8aede

Please sign in to comment.