class RubyMatter::Stringifier

Stringifies data, content and optional excerpt, based on the arguments supplied to the initializer.

Public Class Methods

new( content, data: {}, delimiters:, language: nil, engines: {}, excerpt: nil, excerpt_separator: nil ) click to toggle source

Instantiate the stringifier. This should not need to be called directly, as the RubyMatter module methods will handle this. For more information, see the following method that instantiates a stringifier:

Returns

(RubyMatter::Stringifier)

An instance of the stringifier.

# File lib/ruby_matter/stringifier.rb, line 19
def initialize(
  content,
  data: {},
  delimiters:,
  language: nil,
  engines: {},
  excerpt: nil,
  excerpt_separator: nil
)
  @content = content
  @data = data
  @delimiters = delimiters
  @language = language
  @engines = engines
  @excerpt = excerpt
  @excerpt_separator = excerpt_separator
end

Public Instance Methods

stringify() click to toggle source

Stringifies content, data, and optional excerpt, using the delimiters, language and engines.

Returns

(String)

The result of stringifying the supplied arguments.

# File lib/ruby_matter/stringifier.rb, line 44
def stringify
  matter + excerpt + content
end

Private Instance Methods

closing() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 64
def closing
  @delimiters.last
end
content() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 86
def content
  @content ? newline(@content) : ''
end
engine() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 50
def engine
  @engine ||= @engines[@language]
end
excerpt() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 80
def excerpt
  return '' unless @excerpt

  newline(@excerpt) + newline(seperator)
end
matter() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 72
def matter
  stringifier.call(@data).then do |string|
    return '' if string.strip == '{}'

    newline(opening) + newline(string) + newline(closing)
  end
end
newline(line) click to toggle source
# File lib/ruby_matter/stringifier.rb, line 90
def newline(line)
  line[-1] == "\n" ? line : "#{line}\n"
end
opening() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 60
def opening
  @delimiters.first
end
seperator() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 68
def seperator
  @excerpt_separator || closing
end
stringifier() click to toggle source
# File lib/ruby_matter/stringifier.rb, line 54
def stringifier
  @stringifier ||= (
    engine && engine[:stringify]
  ) || raise(EngineError.new(language: language))
end