File Coverage

blib/lib/Log/Report/Lexicon/Index.pm
Criterion Covered Total %
statement 63 68 92.6
branch 15 28 53.5
condition 21 39 53.8
subroutine 15 15 100.0
pod 6 6 100.0
total 120 156 76.9


line stmt bran cond sub pod time code
1             # Copyrights 2007-2017 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5 3     3   17 use warnings;
  3         4  
  3         115  
6 3     3   15 use strict;
  3         6  
  3         84  
7              
8             package Log::Report::Lexicon::Index;
9 3     3   14 use vars '$VERSION';
  3         3  
  3         117  
10             $VERSION = '1.09';
11              
12              
13 3     3   13 use Log::Report 'log-report-lexicon';
  3         6  
  3         13  
14 3     3   627 use Log::Report::Util qw/parse_locale/;
  3         5  
  3         125  
15 3     3   15 use File::Find ();
  3         6  
  3         2507  
16              
17             # The next two need extension when other lexicon formats are added
18 27     27   290 sub _understand_file_format($) { $_[0] =~ qr/\.(?:gmo|mo|po)$/i }
19              
20             sub _find($$)
21 157     157   213 { my ($index, $name) = (shift, lc shift);
22 157 50 66     770 $index->{"$name.mo"} || $index->{"name.gmo"} || $index->{"$name.po"}; # prefer mo
23             }
24              
25             # On windows, other locale names are used. They will get translated
26             # into the Linux (ISO) convensions.
27              
28             my $locale_unifier;
29             if($^O eq 'MSWin32')
30             { require Log::Report::Win32Locale;
31             Log::Report::Win32Locale->import;
32             $locale_unifier = sub { iso_locale($_[0]) };
33             }
34             else
35             { # some UNIXes do not understand "POSIX"
36             $locale_unifier = sub { uc $_[0] eq 'POSIX' ? 'c' : lc $_[0] };
37             }
38              
39              
40             sub new($;@)
41 2     2 1 5 { my ($class, $dir) = (shift, shift);
42 2         15 bless {dir => $dir, @_}, $class; # dir before first argument.
43             }
44              
45             #-------------------
46              
47 4     4 1 821 sub directory() {shift->{dir}}
48              
49             #-------------------
50              
51             sub index()
52 20     20 1 23 { my $self = shift;
53 20 100       54 return $self->{index} if exists $self->{index};
54              
55 2         5 my $dir = $self->directory;
56 2         27 my $strip_dir = qr!\Q$dir/!;
57              
58 2         6 $self->{index} = {};
59             File::Find::find
60             ( +{ wanted => sub
61 31 100 66 31   457 { -f && !m[/\.] && _understand_file_format($_) or return 1;
      100        
62 15         71 (my $key = $_) =~ s/$strip_dir//;
63 15         30 $self->addFile($key, $_);
64 15         202 1;
65             }
66 2         247 , follow => 1
67             , no_chdir => 1
68             , follow_skip => 2
69             } , $dir
70             );
71              
72 2         20 $self->{index};
73             }
74              
75              
76             sub addFile($;$)
77 16     16 1 26 { my ($self, $base, $abs) = @_;
78 16   66     26 $abs ||= File::Spec->catfile($self->directory, $base);
79 16         21 $base =~ s!\\!/!g; # dos->unix
80 16         36 $self->{index}{lc $base} = $abs;
81             }
82              
83              
84             sub find($$)
85 14     14 1 815 { my $self = shift;
86 14         25 my $domain = lc shift;
87 14         21 my $locale = $locale_unifier->(shift);
88              
89 14         34 my $index = $self->index;
90 14 50       30 keys %$index or return undef;
91              
92 14         36 my ($lang, $terr, $cs, $modif) = parse_locale $locale;
93 14 50       409 unless(defined $lang)
94 0 0       0 { defined $locale or $locale = '';
95             # avoid problem with recursion, not translatable!
96 0         0 print STDERR "illegal locale $locale, when looking for $domain";
97 0         0 return undef;
98             }
99              
100 14 100       32 $terr = defined $terr ? '_'.$terr : '';
101 14 50       25 $cs = defined $cs ? '.'.$cs : '';
102 14 50       19 $modif = defined $modif ? '@'.$modif : '';
103              
104 14         23 (my $normcs = $cs) =~ s/[^a-z0-9]//g;
105 14 50       24 if(length $normcs)
106 0 0       0 { $normcs = "iso$normcs" if $normcs !~ /[^0-9-]/;
107 0         0 $normcs = '.'.$normcs;
108             }
109              
110 14         16 my $fn;
111 14         34 for my $f ("/lc_messages/$domain", "/$domain")
112 28   33     77 { $fn
      33        
113             ||= _find($index, "$lang$terr$cs$modif$f")
114             || _find($index, "$lang$terr$normcs$modif$f")
115             || _find($index, "$lang$terr$modif$f")
116             || _find($index, "$lang$modif$f")
117             || _find($index, "$lang$f");
118             }
119              
120             $fn
121 14 0 66     63 || _find($index, "$domain/$lang$terr$cs$modif")
      66        
      66        
      33        
      33        
      33        
122             || _find($index, "$domain/$lang$terr$normcs$modif")
123             || _find($index, "$domain/$lang$terr$modif")
124             || _find($index, "$domain/$lang$cs$modif")
125             || _find($index, "$domain/$lang$normcs$modif")
126             || _find($index, "$domain/$lang$modif")
127             || _find($index, "$domain/$lang");
128             }
129              
130              
131             sub list($;$)
132 4     4 1 539 { my $self = shift;
133 4         9 my $domain = lc shift;
134 4         6 my $filter = shift;
135 4         9 my $index = $self->index;
136 4         81 my @list = map $index->{$_}, grep m!\b\Q$domain\E\b!, keys %$index;
137              
138 4 100       22 defined $filter
139             or return @list;
140              
141 1 50 33     14 $filter = qr/\.\Q$filter\E$/i
142             if defined $filter && ref $filter ne 'Regexp';
143              
144 1         14 grep $_ =~ $filter, @list;
145             }
146              
147             #-------------------------------------
148              
149              
150             1;