line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Beautify; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
162544
|
use 5.006; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
13
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
72
|
|
5
|
2
|
|
|
2
|
|
15
|
use warnings; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
1656
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
12
|
|
|
|
|
|
|
beautify enable_feature disable_feature features enabled_features |
13
|
|
|
|
|
|
|
enable_all disable_all |
14
|
|
|
|
|
|
|
) ] ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @EXPORT = qw(); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
our $VERSION = '0.08'; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 NAME |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Text::Beautify - Beautifies text |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Text::Beautify; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
$text = "badly written text ,,you know ?" |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
$new_text = beautify($text); |
33
|
|
|
|
|
|
|
# $new_text now holds "Badly written text, you know?" |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# or |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$text = Text::Beautify->new("badly written text ,,you know ?"); |
38
|
|
|
|
|
|
|
$new_text = $text->beautify; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# and also |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
enable_feature('repeated_punctuation'); # enables the feature |
43
|
|
|
|
|
|
|
disable_feature('trailing_space'); # disables the feature |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
@features_enables = enabled_features(); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
@all_features = features(); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
enable_all(); |
50
|
|
|
|
|
|
|
disable_all(); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 DESCRIPTION |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Beautifies text. This involves operations like squeezing double spaces, |
55
|
|
|
|
|
|
|
removing spaces from the beginning and end of lines, upper casing the |
56
|
|
|
|
|
|
|
first character in a string, etc. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
You can enable / disable features with I / |
59
|
|
|
|
|
|
|
I. These commands return a true value if they |
60
|
|
|
|
|
|
|
are successful. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
To know which features are beautified, see FEATURES |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 FEATURES |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
All features are enabled by default |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=over 4 |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item * heading_space |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Removes heading spaces |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item * trailing_space |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Removes trailing spaces |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item * double_spaces |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Squeezes double spaces |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item * repeated_punctuation |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Squeezes repeated punctuation |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item * space_in_front_of_punctuation |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
Removes spaces in front of punctuation |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item * space_after_punctuation |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Puts a spaces after punctuation |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item * uppercase_first |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Uppercases the first character in the string |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=back |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $debug = 0; |
103
|
|
|
|
|
|
|
my (%features,@features,%status); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
BEGIN { |
106
|
2
|
|
|
2
|
|
6
|
my $empt = '\'\''; |
107
|
2
|
|
|
|
|
81
|
%features = ( |
108
|
|
|
|
|
|
|
heading_space => [[qr/^ +/ , $empt ]], |
109
|
|
|
|
|
|
|
trailing_space => [[qr/ +$/ , $empt ]], |
110
|
|
|
|
|
|
|
space_in_front_of_punctuation => [[qr/ +(?=[,!?]|[:;](?![-)(]))/,$empt ]], |
111
|
|
|
|
|
|
|
double_spaces => [[qr/ +/ , '\' \'' ]], |
112
|
|
|
|
|
|
|
repeated_punctuation => [[qr/([;:,!?])(?=\1)/ , $empt ], |
113
|
|
|
|
|
|
|
[qr/\.{3,}/ , '\'...\''], |
114
|
|
|
|
|
|
|
[qr/(?
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
space_after_punctuation =>[[qr/([;:,!?])(?=[[:alnum:]])/, '"$1 "' ]], |
117
|
|
|
|
|
|
|
uppercase_first => [[qr/([.!?]+\s*[a-z])/i , 'uc($1)' ], |
118
|
|
|
|
|
|
|
[qr/^(\s*[[:alnum:]])/ , 'uc($1)' ], |
119
|
|
|
|
|
|
|
[qr/(?<=[!?] )([a-z])/ , 'uc($1)' ], |
120
|
|
|
|
|
|
|
[qr/(?<=[^.]\. )([a-z])/ , 'uc($1)' ]], |
121
|
|
|
|
|
|
|
); |
122
|
|
|
|
|
|
|
|
123
|
2
|
|
|
|
|
10
|
@features = qw( |
124
|
|
|
|
|
|
|
heading_space |
125
|
|
|
|
|
|
|
trailing_space |
126
|
|
|
|
|
|
|
double_spaces |
127
|
|
|
|
|
|
|
repeated_punctuation |
128
|
|
|
|
|
|
|
space_in_front_of_punctuation |
129
|
|
|
|
|
|
|
space_after_punctuation |
130
|
|
|
|
|
|
|
uppercase_first |
131
|
|
|
|
|
|
|
); |
132
|
|
|
|
|
|
|
|
133
|
2
|
|
|
|
|
4
|
%status = map { ( $_ , 1 ) } @features; # all features enabled by default |
|
14
|
|
|
|
|
11748
|
|
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 METHODS |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 new |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Creates a new Text::Beautify object |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub new { |
145
|
1
|
|
|
1
|
1
|
24
|
my ($self,@text) = @_; |
146
|
1
|
|
|
|
|
6
|
bless \@text, 'Text::Beautify'; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head2 beautify |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Applies all the enabled features |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub beautify { |
156
|
|
|
|
|
|
|
|
157
|
38
|
|
|
38
|
1
|
60
|
my @text; |
158
|
38
|
50
|
|
|
|
110
|
if (ref($_[0]) eq 'Text::Beautify') { |
159
|
0
|
|
|
|
|
0
|
my $self = shift; |
160
|
0
|
|
|
|
|
0
|
@text = @$self; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
else { |
163
|
38
|
50
|
|
|
|
125
|
@text = wantarray ? @_ : $_[0]; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
38
|
|
|
|
|
125
|
for (join "\n", @text) { |
167
|
|
|
|
|
|
|
|
168
|
38
|
|
|
|
|
69
|
for my $feature (@features) { |
169
|
266
|
100
|
|
|
|
684
|
next unless $status{$feature}; |
170
|
154
|
|
|
|
|
237
|
my ($str,$end) = ('',''); |
171
|
154
|
50
|
|
|
|
307
|
($str,$end) = ("<$feature>","$feature>") if $debug; |
172
|
|
|
|
|
|
|
|
173
|
154
|
|
|
|
|
197
|
for my $f (@{$features{$feature}}) { |
|
154
|
|
|
|
|
325
|
|
174
|
264
|
|
|
|
|
1480
|
s/$$f[0]/$str . (eval $$f[1]) . $end/ge; |
|
82
|
|
|
|
|
16229
|
|
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
38
|
|
|
|
|
760
|
return $_; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 enabled_features |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Returns a list with the enabled features |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=cut |
188
|
|
|
|
|
|
|
|
189
|
21
|
|
|
21
|
1
|
738
|
sub enabled_features { grep $status{$_}, keys %features; } |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head2 features |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Returns a list containing all the features |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=cut |
196
|
|
|
|
|
|
|
|
197
|
19
|
|
|
19
|
1
|
121
|
sub features { keys %features; } |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 enable_feature |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Enables a feature |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=cut |
204
|
|
|
|
|
|
|
|
205
|
18
|
|
|
18
|
1
|
79
|
sub enable_feature { _auto_feature(1,@_); } |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head2 disable_feature |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Disables a feature |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=cut |
212
|
|
|
|
|
|
|
|
213
|
32
|
|
|
32
|
1
|
129
|
sub disable_feature { _auto_feature(0,@_); } |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 enable_all |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Enables all features |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=cut |
220
|
|
|
|
|
|
|
|
221
|
2
|
|
|
2
|
1
|
8
|
sub enable_all { _auto_feature(1,features()) } |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 disable_all |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Disables all features |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
228
|
|
|
|
|
|
|
|
229
|
2
|
|
|
2
|
1
|
12
|
sub disable_all { _auto_feature(0,features()) } |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
sub _auto_feature { |
232
|
54
|
|
|
54
|
|
73
|
my $newstatus = shift; |
233
|
54
|
50
|
|
|
|
111
|
for (@_) { defined $features{$_} || return undef; } |
|
210
|
|
|
|
|
27948
|
|
234
|
54
|
|
|
|
|
113
|
for (@_) { $status{$_} = $newstatus; } |
|
210
|
|
|
|
|
27920
|
|
235
|
|
|
|
|
|
|
1 |
236
|
54
|
|
|
|
|
950
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
1; |
239
|
|
|
|
|
|
|
__END__ |