Sha256: 11c714202315d75e81d0d33e91bcea5cb1cb54fa5823e06923325f867f2670f1

Contents?: true

Size: 1.06 KB

Versions: 14

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Nanoc::Filters
  # @api private
  class ERB < Nanoc::Filter
    identifier :erb

    requires 'erb'

    # Runs the content through [ERB](http://ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html).
    #
    # @param [String] content The content to filter
    #
    # @option params [Integer] :safe_level (nil) The safe level (`$SAFE`) to
    #   use while running this filter
    #
    # @option params [String] :trim_mode (nil) The trim mode to use
    #
    # @return [String] The filtered content
    def run(content, params = {})
      # Add locals
      assigns.merge!(params[:locals] || {})

      # Create context
      context = ::Nanoc::Core::Context.new(assigns)

      # Get binding
      proc = assigns[:content] ? -> { assigns[:content] } : nil
      assigns_binding = context.get_binding(&proc)

      # Get result
      safe_level = params[:safe_level]
      trim_mode = params[:trim_mode]
      erb = ::ERB.new(content, safe_level, trim_mode)
      erb.filename = filename
      erb.result(assigns_binding)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
nanoc-4.11.14 lib/nanoc/filters/erb.rb
nanoc-4.11.13 lib/nanoc/filters/erb.rb
nanoc-4.11.12 lib/nanoc/filters/erb.rb
nanoc-4.11.11 lib/nanoc/filters/erb.rb
nanoc-4.11.10 lib/nanoc/filters/erb.rb
nanoc-4.11.9 lib/nanoc/filters/erb.rb
nanoc-4.11.8 lib/nanoc/filters/erb.rb
nanoc-4.11.7 lib/nanoc/filters/erb.rb
nanoc-4.11.6 lib/nanoc/filters/erb.rb
nanoc-4.11.5 lib/nanoc/filters/erb.rb
nanoc-4.11.4 lib/nanoc/filters/erb.rb
nanoc-4.11.3 lib/nanoc/filters/erb.rb
nanoc-4.11.2 lib/nanoc/filters/erb.rb
nanoc-4.11.1 lib/nanoc/filters/erb.rb