File Coverage

blib/lib/Lingua/EN/Titlecase/Simple.pm
Criterion Covered Total %
statement 33 33 100.0
branch 13 16 81.2
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 55 58 94.8


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2 2     2   156647 use 5.008001; use strict; use warnings; use utf8;
  2     2   18  
  2     2   12  
  2     2   4  
  2         67  
  2         13  
  2         3  
  2         54  
  2         1323  
  2         30  
  2         10  
3              
4             package Lingua::EN::Titlecase::Simple;
5             $Lingua::EN::Titlecase::Simple::VERSION = '1.003';
6             # ABSTRACT: John Gruber's headline capitalization script
7              
8             our @SMALL_WORD
9             = qw/ (?
10              
11             my $apos = q/ (?: ['’] [[:lower:]]* )? /;
12              
13             sub titlecase {
14 4 50   4 1 13736 my @str = @_ or return;
15              
16 4         13 for ( @str ) {
17 39         216 s{\A\s+}{}, s{\s+\z}{};
18              
19 39 100       196 $_ = lc $_ unless /[[:lower:]]/;
20              
21 2     2   24 s{
  2         4  
  2         41  
  39         6222  
22             \b (_*) (?:
23             ( (?<=[ ][/\\]) [[:alpha:]]+ [-_[:alpha:]/\\]+ | # file path or
24             [-_[:alpha:]]+ [@.:] [-_[:alpha:]@.:/]+ $apos ) # URL, domain, or email
25             |
26 2         569 ( (?i) ${\join '|', @SMALL_WORD} $apos ) # or small word (case-insensitive)
27             |
28             ( [[:alpha:]] [[:lower:]'’()\[\]{}]* $apos ) # or word w/o internal caps
29             |
30             ( [[:alpha:]] [[:alpha:]'’()\[\]{}]* $apos ) # or some other word
31             ) (?= _* \b )
32             }{
33 224 100       62600 $1 .
    100          
    100          
34             ( defined $2 ? $2 # preserve URL, domain, or email
35             : defined $3 ? lc $3 # lowercase small word
36             : defined $4 ? ucfirst $4 # capitalize lower-case word
37             : $5 ) # preserve other kinds of word
38             }exgo;
39              
40             # exceptions for small words: capitalize at start and end of title
41 39         475 s{
42             ( \A [[:punct:]]* # start of title...
43             | [:.;?!][ ]+ # or of subsentence...
44             | [ ]['"“‘(\[][ ]* ) # or of inserted subphrase...
45 2         352 ( ${\join '|', @SMALL_WORD} ) \b # ... followed by small word
46             }{$1\u\L$2}xigo;
47              
48 39         392 s{
49 2         195 \b ( ${\join '|', @SMALL_WORD} ) # small word...
50             (?= [[:punct:]]* \Z # ... at the end of the title...
51             | ['"’”)\]] [ ] ) # ... or of an inserted subphrase?
52             }{\u\L$1}xigo;
53             }
54              
55 4 50       72 wantarray ? @str : ( @str > 1 ) ? \@str : $str[0];
    100          
56             }
57              
58             sub import {
59 2     2   29 my ( $class, $pkg, $file, $line ) = ( shift, caller );
60 2         10 die "Unknown symbol: $_ at $file line $line.\n" for grep 'titlecase' ne $_, @_;
61 2     2   1017 no strict 'refs';
  2         7  
  2         384  
62 2 50       10 *{ $pkg . '::titlecase' } = \&titlecase if @_;
  2         63  
63             }
64              
65             return 1 if defined caller;
66              
67             eval 'use open qw( :encoding(UTF-8) :std ); 1' or die;
68             my $opt_force = @ARGV && '-f' eq $ARGV[0];
69             shift @ARGV if $opt_force;
70             shift @ARGV if @ARGV && '--' eq $ARGV[0];
71             print titlecase( $opt_force ? lc : $_ ), "\n" while readline;