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


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/github_changelog_generator/reader.rb', line 26

def initialize(options = {})
  defaults = {
    heading_level: "##",
    heading_structures: [
      /^## \[(?<version>.+?)\]\((?<url>.+?)\)( \((?<date>.+?)\))?$/,
      /^## (?<version>.+?)( \((?<date>.+?)\))?$/
    ]
  }

  @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


69
70
71
72
73
74
75
76
77
78
# File 'lib/github_changelog_generator/reader.rb', line 69

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:


51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/github_changelog_generator/reader.rb', line 51

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


80
81
82
# File 'lib/github_changelog_generator/reader.rb', line 80

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