[Rt-commit] rt branch, 4.0/text-wrap-and-quoting-long-lines, created. rt-4.0.6-250-ge5cdeef

Jim Brandt jbrandt at bestpractical.com
Mon Aug 6 16:28:21 EDT 2012


The branch, 4.0/text-wrap-and-quoting-long-lines has been created
        at  e5cdeef09027a21ac1d86a5df43219de1c05d214 (commit)

- Log -----------------------------------------------------------------
commit e5cdeef09027a21ac1d86a5df43219de1c05d214
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Mon Aug 6 16:23:38 2012 -0400

    Quote replies properly when re-wrapping
    
    When re-wrapping text that already has email quoting, the
    quoting should be maintained in the newly re-wrapped text.
    See:
    
    http://issues.bestpractical.com/Ticket/Display.html?id=16857

diff --git a/lib/RT/Transaction.pm b/lib/RT/Transaction.pm
index 5b3641f..0d2ba53 100644
--- a/lib/RT/Transaction.pm
+++ b/lib/RT/Transaction.pm
@@ -371,13 +371,13 @@ sub Content {
         }
 
         if ( $max > 76 ) {
+            use Text::Quoted;
             require Text::Wrapper;
-            my $wrapper = Text::Wrapper->new(
-                columns    => $args{'Wrap'},
-                body_start => ( $max > 70 * 3 ? '   ' : '' ),
-                par_start  => ''
-            );
-            $content = $wrapper->wrap($content);
+
+            my $structure = extract($content);
+            $content = $self->QuoteWrap(content_ref => $structure,
+                                        cols => $args{'Wrap'},
+                                        max => $max );
         }
 
         $content =~ s/^/> /gm;
@@ -388,6 +388,51 @@ sub Content {
     return ($content);
 }
 
+=head2 QuoteWrap PARAMHASH
+
+Wrap the contents of transactions based on Wrap settings, maintaining
+the quote character from the original.
+
+=cut
+
+sub QuoteWrap {
+    my $self = shift;
+    my %args = @_;
+    my $ref = $args{content_ref};
+    my $final_string;
+
+    if ( ref $ref eq 'ARRAY' ){
+        foreach my $array (@$ref){
+            $final_string .= $self->QuoteWrap(content_ref => $array,
+                                              cols => $args{cols},
+                                              max => $args{max} );
+        }
+    }
+    elsif ( ref $ref eq 'HASH' ){
+        return $ref->{quoter} . "\n" if $ref->{empty}; # Blank line
+
+        my $col = $args{cols} - (length $ref->{quoter});
+        my $wrapper = Text::Wrapper->new(
+                         columns    => $col,
+                         body_start => ( $args{max} > 70 * 3 ? '   ' : '' ),
+                         par_start  => '' );
+
+        my $tmp = join ' ', split /\n/, $ref->{text};
+        my $wrap = $wrapper->wrap($tmp);
+        my $quoter = $ref->{quoter};
+
+        # Only add the space if actually quoting
+        $quoter .= ' ' if length $quoter;
+        $wrap =~ s/^(.*)$/$quoter$1/mg;
+
+        return $wrap;
+    }
+    else{
+        $RT::Logger->warning("Can't apply quoting with $ref");
+        return;
+    }
+    return $final_string;
+}
 
 
 =head2 Addresses
diff --git a/t/api/transaction.t b/t/api/transaction.t
new file mode 100644
index 0000000..d86cabb
--- /dev/null
+++ b/t/api/transaction.t
@@ -0,0 +1,151 @@
+
+use strict;
+use warnings;
+use RT;
+use RT::Test tests => 13;
+
+use_ok('RT::Transaction');
+
+diag "Test quoting on transaction content";
+{
+    my $mail = <<'.';
+From: root at localhost
+Subject: Testing quoting on long lines
+Content-Type: text/plain
+
+> This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+
+This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+.
+
+    my ( $status, $id ) = RT::Test->send_via_mailgate($mail);
+    is( $status >> 8, 0, "The mail gateway exited normally" );
+    ok( $id, "Created ticket $id" );
+    my $ticket = RT::Ticket->new( RT->SystemUser );
+    $ticket->Load( $id );
+    my $txns = $ticket->Transactions;
+    my $txn = $txns->Next;
+
+    my $expected = <<'QUOTED';
+> > This is a line that is longer than the 70 characters that will
+> > demonstrate quoting when text is wrapped to multiple lines.
+> 
+> This is a line that is longer than the 70 characters that will
+> demonstrate quoting when text is wrapped to multiple lines.
+QUOTED
+
+    my ($content) = $txn->Content( Quote => 1 );
+    like( $content, qr/$expected/, 'Text quoted properly');
+}
+
+diag "More complex quoting";
+{
+    my $mail = <<'.';
+From: root at localhost
+Subject: Testing quoting on long lines
+Content-Type: text/plain
+
+# # This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+# # This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+> This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+> This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+
+This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+.
+
+    my ( $status, $id ) = RT::Test->send_via_mailgate($mail);
+    is( $status >> 8, 0, "The mail gateway exited normally" );
+    ok( $id, "Created ticket $id" );
+    my $ticket = RT::Ticket->new( RT->SystemUser );
+    $ticket->Load( $id );
+    my $txns = $ticket->Transactions;
+    my $txn = $txns->Next;
+
+    my $expected = <<'QUOTED';
+> # # This is a line that is longer than the 70 characters that will
+> # # demonstrate quoting when text is wrapped to multiple lines. This is
+> # # a line that is longer than the 70 characters that will demonstrate
+> # # quoting when text is wrapped to multiple lines.
+> > This is a line that is longer than the 70 characters that will
+> > demonstrate quoting when text is wrapped to multiple lines. This is a
+> > line that is longer than the 70 characters that will demonstrate
+> > quoting when text is wrapped to multiple lines.
+> 
+> This is a line that is longer than the 70 characters that will
+> demonstrate quoting when text is wrapped to multiple lines. This is a
+> line that is longer than the 70 characters that will demonstrate
+> quoting when text is wrapped to multiple lines.
+QUOTED
+
+    my ($content) = $txn->Content( Quote => 1 );
+    like( $content, qr/$expected/, 'Text quoted properly');
+}
+
+diag "Test different wrap value";
+{
+    my $mail = <<'.';
+From: root at localhost
+Subject: Testing quoting on long lines
+Content-Type: text/plain
+
+> This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+
+This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+.
+
+    my ( $status, $id ) = RT::Test->send_via_mailgate($mail);
+    is( $status >> 8, 0, "The mail gateway exited normally" );
+    ok( $id, "Created ticket $id" );
+    my $ticket = RT::Ticket->new( RT->SystemUser );
+    $ticket->Load( $id );
+    my $txns = $ticket->Transactions;
+    my $txn = $txns->Next;
+
+    my $expected = <<'QUOTED';
+> > This is a line that is longer
+> > than the 70 characters that
+> > will demonstrate quoting when
+> > text is wrapped to multiple
+> > lines.
+> 
+> This is a line that is longer
+> than the 70 characters that
+> will demonstrate quoting when
+> text is wrapped to multiple
+> lines.
+QUOTED
+
+    my ($content) = $txn->Content( Quote => 1, Wrap => 30 );
+    like( $content, qr/$expected/, 'Text quoted properly');
+}
+
+diag "Test no quoting on transaction content";
+{
+    my $mail = <<'.';
+From: root at localhost
+Subject: Testing quoting on long lines
+Content-Type: text/plain
+
+> This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+
+This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+.
+
+    my ( $status, $id ) = RT::Test->send_via_mailgate($mail);
+    is( $status >> 8, 0, "The mail gateway exited normally" );
+    ok( $id, "Created ticket $id" );
+    my $ticket = RT::Ticket->new( RT->SystemUser );
+    $ticket->Load( $id );
+    my $txns = $ticket->Transactions;
+    my $txn = $txns->Next;
+
+    my $expected = <<'QUOTED';
+> This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+
+This is a line that is longer than the 70 characters that will demonstrate quoting when text is wrapped to multiple lines.
+QUOTED
+
+    my ($content) = $txn->Content( ); # Quote defaults to 0
+    like( $content, qr/$expected/, 'Text quoted properly');
+}

-----------------------------------------------------------------------


More information about the Rt-commit mailing list