#!/usr/bin/ruby
# encoding: utf-8
require 'rubygems'
infile = File.new('DefaultKeyBinding.dict','r')
input = infile.read
infile.close
output = ''
style =<
STYLE
intro =<\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.close