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.



34
35
36
37
# File 'bridgetown-core/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 'bridgetown-core/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 'bridgetown-core/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 'bridgetown-core/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 'bridgetown-core/lib/bridgetown-core/converter.rb', line 18

def supports_slots? = @support_slots

.template_engine(name = nil) ⇒ Object



24
25
26
27
28
# File 'bridgetown-core/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.



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

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

#determine_template_engine(convertible) ⇒ Object



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

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



97
98
99
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 97

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

#line_start(convertible) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 77

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



53
54
55
# File 'bridgetown-core/lib/bridgetown-core/converter.rb', line 53

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)



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

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