class MarkdownToRspec::ToRspec::Formatter::Markdown

Attributes

items[R]
markdown[R]
parsed_markdown[R]

Public Class Methods

new(markdown:) click to toggle source
# File lib/markdown_to_rspec/to_rspec/formatter/markdown.rb, line 12
def initialize(markdown:)
  @markdown = markdown
  @parsed_markdown = RDoc::Markdown.parse(markdown)
  @items = []
end

Public Instance Methods

call() click to toggle source
# File lib/markdown_to_rspec/to_rspec/formatter/markdown.rb, line 20
def call
  parsed_markdown.parts.each do |part|
    item = format(part, find_parent(part))
    @items << item
  end
  # Each item renders a child element, so only top-level items are converted.
  items.select { |item| item.parent.nil? }.map(&:convert).join("\n")
end

Private Instance Methods

find_parent(rdoc_item) click to toggle source
# File lib/markdown_to_rspec/to_rspec/formatter/markdown.rb, line 31
def find_parent(rdoc_item)
  return headings.last unless rdoc_item.respond_to?(:level)

  # Get a lower level Header than rdoc_item most recent one (lower is higher)
  headings.reverse.find { |heading| heading.level < rdoc_item.level }
end
format(part, parent) click to toggle source
# File lib/markdown_to_rspec/to_rspec/formatter/markdown.rb, line 42
def format(part, parent)
  formatters = {
    'RDoc::Markup::Heading' => Formatter::Heading.new,
    'RDoc::Markup::List' => Formatter::List.new,
    'RDoc::Markup::Paragraph' => Formatter::Paragraph.new
  }
  formatters[part.class.name]&.call(part, parent)
end
headings() click to toggle source
# File lib/markdown_to_rspec/to_rspec/formatter/markdown.rb, line 38
def headings
  items.select { |item| item.class == Item::Heading }
end