strip trailing whitespace and add a trailing newline