Permalink
Browse files

Support for Stylus

  • Loading branch information...
1 parent 314e07e commit 8c7f2ced60c1a3109b5b9ddf3f0c9d278e912abe @ghempton ghempton committed with ghempton May 13, 2012
View
@@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
+node_modules
+.project
@@ -16,6 +16,7 @@ module Filters
require "rake-pipeline-web-filters/minispade_filter"
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/tilt_filter"
require "rake-pipeline-web-filters/coffee_script_filter"
require "rake-pipeline-web-filters/yui_javascript_filter"
@@ -32,6 +32,12 @@ def sass(*args, &block)
filter(Rake::Pipeline::Web::Filters::SassFilter, *args, &block)
end
alias_method :scss, :sass
+
+ # Add a new {StylusFilter} to the pipeline.
+ # @see StylusFilter#initialize
+ def stylus(*args, &block)
+ filter(Rake::Pipeline::Web::Filters::StylusFilter, *args, &block)
+ end
# Add a new {TiltFilter} to the pipeline.
# @see TiltFilter#initialize
@@ -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 Stylus
+ # to CSS using the Stylus compiler
+ #
+ # Requires http://rubygems.org/gems/stylus
+ #
+ # You will need the `node` command on your runtime
+ # for this to work. See https://github.com/lucasmazza/ruby-stylus
+ # for more information.
+ #
+ # @example
+ # !!!ruby
+ # Rake::Pipeline.build do
+ # input "app/assets", "**/*.styl"
+ # output "public"
+ #
+ # # Compile each Stylus file under the app/assets
+ # # directory.
+ # filter Rake::Pipeline::Web::Filters::StylusFilter
+ # end
+ class StylusFilter < Rake::Pipeline::Filter
+ include Rake::Pipeline::Web::Filters::FilterWithDependencies
+
+ attr_reader :options
+
+ # @param [Hash] options options to pass to Stylus
+ # @option options [Array] :use Plugins to import from Node
+ # @option options [Boolean] :debug Output debug info
+ # @param [Proc] block a block to use as the Filter's
+ # {#output_name_generator}.
+ def initialize(options={}, &block)
+ block ||= proc { |input| input.sub(/\.(styl)$/, '.css') }
+ super(&block)
+ @options = options
+ end
+
+ def generate_output(inputs, output)
+ options.each do |key, value|
+ if key == :use
+ Stylus.send key, *value
+ next
+ end
+ Stylus.send "#{key}=", value
+ end
+ inputs.each do |input|
+ output.write Stylus.compile(input.read)
+ end
+ end
+
+ private
+
+ def external_dependencies
+ [ 'stylus' ]
+ end
+
+ end
+end
@@ -29,4 +29,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "less"
gem.add_development_dependency "json"
gem.add_development_dependency "therubyracer"
+ gem.add_development_dependency "stylus"
end
View
@@ -25,6 +25,13 @@ def filter
filter.should be_kind_of(Rake::Pipeline::Web::Filters::SassFilter)
end
end
+
+ describe "#stylus" do
+ it "creates a StylusCompiler" do
+ dsl.stylus
+ filter.should be_kind_of(Rake::Pipeline::Web::Filters::StylusFilter)
+ end
+ end
describe "#tilt" do
it "creates a TiltFilter" do
View
@@ -0,0 +1,69 @@
+describe "StylusFilter" do
+ StylusFilter ||= Rake::Pipeline::Web::Filters::StylusFilter
+ MemoryFileWrapper ||= Rake::Pipeline::SpecHelpers::MemoryFileWrapper
+
+ let(:styl_input) { <<-STYLUS }
+border-radius()
+ -webkit-border-radius arguments
+ -moz-border-radius arguments
+ border-radius arguments
+
+body
+ font 12px Helvetica, Arial, sans-serif
+
+a.button
+ border-radius 5px
+STYLUS
+
+ let(:expected_css_output) { <<-CSS }
+body {
+ font: 12px Helvetica, Arial, sans-serif;
+}
+a.button {
+ -webkit-border-radius: 5px;
+ -moz-border-radius: 5px;
+ border-radius: 5px;
+}
+CSS
+
+ 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=styl_input)
+ filter.file_wrapper_class = MemoryFileWrapper
+ filter.input_files = [input_file("border.styl", input)]
+ filter.output_root = "/path/to/output"
+ filter.rake_application = Rake::Application.new
+ filter
+ end
+
+ it "generates output" do
+ filter = setup_filter StylusFilter.new
+
+ filter.output_files.should == [output_file("border.css")]
+
+ tasks = filter.generate_rake_tasks
+ tasks.each(&:invoke)
+
+ file = MemoryFileWrapper.files["/path/to/output/border.css"]
+ file.body.should == expected_css_output
+ file.encoding.should == "UTF-8"
+ end
+
+ describe "naming output files" do
+ it "translates .styl extensions to .css by default" do
+ filter = setup_filter StylusFilter.new
+ filter.output_files.first.path.should == "border.css"
+ end
+
+ it "accepts a block to customize output file names" do
+ filter = setup_filter(StylusFilter.new { |input| "octopus" })
+ filter.output_files.first.path.should == "octopus"
+ end
+ end
+end

0 comments on commit 8c7f2ce

Please sign in to comment.