Sha256: eb57016e90bd606613a9824a133b9ad20f34303e316af04c8c6887a3596deaed

Contents?: true

Size: 1.81 KB

Versions: 15

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module Nanoc::Int
  class CompilationContext
    attr_reader :site
    attr_reader :compiled_content_cache
    attr_reader :snapshot_repo

    def initialize(action_provider:, reps:, site:, compiled_content_cache:, snapshot_repo:)
      @action_provider = action_provider
      @reps = reps
      @site = site
      @compiled_content_cache = compiled_content_cache
      @snapshot_repo = snapshot_repo
    end

    def filter_name_and_args_for_layout(layout)
      seq = @action_provider.action_sequence_for(layout)
      if seq.nil? || seq.size != 1 || !seq[0].is_a?(Nanoc::Int::ProcessingActions::Filter)
        raise Nanoc::Int::Errors::UndefinedFilterForLayout.new(layout)
      end
      [seq[0].filter_name, seq[0].params]
    end

    def create_view_context(dependency_tracker)
      Nanoc::ViewContext.new(
        reps: @reps,
        items: @site.items,
        dependency_tracker: dependency_tracker,
        compilation_context: self,
        snapshot_repo: @snapshot_repo,
      )
    end

    def assigns_for(rep, dependency_tracker)
      last_content = @snapshot_repo.get(rep, :last)
      content_or_filename_assigns =
        if last_content.binary?
          { filename: last_content.filename }
        else
          { content: last_content.string }
        end

      view_context = create_view_context(dependency_tracker)

      content_or_filename_assigns.merge(
        item: Nanoc::ItemWithRepsView.new(rep.item, view_context),
        rep: Nanoc::ItemRepView.new(rep, view_context),
        item_rep: Nanoc::ItemRepView.new(rep, view_context),
        items: Nanoc::ItemCollectionWithRepsView.new(@site.items, view_context),
        layouts: Nanoc::LayoutCollectionView.new(@site.layouts, view_context),
        config: Nanoc::ConfigView.new(@site.config, view_context),
      )
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
nanoc-4.8.9 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.8 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.7 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.6 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.5 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.4 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.3 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.2 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.1 lib/nanoc/base/services/compilation_context.rb
nanoc-4.8.0 lib/nanoc/base/services/compilation_context.rb
nanoc-4.7.14 lib/nanoc/base/services/compilation_context.rb
nanoc-4.7.13 lib/nanoc/base/services/compilation_context.rb
nanoc-4.7.12 lib/nanoc/base/services/compilation_context.rb
nanoc-4.7.11 lib/nanoc/base/services/compilation_context.rb
nanoc-4.7.10 lib/nanoc/base/services/compilation_context.rb