blib/lib/Text/Original.pm

filter: Statementstmt 100% 64 / 64 Branchbran 68.4% 26 / 38 Conditioncond 88.2% 15 / 17 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 88.4% 123 / 139 CC 27 SCAR 31.1
CC
27
Cov
88%
_significant_signal
28.8
first_lines
2.0
BEGIN
1.0
CRAP 22.3
↑ Summary
1 package Text::Original;
2 1 ▶use 5.006; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Text::Original - Find original, non-quoted text in a message
8  
9 =head1 SYNOPSIS
10  
11 use Text::Original;
12 my $sentence = first_sentence($email->body);
13  
14 =head1 FUNCTIONS
15  
16 =cut
17  
18  
19 1 our @ISA = qw(Exporter);
20 1 our %EXPORT_TAGS = ( 'all' => [ qw( first_lines first_paragraph first_sentence) ] );
21 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
22 1 our @EXPORT = ( @{ $EXPORT_TAGS{'all'} } );
23  
24 1 our $VERSION = '1.5';
25  
26 =head2 first_lines
27  
28 first_lines($text, 20);
29  
30 Returns the a number of lines after the first non blank, none quoted
31 line of the body of the email.
32  
33 It will guess at attribution lines and skip them as well.
34  
35 It will return super cited lines. This is the super-citers'
36 fault, not ours.
37  
38 It won't catch all types of attribution lines;
39  
40 It can optionally be passed a number of lines to get.
41  
42 =cut
43  
44 2 ▶sub first_lines {
Subroutine first_lines: called CC=2 SCAR=6.9
Pod documented
45 2 my $text = shift;
46 2 ▶ my $num = shift || 1;
Condition100%2/2
shift() || 1
l!l
11
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
47  
48 2 return _significant_signal($text, lines => $num);
49 }
50  
51  
52 =head2 first_paragraph
53  
54 Returns the first original paragraph of the message
55  
56 =cut
57  
58 2 ▶sub first_paragraph {
Subroutine first_paragraph: called CC=1 SCAR=0.0
Pod documented
59 2 return _significant_signal(shift, para => 1);
60 }
61  
62 =head2 first_sentence
63  
64 Returns the first original sentence of the message
65  
66 =cut
67  
68 1 ▶sub first_sentence {
Subroutine first_sentence: called CC=1 SCAR=0.0
Pod documented
69 1 my $text = first_paragraph(shift);
70 1 $text =~ s/(\p{STerm}) .*/$1/s;
71 1 return $text;
72 }
73  
74 # Kudos to Damian Conway for this bit.
75 1 my $quotechar = qq{[!#%=|:]};
76 1 my $quotechunk = qq{(?:$quotechar(?![a-z])|[a-z]*>+)};
77 1 my $quoter = qq{(?:(?i)(?:$quotechunk(?:[ \\t]*$quotechunk)*))};
78  
79 3 ▶sub _significant_signal {
Subroutine _significant_signal: called CC=26 SCAR=33.6
80 3 my $text = shift;
81 3 my %opts = @_;
82  
83 3 my $return = "";
84 3 my $lines = 0;
85  
86 # get all the lines from the main part of the body
87 3 my @lines = split /$/m, $text;
88  
89 # right, find the start of the original content or quoted
90 # content (i.e. skip past the attributation)
91 3 my $not_started = 1;
92 3 ▶ while (@lines && $not_started) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $not_started) 21 3 24
Condition66%2/3
@lines and $not_started
!ll&&!rl&&r
0321
MC/DC50%1/2
@lines and $not_started
@lines $not_started
Truth table66%2/3
A and B
A= @linesB= $not_started
ABresult
0X0
100
111
93 # next line
94 21 local $_ = shift @lines;
95 #print "}}$_";
96  
97 # blank lines, euurgh
98 21 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 6 15 21
99 # quotes (we don't count quoted From's)
100 15 ▶ next if /^\s*>(?!From)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*>(?!From)/ 9 6 15
101 # Other kinds of quoter:
102 6 ▶ next if /^\s*$quoter/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$quoter/ 0 6 6
103 # skip obvious attribution
104 6 ▶ next if /^\s*On (Mon|Tue|Wed|Thu|Fri|Sat|Sun)/i;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*On (Mon|Tue|Wed|Thu|Fri|Sat|Sun)/i 3 3 6
105 3 ▶ next if /\d{4}-?\w{2,3}-?\d{2}.*\d+:\d+:\d+/i; # Looks like a date
Branch: 1 branch
BranchTrueFalseTotal
if /\d{4}-?\w{2,3}-?\d{2}.*\d+:\d+:\d+/i 0 3 3
106 3 ▶ next if /^\w+(\s\w+)?:$/; # lathos' minimalist attributions. :)
Branch: 1 branch
BranchTrueFalseTotal
if /^\w+(\s\w+)?:$/ 0 3 3
107 3 ▶ next if /^\s*.+=? wrote:/i;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*.+=? wrote:/i 0 3 3
108  
109 # skip signed messages
110 3 ▶ next if /^\s*-----/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*-----/ 0 3 3
111 3 ▶ next if /^Hash:/;
Branch: 1 branch
BranchTrueFalseTotal
if /^Hash:/ 0 3 3
112  
113 # annoying hi messages (this won't work with i18n)
114 3 ▶ next if /^\s*(?:hello|hi|hey|greetings|salut
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(?:hello|hi|hey|greetings|salut |good (?:morning|afternoon|day|evening)) (?:\W.{0,14})?\s*$/six 0 3 3
115 |good (?:morning|afternoon|day|evening))
116 (?:\W.{0,14})?\s*$/ixs;
117  
118 # snips
119 3 ▶ next if m~\s* # whitespace
Branch: 1 branch
BranchTrueFalseTotal
if /\s* # whitespace [<.=-_*+({\[]*? # opening bracket (?:snip|cut|delete|deleted) # snip? [^>}\]]*? # some words? [>.=-_*+)}\]]*? # closing bracket \s*$ # end of the line /xi 0 3 3
120 [<.=-_*+({\[]*? # opening bracket
121 (?:snip|cut|delete|deleted) # snip?
122 [^>}\]]*? # some words?
123 [>.=-_*+)}\]]*? # closing bracket
124 \s*$ # end of the line
125 ~xi;
126  
127 # [.. foo ..] or ...foo.. or so on
128 3 ▶ next if m~\s*\[?\.\..*?\.\.]?\s*$~;
Branch: 1 branch
BranchTrueFalseTotal
if /\s*\[?\.\..*?\.\.]?\s*$/ 0 3 3
129  
130 # ... or [...]
131 3 ▶ next if m~\s*\[?\.\.\.]?\s*$~;
Branch: 1 branch
BranchTrueFalseTotal
if /\s*\[?\.\.\.]?\s*$/ 0 3 3
132  
133 # if we got this far then we've probably got past the
134 # attibutation lines
135 3 unshift @lines, $_; # undo the shift
136 3 undef $not_started; # and say we've started.
137 }
138  
139 # okay, let's _try_ to build up some content then
140 3 foreach (@lines) {
141 # are we at the end of a paragraph?
142 last if (defined $opts{'para'} # paragraph mode?
143 7 ▶ && $opts{'para'}==1
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"para"} and $opts{"para"} == 1 and $lines > 0 and /^\s*$/ 1 6 7
Condition88%8/9
defined $opts{"para"} and $opts{"para"} == 1 and $lines > 0 and /^\s*$/
!ll&&!rl&&r
421
defined $opts{"para"} and $opts{"para"} == 1 and $lines > 0
!ll&&!rl&&r
313
defined $opts{"para"} and $opts{"para"} == 1
!ll&&!rl&&r
304
MC/DC75%3/4
defined $opts{"para"} and $opts{"para"} == 1 and $lines > 0 and /^\s*$/
defined $opts{"para"} $opts{"para"} == 1 $lines > 0 /^\s*$/
Truth table80%4/5
A and B and C and D
A= defined $opts{"para"}B= $opts{"para"} == 1C= $lines > 0D= /^\s*$/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
144 && $lines>0 # got some lines aready?
145 && /^\s*$/); # and now we've found a gap?
146  
147 # blank lines, euurgh
148 6 ▶ next if /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 6 6
149 # quotes (we don't count quoted From's)
150 6 ▶ next if /^\s*>(?!From)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*>(?!From)/ 0 6 6
151  
152 # if we got this far then the line was a useful one
153 6 $lines++;
154  
155 # sort of munged Froms
156 6 s/^>From/From/;
157 6 s/^\n+//;
158 6 ▶ $return .= "\n" if $lines>1;
Branch: 1 branch
BranchTrueFalseTotal
if $lines > 1 3 3 6
159 6 $return .= $_;
160 6 ▶ last if (defined $opts{'lines'} && $opts{'lines'}==$lines);
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"lines"} and $opts{"lines"} == $lines 2 4 6
Condition100%3/3
defined $opts{"lines"} and $opts{"lines"} == $lines
!ll&&!rl&&r
312
MC/DC100%2/2
defined $opts{"lines"} and $opts{"lines"} == $lines
defined $opts{"lines"} $opts{"lines"} == $lines
Truth table100%3/3
A and B
A= defined $opts{"lines"}B= $opts{"lines"} == $lines
ABresult
0X0
100
111
161 }
162 3 return $return;
163 }
164  
165 1 memoize('_significant_signal');
166  
167 1 1;
168  
169 =head1 EXPORTS
170  
171 All of the above.
172  
173 =head1 AUTHOR
174  
175 Simon Wistow and the Mariachi project.
176 See http://siesta.unixbeard.net/
177  
178 Packaged by Simon Cozens
179  
180 Currently maintained by Simon Wistow <simon@thegestalt.org>
181  
182 =head1 COPYRIGHT
183  
184 Copyright 2004 The Siesta Project
185  
186 This library is free software; you can redistribute it and/or modify it
187 under the same terms as Perl itself.
↑ Summary