Class: GitHubChangelogGenerator::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/github_changelog_generator/reader.rb

Overview

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

Example:

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Reader

Returns a new instance of Reader.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

Instance Method Details

#parse(data) ⇒ Array<Hash>

Parse the given ChangeLog data into a list of Hashes

Parameters:

  • data (String)

    File data from the ChangeLog.md

Returns:

  • (Array<Hash>)

    Parsed data, e.g. [{ ‘version’ => …, ‘url’ => …, ‘date’ => …, ‘content’ => …}, …]



73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Hash

Parse a single heading and return a Hash

The following heading structures are currently valid:

Parameters:

  • heading (String)

    Heading from the ChangeLog File

Returns:

  • (Hash)

    Returns a structured Hash with version, url and date



55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



84
85
86
# File 'lib/github_changelog_generator/reader.rb', line 84

def read(file_path)
  parse File.read(file_path)
end