File Coverage

blib/lib/Lingua/EN/Titlecase/Simple.pm
Criterion Covered Total %
statement 30 30 100.0
branch 14 16 87.5
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 52 54 96.3


line stmt bran cond sub pod time code
1 3     3   646301 use 5.008001; use strict; use warnings; use utf8;
  3     3   13  
  3     3   21  
  3     3   5  
  3         109  
  3         22  
  3         7  
  3         365  
  3         1989  
  3         1135  
  3         21  
2              
3             package Lingua::EN::Titlecase::Simple;
4              
5             our $VERSION = '1.015';
6              
7             our @SMALL_WORD
8             = qw/ (?
9              
10             my $apos = q/ (?: ['’] [[:lower:]]* )? /;
11              
12             sub titlecase {
13 4 50   4 1 521030 my @str = @_ or return;
14              
15 4         33 my $small_re = join '|', @SMALL_WORD;
16              
17 4         13 for ( @str ) {
18 41         220 s{\A\s+}{}, s{\s+\z}{};
19              
20 41 100       173 $_ = lc $_ if not /[[:lower:]]/;
21              
22 41         1840 s{
23             \b (_*) (?:
24             ( (?<=[ ][/\\]) [[:alpha:]]+ [-_[:alpha:]/\\]+ | # file path or
25             [-_[:alpha:]]+ [@.:] [-_[:alpha:]@.:/]+ $apos | # URL, domain, or email or
26             [0-9] [0-9,._ ]+ $apos ) # a numeric literal
27             |
28             ( (?i: $small_re ) $apos ) # or small word (case-insensitive)
29             |
30             ( [[:alpha:]] [[:lower:]'’()\[\]{}]* $apos ) # or word w/o internal caps
31             |
32             ( [[:alpha:]] [[:alpha:]'’()\[\]{}]* $apos ) # or some other word
33             ) (_*) \b
34             }{
35 238 100       2298 $1 . (
    100          
    100          
36             defined $2 ? $2 # preserve URL, domain, or email
37             : defined $3 ? "\L$3" # lowercase small word
38             : defined $4 ? "\u\L$4" # capitalize word w/o internal caps
39             : $5 # preserve other kinds of word
40             ) . $6
41             }xeg;
42              
43              
44             # Exceptions for small words: capitalize at start and end of title
45 41         1311 s{
46             ( \A [[:punct:]]* # start of title...
47             | [:.;?!][ ]+ # or of subsentence...
48             | [ ]['"“‘(\[][ ]* ) # or of inserted subphrase...
49             ( $small_re ) \b # ... followed by small word
50             }{$1\u\L$2}xig;
51              
52 41         891 s{
53             \b ( $small_re ) # small word...
54             (?= [[:punct:]]* \Z # ... at the end of the title...
55             | ['"’”)\]] [ ] ) # ... or of an inserted subphrase?
56             }{\u\L$1}xig;
57              
58             # Exceptions for small words in hyphenated compound words
59             ## e.g. "in-flight" -> In-Flight
60 41         962 s{
61             \b
62             (?
63             ( $small_re )
64             (?= -[[:alpha:]]+) # lookahead for "-someword"
65             }{\u\L$1}xig;
66              
67             ## # e.g. "Stand-in" -> "Stand-In" (Stand is already capped at this point)
68 41         484 s{
69             \b
70             (?
71             ( [[:alpha:]]+- ) # $1 = first word and hyphen, should already be properly capped
72             ( $small_re ) # ... followed by small word
73             (?! - ) # Negative lookahead for another '-'
74             }{$1\u$2}xig;
75             }
76              
77 4 50       58 wantarray ? @str : ( @str > 1 ) ? \@str : $str[0];
    100          
78             }
79              
80             sub import {
81 3     3   28 my ( $class, $pkg, $file, $line ) = ( shift, caller );
82 3         15 die "Unknown symbol: $_ at $file line $line.\n" for grep 'titlecase' ne $_, @_;
83 3     3   2590 no strict 'refs';
  3         8  
  3         445  
84 3 100       77 *{ $pkg . '::titlecase' } = \&titlecase if @_;
  2         87  
85             }
86              
87             1;
88              
89             __END__