]> Sergey Matveev's repositories - public-inbox.git/commitdiff
view: consolidate whitespace stripping from messages
authorEric Wong <e@80x24.org>
Tue, 1 Mar 2016 02:45:34 +0000 (02:45 +0000)
committerEric Wong <e@80x24.org>
Tue, 1 Mar 2016 02:45:34 +0000 (02:45 +0000)
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.

lib/PublicInbox/View.pm

index de7bdf9c073eaba6c762f0fb8ae01f73200e0d33..61eb890f2ff46f3a076753c25e2ccf0343b707bb 100644 (file)
@@ -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;
 }