-
Notifications
You must be signed in to change notification settings - Fork 36
/
less_filter.rb
53 lines (48 loc) · 1.55 KB
/
less_filter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'rake-pipeline-web-filters/filter_with_dependencies'
module Rake::Pipeline::Web::Filters
# A filter that compiles input files written in Less
# to CSS using the Less compiler.
#
# Requires {https://rubygems.org/gems/less-js less-js}
#
# @example
# !!!ruby
# Rake::Pipeline.build do
# input "app/assets", "**/*.less"
# output "public"
#
# # Compile each less file with Less.js
# filter Rake::Pipeline::Web::Filters::LessFilter
# end
class LessFilter < Rake::Pipeline::Filter
include Rake::Pipeline::Web::Filters::FilterWithDependencies
# @return [Hash] a hash of options to pass to Less
# when compiling.
attr_reader :options
# @param [Proc] block a block to use as the Filter's
# {#output_name_generator}.
def initialize(options={}, context = nil, &block)
block ||= proc { |input| input.sub(/\.less$/, '.css') }
super(&block)
@options = options
end
# Implement the {#generate_output} method required by
# the {Filter} API. Attempts to compile each input file
# with LessJs.
#
# @param [Array<FileWrapper>] inputs an Array of
# {FileWrapper} objects representing the inputs to
# this filter.
# @param [FileWrapper] output a single {FileWrapper}
# object representing the output.
def generate_output(inputs, output)
parser = Less::Parser.new options
inputs.each do |input|
output.write parser.parse(input.read).to_css
end
end
def external_dependencies
[ 'less' ]
end
end
end