class Mato::Converter

Constants

FLAVORES

Attributes

content[R]
content_lines[R]

@return [Array<String>]

flavor[R]
processor[R]

Public Class Methods

new(processor, content, flavor) click to toggle source
# File lib/mato/converter.rb, line 20
def initialize(processor, content, flavor)
  unless FLAVORES.include?(flavor)
    raise "Unsupported flavor #{flavor.inspect}, it must be one of: #{FLAVORES.map(&:inspect).join(' ')}"
  end

  @processor = processor
  @content = content
  @content_lines = content.split(/\n/)
  @flavor = flavor
end

Public Instance Methods

convert_headings!(document) click to toggle source
# File lib/mato/converter.rb, line 45
def convert_headings!(document)
  document.walk.select do |node|
    node.type == :text &&
      node.sourcepos[:start_column] == 1 &&
      node.parent.type == :paragraph &&
      node.parent.parent.type == :document
  end.reverse_each do |node|
    replacement = node.string_content.gsub(/\A(#+)(?=\S)/, '\1 ')

    if node.string_content != replacement
      pos = node.sourcepos
      content_lines[pos[:start_line] - 1][(pos[:start_column] - 1)...pos[:end_column]] = replacement
    end
  end
end
run() click to toggle source
# File lib/mato/converter.rb, line 31
def run
  # @type [CommonMarker::Node]
  document = processor.parse_markdown(content)

  convert_headings!(document)

  content_lines.join("\n").tap do |c|
    # fixup newlines removed by String#split
    content.scan(/\n+\z/) do |matched|
      c << matched
    end
  end
end