public

  • Download Gist
convert_changelog_to_markdown.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#!/usr/bin/env ruby
 
# Used to convert Rails CHANGELOGs to Markdown format.
# Usage: convert_changelog_to_markdown.rb CHANGELOG > CHANGELOG.md
 
text = File.read(ARGV[0])
 
processed = ''
 
def fixup_line(line)
# Replace [Author Name] with *Author Name* so it will be italicised
line.sub!(/\[([^\]]+)\](?=\n)/, '*\1*')
 
# Avoid falling foul of XSS filter
line.sub!('<SCRIPT>', 'SCRIPT')
end
 
text.each_line do |line|
# Headings
if line =~ /^\*(.*)\*( (\(.*\)|\[.*\]))*$/
line.gsub!('*', '')
line.gsub!(/[\[\]]/, '')
line.gsub!(/^(.*)$/, '## \1 ##')
 
# The start of an item
elsif line =~ /^\*/
line.sub!(/^\*\s*/, '* ')
 
fixup_line(line)
 
# Anything else (i.e. additional lines in items)
elsif line =~ /^(\s*)(.+)$/
line = " #{$2}\n" if $1.length < 2
line = " #{line}"
 
# Try to detect code blocks and indent so they will be formatted as code
if line =~ /^\s{6,6}/
line.sub!(/^\s{6,6}/) { " " * 8 }
else
# If a line starts with a '#', prevent it being seen as a heading
line.sub!(/^\s{4,4}\#/) { " \\#" }
 
fixup_line(line)
end
end
 
processed << line
end
 
puts processed

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.