Create a gist now

Instantly share code, notes, and snippets.

#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment