Class: Bridgetown::Converter

Inherits:
Plugin
  • Object
show all
Defined in:
bridgetown-core/lib/bridgetown-core/converter.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Prioritizable

#<=>, included

Constructor Details

#initialize(config = {}) ⇒ Converter

Initialize the converter.

Returns an initialized Converter.



47
48
49
50
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 47

def initialize(config = {})
  super
  @config = config
end

Class Attribute Details

.extname_listObject

Returns the value of attribute extname_list.



6
7
8
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 6

def extname_list
  @extname_list
end

Class Method Details

.helper_delimiters(delimiters = nil) ⇒ Object

Set or return the delimiters used for helper calls in template code (e.g. ["<%=", "%>"] for ERB). This is purely informational for the framework’s benefit, not used within a rendering pipeline.

Parameters:

  • delimiters (Array<String>) (defaults to: nil)

    delimiters



25
26
27
28
29
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 25

def helper_delimiters(delimiters = nil)
  return @helper_delimiters if delimiters.nil?

  @helper_delimiters = delimiters
end

.input(extnames) ⇒ Object

Converters can provide one or more extensions they accept. Examples:

  • input :erb
  • input %i(xls xlsx)

Parameters:

  • extnames (Array<Symbol>)

    extensions



14
15
16
17
18
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 14

def input(extnames)
  extnames = Array(extnames)
  self.extname_list ||= []
  self.extname_list += extnames.map { |e| ".#{e.to_s.downcase}" }
end

.support_slots(bool = true) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



33
34
35
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 33

def support_slots(bool = true) # rubocop:disable Style/OptionalBooleanParameter
  @support_slots = bool == true
end

.supports_slots?Boolean

Returns:

  • (Boolean)


31
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 31

def supports_slots? = @support_slots

.template_engine(name = nil) ⇒ Object



37
38
39
40
41
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 37

def template_engine(name = nil)
  return @template_engine unless name

  @template_engine = name.to_s
end

Instance Method Details

#convert(content, convertible = nil) ⇒ String

Logic to do the content conversion.

Parameters:

Returns:

  • (String)

    the converted content.



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

def convert(content, convertible = nil) # rubocop:disable Lint/UnusedMethodArgument
  content
end

#determine_template_engine(convertible) ⇒ Object



70
71
72
73
74
75
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 70

def determine_template_engine(convertible)
  template_engine = self.class.template_engine
  convertible_engine = convertible.data["template_engine"].to_s
  convertible_engine == template_engine ||
    (convertible_engine == "" && @config["template_engine"].to_s == template_engine)
end

#inspectObject



116
117
118
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 116

def inspect
  "#<#{self.class}#{" #{self.class.extname_list.join(", ")}" if self.class.extname_list}>"
end

#line_start(convertible) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 90

def line_start(convertible) # rubocop:disable Metrics/CyclomaticComplexity
  fmlc = case convertible
         when Bridgetown::Layout
           convertible.front_matter_line_count
         when Bridgetown::Resource::Base
           convertible.model.origin.respond_to?(:front_matter_line_count) &&
             convertible.model.origin.front_matter_line_count
         when Bridgetown::GeneratedPage
           res = convertible.original_resource
           if res&.model&.origin.respond_to?(:front_matter_line_count)
             res.model.origin.front_matter_line_count
           end
         end

  if fmlc
    # We add 4 because the line counts are only for the interior of the front matter and we need
    # to include 2 more lines for the delimiters, plus a blank line, plus 1 to get to content
    #
    # That shorthand breaks down if someone authors *no* blank line after front matter, or if
    # there are multiple blank lines. But those are not best practices, so we'll just fudge it.
    fmlc + 4
  else
    1
  end
end

#matches(ext, _convertible = nil) ⇒ Boolean

Does the given extension match this converter’s list of acceptable extensions?

Parameters:

Returns:

  • (Boolean)

    Whether the extension matches one in the list



66
67
68
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 66

def matches(ext, _convertible = nil) # rubocop:disable Naming/PredicateMethod
  (self.class.extname_list || []).include?(ext.downcase)
end

#output_ext(ext) ⇒ String

You can override this in Converter subclasses as needed. Default is “.html”, unless the converter is a template engine and the input file doesn’t match the normal template extension

Parameters:

  • ext (String)

    the extension of the original file

Returns:

  • (String)

    The output file extension (including the dot)



82
83
84
85
86
87
88
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 82

def output_ext(ext)
  if self.class.template_engine
    (self.class.extname_list || []).include?(ext.downcase) ? ".html" : ext
  else
    ".html"
  end
end