Sha256: db63154016e956c83493e04d76c52408c5e66ee4881f3247b533f8000631b572

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module Nanoc2::Helpers

  # Nanoc2::Helpers::Filtering provides a filter method, which allows parts of
  # a page to be filtered.
  #
  # For example, the following piece of code only runs the "rubypants" filter
  # on the second paragraph:
  #
  #   <p>Lorem ipsum dolor sit amet...</p>
  #   <% filter :rubypants do %>
  #     <p>Consectetur adipisicing elit...</p>
  #   <% end %>
  #
  # This helper likely only works with ERB (and perhaps Erubis).
  #
  # To activate this helper, +include+ it, like this:
  #
  #   include Nanoc2::Helpers::Filtering
  module Filtering

    # Filters the content in the given block and outputs it.
    def filter(filter_name, &block)
      # Capture block
      data = capture(&block)

      # Find filter
      filter = Nanoc2::Filter.named(filter_name).new(@_obj_rep)

      # Filter captured data
      filtered_data = filter.run(data)

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

  private

    def capture(*args, &block)
      buffer = eval('_erbout', block.binding)

      pos = buffer.length
      block.call(*args)

      data = buffer[pos..-1]

      buffer[pos..-1] = ''

      data
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nanoc2-2.2.3 lib/nanoc2/helpers/filtering.rb