Module: Bridgetown::Transformable

Included in:
GeneratedPage, Resource::Transformer, Slot
Defined in:
lib/bridgetown-core/concerns/transformable.rb

Instance Method Summary collapse

Instance Method Details

#transform_content(document, alternate_content: nil) {|converter, index, output| ... } ⇒ Object

Transforms an input document by running it through available converters (requires a converter method to be present on the including class)

Parameters:

Yield Parameters:

Returns:

  • String



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bridgetown-core/concerns/transformable.rb', line 14

def transform_content(document, alternate_content: nil)
  converters.each_with_index.inject(
    (alternate_content || document.content).to_s
  ) do |content, (converter, index)|
    output = if converter.method(:convert).arity == 1
               converter.convert content
             else
               converter.convert content, document
             end

    yield converter, index, output if block_given?

    output.html_safe
  rescue StandardError => e
    Bridgetown.logger.error "Conversion error:",
                            "#{converter.class} encountered an error while " \
                            "converting `#{document.relative_path}'"
    raise e
  end
end

#transform_with_layout(layout, output, document) {|converter, layout_output| ... } ⇒ Object

Transforms an input document by placing it within the specified layout

Parameters:

Yield Parameters:

Returns:

  • String



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bridgetown-core/concerns/transformable.rb', line 43

def transform_with_layout(layout, output, document)
  layout_converters = site.matched_converters_for_convertible(layout)
  layout_input = layout.content.dup

  layout_converters.inject(layout_input) do |content, converter|
    next(content) unless [2, -2].include?(converter.method(:convert).arity) # rubocop:disable Performance/CollectionLiteralInLoop

    layout.current_document = document
    layout.current_document_output = output
    layout_output = converter.convert content, layout

    yield converter, layout_output if block_given?

    layout_output
  rescue StandardError => e
    Bridgetown.logger.error "Conversion error:",
                            "#{converter.class} encountered an error while " \
                            "converting `#{document.relative_path}'"
    raise e
  end
end