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.



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

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



20
21
22
# File 'lib/bridgetown-core/converter.rb', line 20

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

.supports_slots?Boolean

Returns:

  • (Boolean)


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

def supports_slots? = @support_slots

.template_engine(name = nil) ⇒ Object



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

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.



45
46
47
# File 'lib/bridgetown-core/converter.rb', line 45

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

#determine_template_engine(convertible) ⇒ Object



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

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



102
103
104
# File 'lib/bridgetown-core/converter.rb', line 102

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

#line_start(convertible) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/bridgetown-core/converter.rb', line 82

def line_start(convertible) # rubocop:disable Metrics/PerceivedComplexity
  if convertible.is_a?(Bridgetown::Resource::Base) &&
      convertible.model.origin.respond_to?(:front_matter_line_count)
    if convertible.model.origin.front_matter_line_count.nil?
      1
    else
      convertible.model.origin.front_matter_line_count + 4
    end
  elsif convertible.is_a?(Bridgetown::GeneratedPage) && convertible.original_resource
    res = convertible.original_resource
    if res.model.origin.respond_to?(:front_matter_line_count)
      res.model.origin.front_matter_line_count + 4
    else
      1
    end
  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



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

def matches(ext, _convertible = nil)
  (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)



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

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