Class: KeepTheChange::Parser
- Inherits:
-
Object
- Object
- KeepTheChange::Parser
- Defined in:
- lib/keepthechange/parser.rb
Overview
Changelog Parser class. Use a separate instance for each Changelog.
Constant Summary collapse
- VERSION_HEADER_RE =
Version header RE (these are searched for in the Changelog to find actual versions)
/^## \[(\d+\.\d+\.\d+)\] - (\d{4}-\d{2}-\d{2}).*$/
- SECTION_HEADER_RE =
Section header RE (these are searched inside each version changeset to find sections)
/^### (.*)$/
Instance Method Summary collapse
-
#combine_changes(since_version, to_version = nil, print_header = true) ⇒ Object
Combine changes for multiple versions.
-
#delta_changes(since_version, to_version = nil) ⇒ Object
Combine changes for multiple versions.
-
#initialize(changelog: '', version_header_re: VERSION_HEADER_RE, section_header_re: SECTION_HEADER_RE) ⇒ KeepTheChange::Parser
constructor
Ready to parse!.
-
#parse ⇒ Hash
Parse the Changelog and return a hash of changes.
Constructor Details
#initialize(changelog: '', version_header_re: VERSION_HEADER_RE, section_header_re: SECTION_HEADER_RE) ⇒ KeepTheChange::Parser
Returns Ready to parse!.
18 19 20 21 22 23 |
# File 'lib/keepthechange/parser.rb', line 18 def initialize(changelog: '', version_header_re: VERSION_HEADER_RE, section_header_re: SECTION_HEADER_RE) @changelog = changelog @version_header_re = version_header_re @section_header_re = section_header_re @changelog_hash = {} end |
Instance Method Details
#combine_changes(since_version, to_version = nil, print_header = true) ⇒ Object
Combine changes for multiple versions. The ‘since_version` is your `current` version. This means that changes will start combining from the next version up.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/keepthechange/parser.rb', line 50 def combine_changes(since_version, to_version = nil, print_header = true) parse if @changelog_hash.empty? combined_changes = {} filtered_changes = Marshal.load(Marshal.dump(@changelog_hash)) if to_version filtered_changes.delete_if do |key, _| SemVersion.new(key) <= SemVersion.new(since_version) || SemVersion.new(key) > SemVersion.new(to_version) end else filtered_changes.delete_if { |key, _| SemVersion.new(key) <= SemVersion.new(since_version) } end filtered_changes.each do |_, changes| changes.each do |section, changes_list| if combined_changes.key? section combined_changes[section] << "\n" << changes_list else combined_changes[section] = changes_list end end end if print_header output = "## Changes since [#{since_version}]" output << " (and up to [#{to_version}])" if to_version else output = '' end combined_changes.each do |section, changes_list| output << "\n\n" unless output.empty? output << "### #{section}" output << "\n#{changes_list}" end output << "\n" end |
#delta_changes(since_version, to_version = nil) ⇒ Object
Combine changes for multiple versions. The ‘since_version` is your `current` version. This means that changes will start combining from the next version up.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/keepthechange/parser.rb', line 95 def delta_changes(since_version, to_version = nil) parse if @changelog_hash.empty? filtered_changes = Marshal.load(Marshal.dump(@changelog_hash)) if to_version filtered_changes.delete_if do |key, _| SemVersion.new(key) <= SemVersion.new(since_version) || SemVersion.new(key) > SemVersion.new(to_version) end else filtered_changes.delete_if { |key, _| SemVersion.new(key) <= SemVersion.new(since_version) } end output = '' filtered_changes.each do |version, changeset| output << "## #{version}\n" changeset.each do |section, changes| output << "### #{section}\n" output << changes << "\n\n" end end output end |
#parse ⇒ Hash
Parse the Changelog and return a hash of changes.
@changelog_hash = {
'1.0.0' => {
'Added' => "- Change 1\n- Change 2",
'Fixed' => "- Bugfix"
}
}
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/keepthechange/parser.rb', line 34 def parse @changelog.scan(@version_header_re) do |match| version_changes = Regexp.last_match.post_match scanner = StringScanner.new(version_changes) scanner.scan_until(@version_header_re) version_changes = parse_version_changes(scanner.pre_match || version_changes) @changelog_hash[match[0]] = version_changes end @changelog_hash end |