Skip to content

Instantly share code, notes, and snippets.

@jonleighton
Created November 4, 2011 12:59
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jonleighton/1339263 to your computer and use it in GitHub Desktop.
#!/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