File Coverage

blib/lib/Locale/Maketext/Extract/Run.pm
Criterion Covered Total %
statement 82 95 86.3
branch 23 42 54.7
condition 3 19 15.7
subroutine 13 16 81.2
pod 0 3 0.0
total 121 175 69.1


line stmt bran cond sub pod time code
1             package Locale::Maketext::Extract::Run;
2             $Locale::Maketext::Extract::Run::VERSION = '1.00';
3 2     2   15792 use strict;
  2         5  
  2         104  
4 2     2   13 use vars qw( @ISA @EXPORT_OK );
  2         3  
  2         285  
5 2     2   1902 use File::Spec::Functions qw(catfile);
  2         1779  
  2         169  
6              
7             # ABSTRACT: Module interface to xgettext.pl
8              
9              
10 2     2   13 use Cwd;
  2         3  
  2         238  
11 2     2   11 use Config ();
  2         12  
  2         32  
12 2     2   10 use File::Find;
  2         4  
  2         252  
13 2     2   2789 use Getopt::Long;
  2         29387  
  2         13  
14 2     2   2417 use Locale::Maketext::Extract;
  2         8  
  2         83  
15 2     2   16 use Exporter;
  2         4  
  2         132  
16              
17 2 50   2   52 use constant HAS_SYMLINK => ( $Config::Config{d_symlink} ? 1 : 0 );
  2         4  
  2         3137  
18              
19             @ISA = 'Exporter';
20             @EXPORT_OK = 'xgettext';
21              
22 1     1 0 1127 sub xgettext { __PACKAGE__->run(@_) }
23              
24             sub run {
25 1     1 0 3 my $self = shift;
26 1         4 local @ARGV = @_;
27              
28 1         2 my %opts;
29 1         6 Getopt::Long::Configure("no_ignore_case");
30 1 50       42 Getopt::Long::GetOptions(
31             \%opts, 'f|files-from:s@',
32             'D|directory:s@', 'u|use-gettext-style|unescaped',
33             'g|gnu-gettext', 'o|output:s@',
34             'd|default-domain:s', 'p|output-dir:s@',
35             'P|plugin:s@', 'W|wrap!',
36             'w|warnings!', 'v|verbose+',
37             'h|help',
38             ) or help();
39              
40 1 50       1102 help() if $opts{h};
41              
42 1         2 my %extract_options = %{ $self->_parse_extract_options( \%opts ) };
  1         4  
43              
44 1 50 0     4 my @po = @{ $opts{o} || [ ( $opts{d} || 'messages' ) . '.po' ] };
  1         5  
45              
46 1 50       2 foreach my $file ( @{ $opts{f} || [] } ) {
  1         5  
47 1 50       41 open FILE, $file or die "Cannot open $file: $!";
48 1         22 while () {
49 2         6 chomp;
50 2 50 33     263 push @ARGV, $_ if -r and !-d;
51             }
52             }
53              
54 1 50       4 foreach my $dir ( @{ $opts{D} || [] } ) {
  1         9  
55             File::Find::find(
56             { wanted => sub {
57 0 0   0   0 if (-d) {
58 0         0 $File::Find::prune
59             = /^(\.svn|blib|autogen|var|m4|local|CVS|\.git)$/;
60 0         0 return;
61             }
62              
63             # Only extract from non-binary, normal files
64 0 0 0     0 return unless ( -f or -s ) and -T;
      0        
65             return
66 0 0 0     0 if (/\.pot?$|\.bak$|~|,D|,B$/i)
67             || (/^[\.#]/);
68 0         0 push @ARGV, $File::Find::name;
69             },
70 0         0 follow => HAS_SYMLINK,
71             },
72             $dir
73             );
74             }
75              
76 1 50       4 @ARGV = ('-') unless @ARGV;
77 1         6 s!^\.[/\\]!! for @ARGV;
78              
79 1         13 my $cwd = getcwd();
80              
81 1         12 my $Ext = Locale::Maketext::Extract->new(%extract_options);
82 1 50       4 foreach my $dir ( @{ $opts{p} || ['.'] } ) {
  1         10  
83 1         10 $Ext->extract_file($_) for grep !/\.po$/i, @ARGV;
84 1         4 foreach my $po (@po) {
85 1 50 33     45 $Ext->read_po($po) if -r $po and -s _;
86 1 50       10 $Ext->compile( $opts{u} ) or next;
87 1         14 $Ext->write_po( catfile( $dir, $po ), $opts{g} );
88             }
89             }
90             }
91              
92             sub _parse_extract_options {
93 12     12   10842 my $self = shift;
94 12         21 my $opts = shift;
95              
96             # If a list of plugins is specified, then we use those modules
97             # plus their default list of file extensions
98             # and warnings enabled by default
99              
100 12   50     89 my %extract_options
101             = ( verbose => $opts->{v}, wrap => $opts->{W} || 0 );
102              
103 12 100       32 if ( my $plugin_args = $opts->{P} ) {
104              
105             # file extension with potentially multiple dots eg .tt.html
106 10         16 my %plugins;
107              
108 10         17 foreach my $param (@$plugin_args) {
109 10         56 my ( $plugin, $args )
110             = ( $param =~ /^([a-z_]\w+(?:::\w+)*)(?:=(.+))?$/i );
111 10 100       33 die "Couldn't understand plugin option '$param'"
112             unless $plugin;
113 9         8 my @extensions;
114 9 100       24 if ($args) {
115 8         22 foreach my $arg ( split /,/, $args ) {
116 14 100       28 if ( $arg eq '*' ) {
117 2         5 @extensions = ('*');
118 2         4 last;
119             }
120 12         46 my ($extension) = ( $arg =~ /^\.?(\w+(?:\.\w+)*)$/ );
121 12 100       34 die "Couldn't understand '$arg' in plugin '$param'"
122             unless defined $extension;
123 11         26 push @extensions, $extension;
124             }
125             }
126              
127 8         31 $plugins{$plugin} = \@extensions;
128             }
129 8         16 $extract_options{plugins} = \%plugins;
130 8 50       25 $extract_options{warnings} = exists $opts->{w} ? $opts->{w} : 1;
131             }
132              
133             # otherwise we default to the original xgettext.pl modules
134             # with warnings disabled by default
135             else {
136 2         6 $extract_options{warnings} = $opts->{w};
137             }
138 10         32 return \%extract_options;
139              
140             }
141              
142             sub help {
143 0     0 0   local $SIG{__WARN__} = sub { };
  0     0      
144 0           { exec "perldoc $0"; }
  0            
145 0           { exec "pod2text $0"; }
  0            
146             }
147              
148             1;
149              
150             __END__