File Coverage

blib/lib/Log/Report/Lexicon/Index.pm
Criterion Covered Total %
statement 60 65 92.3
branch 16 28 57.1
condition 21 39 53.8
subroutine 14 14 100.0
pod 6 6 100.0
total 117 152 76.9


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Log-Report-Lexicon version 1.15.
2             # The POD got stripped from this file by OODoc version 3.05.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2007-2025 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11             #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION ***
12             #oodist: This file contains OODoc-style documentation which will get stripped
13             #oodist: during its release in the distribution. You can use this file for
14             #oodist: testing, however the code of this development version may be broken!
15             #oorestyle: old style disclaimer to be removed.
16              
17             # This code is part of distribution Log-Report-Lexicon. Meta-POD processed
18             # with OODoc into POD and HTML manual-pages. See README.md
19             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
20              
21             package Log::Report::Lexicon::Index;{
22             our $VERSION = '1.15';
23             }
24              
25              
26 3     3   23 use warnings;
  3         5  
  3         173  
27 3     3   16 use strict;
  3         6  
  3         77  
28              
29 3     3   12 use Log::Report 'log-report-lexicon';
  3         15  
  3         35  
30 3     3   977 use Log::Report::Util qw/parse_locale/;
  3         9  
  3         197  
31 3     3   20 use File::Find ();
  3         5  
  3         4374  
32              
33             # The next two need extension when other lexicon formats are added
34 28     28   532 sub _understand_file_format($) { $_[0] =~ qr/\.(?:gmo|mo|po)$/i }
35              
36             sub _find($$)
37 157     157   198 { my ($index, $name) = (shift, lc shift);
38 157 100 66     768 $index->{"$name.mo"} || $index->{"$name.gmo"} || $index->{"$name.po"}; # prefer mo
39             }
40              
41             # On windows, other locale names are used. They will get translated
42             # into the Linux (ISO) convensions.
43              
44             my $locale_unifier;
45             if($^O eq 'MSWin32')
46             { require Log::Report::Win32Locale;
47             Log::Report::Win32Locale->import;
48             $locale_unifier = sub { iso_locale($_[0]) };
49             }
50             else
51             { # some UNIXes do not understand "POSIX"
52             $locale_unifier = sub { uc $_[0] eq 'POSIX' ? 'c' : lc $_[0] };
53             }
54              
55             #--------------------
56              
57             sub new($;@)
58 2     2 1 7 { my ($class, $dir) = (shift, shift);
59 2         21 bless +{ dir => $dir, @_ }, $class; # dir before first argument.
60             }
61              
62             #--------------------
63              
64 4     4 1 1305 sub directory() { $_[0]->{dir} }
65              
66             #--------------------
67              
68             sub index()
69 20     20 1 27 { my $self = shift;
70 20 100       69 return $self->{index} if exists $self->{index};
71              
72 2         7 my $dir = $self->directory;
73 2         45 my $strip_dir = qr!\Q$dir/!;
74              
75 2         10 $self->{index} = {};
76             File::Find::find( +{
77             wanted => sub {
78 32 100 66 32   1045 -f && !m[/\.] && _understand_file_format($_) or return 1;
      100        
79 15         63 (my $key = $_) =~ s/$strip_dir//;
80 15         44 $self->addFile($key, $_);
81 15         368 1;
82             },
83 2         377 follow => 1,
84             no_chdir => 1,
85             follow_skip => 2
86             }, $dir);
87              
88 2         22 $self->{index};
89             }
90              
91              
92             sub addFile($;$)
93 16     16 1 35 { my ($self, $base, $abs) = @_;
94 16   66     40 $abs ||= File::Spec->catfile($self->directory, $base);
95 16         27 $base =~ s!\\!/!g; # dos->unix
96 16         57 $self->{index}{lc $base} = $abs;
97             }
98              
99              
100             sub find($$)
101 14     14 1 1329 { my $self = shift;
102 14         28 my $domain = lc shift;
103 14         29 my $locale = $locale_unifier->(shift);
104              
105 14         31 my $index = $self->index;
106 14 50       32 keys %$index or return undef;
107              
108 14         41 my ($lang, $terr, $cs, $modif) = parse_locale $locale;
109 14 50       418 unless(defined $lang)
110 0 0       0 { defined $locale or $locale = '';
111             # avoid problem with recursion, not translatable!
112 0         0 print STDERR "illegal locale $locale, when looking for $domain";
113 0         0 return undef;
114             }
115              
116 14 100       28 $terr = defined $terr ? '_'.$terr : '';
117 14 50       21 $cs = defined $cs ? '.'.$cs : '';
118 14 50       24 $modif = defined $modif ? '@'.$modif : '';
119              
120 14         23 (my $normcs = $cs) =~ s/[^a-z0-9]//g;
121 14 50       24 if(length $normcs)
122 0 0       0 { $normcs = "iso$normcs" if $normcs !~ /[^0-9-]/;
123 0         0 $normcs = '.'.$normcs;
124             }
125              
126 14         13 my $fn;
127 14         33 for my $f ("/lc_messages/$domain", "/$domain")
128 28   33     74 { $fn
      33        
129             ||= _find($index, "$lang$terr$cs$modif$f")
130             || _find($index, "$lang$terr$normcs$modif$f")
131             || _find($index, "$lang$terr$modif$f")
132             || _find($index, "$lang$modif$f")
133             || _find($index, "$lang$f");
134             }
135              
136             $fn
137 14 0 66     39 || _find($index, "$domain/$lang$terr$cs$modif")
      66        
      66        
      33        
      33        
      33        
138             || _find($index, "$domain/$lang$terr$normcs$modif")
139             || _find($index, "$domain/$lang$terr$modif")
140             || _find($index, "$domain/$lang$cs$modif")
141             || _find($index, "$domain/$lang$normcs$modif")
142             || _find($index, "$domain/$lang$modif")
143             || _find($index, "$domain/$lang");
144             }
145              
146              
147             sub list($;$)
148 4     4 1 820 { my $self = shift;
149 4         31 my $domain = lc shift;
150 4         8 my $filter = shift;
151 4         16 my $index = $self->index;
152 4         127 my @list = map $index->{$_}, grep m!\b\Q$domain\E\b!, keys %$index;
153              
154 4 100       31 defined $filter
155             or return @list;
156              
157 1 50 33     28 $filter = qr/\.\Q$filter\E$/i
158             if defined $filter && ref $filter ne 'Regexp';
159              
160 1         30 grep $_ =~ $filter, @list;
161             }
162              
163             #--------------------
164              
165             1;