class GitHubChangelogGenerator::Reader

A Reader to read an existing ChangeLog file and return a structured object

Example:

reader = GitHubChangelogGenerator::Reader.new
content = reader.read('./CHANGELOG.md')

Public Class Methods

new(options = {}) click to toggle source
# File lib/github_changelog_generator/reader.rb, line 28
def initialize(options = {})
  defaults = {
    heading_level: "##",
    heading_structures: [
      /^## \[(?<version>.+?)\](\((?<url>.+?)\))?( \((?<date>.+?)\))?$/, # rubocop:disable Lint/MixedRegexpCaptureTypes
      /^## (?<version>.+?)( \((?<date>.+?)\))?$/ # rubocop:disable Lint/MixedRegexpCaptureTypes
    ]
  }

  @options = options.merge(defaults)

  @heading_level = @options[:heading_level]
  @heading_structures = @options[:heading_structures]
end

Public Instance Methods

parse(data) click to toggle source

Parse the given ChangeLog data into a list of Hashes

@param [String] data File data from the ChangeLog.md @return [Array<Hash>] Parsed data, e.g. [{ 'version' => …, 'url' => …, 'date' => …, 'content' => …}, …]

# File lib/github_changelog_generator/reader.rb, line 73
def parse(data)
  sections = data.split(/^## .+?$/)
  headings = data.scan(/^## .+?$/)

  headings.each_with_index.map do |heading, index|
    section = parse_heading(heading)
    section["content"] = sections.at(index + 1)
    section
  end
end
parse_heading(heading) click to toggle source

Parse a single heading and return a Hash

The following heading structures are currently valid:

@param [String] heading Heading from the ChangeLog File @return [Hash] Returns a structured Hash with version, url and date

# File lib/github_changelog_generator/reader.rb, line 55
def parse_heading(heading)
  captures = { "version" => nil, "url" => nil, "date" => nil }

  @heading_structures.each do |regexp|
    matches = Regexp.new(regexp).match(heading)
    if matches
      captures.merge!(Hash[matches.names.zip(matches.captures)])
      break
    end
  end

  captures
end
read(file_path) click to toggle source
# File lib/github_changelog_generator/reader.rb, line 84
def read(file_path)
  parse File.read(file_path)
end