File Coverage

blib/lib/Locale/Maketext/Lexicon/Getcontext.pm
Criterion Covered Total %
statement 58 69 84.0
branch 29 52 55.7
condition 15 34 44.1
subroutine 7 9 77.7
pod 0 5 0.0
total 109 169 64.5


line stmt bran cond sub pod time code
1             package Locale::Maketext::Lexicon::Getcontext;
2              
3 2     2   81577 use strict;
  2         3  
  2         52  
4              
5 2     2   973 use Locale::Maketext::Lexicon::Gettext;
  2         2662  
  2         196  
6              
7             our $VERSION = "0.03";
8             my ( $InputEncoding, $OutputEncoding, $DoEncoding );
9              
10 0     0 0 0 sub input_encoding {$InputEncoding}
11 0     0 0 0 sub output_encoding {$OutputEncoding}
12              
13             # functions
14             *parse_mo = \&Locale::Maketext::Lexicon::Gettext::parse_mo;
15             *_unescape = \&Locale::Maketext::Lexicon::Gettext::_unescape;
16             *_gettext_to_maketext
17             = \&Locale::Maketext::Lexicon::Gettext::_gettext_to_maketext;
18              
19              
20              
21             #
22             # parse()
23             # -----
24             # copy-pasted from Locale::Maketext::Lexicon::Gettext, with s/msgid/msgctxt/g
25             #
26             sub parse {
27 4     4 0 8762 my $self = shift;
28 4         6 my ( %var, $key, @ret );
29 0         0 my @metadata;
30 0         0 my @comments;
31 0         0 my @fuzzy;
32              
33 4         8 $InputEncoding = $OutputEncoding = $DoEncoding = undef;
34              
35 2     2   9 use Carp;
  2         6  
  2         1569  
36 4 50       12 Carp::cluck "Undefined source called\n" unless defined $_[0];
37              
38             # Check for magic string of MO files
39 4 50 33     24 return parse_mo( join( '', @_ ) )
40             if ( $_[0] =~ /^\x95\x04\x12\xde/ or $_[0] =~ /^\xde\x12\x04\x95/ );
41              
42 4         13 local $^W; # no 'uninitialized' warnings, please.
43              
44 4         24 require Locale::Maketext::Lexicon;
45 4         18 my $KeepFuzzy = Locale::Maketext::Lexicon::option('keep_fuzzy');
46 4   33     32 my $UseFuzzy = $KeepFuzzy
47             || Locale::Maketext::Lexicon::option('use_fuzzy');
48 4         18 my $AllowEmpty = Locale::Maketext::Lexicon::option('allow_empty');
49             my $process = sub {
50 1734 50 33 1734   5950 if ( length( $var{msgstr} ) and ( $UseFuzzy or !$var{fuzzy} ) ) {
    0 33        
51 1734         2523 push @ret, ( map transform($_), @var{ 'msgctxt', 'msgstr' } );
52             }
53             elsif ($AllowEmpty) {
54 0         0 push @ret, ( transform( $var{msgctxt} ), '' );
55             }
56 1734 100       17597 if ( $var{msgctxt} eq '' ) {
57 4         6 push @metadata, parse_metadata( $var{msgstr} );
58             }
59             else {
60 1730         1927 push @comments, $var{msgctxt}, $var{msgcomment};
61             }
62 1734 0 33     2110 if ( $KeepFuzzy && $var{fuzzy} ) {
63 0         0 push @fuzzy, $var{msgctxt}, 1;
64             }
65 1734         3208 %var = ();
66 4         25 };
67              
68             # Parse PO files
69 4         8 foreach (@_) {
70 7697         27463 s/[\015\012]*\z//; # fix CRLF issues
71              
72             /^(msgctxt|msgstr) +"(.*)" *$/
73             ? do { # leading strings
74 3464         6204 $var{$1} = $2;
75 3464         3640 $key = $1;
76             }
77             :
78              
79             /^"(.*)" *$/
80             ? do { # continued strings
81 765         1079 $var{$key} .= $1;
82             }
83             :
84              
85             /^# (.*)$/
86             ? do { # user comments
87 0         0 $var{msgcomment} .= $1 . "\n";
88             }
89             :
90              
91             /^#, +(.*) *$/
92             ? do { # control variables
93 0         0 $var{$_} = 1 for split( /,\s+/, $1 );
94             }
95             :
96              
97             /^ *$/ && %var
98 7697 100 66     25908 ? do { # interpolate string escapes
    50          
    50          
    100          
    100          
99 1734         1963 $process->($_);
100             }
101             : ();
102              
103             }
104              
105             # do not silently skip last entry
106 4 50       15 $process->() if keys %var != 0;
107              
108 0         0 push @ret, map { transform($_) } @var{ 'msgctxt', 'msgstr' }
109 4 50       13 if length $var{msgstr};
110             push @metadata, parse_metadata( $var{msgstr} )
111 4 50       24 if $var{msgctxt} eq '';
112              
113             return wantarray
114 4 50       2046 ? ( { @metadata, @ret }, {@comments}, {@fuzzy} )
115             : ( { @metadata, @ret } );
116              
117             }
118              
119              
120             #
121             # parse_metadata()
122             # --------------
123             # copy-pasted from Locale::Maketext::Lexicon::Gettext, with no change,
124             # because it accesses the $InputEncoding and $OutputEncoding vars
125             #
126             sub parse_metadata {
127             return map {
128 8     8 0 15 (/^([^\x00-\x1f\x80-\xff :=]+):\s*(.*)$/)
129             ? ( $1 eq 'Content-Type' )
130 32 100       279 ? do {
    50          
131 4         7 my $enc = $2;
132 4 50       36 if ( $enc =~ /\bcharset=\s*([-\w]+)/i ) {
133 4   50     11 $InputEncoding = $1 || '';
134 4   50     25 $OutputEncoding
135             = Locale::Maketext::Lexicon::encoding()
136             || '';
137 4 50       50 $InputEncoding = 'utf8'
138             if $InputEncoding =~ /^utf-?8$/i;
139 4 50       11 $OutputEncoding = 'utf8'
140             if $OutputEncoding =~ /^utf-?8$/i;
141 4 50 33     6 if (Locale::Maketext::Lexicon::option('decode')
      33        
142             and ( !$OutputEncoding
143             or $InputEncoding ne $OutputEncoding )
144             )
145             {
146 4 50       37 require Encode::compat if $] < 5.007001;
147 4         19 require Encode;
148 4         4 $DoEncoding = 1;
149             }
150             }
151 4         7 ( "__Content-Type", $enc );
152             }
153             : ( "__$1", $2 )
154             : ();
155             } split( /\r*\n+\r*/, transform(pop) );
156             }
157              
158              
159             #
160             # transform()
161             # ---------
162             # copy-pasted from Locale::Maketext::Lexicon::Gettext, with no change,
163             # because it accesses the $InputEncoding and $OutputEncoding vars
164             #
165             sub transform {
166 3476     3476 0 13807 my $str = shift;
167              
168 3476 100 66     8513 if ( $DoEncoding and $InputEncoding ) {
169 3464 50       6091 $str
170             = ( $InputEncoding eq 'utf8' )
171             ? Encode::decode_utf8($str)
172             : Encode::decode( $InputEncoding, $str );
173             }
174              
175 3476         37202 $str =~ s/\\([0x]..|c?.)/qq{"\\$1"}/eeg;
  1625         59746  
176              
177 3476 50 66     9653 if ( $DoEncoding and $OutputEncoding ) {
178 0 0       0 $str
179             = ( $OutputEncoding eq 'utf8' )
180             ? Encode::encode_utf8($str)
181             : Encode::encode( $OutputEncoding, $str );
182             }
183              
184 3476         4945 return _gettext_to_maketext($str);
185             }
186              
187              
188             __PACKAGE__
189              
190             __END__