#!/usr/bin/ruby -w

def strip(input)
   do_output = true
   input.each_line do |line|
      if do_output
         do_output = line !~ /^_{5,}$/
      else
         do_output = line =~ /^(|>.*)$/ # Stop skipping at empty lines (bad idea?) or any quoted lines
      end

      puts line if do_output
   end
end

# The rest of this file is for more easily testing the function above

autoload :StringIO, 'stringio'

def with_captured_stdout
  old_stdout = $stdout
  $stdout = StringIO.new
  yield
  $stdout.string
ensure
  $stdout = old_stdout
end

if STDIN.tty? then
   delim1 = "<" * 80 + "\n"
   delim2 = "< " * 39 + "<\n"
   DATA.read.split(delim1).each do |inputoutput|
      example = inputoutput.split(delim2)
      if not example.empty?
         # puts "Testing: #{example}"
         output = with_captured_stdout { strip(example[0]) }
         if(output != example[1])
            abort("******************\nUnexpected output:\n******************\n#{output}****************\nExpected output:\n****************\n#{example[1]}")
         end
      end
   end
else
   str = $stdin.read
   strip(str)
end

__END__
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Message
_______________________________________________
mailmate mailing list
mailmate@lists.freron.com
https://lists.freron.com/listinfo/mailmate
< < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < <
Message
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Message
_______________________________________________
mailmate mailing list
mailmate@lists.freron.com
https://lists.freron.com/listinfo/mailmate
> More message
< < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < <
Message
> More message
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Message
_______________________________________________
mailmate mailing list
mailmate@lists.freron.com
https://lists.freron.com/listinfo/mailmate

More message
< < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < <
Message

More message
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
