File Coverage

blib/lib/Log/Report/Lexicon/Index.pm
Criterion Covered Total %
statement 39 68 57.3
branch 3 28 10.7
condition 3 36 8.3
subroutine 12 15 80.0
pod 6 6 100.0
total 63 153 41.1


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 2     2   15 use warnings;
  2         5  
  2         65  
6 2     2   15 use strict;
  2         5  
  2         72  
7              
8             package Log::Report::Lexicon::Index;
9 2     2   12 use vars '$VERSION';
  2         5  
  2         97  
10             $VERSION = '1.08';
11              
12              
13 2     2   15 use Log::Report 'log-report-lexicon';
  2         7  
  2         16  
14 2     2   643 use Log::Report::Util qw/parse_locale/;
  2         6  
  2         138  
15 2     2   19 use File::Find ();
  2         6  
  2         2690  
16              
17             # The next two need extension when other lexicon formats are added
18 0     0   0 sub _understand_file_format($) { $_[0] =~ qr/\.[mp]o$/i }
19              
20             sub _find($$)
21 0     0   0 { my ($index, $name) = (shift, lc shift);
22 0 0       0 $index->{"$name.mo"} || $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 1     1 1 5 { my ($class, $dir) = (shift, shift);
42 1         14 bless {dir => $dir, @_}, $class; # dir before first argument.
43             }
44              
45              
46 2     2 1 48 sub directory() {shift->{dir}}
47              
48              
49             sub index()
50 2     2 1 20 { my $self = shift;
51 2 100       18 return $self->{index} if exists $self->{index};
52              
53 1         5 my $dir = $self->directory;
54 1         26 my $strip_dir = qr!\Q$dir/!;
55              
56 1         6 $self->{index} = {};
57             File::Find::find
58             ( +{ wanted => sub
59 1 0 33 1   123 { -f && !m[/\.] && _understand_file_format($_) or return 1;
      33        
60 0         0 (my $key = $_) =~ s/$strip_dir//;
61 0         0 $self->addFile($key, $_);
62 0         0 1;
63             }
64 1         170 , follow => 1
65             , no_chdir => 1
66             , follow_skip => 2
67             } , $dir
68             );
69              
70 1         14 $self->{index};
71             }
72              
73              
74             sub addFile($;$)
75 1     1 1 4 { my ($self, $base, $abs) = @_;
76 1   33     10 $abs ||= File::Spec->catfile($self->directory, $base);
77 1         5 $base =~ s!\\!/!g; # dos->unix
78 1         8 $self->{index}{lc $base} = $abs;
79             }
80              
81              
82             sub find($$)
83 0     0 1 0 { my $self = shift;
84 0         0 my $domain = lc shift;
85 0         0 my $locale = $locale_unifier->(shift);
86              
87 0         0 my $index = $self->index;
88 0 0       0 keys %$index or return undef;
89              
90 0         0 my ($lang, $terr, $cs, $modif) = parse_locale $locale;
91 0 0       0 unless(defined $lang)
92 0 0       0 { defined $locale or $locale = '';
93             # avoid problem with recursion, not translatable!
94 0         0 print STDERR "illegal locale $locale, when looking for $domain";
95 0         0 return undef;
96             }
97              
98 0 0       0 $terr = defined $terr ? '_'.$terr : '';
99 0 0       0 $cs = defined $cs ? '.'.$cs : '';
100 0 0       0 $modif = defined $modif ? '@'.$modif : '';
101              
102 0         0 (my $normcs = $cs) =~ s/[^a-z\d]//g;
103 0 0       0 if(length $normcs)
104 0 0       0 { $normcs = "iso$normcs" if $normcs !~ /\D/;
105 0         0 $normcs = '.'.$normcs;
106             }
107              
108 0         0 my $fn;
109 0         0 for my $f ("/lc_messages/$domain", "/$domain")
110 0   0     0 { $fn
      0        
111             ||= _find($index, "$lang$terr$cs$modif$f")
112             || _find($index, "$lang$terr$normcs$modif$f")
113             || _find($index, "$lang$terr$modif$f")
114             || _find($index, "$lang$modif$f")
115             || _find($index, "$lang$f");
116             }
117              
118             $fn
119 0 0 0     0 || _find($index, "$domain/$lang$terr$cs$modif")
      0        
      0        
      0        
      0        
      0        
120             || _find($index, "$domain/$lang$terr$normcs$modif")
121             || _find($index, "$domain/$lang$terr$modif")
122             || _find($index, "$domain/$lang$cs$modif")
123             || _find($index, "$domain/$lang$normcs$modif")
124             || _find($index, "$domain/$lang$modif")
125             || _find($index, "$domain/$lang");
126             }
127              
128              
129             sub list($;$)
130 2     2 1 6 { my $self = shift;
131 2         9 my $domain = lc shift;
132 2         6 my $filter = shift;
133 2         11 my $index = $self->index;
134 2         33 my @list = map $index->{$_}, grep m!\b\Q$domain\E\b!, keys %$index;
135              
136 2 50       20 defined $filter
137             or return @list;
138              
139 0 0 0       $filter = qr/\.\Q$filter\E$/i
140             if defined $filter && ref $filter ne 'Regexp';
141              
142 0           grep $_ =~ $filter, @list;
143             }
144              
145             #-------------------------------------
146              
147              
148             1;