Class: Schmersion::Formatters::Markdown

Inherits:
Schmersion::Formatter show all
Defined in:
lib/schmersion/formatters/markdown.rb

Instance Attribute Summary

Attributes inherited from Schmersion::Formatter

#filename

Instance Method Summary collapse

Methods inherited from Schmersion::Formatter

#initialize

Constructor Details

This class inherits a constructor from Schmersion::Formatter

Instance Method Details

#generate(repo, version) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/schmersion/formatters/markdown.rb', line 9

def generate(repo, version)
  lines = []
  lines << "## #{version.version}\n"

  sections_for_commits(version.commits).each do |section, commits|
    lines << "### #{section['title']}\n"
    commits.each do |_, scope_commits|
      scope_commits.sort_by { |c| c.message.description.upcase }.each do |commit|
        lines << commit_line(repo, commit)
      end
    end
    lines << nil
  end
  lines.join("\n")
end

#insert(part) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/schmersion/formatters/markdown.rb', line 25

def insert(part)
  text_to_insert = "#{header}\n#{part}"

  unless File.file?(@filename)
    File.write(@filename, text_to_insert)
    return
  end

  # If we already have a changelog markdown file, we're going to remove
  # everyting up to the first level 2 heading and replace it with the
  # header and the new version's data.
  contents = File.read(@filename)
  contents_without_header = contents.sub(/\A.*?##/m, '##')
  File.write(@filename, "#{text_to_insert}\n#{contents_without_header}")

  true
end