Class: Bridgetown::Converter

Inherits:
Plugin
  • Object
show all
Defined in:
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.



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

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

Class Attribute Details

.extname_listObject

Returns the value of attribute extname_list.



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

def extname_list
  @extname_list
end

Class Method Details

.input(extnames) ⇒ Object

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

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


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

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



22
23
24
# File 'lib/bridgetown-core/converter.rb', line 22

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

.supports_slots?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/bridgetown-core/converter.rb', line 18

def supports_slots?
  @support_slots == true
end

Instance Method Details

#convert(content, convertible = nil) ⇒ String

Logic to do the content conversion.

Parameters:

Returns:

  • (String)

    the converted content.



41
42
43
# File 'lib/bridgetown-core/converter.rb', line 41

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

#inspectObject



74
75
76
# File 'lib/bridgetown-core/converter.rb', line 74

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

#line_start(convertible) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/bridgetown-core/converter.rb', line 65

def line_start(convertible)
  if convertible.is_a?(Bridgetown::Resource::Base) &&
      convertible.model.origin.respond_to?(:front_matter_line_count)
    convertible.model.origin.front_matter_line_count + 4
  else
    1
  end
end

#matches(ext) ⇒ Boolean

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

Parameters:

  • ext (String)

    The file's extension (including the dot)

Returns:

  • (Boolean)

    Whether the extension matches one in the list



51
52
53
# File 'lib/bridgetown-core/converter.rb', line 51

def matches(ext)
  (self.class.extname_list || []).include?(ext.downcase)
end

#output_ext(_ext) ⇒ String

You can override this in Converter subclasses as needed. Default is ".html"

Parameters:

  • ext (String)

    The extension of the original file

Returns:

  • (String)

    The output file extension (including the dot)



61
62
63
# File 'lib/bridgetown-core/converter.rb', line 61

def output_ext(_ext)
  ".html"
end