implement folding of trailing newlines

Fixes #20.