Class: Nanoc::Int::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/base/services/executor.rb

Defined Under Namespace

Classes: OutputNotWrittenError

Instance Method Summary collapse

Constructor Details

#initialize(rep, compilation_context, dependency_tracker) ⇒ Executor

Returns a new instance of Executor



10
11
12
13
14
# File 'lib/nanoc/base/services/executor.rb', line 10

def initialize(rep, compilation_context, dependency_tracker)
  @rep = rep
  @compilation_context = compilation_context
  @dependency_tracker = dependency_tracker
end

Instance Method Details

#assigns_for(rep) ⇒ Object



82
83
84
# File 'lib/nanoc/base/services/executor.rb', line 82

def assigns_for(rep)
  @compilation_context.assigns_for(rep, @dependency_tracker)
end

#filter(filter_name, filter_args = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nanoc/base/services/executor.rb', line 16

def filter(filter_name, filter_args = {})
  filter = filter_for_filtering(@rep, filter_name)

  begin
    Nanoc::Int::NotificationCenter.post(:filtering_started, @rep, filter_name)

    # Run filter
    last = @rep.snapshot_contents[:last]
    source = @rep.binary? ? last.filename : last.string
    filter_args.freeze
    result = filter.setup_and_run(source, filter_args)
    @rep.snapshot_contents[:last] =
      if filter.class.to_binary?
        Nanoc::Int::BinaryContent.new(filter.output_filename).tap(&:freeze)
      else
        Nanoc::Int::TextualContent.new(result).tap(&:freeze)
      end

    # Check whether file was written
    if filter.class.to_binary? && !File.file?(filter.output_filename)
      raise OutputNotWrittenError.new(filter_name, filter.output_filename)
    end
  ensure
    Nanoc::Int::NotificationCenter.post(:filtering_ended, @rep, filter_name)
  end
end

#filter_for_filtering(rep, filter_name) ⇒ Object

Raises:

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


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/nanoc/base/services/executor.rb', line 104

def filter_for_filtering(rep, filter_name)
  klass = Nanoc::Filter.named(filter_name)
  raise Nanoc::Int::Errors::UnknownFilter.new(filter_name) if klass.nil?

  if klass.from_binary? && !rep.binary?
    raise Nanoc::Int::Errors::CannotUseBinaryFilter.new(rep, klass)
  elsif !klass.from_binary? && rep.binary?
    raise Nanoc::Int::Errors::CannotUseTextualFilter.new(rep, klass)
  end

  klass.new(assigns_for(rep))
end

#find_layout(arg) ⇒ Object

Raises:

  • (Nanoc::Int::Errors::UnknownLayout)


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/nanoc/base/services/executor.rb', line 90

def find_layout(arg)
  req_id = arg.__nanoc_cleaned_identifier
  layout = layouts.find { |l| l.identifier == req_id }
  return layout if layout

  if use_globs?
    pat = Nanoc::Int::Pattern.from(arg)
    layout = layouts.find { |l| pat.match?(l.identifier) }
    return layout if layout
  end

  raise Nanoc::Int::Errors::UnknownLayout.new(arg)
end

#layout(layout_identifier, extra_filter_args = nil) ⇒ Object

Raises:

  • (Nanoc::Int::Errors::CannotLayoutBinaryItem)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nanoc/base/services/executor.rb', line 43

def layout(layout_identifier, extra_filter_args = nil)
  layout = find_layout(layout_identifier)
  filter_name, filter_args = *@compilation_context.filter_name_and_args_for_layout(layout)
  if filter_name.nil?
    raise Nanoc::Int::Errors::Generic, "Cannot find rule for layout matching #{layout_identifier}"
  end
  filter_args = filter_args.merge(extra_filter_args || {})
  filter_args.freeze

  # Check whether item can be laid out
  raise Nanoc::Int::Errors::CannotLayoutBinaryItem.new(@rep) if @rep.binary?

  # Create filter
  klass = Nanoc::Filter.named(filter_name)
  raise Nanoc::Int::Errors::UnknownFilter.new(filter_name) if klass.nil?
  view_context = @compilation_context.create_view_context(@dependency_tracker)
  layout_view = Nanoc::LayoutView.new(layout, view_context)
  filter = klass.new(assigns_for(@rep).merge(layout: layout_view))

  # Visit
  @dependency_tracker.bounce(layout, raw_content: true)

  begin
    Nanoc::Int::NotificationCenter.post(:filtering_started, @rep, filter_name)

    # Layout
    content = layout.content
    arg = content.binary? ? content.filename : content.string
    res = filter.setup_and_run(arg, filter_args)
    @rep.snapshot_contents[:last] = Nanoc::Int::TextualContent.new(res).tap(&:freeze)
  ensure
    Nanoc::Int::NotificationCenter.post(:filtering_ended, @rep, filter_name)
  end
end

#layoutsObject



86
87
88
# File 'lib/nanoc/base/services/executor.rb', line 86

def layouts
  @compilation_context.site.layouts
end

#snapshot(snapshot_name) ⇒ Object



78
79
80
# File 'lib/nanoc/base/services/executor.rb', line 78

def snapshot(snapshot_name)
  @rep.snapshot_contents[snapshot_name] = @rep.snapshot_contents[:last]
end

#use_globs?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/nanoc/base/services/executor.rb', line 117

def use_globs?
  @compilation_context.site.config[:string_pattern_type] == 'glob'
end