#!/usr/bin/ruby
require 'yaml'
require 'nkf'

# Get mail data from STDIN.
maildata = []

# Header to UTF-8, concat multiline term, and push maildata Array.
NKF.nkf("-w -Lu -m", STDIN.gets("")).each_line do | l |
  if l =~ /^\s*$/
    break
  elsif l =~ /^\s+/
    maildata.last.concat(l.lstrip)
  else
    maildata.push(l)
  end
end

# Hash object for a header.
header = {}

maildata.each do |i|
  if i =~ /\A([-_A-Za-z0-9]+):/ # match header format?
    header[$1] = $'
  else
    next
  end
end

# output Header data to YAML.
print YAML.dump(header)
