#!/usr/bin/ruby
# encoding: utf-8
require 'rubygems'
infile = File.new('DefaultKeyBinding.dict','r')
input = infile.read
infile.close
date = Time.now.strftime('%m/%d/%Y')
output = ''
style =<
STYLE
intro =<Installation: Copy the DefaultKeyBindings.dict file to the `~/Library/KeyBindings/` directory (create `KeyBindings` if it doesn't already exist). 
Any open applications will need to be re-started before the key bindings will take effect -- or log out and log back in.
Documentation (last updated #{date}.)
*Grouped items begin with the groups shortcut (if exists), followed by a subgroup (if exists) followed by the keys specified.*
INTRO
outro =<\s*(.*)$/
    note += " " + $1
  elsif line =~ /^\s*\};\s*$/
    level -= 1
    if level == 1
      subgroup_command = ''
      subgroup_desc = ''
      output += "|||||\n"
    elsif level == 0
      output += "[ #{group_desc} ]\n\n"
      group_command = ''
      group_desc = ''
    end
    next
  elsif line =~ /^\s*\/\/\s*(.*)/
    desc = $1
    next
  elsif line =~ /^\s*"([^"]+)"\s*=\s*\{.*?\/\/\s*(.*)/
    level += 1
    if level == 1
      group_command = translate_command($1)
      group_desc = $2
      output += "\n|#{group_desc} (#{group_command})||||\n|:----:|:----:|:----:|:----|\n"
    elsif level == 2
      subgroup_command = translate_command($1)
      subgroup_desc = $2
      output += "|#{subgroup_desc} (#{subgroup_command})||| |\n"
    else
      prefix = $1
    end
    next
  elsif line =~ /^\s*"([^"]+)"\s*=\s*\(/
    command = translate_command($1)
    note = "(#{note})" if note != ''
    if level == 0
      toplevel.push("|#{command}|#{desc} #{note}|\n")
    else
      command = prefix + "," + command if prefix
      output += "|#{group_command} |#{subgroup_command} |#{command} |#{desc} #{note}|\n"
    end
    note = ''
  end
}
topoutput = "|General Commands||\n|Key|Function|\n|:----:|:----|\n"
toplevel.each {|line|
  topoutput += line
}
topoutput += "[ General Commands ]\n\n"
# output = style + topoutput + output
output = topoutput + output
htmlout = %x{echo #{e_sh output}|/usr/local/bin/multimarkdown}
outfile = File.new('readme.md','w')
outfile.puts intro
outfile.puts htmlout
outfile.puts outro
outfile.close