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

Inherits:
Filter
  • Object
show all
Includes:
FilterWithDependencies
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.

Requires / tilt

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 = {}, context = nil, &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.



38
39
40
41
42
# File 'lib/rake-pipeline-web-filters/tilt_filter.rb', line 38

def initialize(options={}, context = nil, &block)
  super(&block)
  @options = options
  @context = context || Object.new
end

Instance Attribute Details

#contextObject (readonly)

Returns an object to use as the rendering context.

Returns:

  • (Object)

    an object to use as the rendering context.



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

def context
  @context
end

#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.



28
29
30
# File 'lib/rake-pipeline-web-filters/tilt_filter.rb', line 28

def options
  @options
end

Instance Method Details

#external_dependenciesObject



66
67
68
# File 'lib/rake-pipeline-web-filters/tilt_filter.rb', line 66

def external_dependencies
  [ 'tilt' ]
end

#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.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rake-pipeline-web-filters/tilt_filter.rb', line 54

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(context)
    else
      input.read
    end

    output.write out
  end
end