File Coverage

blib/lib/Log/Report/Extract/Template.pm
Criterion Covered Total %
statement 79 85 92.9
branch 18 30 60.0
condition 4 11 36.3
subroutine 12 12 100.0
pod 3 5 60.0
total 116 143 81.1


line stmt bran cond sub pod time code
1             # Copyrights 2007-2018 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             # This code is part of distribution Log-Report-Lexicon. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Log::Report::Extract::Template;
10 2     2   1415 use vars '$VERSION';
  2         4  
  2         96  
11             $VERSION = '1.11';
12              
13 2     2   10 use base 'Log::Report::Extract';
  2         4  
  2         565  
14              
15 2     2   11 use warnings;
  2         4  
  2         38  
16 2     2   9 use strict;
  2         3  
  2         50  
17              
18 2     2   9 use Log::Report 'log-report-lexicon';
  2         3  
  2         8  
19              
20              
21             sub init($)
22 1     1 0 3 { my ($self, $args) = @_;
23 1         5 $self->SUPER::init($args);
24             $self->{LRET_domain} = $args->{domain}
25 1 50       3 or error "template extract requires explicit domain";
26              
27 1         2 $self->{LRET_pattern} = $args->{pattern};
28 1         3 $self;
29             }
30              
31             #----------
32              
33 2     2 1 10 sub domain() {shift->{LRET_domain}}
34 1     1 1 5 sub pattern() {shift->{LRET_pattern}}
35              
36             #----------
37              
38             sub process($@)
39 1     1 1 549 { my ($self, $fn, %opts) = @_;
40              
41 1   50     5 my $charset = $opts{charset} || 'utf-8';
42 1         4 info __x"processing file {fn} in {charset}", fn=> $fn, charset => $charset;
43              
44 1 50 33     92 my $pattern = $opts{pattern} || $self->pattern
45             or error __"need pattern to scan for, either via new() or process()";
46              
47             # Slurp the whole file
48 1         3 local *IN;
49 1 50   1   26 open IN, "<:encoding($charset)", $fn
  1         5  
  1         2  
  1         11  
50             or fault __x"cannot read template from {fn}", fn => $fn;
51              
52 1         862 undef $/;
53 1         27 my $text = ;
54 1         34 close IN;
55              
56 1         4 my $domain = $self->domain;
57 1         7 $self->_reset($domain, $fn);
58              
59 1 50       7 if(ref $pattern eq 'CODE')
    50          
60 0         0 { return $pattern->($fn, \$text);
61             }
62             elsif($pattern =~ m/^TT([12])-(\w+)$/)
63 1         12 { return $self->scanTemplateToolkit($1, $2, $fn, \$text);
64             }
65             else
66 0         0 { error __x"unknown pattern {pattern}", pattern => $pattern;
67             }
68 0         0 ();
69             }
70              
71             sub _no_escapes_in($$$$)
72 12     12   23 { my ($msgid, $plural, $fn, $linenr) = @_;
73 12 100 33     48 return if $msgid !~ /\&\w+\;/
    50          
74             && (defined $plural ? $plural !~ /\&\w+\;/ : 1);
75 0 0       0 $msgid .= "|$plural" if defined $plural;
76              
77 0         0 warning __x"msgid '{msgid}' contains html escapes, don't do that. File {fn} line {linenr}"
78             , msgid => $msgid, fn => $fn, linenr => $linenr;
79             }
80              
81             sub scanTemplateToolkit($$$$)
82 1     1 0 5 { my ($self, $version, $function, $fn, $textref) = @_;
83              
84             # Split the whole file on the pattern in four fragments per match:
85             # (text, leading, needed trailing, text, leading, ...)
86             # f.i. ('', '[% loc("', 'some-msgid', '", params) %]', ' more text')
87 1 50       33 my @frags = $version==1
88             ? split(/[\[%]%(.*?)%[%\]]/s, $$textref)
89             : split(/\[%(.*?)%\]/s, $$textref);
90              
91 1         3 my $domain = $self->domain;
92 1         2 my $linenr = 1;
93 1         1 my $msgs_found = 0;
94              
95             # pre-compile the regexes, for performance
96 1         23 my $pipe_func_block = qr/^\s*\|\s*$function\b/;
97 1         22 my $msgid_pipe_func = qr/^\s*(["'])([^\r\n]+?)\1\s*\|\s*$function\b/;
98 1         21 my $func_msgid_multi = qr/(\b$function\s*\(\s*)(["'])([^\r\n]+?)\2/s;
99              
100 1         5 while(@frags > 2)
101 12         19 { my ($skip_text, $take) = (shift @frags, shift @frags);
102 12         39 $linenr += $skip_text =~ tr/\n//;
103 12 100       60 if($take =~ $pipe_func_block)
104             { # [% | loc(...) %] $msgid [%END%]
105 1 50 33     16 if(@frags < 2 || $frags[1] !~ /^\s*END\s*$/)
106 0         0 { error __x"template syntax error, no END in {fn} line {line}"
107             , fn => $fn, line => $linenr;
108             }
109 1         3 my $msgid = $frags[0]; # next content
110 1 50       6 my $plural = $msgid =~ s/\|(.*)// ? $1 : undef;
111 1         3 _no_escapes_in $msgid, $plural, $fn, $linenr;
112              
113 1         4 $self->store($domain, $fn, $linenr, $msgid, $plural);
114 1         2 $msgs_found++;
115              
116 1         2 $linenr += $take =~ tr/\n//;
117 1         2 next;
118             }
119              
120 11 100       38 if($take =~ $msgid_pipe_func)
121             { # [% $msgid | loc(...) %]
122 1         3 my $msgid = $2;
123 1 50       4 my $plural = $msgid =~ s/\|(.*)// ? $1 : undef;
124 1         3 _no_escapes_in $msgid, $plural, $fn, $linenr;
125              
126 1         4 $self->store($domain, $fn, $linenr, $msgid, $plural);
127 1         2 $msgs_found++;
128              
129 1         3 $linenr += $take =~ tr/\n//;
130 1         3 next;
131             }
132              
133             # loc($msgid, ...) form, can appear more than once
134 10         94 my @markup = split $func_msgid_multi, $take;
135 10         20 while(@markup > 4)
136             { # quads with text, call, quote, msgid
137 10         18 $linenr += ($markup[0] =~ tr/\n//)
138             + ($markup[1] =~ tr/\n//);
139 10         15 my $msgid = $markup[3];
140 10 100       31 my $plural = $msgid =~ s/\|(.*)// ? $1 : undef;
141 10         24 _no_escapes_in $msgid, $plural, $fn, $linenr;
142              
143 10         34 $self->store($domain, $fn, $linenr, $msgid, $plural);
144 10         15 $msgs_found++;
145 10         25 splice @markup, 0, 4;
146             }
147 10         27 $linenr += $markup[-1] =~ tr/\n//; # rest of container
148             }
149             # $linenr += $frags[-1] =~ tr/\n//; # final page fragment not needed
150              
151 1         5 $msgs_found;
152             }
153              
154             #----------------------------------------------------
155              
156             1;