File Coverage

blib/lib/App/FileTools/BulkRename/UserCommands/AutoFormat.pm
Criterion Covered Total %
statement 41 41 100.0
branch 12 12 100.0
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 62 63 98.4


line stmt bran cond sub pod time code
1             package App::FileTools::BulkRename::UserCommands::AutoFormat;
2             # ABSTRACT: English title rewriting routines.
3 1     1   1041 use strict;
  1         3  
  1         40  
4 1     1   6 use warnings;
  1         2  
  1         54  
5              
6             BEGIN
7 1     1   74 { our $VERSION = substr '$$Version: 0.07 $$', 11, -3; }
8              
9             require Exporter;
10              
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK=qw(afmt);
13              
14 1     1   6 use Carp;
  1         1  
  1         83  
15 1     1   1504 use Contextual::Return;
  1         30550  
  1         6  
16 1     1   5360 use Text::Autoformat;
  1         86704  
  1         135  
17 1     1   2328 use Lingua::EN::Titlecase;
  1         8579  
  1         9  
18              
19 1     1   626 use App::FileTools::BulkRename::Common qw(modifiable);
  1         2  
  1         274  
20              
21             #
22             # Our eventual hope is that we can eventually handle a great variety
23             # of title formats. Initially we went with the recommended
24             # Text::Autoformat module for everything, but found it inadequate in a
25             # wide variety of ways, such as rendering "façade" as "FaçAde".
26             #
27             # One day we should be able to handle all of the cases
28             # below. Currently we only do cases 1,2,3 and 9.
29             #
30             # 1) Uppercase:
31             # "THE VITAMINS ARE IN MY FRESH CALIFORNIA RAISINS"
32             # 2) Start-Case: Capitalize all words
33             # "The Vitamins Are In My Fresh California Raisins"
34             # 3) Title-Case 1: Capitalize all but internal articles, prepositions,
35             # and conjunctions:
36             # "The Vitamins Are in My Fresh California Raisins"
37             # 4) Title-Case 2: Capitalize all but internal articles, prepositions,
38             # conjunctions and forms of 'to be':
39             # "The Vitamins are in My Fresh California Raisins"
40             # 5) Title-Case 3: Capitalize all but internal closed-class function words
41             # (these are the word classes that are strongly conserved in a language,
42             # and to which it is hard to add new members):
43             # "The Vitamins are in my Fresh California Raisins"
44             # 6) Noun-Case: Just capitalize nouns
45             # "The Vitamins are in my fresh California Raisins"
46             # 7) Sentence case: Just the first word and proper nouns (and a few
47             # other exceptions for English Prose):
48             # "The vitamins are in my fresh California raisins"
49             # 8) Proper-Noun Case: Only proper nouns are capitalized.
50             # "the vitamins are in my fresh California raisins"
51             # 9) Lowercase
52             # "the vitamins are in my fresh california raisins"
53              
54              
55             # autoformat shim. Takes a case conversion name, and optionally
56             # something to convert. If not passed anything to convert, it will
57             # work on $_. If called in void context, it will modify its input
58             # (including $_, when appropriate).
59              
60             my $TC = new Lingua::EN::Titlecase;
61              
62             sub afmt
63 135     135 0 67904 { my $case = shift;
64 135         405 my $opt = { case => $case };
65 135         185 my @ret;
66              
67 135 100       448 return afmt($case,$_) unless @_;
68              
69 90         178 foreach my $in (@_)
70 90         91 { my $out;
71              
72 90 100       367 if( !defined($in) )
    100          
    100          
73 6         8 { $out = undef; }
74             elsif( $in eq '' )
75 6         9 { $out = ''; }
76             elsif( $case eq 'highlight' )
77 18         85 { $out = $TC->title($in); }
78             else
79             {
80             # autoformat gags on blanks and undef's
81 60         189 $out = autoformat($in, $opt);
82              
83 60         134012 chomp($out); chomp($out);
  60         96  
84             }
85 90 100       1852 if( VOID )
86 30         426 { modifiable($in,$_) = $out; }
87             else
88 60         866 { push @ret, $out; }
89             }
90              
91 90 100       287 if( SCALAR )
92 30         490 { return $ret[0]; }
93             else
94 60         881 { return @ret; }
95             }
96              
97             1;