2016-09-22 17:16:29 +00:00
|
|
|
# frozen_string_literal: true
|
2017-05-24 22:11:02 +00:00
|
|
|
|
2015-03-25 13:40:24 +00:00
|
|
|
#
|
|
|
|
# Author:: Enrico Stahn <mail@enricostahn.com>
|
|
|
|
#
|
|
|
|
# Copyright 2014, Zanui, <engineering@zanui.com.au>
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
|
|
|
module GitHubChangelogGenerator
|
|
|
|
# A Reader to read an existing ChangeLog file and return a structured object
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# reader = GitHubChangelogGenerator::Reader.new
|
|
|
|
# content = reader.read('./CHANGELOG.md')
|
|
|
|
class Reader
|
2015-03-26 04:57:51 +00:00
|
|
|
def initialize(options = {})
|
|
|
|
defaults = {
|
2015-04-02 09:05:26 +00:00
|
|
|
heading_level: "##",
|
2015-03-26 04:57:51 +00:00
|
|
|
heading_structures: [
|
|
|
|
/^## \[(?<version>.+?)\]\((?<url>.+?)\)( \((?<date>.+?)\))?$/,
|
|
|
|
/^## (?<version>.+?)( \((?<date>.+?)\))?$/
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
@options = options.merge(defaults)
|
|
|
|
|
|
|
|
@heading_level = @options[:heading_level]
|
|
|
|
@heading_structures = @options[:heading_structures]
|
|
|
|
end
|
|
|
|
|
2015-03-25 13:40:24 +00:00
|
|
|
# Parse a single heading and return a Hash
|
|
|
|
#
|
|
|
|
# The following heading structures are currently valid:
|
|
|
|
# - ## [v1.0.2](https://github.com/zanui/chef-thumbor/tree/v1.0.1) (2015-03-24)
|
|
|
|
# - ## [v1.0.2](https://github.com/zanui/chef-thumbor/tree/v1.0.1)
|
|
|
|
# - ## v1.0.2 (2015-03-24)
|
|
|
|
# - ## v1.0.2
|
|
|
|
#
|
|
|
|
# @param [String] heading Heading from the ChangeLog File
|
|
|
|
# @return [Hash] Returns a structured Hash with version, url and date
|
|
|
|
def parse_heading(heading)
|
2015-04-02 09:05:26 +00:00
|
|
|
captures = { "version" => nil, "url" => nil, "date" => nil }
|
2015-03-25 13:40:24 +00:00
|
|
|
|
2015-03-26 04:57:51 +00:00
|
|
|
@heading_structures.each do |regexp|
|
2015-03-25 13:40:24 +00:00
|
|
|
matches = Regexp.new(regexp).match(heading)
|
2015-10-23 05:32:52 +00:00
|
|
|
if matches
|
|
|
|
captures.merge!(Hash[matches.names.zip(matches.captures)])
|
|
|
|
break
|
|
|
|
end
|
2015-03-25 13:40:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
captures
|
|
|
|
end
|
|
|
|
|
2015-10-23 05:44:20 +00:00
|
|
|
# Parse the given ChangeLog data into a list of Hashes
|
2015-03-25 13:40:24 +00:00
|
|
|
#
|
|
|
|
# @param [String] data File data from the ChangeLog.md
|
2015-10-23 05:44:20 +00:00
|
|
|
# @return [Array<Hash>] Parsed data, e.g. [{ 'version' => ..., 'url' => ..., 'date' => ..., 'content' => ...}, ...]
|
2015-03-25 13:40:24 +00:00
|
|
|
def parse(data)
|
|
|
|
sections = data.split(/^## .+?$/)
|
|
|
|
headings = data.scan(/^## .+?$/)
|
|
|
|
|
2015-10-23 05:44:20 +00:00
|
|
|
headings.each_with_index.map do |heading, index|
|
|
|
|
section = parse_heading(heading)
|
|
|
|
section["content"] = sections.at(index + 1)
|
|
|
|
section
|
2015-03-25 13:40:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read(file_path)
|
|
|
|
parse File.read(file_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|