Class: Rake::Pipeline::Web::Filters::LessFilter

Inherits:
Filter
  • Object
show all
Includes:
FilterWithDependencies
Defined in:
lib/rake-pipeline-web-filters/less_filter.rb

Overview

A filter that accepts a series of inputs and translates them using the Tilt template interface, which will attempt to guess which template language to use based on the input file extension.

Requires less-js

Examples:

Rake::Pipeline.build do
  input "app/assets", "**/*.less"
  output "public"

  # Compile each less file with Less.js
  filter Rake::Pipeline::Web::Filters::LessFilter
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, context = nil, &block) ⇒ LessFilter

Returns a new instance of LessFilter.

Parameters:

  • block (Proc)

    a block to use as the Filter’s #output_name_generator.



29
30
31
32
33
# File 'lib/rake-pipeline-web-filters/less_filter.rb', line 29

def initialize(options={}, context = nil, &block)
  block ||= proc { |input| input.sub(/\.less$/, '.css') }
  super(&block)
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns a hash of options to pass to Less when compiling.

Returns:

  • (Hash)

    a hash of options to pass to Less when compiling.



25
26
27
# File 'lib/rake-pipeline-web-filters/less_filter.rb', line 25

def options
  @options
end

Instance Method Details

#external_dependenciesObject



51
52
53
# File 'lib/rake-pipeline-web-filters/less_filter.rb', line 51

def external_dependencies
  [ 'less' ]
end

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. Attempts to compile each input file with LessJs.

Parameters:

  • inputs (Array<FileWrapper>)

    an Array of FileWrapper objects representing the inputs to this filter.

  • output (FileWrapper)

    a single FileWrapper object representing the output.



44
45
46
47
48
49
# File 'lib/rake-pipeline-web-filters/less_filter.rb', line 44

def generate_output(inputs, output)
  parser = Less::Parser.new options
  inputs.each do |input|
    output.write parser.parse(input.read).to_css
  end
end