
jbrandt at bestpractical
Aug 9, 2012, 6:32 AM
Post #1 of 1
(87 views)
Permalink
|
|
rt branch, 4.0/text-wrap-and-quoting-long-lines, created. rt-4.0.6-250-gb83cb65
|
|
The branch, 4.0/text-wrap-and-quoting-long-lines has been created at b83cb65eca749b21f152cc97f2425a94f6eec414 (commit) - Log ----------------------------------------------------------------- commit b83cb65eca749b21f152cc97f2425a94f6eec414 Author: Jim Brandt <jbrandt [at] bestpractical> 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..59695e1 100644 --- a/lib/RT/Transaction.pm +++ b/lib/RT/Transaction.pm @@ -363,24 +363,9 @@ sub Content { } if ( $args{'Quote'} ) { + $content = $self->ApplyQuoteWrap(content => $content, + cols => $args{'Wrap'} ); - # What's the longest line like? - my $max = 0; - foreach ( split ( /\n/, $content ) ) { - $max = length if length > $max; - } - - if ( $max > 76 ) { - require Text::Wrapper; - my $wrapper = Text::Wrapper->new( - columns => $args{'Wrap'}, - body_start => ( $max > 70 * 3 ? ' ' : '' ), - par_start => '' - ); - $content = $wrapper->wrap($content); - } - - $content =~ s/^/> /gm; $content = $self->loc("On [_1], [_2] wrote:", $self->CreatedAsString, $self->CreatorObj->Name) . "\n$content\n\n"; } @@ -388,6 +373,79 @@ sub Content { return ($content); } +=head2 ApplyQuoteWrap PARAMHASH + +Wrapper to calculate wrap criteria and apply quote wrapping if needed. + +=cut + +sub ApplyQuoteWrap { + my $self = shift; + my %args = @_; + my $content = $args{content}; + + # What's the longest line like? + my $max = 0; + foreach ( split ( /\n/, $args{content} ) ) { + $max = length if length > $max; + } + + if ( $max > 76 ) { + require Text::Quoted; + require Text::Wrapper; + + my $structure = Text::Quoted::extract($args{content}); + $content = $self->QuoteWrap(content_ref => $structure, + cols => $args{cols}, + max => $max ); + } + + $content =~ s/^/> /gm; # use regex since string might be multi-line + 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 ); + + 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/mg; # use regex since string might be multi-line + + 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..9bcb536 --- /dev/null +++ b/t/api/transaction.t @@ -0,0 +1,181 @@ + +use strict; +use warnings; +use RT; +use RT::Test tests => 16; + +use_ok('RT::Transaction'); + +diag "Test quoting on transaction content"; +{ + my $mail = <<'.'; +From: root [at] localhos +Subject: Testing quoting on long lines +Content-Type: text/plain + +> This is a short line. + +This is a short line. +. + + 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 short line. +> +> This is a short line. +QUOTED + + my ($content) = $txn->Content( Quote => 1 ); + like( $content, qr/$expected/, 'Text quoted properly'); +} + +diag "Test quoting on transaction content with lines > 70 chars"; +{ + my $mail = <<'.'; +From: root [at] localhos +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] localhos +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] localhos +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] localhos +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'); +} ----------------------------------------------------------------------- _______________________________________________ Rt-commit mailing list Rt-commit [at] lists http://lists.bestpractical.com/cgi-bin/mailman/listinfo/rt-commit
|