Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ spec/reports | |
test/tmp | ||
test/version_tmp | ||
tmp | ||
node_modules | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |