line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Widont; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
23773
|
use strict; |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
80
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
76
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
22
|
use Carp qw( croak ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
189
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# By default export the 'widont' function and 'nbsp' constant. |
12
|
2
|
|
|
2
|
|
12
|
use base qw/ Exporter /; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
384
|
|
13
|
|
|
|
|
|
|
our @EXPORT = qw( widont nbsp ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Text::Widont - Suppress typographic widows |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
use Text::Widont; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# For a single string... |
25
|
|
|
|
|
|
|
my $string = 'Look behind you, a Three-Headed Monkey!'; |
26
|
|
|
|
|
|
|
print widont($string, nbsp->{html}); # "...a Three-Headed Monkey!" |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# For a number of strings... |
29
|
|
|
|
|
|
|
my $strings = [ |
30
|
|
|
|
|
|
|
'You fight like a dairy farmer.', |
31
|
|
|
|
|
|
|
'How appropriate. You fight like a cow.', |
32
|
|
|
|
|
|
|
]; |
33
|
|
|
|
|
|
|
print join "\n", @{ widont( $strings, nbsp->{html} ) }; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Or the L |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
use Text::Widont qw( nbsp ); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $tw = Text::Widont->new( nbsp => nbsp->{html} ); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $string = "I'm selling these fine leather jackets."; |
42
|
|
|
|
|
|
|
print $tw->widont($string); # "...fine leather jackets." |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
Collins English Dictionary defines a "widow" in typesetting as: |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
A short line at the end of a paragraph, especially one that occurs as the |
50
|
|
|
|
|
|
|
top line of a page or column. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
For example, in the text... |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
How much wood could a woodchuck |
55
|
|
|
|
|
|
|
chuck if a woodchuck could chuck |
56
|
|
|
|
|
|
|
wood? |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
...the word "wood" at the end is considered a widow. Using C, |
59
|
|
|
|
|
|
|
that sentence would instead appear as... |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
How much wood could a woodchuck |
62
|
|
|
|
|
|
|
chuck if a woodchuck could |
63
|
|
|
|
|
|
|
chuck wood? |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 NON-BREAKING SPACE TYPES |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
C exports a hash ref, C, that contains the following |
69
|
|
|
|
|
|
|
representations of a non-breaking space to be used with the widont function: |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=over |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item html |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
The C< > HTML character entity. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=item html_hex |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
The C< > HTML character entity. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item html_dec |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
The C< > HTML character entity. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item unicode |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Unicode's "No-Break Space" character. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=back |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
2
|
|
|
|
|
883
|
use constant nbsp => { |
96
|
|
|
|
|
|
|
html => ' ', |
97
|
|
|
|
|
|
|
html_dec => ' ', |
98
|
|
|
|
|
|
|
html_hex => ' ', |
99
|
|
|
|
|
|
|
unicode => pack( 'U', 0x00A0 ), |
100
|
2
|
|
|
2
|
|
17
|
}; |
|
2
|
|
|
|
|
4
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 FUNCTIONS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 widont( $string, $nbsp ) |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
The C function takes a string and returns a copy with the space |
108
|
|
|
|
|
|
|
between the final two words replaced with the given C<$nbsp>. C<$string> can |
109
|
|
|
|
|
|
|
optionally be a reference to an array of strings to transform. In this case |
110
|
|
|
|
|
|
|
strings will be modified in place as well as a copy returned. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
In the absence of an explicit C<$nbsp>, Unicode's No-Break Space character |
113
|
|
|
|
|
|
|
will be used. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# This function also acts as an object method as described in the next POD |
120
|
|
|
|
|
|
|
# section. |
121
|
|
|
|
|
|
|
sub widont { |
122
|
4
|
|
|
4
|
1
|
1263
|
my ( $self, $string, $nbsp ); |
123
|
4
|
|
|
|
|
8
|
$string = shift; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# Check to see if the subroutine has been called as an object method... |
126
|
4
|
100
|
|
|
|
14
|
if ( ref $string eq 'Text::Widont' ) { |
127
|
2
|
|
|
|
|
6
|
$self = $string; |
128
|
2
|
|
|
|
|
4
|
$string = shift; |
129
|
|
|
|
|
|
|
|
130
|
2
|
50
|
|
|
|
14
|
$nbsp = $self->{nbsp} eq 'html' ? nbsp->{html} |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
131
|
|
|
|
|
|
|
: $self->{nbsp} eq 'html_dec' ? nbsp->{html_dec} |
132
|
|
|
|
|
|
|
: $self->{nbsp} eq 'html_hex' ? nbsp->{html_hex} |
133
|
|
|
|
|
|
|
: $self->{nbsp} eq 'unicode' ? nbsp->{unicode} |
134
|
|
|
|
|
|
|
: $self->{nbsp}; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Make sure a $string was passed... |
138
|
4
|
50
|
|
|
|
11
|
croak 'widont requires a string' if !defined $string; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# $nbsp defaults to unicode... |
141
|
4
|
|
33
|
|
|
20
|
$nbsp ||= shift || nbsp->{unicode}; |
|
|
|
66
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Iterate over the string(s) to perform the transformation... |
145
|
4
|
100
|
|
|
|
15
|
foreach ( ref $string eq 'ARRAY' ? @$string : $string ) { |
146
|
6
|
|
|
|
|
63
|
s/([^\s])\s+([^\s]+\s*)$/$1$nbsp$2/; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
4
|
|
|
|
|
17
|
return $string; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 METHODS |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
C also provides an object oriented interface. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 -Enew( nbsp => $nbsp ) |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Instantiates a new C object. C is an optional argument |
160
|
|
|
|
|
|
|
that will be used when performing the substitution. It defaults to Unicode's |
161
|
|
|
|
|
|
|
No-Break Space character. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
sub new { |
168
|
2
|
|
|
2
|
1
|
1852
|
my $class = shift; |
169
|
2
|
|
|
|
|
9
|
my $self = bless { @_ }, $class; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# Default to No-Break Space. |
172
|
2
|
|
66
|
|
|
26
|
$self->{nbsp} ||= nbsp->{unicode}; |
173
|
|
|
|
|
|
|
|
174
|
2
|
|
|
|
|
7
|
return $self; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head2 -Ewidont( $string ) |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Performs the substitution described L, using the object's |
181
|
|
|
|
|
|
|
C property and the given string. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# sub widont {} already defined above. |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; # End of the module code; everything from here is documentation... |
192
|
|
|
|
|
|
|
__END__ |