class WhatTheGem::Changes::MarkdownParser

Public Instance Methods

versions() click to toggle source
# File lib/whatthegem/changes/markdown_parser.rb, line 4
def versions
  nodes = I::Kramdowns.elements(content)
  level = detect_version_level(nodes) # find level of headers which contain version

  sections = sections(nodes, level)
    .select { |title,| title.match?(VERSION_LINE_REGEXP) }
    .map(&method(:make_version))
    .sort_by(&:number) # it is internally converted to Gem::Version, so "1.12" is correctly > "1.2"
end

Private Instance Methods

detect_version_level(nodes) click to toggle source
# File lib/whatthegem/changes/markdown_parser.rb, line 25
def detect_version_level(nodes)
  nodes
    .select { |n| n.type == :header && n.options[:raw_text].match?(VERSION_LINE_REGEXP) }
    .map { |n| n.options[:level] }.min
end
make_version((title, nodes)) click to toggle source
# File lib/whatthegem/changes/markdown_parser.rb, line 31
def make_version((title, nodes))
  # TODO: date, if known
  Version.new(
    number: title[VERSION_LINE_REGEXP, :version],
    header: title,
    body: nodes.map(&I::Kramdowns.method(:el2md)).join
  )
end
sections(nodes, level) click to toggle source
# File lib/whatthegem/changes/markdown_parser.rb, line 16
def sections(nodes, level)
  nodes
    .chunk { |n| n.type == :header && n.options[:level] == level } # chunk into sections by header
    .drop_while { |header,| !header }                              # drop before first header
    .map(&:last)                                                   # drop `true`, `false` flags after chunk
    .each_slice(2)                                                 # join header with subsequent nodes
    .map { |(h, *), nodes| [h.options[:raw_text], nodes] }
end