Class: GitHubChangelogGenerator::Reader
- Inherits:
-
Object
- Object
- GitHubChangelogGenerator::Reader
- 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
-
#initialize(options = {}) ⇒ Reader
constructor
A new instance of Reader.
-
#parse(data) ⇒ Array<Hash>
Parse the given ChangeLog data into a list of Hashes.
-
#parse_heading(heading) ⇒ Hash
Parse a single heading and return a Hash.
- #read(file_path) ⇒ Object
Constructor Details
permalink #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( = {}) defaults = { heading_level: "##", heading_structures: [ /^## \[(?<version>.+?)\]\((?<url>.+?)\)( \((?<date>.+?)\))?$/, # rubocop:disable Lint/MixedRegexpCaptureTypes /^## (?<version>.+?)( \((?<date>.+?)\))?$/ # rubocop:disable Lint/MixedRegexpCaptureTypes ] } @options = .merge(defaults) @heading_level = @options[:heading_level] @heading_structures = @options[:heading_structures] end |
Instance Method Details
permalink #parse(data) ⇒ Array<Hash>
Parse the given ChangeLog data into a list of Hashes
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/github_changelog_generator/reader.rb', line 71 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 |
permalink #parse_heading(heading) ⇒ Hash
Parse a single heading and return a Hash
The following heading structures are currently valid:
-
## [v1.0.2](github.com/zanui/chef-thumbor/tree/v1.0.1) (2015-03-24)
-
## [v1.0.2](github.com/zanui/chef-thumbor/tree/v1.0.1)
-
## v1.0.2 (2015-03-24)
-
## v1.0.2
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/github_changelog_generator/reader.rb', line 53 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 |
permalink #read(file_path) ⇒ Object
[View source]
82 83 84 |
# File 'lib/github_changelog_generator/reader.rb', line 82 def read(file_path) parse File.read(file_path) end |