From: Eric Wong Date: Tue, 1 Mar 2016 02:45:34 +0000 (+0000) Subject: view: consolidate whitespace stripping from messages X-Git-Tag: v1.0.0~668 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=f5eb6bb83558f3dabdb2fbbf7c1c926a726bfed9;p=public-inbox.git view: consolidate whitespace stripping from messages We now keep intermediate blank lines in messages, since it could be used to denote logical gaps in the message (such as giving readers a chance to opt out of "spoiler" information). However leading blank lines, trailing blank lines, and trailing whitespace have no useful value we can discern; so drop those entirely to prevent clients from eating up vertical whitespace. --- diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index de7bdf9c..61eb890f 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -254,15 +254,10 @@ sub index_walk { my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_; my $s = add_text_body($enc, $part, $part_nr, $fhref); - # kill any leading or trailing whitespace lines - $s =~ s/^\s*$//sgm; - $s =~ s/\s+\z//s; + return if $s eq ''; + + $s .= "\n"; # ensure there's a trailing newline - if ($s ne '') { - # kill per-line trailing whitespace - $s =~ s/[ \t]+$//sgm; - $s .= "\n" unless $s =~ /\n\z/s; - } $fh->write($s); } @@ -289,7 +284,9 @@ sub multipart_text_as_html { # scan through all parts, looking for displayable text $mime->walk_parts(sub { my ($part) = @_; - $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx, 1); + $part = add_text_body($enc, $part, \$part_nr, $full_pfx, 1); + $rv .= $part; + $rv .= "\n" if $part ne ''; }); $mime->body_set(''); $rv; @@ -435,8 +432,11 @@ sub add_text_body { $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1, $do_anchor); } - $s .= "\n" unless $s =~ /\n\z/s; ++$$part_nr; + + $s =~ s/[ \t]+$//sgm; # kill per-line trailing whitespace + $s =~ s/\A\n+//s; # kill leading blank lines + $s =~ s/\s+\z//s; # kill all trailing spaces (final "\n" added if ne '') $s; }