Class: MarkdownComposer::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_composer/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources:, plan:, options: {}) ⇒ Executor

Returns a new instance of Executor.



7
8
9
10
11
12
13
# File 'lib/markdown_composer/executor.rb', line 7

def initialize(sources:, plan:, options: {})
  @sources = Array(sources).map { |source| Source.build(source) }
  @plan = plan
  @options = options
  @diagnostics = Diagnostics.new
  @stages = {}
end

Instance Attribute Details

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



5
6
7
# File 'lib/markdown_composer/executor.rb', line 5

def diagnostics
  @diagnostics
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/markdown_composer/executor.rb', line 5

def options
  @options
end

#planObject (readonly)

Returns the value of attribute plan.



5
6
7
# File 'lib/markdown_composer/executor.rb', line 5

def plan
  @plan
end

#sourcesObject (readonly)

Returns the value of attribute sources.



5
6
7
# File 'lib/markdown_composer/executor.rb', line 5

def sources
  @sources
end

#stagesObject (readonly)

Returns the value of attribute stages.



5
6
7
# File 'lib/markdown_composer/executor.rb', line 5

def stages
  @stages
end

Instance Method Details

#callObject



15
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
42
43
44
45
46
47
48
49
50
# File 'lib/markdown_composer/executor.rb', line 15

def call
  Validator.new(plan, sources: sources, options: options, diagnostics: diagnostics).call
  return result(CompositionBuffer.new(diagnostics: diagnostics), html: nil) if diagnostics.any_errors?

  buffer = CompositionBuffer.new(initial_buffer, diagnostics: diagnostics)
  return result(buffer, html: nil) if diagnostics.any_errors?

  indexes = {}
  previous_source = nil

  plan.steps.each_with_index do |step, index|
    row_path = "compose[#{index}]"
    source_ref = effective_source_ref(step["source"], previous_source)
    source_index = source_index_for(source_ref, buffer, indexes, path: "#{row_path}.source")
    next unless source_index

    resolver = SelectionResolver.new(index: source_index, options: options, diagnostics: diagnostics, path: "#{row_path}.select")
    units = if step["action"] == "remove_buffer_target"
      []
    else
      resolver.resolve_with_includes(step["select"], step["include"])
    end
    diagnostics.warn("selection.empty", "Step selected no content", path: row_path) if units.empty? && step["action"] != "remove_buffer_target"
    apply_action(buffer, step, units, row_path: row_path)
    stages["step_#{index + 1}"] = buffer.markdown if options.fetch(:stages, false)
    previous_source = source_ref
  end

  stages["composed"] = buffer.markdown if options.fetch(:stages, false)
  TransformRunner.new(buffer: buffer, transforms: plan.transforms, output: plan.output, options: options, diagnostics: diagnostics, stages: stages).call
  html = if plan.output == "html"
    rendered = MarkdownRenderer.to_html(buffer.markdown, diagnostics: diagnostics)
    MarkdownRenderer.apply_link_modes(rendered, html_link_modes, diagnostics: diagnostics)
  end
  result(buffer, html: html)
end