Class: Rake::Pipeline::Web::Filters::TiltFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/rake-pipeline-web-filters/tilt_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.

Examples:

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

  # Compile each SCSS file using Tilt, replacing the
  # scss extension with css.
  filter(Rake::Pipeline::Web::Filters::TiltFilter) do |input|
    input.sub(/\.scss$/, 'css')
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ TiltFilter

Returns a new instance of TiltFilter.

Parameters:

  • options (Hash) (defaults to: {})

    options to pass to the Tilt template class constructor.

  • block (Proc)

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



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

def initialize(options={}, &block)
  super(&block)
  @options = options
end

Instance Attribute Details

#optionsHash (readonly)

Returns a hash of options to pass to Tilt when rendering.

Returns:

  • (Hash)

    a hash of options to pass to Tilt when rendering.



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

def options
  @options
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. Attempts to compile each input file with Tilt, passing the file through unchanged if Tilt can’t find a template class for the file.

Parameters:

  • inputs (Array<FileWrapper>)

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

  • output (FileWrapper)

    a single FileWrapper object representing the output.



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rake-pipeline-web-filters/tilt_filter.rb', line 45

def generate_output(inputs, output)
  inputs.each do |input|
    out = if (template_class = Tilt[input.path])
      template_class.new(nil, 1, options) { |t| input.read }.render
    else
      input.read
    end

    output.write out
  end
end