blib/lib/Text/Quoted.pm

filter: Statementstmt 98.8% 82 / 83 Branchbran 88.2% 30 / 34 Conditioncond 92.8% 39 / 42 MC/DCmcdc 86.9% 20 / 23 Subroutinesub 100% 11 / 11 Podpod 100% 3 / 3 totaltotal 94.3% 185 / 196 CC 37 SCAR 27.0
CC
37
Cov
95%
_classify
24.1
_organize
10.0
_find_below
3.0
CRAP 14.9
↑ Summary
1 package Text::Quoted;
2 7 our $VERSION = "2.10";
3 7 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 require Exporter;
8  
9 7 our @ISA = qw(Exporter);
10 7 our @EXPORT = qw(extract);
11 7 our @EXPORT_OK = qw(set_quote_characters combine_hunks);
12  
13 # Provides either the 'Hang' package -- or, on since version 1.69 of
14 # Text::Autoformat, the 'Text::Autoformat::Hang' package.
15 7 ▶use Text::Autoformat();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶my $hang_package = Hang->can('new') ? "Hang" : "Text::Autoformat::Hang";
Branch: 1 branch
BranchTrueFalseTotal
'Hang'->can('new') ? : 0 7 7
17  
18 7 ▶use Text::Tabs();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 Text::Quoted - Extract the structure of a quoted mail message
23  
24 =head1 SYNOPSIS
25  
26 use Text::Quoted;
27 my $structure = extract($text);
28  
29 # Optionally, customize recognized quote characters:
30 Text::Quoted::set_quote_characters( qr/[:]/ );
31  
32 =head1 DESCRIPTION
33  
34 C<Text::Quoted> examines the structure of some text which may contain
35 multiple different levels of quoting, and turns the text into a nested
36 data structure.
37  
38 The structure is an array reference containing hash references for each
39 paragraph belonging to the same author. Each level of quoting
40 recursively adds another list reference. So for instance, this:
41  
42 > foo
43 > # Bar
44 > baz
45  
46 quux
47  
48 turns into:
49  
50 [
51 [
52 { text => 'foo', quoter => '>', raw => '> foo' },
53 [
54 { text => 'Bar', quoter => '> #', raw => '> # Bar' }
55 ],
56 { text => 'baz', quoter => '>', raw => '> baz' }
57 ],
58  
59 { empty => 1 },
60 { text => 'quux', quoter => '', raw => 'quux' }
61 ];
62  
63 This also tells you about what's in the hash references: C<raw> is the
64 paragraph of text as it appeared in the original input; C<text> is what
65 it looked like when we stripped off the quotation characters, and
66 C<quoter> is the quotation string.
67  
68 =head1 FUNCTIONS
69  
70 =head2 extract
71  
72 my $struct = extract($text, \%arg);
73  
74 Takes a single string argument which is the text to extract quote
75 structure from. Returns a nested datastructure as described above.
76  
77 Second argument is optional: a hashref of options. The only valid
78 argument at present is:
79  
80 no_separators - never mark paragraphs as "separators"
81  
82 Exported by default.
83  
84 =cut
85  
86 22 ▶sub extract {
Subroutine extract: called CC=1 SCAR=0.0
Pod documented
87 22 return _organize( "", _classify( @_ ) );
88 }
89  
90 63 ▶sub _organize {
Subroutine _organize: called CC=10 SCAR=23.0
91 63 my $top_level = shift;
92 63 my @todo = @_;
93 63 ▶ $top_level = '' unless defined $top_level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $top_level 0 63 63
94  
95 63 my @ret;
96  
97 # Recursively form a data structure which reflects the quoting
98 # structure of the list.
99 63 ▶ while (my $line = shift @todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @todo) 152 63 215
100 152 ▶ my $q = defined $line->{quoter}? $line->{quoter}: '';
Branch: 1 branch
BranchTrueFalseTotal
defined $line->{'quoter'} ? : 136 16 152
101 152 ▶ if ( $q eq $top_level ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($q eq $top_level) { } 105 47 152
elsif ($q =~ /^\Q$top_level\E./) { } 41 6 47
102  
103 # Just append lines at "my" level.
104 push @ret, $line
105 if exists $line->{quoter}
106 105 ▶ or exists $line->{empty};
Branch: 1 branch
BranchTrueFalseTotal
if exists $line->{"quoter"} or exists $line->{"empty"} 97 8 105
Condition66%2/3
exists $line->{"quoter"} or exists $line->{"empty"}
l!l&&r!l&&!r
9708
MC/DC50%1/2
exists $line->{"quoter"} or exists $line->{"empty"}
exists $line->{"quoter"} exists $line->{"empty"}
Truth table66%2/3
A or B
A= exists $line->{"quoter"}B= exists $line->{"empty"}
ABresult
1X1
011
000
107 }
108 elsif ( $q =~ /^\Q$top_level\E./ ) {
109  
110 # Find all the lines at a quoting level "below" me.
111 41 my $newquoter = _find_below( $top_level, $line, @todo );
112 41 my @next = $line;
113 push @next, shift @todo while defined $todo[0]->{quoter}
114 41 ▶ and $todo[0]->{quoter} =~ /^\Q$newquoter/;
Branch: 1 branch
BranchTrueFalseTotal
defined $todo[0]{"quoter"} and $todo[0]{"quoter"} =~ /^\Q$newquoter\E/ and do { push @next, shift @todo } 13 41 54
Condition100%3/3
defined $todo[0]{"quoter"} and $todo[0]{"quoter"} =~ /^\Q$newquoter\E/
!ll&&!rl&&r
142713
MC/DC100%2/2
defined $todo[0]{"quoter"} and $todo[0]{"quoter"} =~ /^\Q$newquoter\E/
defined $todo[0]{"quoter"} $todo[0]{"quoter"} =~ /^\Q$newquoter\E/
Truth table100%3/3
A and B
A= defined $todo[0]{"quoter"}B= $todo[0]{"quoter"} =~ /^\Q$newquoter\E/
ABresult
0X0
100
111
115  
116 # And pass them on to _organize()!
117 41 push @ret, _organize( $newquoter, @next );
118 }
119 }
120 63 return \@ret;
121 }
122  
123 # Given, say:
124 # X
125 # > > hello
126 # > foo bar
127 # Stuff
128 #
129 # After "X", we're moving to another level of quoting - but which one?
130 # Naively, you'd pick out the prefix of the next line, "> >", but this
131 # is incorrect - "> >" is actually a "sub-quote" of ">". This routine
132 # works out which is the next level below us.
133  
134 41 ▶sub _find_below {
Subroutine _find_below: called CC=3 SCAR=11.0
135 41 my ( $top_level, @stuff ) = @_;
136  
137 # Find the prefixes, shortest first; return the first one which is
138 # "below" where we are right now but is a proper subset of the next
139 # line.
140 return (
141 22 ▶ sort { length $a <=> length $b }
Condition100%6/6
$_ && /^\Q$top_level\E./ && $stuff[0]{'quoter'} =~ /^\Q$_\E/
!ll&&!rl&&r
851660
$_ and /^\Q$top_level\E./
!ll&&!rl&&r
81476
MC/DC100%3/3
$_ && /^\Q$top_level\E./ && $stuff[0]{'quoter'} =~ /^\Q$_\E/
$_ /^\Q$top_level\E./ $stuff[0]{'quoter'} =~ /^\Q$_\E/
Truth table100%4/4
A and B && C
A= $_B= /^\Q$top_level\E./C= $stuff[0]{'quoter'} =~ /^\Q$_\E/
ABCresult
0XX0
10X0
1100
1111
142 grep $_ && /^\Q$top_level\E./ && $stuff[0]->{quoter} =~ /^\Q$_\E/,
143 map $_->{quoter},
144 @stuff
145 41 )[0];
146 }
147  
148 # BITS OF A TEXT LINE
149  
150 =head2 set_quote_characters
151  
152 Takes a regex (C<qr//>) matching characters that should indicate a
153 quoted line. By default, a very liberal set is used:
154  
155 set_quote_characters(qr/[!#%=|:]/);
156  
157 The character C<< E<gt> >> is always recognized as a quoting character.
158  
159 If C<undef> is provided instead of a regex, only C<< E<gt> >> will
160 remain as a quote character.
161  
162 Not exported by default, but exportable.
163  
164 =cut
165  
166 7 my $separator = qr/[-_]{2,} | [=#*]{3,} | [+~]{4,}/x;
167 7 my ($quotechar, $quotechunk, $quoter);
168  
169 7 set_quote_characters(qr/[!#%=|:]/);
170  
171 9 ▶sub set_quote_characters {
Subroutine set_quote_characters: called CC=2 SCAR=6.9
Pod documented
172 9 $quotechar = shift;
173 9 ▶ $quotechunk = $quotechar
Branch: 1 branch
BranchTrueFalseTotal
$quotechar ? : 8 1 9
174 ? qr/(?!$separator *\z)(?:$quotechar(?!\w)|\w*>+)/
175 : qr/(?!$separator *\z)\w*>+/;
176 9 $quoter = qr/$quotechunk(?:[ \t]*$quotechunk)*/;
177 }
178  
179 =head2 combine_hunks
180  
181 my $text = combine_hunks( $arrayref_of_hunks );
182  
183 Takes the output of C<extract> and turns it back into text.
184  
185 Not exported by default, but exportable.
186  
187 =cut
188  
189 13 ▶sub combine_hunks {
Subroutine combine_hunks: called CC=2 SCAR=6.9
Pod documented
190 13 my ($hunks) = @_;
191  
192 join "",
193 13 ▶ map {; ref $_ eq 'HASH' ? "$_->{raw}\n" : combine_hunks($_) } @$hunks;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 25 10 35
194 }
195  
196 22 ▶sub _classify {
Subroutine _classify: called CC=24 SCAR=31.8
197 22 my ($text, $arg) = @_;
198 22 ▶ $arg ||= {};
Condition100%2/2
$arg ||= {}
l!l
220
MC/DC100%1/1
$arg ||= {}
$arg
Truth table100%2/2
A
A= $arg
Aresult
11
00
199  
200 # If the user passes in a null string, we really want to end up with
201 # _something_
202 22 ▶ return { raw => undef, text => undef, quoter => undef }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 2 20 22
Condition100%3/3
defined $text and length $text
!ll&&!rl&&r
1120
MC/DC100%2/2
defined $text and length $text
defined $text length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
203 unless defined $text && length $text;
204  
205 # DETABIFY
206 20 my @lines = Text::Tabs::expand( split /\n/, $text );
207  
208 # PARSE EACH LINE
209 20 foreach (splice @lines) {
210 136 my %line = ( raw => $_ );
211 136 @line{'quoter', 'text'} = (/\A *($quoter?) *(.*?)\s*\Z/);
212 136 $line{hang} = $hang_package->new( $line{'text'} );
213 136 ▶ $line{empty} = 1 if $line{hang}->empty() && $line{'text'} !~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if $line{"hang"}->empty and not $line{"text"} =~ /\S/ 19 117 136
Condition66%2/3
$line{"hang"}->empty and not $line{"text"} =~ /\S/
!ll&&!rl&&r
011719
MC/DC50%1/2
$line{"hang"}->empty and not $line{"text"} =~ /\S/
$line{"hang"}->empty not $line{"text"} =~ /\S/
Truth table66%2/3
A and not(B)
A= $line{"hang"}->emptyB= not $line{"text"} =~ /\S/
ABresult
0X0
100
111
214 $line{separator} = 1 if $line{text} =~ /\A *$separator *\Z/o
215 136 ▶ and ! $arg->{no_separators};
Branch: 1 branch
BranchTrueFalseTotal
if $line{"text"} =~ /\A *$separator *\Z/o and not $arg->{"no_separators"} 6 130 136
Condition100%3/3
$line{"text"} =~ /\A *$separator *\Z/o and not $arg->{"no_separators"}
!ll&&!rl&&r
12646
MC/DC100%2/2
$line{"text"} =~ /\A *$separator *\Z/o and not $arg->{"no_separators"}
$line{"text"} =~ /\A *$separator *\Z/o not $arg->{"no_separators"}
Truth table100%3/3
A and not(B)
A= $line{"text"} =~ /\A *$separator *\Z/oB= not $arg->{"no_separators"}
ABresult
0X0
100
111
216 136 push @lines, \%line;
217 }
218  
219 # SUBDIVIDE DOCUMENT INTO COHERENT SUBSECTIONS
220  
221 20 my @chunks;
222 20 push @chunks, [ shift @lines ];
223 20 foreach my $line (@lines) {
224 116 ▶ if ( $line->{separator}
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{'separator'} or $line->{'quoter'} ne $chunks[-1][-1]{'quoter'} or $line->{'empty'} or $chunks[-1][-1]{'empty'}) { } 70 46 116
Condition100%9/9
$line->{"separator"} or $line->{"quoter"} ne $chunks[-1][-1]{"quoter"} or $line->{"empty"} or $chunks[-1][-1]{"empty"}
l!l&&r!l&&!r
63746
$line->{"separator"} or $line->{"quoter"} ne $chunks[-1][-1]{"quoter"} or $line->{"empty"}
l!l&&r!l&&!r
57653
$line->{"separator"} or $line->{"quoter"} ne $chunks[-1][-1]{"quoter"}
l!l&&r!l&&!r
65159
MC/DC100%4/4
$line->{"separator"} or $line->{"quoter"} ne $chunks[-1][-1]{"quoter"} or $line->{"empty"} or $chunks[-1][-1]{"empty"}
$line->{"separator"} $line->{"quoter"} ne $chunks[-1][-1]{"quoter"} $line->{"empty"} $chunks[-1][-1]{"empty"}
Truth table100%5/5
A or B or C or D
A= $line->{"separator"}B= $line->{"quoter"} ne $chunks[-1][-1]{"quoter"}C= $line->{"empty"}D= $chunks[-1][-1]{"empty"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
225 || $line->{quoter} ne $chunks[-1][-1]->{quoter}
226 || $line->{empty}
227 || $chunks[-1][-1]->{empty} )
228 {
229 70 push @chunks, [$line];
230 }
231 else {
232 46 push @{ $chunks[-1] }, $line;
233 }
234 }
235  
236 # REDIVIDE INTO PARAGRAPHS
237  
238 20 my @paras;
239 20 foreach my $chunk (@chunks) {
240 90 my $first = 1;
241 90 my $firstfrom;
242 90 foreach my $line ( @{$chunk} ) {
243 136 ▶ if ( $first
Branch: 1 branch
BranchTrueFalseTotal
if ($first or $line->{'quoter'} ne $paras[-1]{'quoter'} or $paras[-1]{'separator'}) { } 95 41 136
Condition83%5/6
$first or $line->{"quoter"} ne $paras[-1]{"quoter"} or $paras[-1]{"separator"}
l!l&&r!l&&!r
90541
$first or $line->{"quoter"} ne $paras[-1]{"quoter"}
l!l&&r!l&&!r
90046
MC/DC66%2/3
$first or $line->{"quoter"} ne $paras[-1]{"quoter"} or $paras[-1]{"separator"}
$first $line->{"quoter"} ne $paras[-1]{"quoter"} $paras[-1]{"separator"}
Truth table75%3/4
A or B or C
A= $firstB= $line->{"quoter"} ne $paras[-1]{"quoter"}C= $paras[-1]{"separator"}
ABCresult
1XX1
01X1
0011
0000
244 || $line->{quoter} ne $paras[-1]->{quoter}
245 || $paras[-1]->{separator} )
246 {
247 95 push @paras, $line;
248 95 $first = 0;
249 # We get warnings from undefined raw and text values if we don't supply alternates
250 95 ▶ $firstfrom = length( $line->{raw} ||'' ) - length( $line->{text} || '');
Condition100%4/4
$line->{'raw'} || ''
l!l
7916
$line->{'text'} || ''
l!l
7619
MC/DC100%1/1
$line->{'raw'} || ''
$line->{'raw'}
MC/DC100%1/1
$line->{'text'} || ''
$line->{'text'}
Truth table100%2/2
A
A= $line->{'raw'}
Aresult
11
00
Truth table100%2/2
A
A= $line->{'text'}
Aresult
11
00
251 }
252 else {
253 my $extraspace =
254 41 length( $line->{raw} ) - length( $line->{text} ) - $firstfrom;
255 41 ▶ $extraspace = 0 if $extraspace < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $extraspace < 0 0 41 41
256 41 $paras[-1]->{text} .= "\n" . q{ } x $extraspace . $line->{text};
257 41 $paras[-1]->{raw} .= "\n" . $line->{raw};
258 }
259 }
260 }
261  
262 # Reapply hangs
263 20 for (grep $_->{'hang'}, @paras) {
264 95 ▶ next unless my $str = (delete $_->{hang})->stringify;
Branch: 1 branch
BranchTrueFalseTotal
unless my $str = delete $_->{"hang"}->stringify 95 0 95
265 0 $_->{text} = $str . " " . $_->{text};
266 }
267 20 return @paras;
268 }
269  
270 =head1 CREDITS
271  
272 Most of the heavy lifting is done by a modified version of Damian
273 Conway's C<Text::Autoformat>.
274  
275 =head1 AUTHOR
276  
277 Best Practical Solutions, LLC E<lt>modules@bestpractical.comE<gt>
278  
279 =head1 LICENSE AND COPYRIGHT
280  
281 This software is Copyright (c) 2004-2015 by Best Practical Solutions, LLC
282  
283 This library is free software; you can redistribute it and/or modify it
284 under the same terms as Perl itself.
285  
286 =cut
287  
288 7 1;
↑ Summary