Qgoda/Splitter.pm

filter: Statementstmt 93.1% 81 / 87 Branchbran 64.5% 31 / 48 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 5 totaltotal 76.5% 124 / 162 CC 34 SCAR 33.7
CC
34
Cov
80%
new
41.0
reassemble
5.0
metaLineNumber
2.3
CRAP 29.2
« Qgoda/Processor/TT2.pm ↑ Summary Qgoda/Init/config.pm »
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Splitter;
20 7 $Qgoda::Splitter::VERSION = 'v0.9.2';
21 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 ▶use Locale::TextDomain qw('qgoda');
Subroutine BEGIN: called CC=1 SCAR=0.0
24 7 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 7 ▶use Qgoda::Util qw(empty front_matter read_body safe_yaml_load);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub new {
29 2 ▶ my ($class, $path) = @_;
Subroutine new: called CC=29 SCAR=37.1
Pod undocumented
30  
31 2 my $front_matter = front_matter $path;
32 2 ▶ if (!defined $front_matter) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $front_matter) 0 2 2
33 0 ▶ my $error = $! ? $! : __"no front matter";
Branch: 1 branch
BranchTrueFalseTotal
$! ? : 0 0 0
34 0 die __x("error reading front matter from '{filename}': {error}\n",
35 filename => $path. error => $error);
36 }
37  
38 2 my $meta = safe_yaml_load $front_matter;
39 2 my %front_lines;
40 2 my $lineno = 1;
41 2 foreach my $line (split /\n/, $front_matter) {
42 7 ++$lineno;
43 7 my $data = eval { safe_yaml_load $line };
44 7 ▶ if (!$@ && $data && ref $data && 'HASH' eq reftype $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@ and $data and ref $data and "HASH" eq builtin::reftype($data)) 7 0 7
Condition33%3/9
not $@ and $data and ref $data and "HASH" eq builtin::reftype($data)
!ll&&!rl&&r
007
not $@ and $data and ref $data
!ll&&!rl&&r
007
not $@ and $data
!ll&&!rl&&r
007
MC/DC0%0/4
not $@ and $data and ref $data and "HASH" eq builtin::reftype($data)
not $@ $data ref $data "HASH" eq builtin::reftype($data)
Truth table20%1/5
not(A) and B and C and D
A= not $@B= $dataC= ref $dataD= "HASH" eq builtin::reftype($data)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
45 7 my @keys = keys %$data;
46 7 foreach my $key (keys %$data) {
47 7 ▶ $front_lines{$key} = $lineno if exists $meta->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta->{$key} 7 0 7
48 }
49 }
50 }
51
52 2 my $body = read_body $path, '';
53 2 ▶ if (!defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 2 2
54 0 ▶ my $error = $! ? $! : __"no body found";
Branch: 1 branch
BranchTrueFalseTotal
$! ? : 0 0 0
55 0 die __x("error reading body from '{filename}': {error}\n",
56 filename => $path. error => $error);
57 }
58  
59 2 my @first = grep { !empty } split /
60 (
61 <!--QGODA-XGETTEXT-->(?:.*?)<!--\/QGODA-XGETTEXT-->
62 |
63 <!--QGODA-NO-XGETTEXT-->(?:.*?)<!--\/QGODA-NO-XGETTEXT-->
64 |
65 [ \011-\015]*
66 \n
67 [ \011-\015]*
68 \n
69 [ \011-\015]*
70 )
71 /sx, $body;
72  
73 2 my @chunks;
74 2 foreach my $chunk (@first) {
75 18 ▶ if ($chunk =~ /^[ \011-\015]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ /^[ \011-\015]+$/) { } 8 10 18
76 8 push @chunks, $chunk;
77 } else {
78 10 ▶ my $head = $chunk =~ s/^([ \011-\015]+)// ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$chunk =~ s/^([ \011-\015]+)// ? : 0 10 10
79 10 ▶ my $tail = $chunk =~ s/([ \011-\015]+)$// ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$chunk =~ s/([ \011-\015]+)$// ? : 2 8 10
80 10 ▶ push @chunks, $head if !empty $head;
Branch: 1 branch
BranchTrueFalseTotal
unless &empty($head) 0 10 10
81 10 ▶ push @chunks, $chunk if !empty $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless &empty($chunk) 10 0 10
82 10 ▶ push @chunks, $tail if !empty $tail;
Branch: 1 branch
BranchTrueFalseTotal
unless &empty($tail) 2 8 10
83 }
84 }
85  
86 2 my $lineno = 3 + $front_matter =~ y/\n/\n/;
87 2 my @entries;
88 2 foreach my $chunk (@chunks) {
89 20 ▶ if ($chunk =~ /[^ \011-\015]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ /[^ \011-\015]+$/) { } 10 10 20
90 10 ▶ if ($chunk =~ /^<!--QGODA-XGETTEXT-->(.*?)<!--\/QGODA-XGETTEXT-->$/s) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk =~ m[^<!--QGODA-XGETTEXT-->(.*?)<!--/QGODA-XGETTEXT-->$]s) { } 1 9 10
elsif ($chunk =~ m[^<!--QGODA-NO-XGETTEXT-->(.*?)<!--/QGODA-NO-XGETTEXT-->$]s) { } 2 7 9
91 1 push @entries, {
92 text => $1,
93 lineno => $lineno,
94 type => 'block',
95 }
96 } elsif ($chunk =~ /^<!--QGODA-NO-XGETTEXT-->(.*?)<!--\/QGODA-NO-XGETTEXT-->$/s) {
97 2 push @entries, {
98 text => $1,
99 lineno => $lineno,
100 type => 'exclude',
101 }
102 } else {
103 7 push @entries, {
104 text => $chunk,
105 lineno => $lineno,
106 type => 'paragraph',
107 }
108 }
109 } else {
110 10 push @entries, {
111 text => $chunk,
112 lineno => $lineno,
113 type => 'whitespace',
114 }
115 }
116  
117 20 $lineno += $chunk =~ y/\n/\n/;
118 }
119  
120 # Parse HTML comments. Maybe this should be optional.
121 2 foreach my $entry (@entries) {
122 20 ▶ if ($entry->{text} =~ s{^[ \011-\015]*<!--(.*?)-->[ \011-\015]*}{}s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"text"} =~ s/^[ \011-\015]*<!--(.*?)-->[ \011-\015]*//s) 1 19 20
123 # We only extract message context hints as they are non-standard.
124 1 my $comment = $1;
125 1 ▶ if ($comment =~ s{xgettext:msgctxt=(.*)}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comment =~ s/xgettext:msgctxt=(.*)//) 1 0 1
126 1 my $msgctxt = $1;
127 1 $msgctxt =~ s{^[ \011-\015]*}{};
128 1 $msgctxt =~ s{[ \011-\015]*$}{};
129 1 ▶ $entry->{msgctxt} = $msgctxt if !empty $msgctxt;
Branch: 1 branch
BranchTrueFalseTotal
unless &empty($msgctxt) 1 0 1
130 }
131  
132 1 $comment =~ s{^[ \011-\015]*}{};
133 1 $comment =~ s{[ \011-\015]*$}{};
134
135 1 ▶ $entry->{comment} = $comment if !empty $comment;
Branch: 1 branch
BranchTrueFalseTotal
unless &empty($comment) 1 0 1
136  
137 # Change to whitespace, if nothing left.
138 1 ▶ $entry->{type} = 'whitespace' if empty $entry->{text};
Branch: 1 branch
BranchTrueFalseTotal
if &empty($entry->{"text"}) 0 1 1
139 }
140 }
141  
142 bless {
143 2 __meta => $meta,
144 __body => $body,
145 __entries => \@entries,
146 __front_lines => \%front_lines
147 }, $class;
148 }
149  
150 sub meta {
151 1 ▶ shift->{__meta};
Subroutine meta: called CC=1 SCAR=0.0
Pod undocumented
152 }
153  
154 sub metaLineNumber {
155 3 ▶ my ($self, $key) = @_;
Subroutine metaLineNumber: called CC=2 SCAR=8.1
Pod undocumented
156  
157 return $self->{__front_lines}->{$key}
158 3 ▶ if exists $self->{__front_lines}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"__front_lines"}{$key} 3 0 3
159  
160 0 return;
161 }
162  
163 sub entries {
164 1 ▶ my ($self) = @_;
Subroutine entries: called CC=1 SCAR=0.0
Pod undocumented
165  
166 9 grep { 'whitespace' ne $_->{type} }
167 10 grep { 'exclude' ne $_->{type} }
168 1 @{$self->{__entries}};
169 }
170  
171 sub reassemble {
172 1 ▶ my ($self, $callback) = @_;
Subroutine reassemble: called CC=5 SCAR=16.2
Pod undocumented
173  
174 1 my $output = '';
175 1 foreach my $entry (@{$self->{__entries}}) {
176 10 ▶ if ('whitespace' eq $entry->{type}) {
Branch: 3 branches
BranchTrueFalseTotal
if ('whitespace' eq $entry->{'type'}) { } 5 5 10
elsif ('block' eq $entry->{'type'}) { } 0 5 5
elsif ('exclude' eq $entry->{'type'}) { } 1 4 5
177 5 $output .= $entry->{text};
178 } elsif ('block' eq $entry->{type}) {
179 $output .= "<!--QGODA-XGETTEXT-->"
180 . $callback->($entry->{text})
181 0 . "<!--/QGODA-XGETTEXT-->";
182 } elsif ('exclude' eq $entry->{type}) {
183 $output .= "<!--QGODA-NO-XGETTEXT-->"
184 . $callback->($entry->{text})
185 1 . "<!--/QGODA-NO-XGETTEXT-->";
186 } else {
187 4 $output .= $callback->($entry->{text});
188 }
189 }
190  
191 1 return $output;
192 }
193  
194 7 1;
« Qgoda/Processor/TT2.pm ↑ Summary Qgoda/Init/config.pm »