Class: Bridgetown::Resource::Transformer

Inherits:
Object
  • Object
show all
Includes:
Transformable
Defined in:
bridgetown-core/lib/bridgetown-core/resource/transformer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transformable

#transform_content, #transform_with_layout

Constructor Details

#initialize(resource) ⇒ Transformer

Returns a new instance of Transformer.



17
18
19
20
21
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 17

def initialize(resource)
  @resource = resource
  @site = resource.site
  @conversions = []
end

Instance Attribute Details

#conversionsArray<Hash> (readonly)

Returns:

  • (Array<Hash>)


9
10
11
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 9

def conversions
  @conversions
end

#resourceBridgetown::Resource::Base (readonly)



12
13
14
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 12

def resource
  @resource
end

#siteBridgetown::Site (readonly)

Returns:



15
16
17
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 15

def site
  @site
end

Instance Method Details

#execute_inline_ruby!Object



51
52
53
54
55
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 51

def execute_inline_ruby!
  return unless site.config.should_execute_inline_ruby?

  Bridgetown::Utils::RubyExec.search_data_for_ruby_code(resource)
end

#final_extString

Returns:

  • (String)


29
30
31
32
33
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 29

def final_ext
  output_ext # we always need this to get run

  permalink_ext || output_ext
end

#inspectObject



57
58
59
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 57

def inspect
  "#<#{self.class} Conversion Steps: #{conversions.length}>"
end

#output_extString

Returns:

  • (String)


24
25
26
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 24

def output_ext
  @output_ext ||= output_ext_from_converters
end

#process!Object

rubocop:disable Metrics/AbcSize



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'bridgetown-core/lib/bridgetown-core/resource/transformer.rb', line 35

def process! # rubocop:disable Metrics/AbcSize
  Bridgetown.logger.debug "Transforming:", resource.relative_path
  resource.around_hook :render do
    resource.content = transform_content(resource) do |converter, index, output|
      conversions[index] = {
        type: :content,
        converter:,
        output: Bridgetown.env.production? ? nil : output,
        output_ext: conversions[index]&.dig(:output_ext) ||
          converter.output_ext(resource.extname),
      }
    end
    resource.place_in_layout? ? place_into_layouts : resource.output = resource.content.dup
  end
end