Module: Nanoc::Helpers::Filtering

Includes:
Capturing
Defined in:
lib/nanoc/helpers/filtering.rb

Overview

Provides functionality for filtering parts of an item or a layout.

Instance Method Summary collapse

Methods included from Capturing

#capture, #content_for

Instance Method Details

#filter(filter_name, arguments = {}, &block) ⇒ void

This method returns an undefined value.

Filters the content in the given block and outputs it. This function does not return anything; instead, the filtered contents is directly appended to the output buffer (_erbout).

This function has been tested with ERB and Haml. Other filters may not work correctly.

Examples:

Running a filter on a part of an item or layout


<p>Lorem ipsum dolor sit amet...</p>
<% filter :rubypants do %>
  <p>Consectetur adipisicing elit...</p>
<% end %>

Parameters:

  • filter_name (Symbol)

    The name of the filter to run on the contents of the block

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

    Arguments to pass to the filter

Raises:

  • (Nanoc::Int::Errors::UnknownFilter)


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
54
55
56
# File 'lib/nanoc/helpers/filtering.rb', line 27

def filter(filter_name, arguments = {}, &block)
  # Capture block
  data = capture(&block)

  # Find filter
  klass = Nanoc::Filter.named(filter_name)
  raise Nanoc::Int::Errors::UnknownFilter.new(filter_name) if klass.nil?

  # Create filter
  assigns = {
    item: @item,
    rep: @rep,
    item_rep: @item_rep,
    items: @items,
    layouts: @layouts,
    config: @config,
    site: @site,
    content: @content,
  }
  filter = klass.new(assigns)

  # Filter captured data
  Nanoc::Int::NotificationCenter.post(:filtering_started, @item_rep.unwrap, filter_name)
  filtered_data = filter.setup_and_run(data, arguments)
  Nanoc::Int::NotificationCenter.post(:filtering_ended, @item_rep.unwrap, filter_name)

  # Append filtered data to buffer
  buffer = eval('_erbout', block.binding)
  buffer << filtered_data
end