Spoon/Formatter.pm

filter: Statementstmt 89.8% 230 / 256 Branchbran 64.6% 53 / 82 Conditioncond 54.1% 13 / 24 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 89.1% 41 / 46 Podpod 0.0% 0 / 9 totaltotal 78.4% 339 / 432 CC 54 SCAR 14.1
CC
54
Cov
82%
parse_phrases
8.7
match_block_format_id
7.6
match_phrase_format_id
7.6
CRAP 4.1
« Spoon/Base.pm ↑ Summary Spoon/Template.pm »
1 package Spoon::Formatter;
2 3 ▶use Spoon::Base -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 const class_id => 'formatter';
5 3 stub 'top_class';
6  
7 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
8 2 $self = super;
9 2 $self->hub;
10 2 return $self;
11 }
12  
13 6 ▶sub text_to_html {
Subroutine text_to_html: called CC=1 SCAR=0.0
Pod undocumented
14 6 $self->text_to_parsed(@_)->to_html;
15 }
16  
17 6 ▶sub text_to_parsed {
Subroutine text_to_parsed: called CC=1 SCAR=0.0
Pod undocumented
18 6 $self->top_class->new(text => shift)->parse;
19 }
20  
21 16 ▶sub table { $self->{table} ||= $self->create_table }
Subroutine table: called CC=2 SCAR=7.1
Pod undocumented
Condition66%2/3
$self->{"table"} ||= $self->create_table
l!l&&r!l&&!r
1510
MC/DC0%0/2
$self->{"table"} ||= $self->create_table
$self->{"table"} $self->create_table
Truth table66%2/3
A ||= B
A= $self->{"table"}B= $self->create_table
ABresult
1X1
011
000
22  
23 1 ▶sub create_table {
Subroutine create_table: called CC=3 SCAR=11.2
Pod undocumented
24 1 my $class_prefix = $self->class_prefix;
25 my %table = map {
26 1 ▶ my $class = /::/ ? $_ : "$class_prefix$_";
Branch: 1 branch
BranchTrueFalseTotal
/::/ ? : 2 0 2
27 2 ▶ $class->can('formatter_id') ? ($class->formatter_id, $class) : ();
Branch: 1 branch
BranchTrueFalseTotal
$class->can('formatter_id') ? : 2 0 2
28 } $self->formatter_classes;
29 1 \ %table;
30 }
31  
32 6 ▶sub wafl_table { $self->{wafl_table} ||= $self->create_wafl_table }
Subroutine wafl_table: called CC=2 SCAR=7.1
Pod undocumented
Condition66%2/3
$self->{"wafl_table"} ||= $self->create_wafl_table
l!l&&r!l&&!r
510
MC/DC0%0/2
$self->{"wafl_table"} ||= $self->create_wafl_table
$self->{"wafl_table"} $self->create_wafl_table
Truth table66%2/3
A ||= B
A= $self->{"wafl_table"}B= $self->create_wafl_table
ABresult
1X1
011
000
33  
34 1 ▶sub create_wafl_table {
Subroutine create_wafl_table: called CC=3 SCAR=15.0
Pod undocumented
35 1 my $class_prefix = $self->class_prefix;
36 my %table = map {
37 0 ▶ my $class = /::/ ? $_ : "$class_prefix$_";
Branch: 1 branch
BranchTrueFalseTotal
/::/ ? : 0 0 0
38 0 ▶ $class->can('wafl_id') ? ($class->wafl_id, $class) : ();
Branch: 1 branch
BranchTrueFalseTotal
$class->can('wafl_id') ? : 0 0 0
39 } $self->wafl_classes;
40 1 $self->add_external_wafl(\ %table);
41 1 \ %table;
42 }
43  
44 1 ▶sub add_external_wafl {
Subroutine add_external_wafl: called CC=3 SCAR=16.5
Pod undocumented
45 1 ▶ return unless $self->hub->registry_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hub->registry_loaded 1 0 1
46 0 my $table = shift;
47 0 my $map = $self->hub->registry->lookup->wafl;
48 0 for my $wafl_id (keys %$map) {
49 0 $table->{$wafl_id} = $map->{$wafl_id};
50 }
51 }
52  
53 1 ▶sub wafl_classes { () }
Subroutine wafl_classes: called CC=1 SCAR=0.0
Pod undocumented
54  
55 package Spoon::Formatter::Unit;
56 3 ▶use Spoon::Base -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 3 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 3 const formatter_id => '';
60 3 const html_start => '';
61 3 const html_end => '';
62 3 const contains_blocks => [];
63 3 const contains_phrases => [];
64 # stub 'pattern_start'; # XXX messes multiple inheritance
65 3 const pattern_end => qr/.*?/;
66  
67 3 field text => '';
68 3 field units => [];
69 3 field start_offset => 0;
70 3 field start_end_offset => 0;
71 # XXX this field is never used
72 #field end_start_offset => 0;
73 3 field end_offset => 0;
74 3 field matched => '';
75 3 field -weak => 'next_unit';
76 3 field -weak => 'prev_unit';
77  
78 8 ▶sub parse {
Subroutine parse: called CC=4 SCAR=13.9
79 8 $self->parse_blocks;
80 8 my $units = $self->units;
81  
82 8 ▶ if (@$units == 1 and not ref $units->[0] and @{$self->contains_phrases}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$units == 1 and not ref $units->[0] and @{$self->contains_phrases;}) 4 4 8
Condition83%5/6
@$units == 1 and not ref $units->[0] and @{$self->contains_phrases;}
!ll&&!rl&&r
224
@$units == 1 and not ref $units->[0]
!ll&&!rl&&r
026
MC/DC66%2/3
@$units == 1 and not ref $units->[0] and @{$self->contains_phrases;}
@$units == 1 not ref $units->[0] @{$self->contains_phrases;}
Truth table75%3/4
A and not(B) and C
A= @$units == 1B= not ref $units->[0]C= @{$self->contains_phrases;}
ABCresult
0XX0
10X0
1100
1111
83 4 $self->text(shift @$units);
84 4 $self->start_offset(0);
85 4 $self->end_offset(0);
86 4 $self->parse_phrases;
87 }
88 8 return $self;
89 }
90  
91 8 ▶sub link_units {
Subroutine link_units: called CC=4 SCAR=13.9
92 8 my $units = shift;
93 8 ▶ for (my $i = 0; $i < @$units; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$units) 8 8 16
94 8 ▶ next unless ref $units->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $units->[$i] 6 2 8
95 2 $units->[$i]->next_unit($units->[$i + 1]);
96 2 ▶ $units->[$i]->prev_unit($units->[$i - 1]) if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 2 2
97 }
98 }
99  
100 # XXX extracted to allow performance analysis
101 # very similar to match_phrase_format_id, so
102 # room for refactor there
103 #
104 # Instead of calling $unit->match make it
105 # possible to call $class->match and have it
106 # work
107 8 ▶sub match_block_format_id {
Subroutine match_block_format_id: called CC=7 SCAR=20.2
108 8 my ($contains, $table, $text) = @_;
109 8 my $match;
110 8 for my $format_id (@$contains) {
111 6 ▶ my $class = $table->{$format_id}
Branch: 1 branch
BranchTrueFalseTotal
my $class = $table->{$format_id} or die "No class for $format_id" 6 0 6
112 or die "No class for $format_id";
113 6 my $unit = $class->new;
114 6 $unit->text($text);
115 6 ▶ $unit->match or next;
Branch: 1 branch
BranchTrueFalseTotal
$unit->match or next 2 4 6
116 2 ▶ $match = $unit
Branch: 1 branch
BranchTrueFalseTotal
if not defined $match or $unit->start_offset < $match->start_offset 2 0 2
Condition33%1/3
not defined $match or $unit->start_offset < $match->start_offset
l!l&&r!l&&!r
200
MC/DC0%0/2
not defined $match or $unit->start_offset < $match->start_offset
not defined $match $unit->start_offset < $match->start_offset
Truth table33%1/3
not(A) or B
A= not defined $matchB= $unit->start_offset < $match->start_offset
ABresult
1X1
011
000
117 if not defined $match or
118 $unit->start_offset < $match->start_offset;
119 2 ▶ last unless $match->start_offset;
Branch: 1 branch
BranchTrueFalseTotal
unless $match->start_offset 2 0 2
120 }
121 8 return $match;
122 }
123
124 8 ▶sub parse_blocks {
Subroutine parse_blocks: called CC=5 SCAR=16.1
125 8 my $text = $self->text;
126 8 $self->text(undef);
127 8 my $units = $self->units;
128 8 my $table = $self->hub->formatter->table;
129 8 my $contains = $self->contains_blocks;
130 8 ▶ while ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 8 2 10
131 8 my $match = $self->match_block_format_id($contains, $table, $text);
132 8 ▶ if (not defined $match) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $match) 6 2 8
133 6 push @$units, $text;
134 6 last;
135 }
136 2 ▶ push @$units, substr($text, 0, $match->start_offset)
Branch: 1 branch
BranchTrueFalseTotal
if $match->start_offset 0 2 2
137 if $match->start_offset;
138 2 $text = substr($text, $match->end_offset);
139 2 $match->unit_match;
140 2 push @$units, $match;
141 }
142 8 $self->link_units($units);
143 8 $_->parse for grep ref($_), @{$self->units};
144 }
145  
146 0 ▶sub match {
Subroutine match: not called CC=2 SCAR=17.9
147 0 ▶ return unless $self->text =~ $self->pattern_block;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->text =~ $self->pattern_block 0 0 0
148 0 $self->set_match;
149 }
150  
151 # XXX extracted to allow performance analysis
152 # very similar to match_block_format_id, so
153 # room for refactor
154 12 ▶sub match_phrase_format_id {
Subroutine match_phrase_format_id: called CC=7 SCAR=20.2
155 12 my ($contains, $table, $text) = @_;
156 12 my $match;
157 12 for my $format_id (@$contains) {
158 8 ▶ my $class = $table->{$format_id}
Branch: 1 branch
BranchTrueFalseTotal
my $class = $table->{$format_id} or die "No class for $format_id" 8 0 8
159 or die "No class for $format_id";
160 # XXX why do we make a new one every time, instead of
161 # just setting text and doing the match? Ah, tests
162 # show they carry some state. oh well
163 8 my $unit = $class->new;
164 8 $unit->text($text);
165 8 ▶ $unit->match_phrase or next;
Branch: 1 branch
BranchTrueFalseTotal
$unit->match_phrase or next 4 4 8
166 4 ▶ $match = $unit
Branch: 1 branch
BranchTrueFalseTotal
if not defined $match or $unit->start_offset < $match->start_offset 4 0 4
Condition33%1/3
not defined $match or $unit->start_offset < $match->start_offset
l!l&&r!l&&!r
400
MC/DC0%0/2
not defined $match or $unit->start_offset < $match->start_offset
not defined $match $unit->start_offset < $match->start_offset
Truth table33%1/3
not(A) or B
A= not defined $matchB= $unit->start_offset < $match->start_offset
ABresult
1X1
011
000
167 if not defined $match or
168 $unit->start_offset < $match->start_offset;
169 4 ▶ last if $match->start_offset == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $match->start_offset == 0 4 0 4
170 }
171 12 return $match;
172 }
173  
174 8 ▶sub parse_phrases {
Subroutine parse_phrases: called CC=8 SCAR=21.7
175 8 my $text = $self->text;
176 8 $self->text(undef);
177 8 my $units = $self->units;
178 8 my $table = $self->hub->formatter->table;
179 8 my $contains = $self->contains_phrases;
180 8 ▶ while ($text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text) 12 0 12
181 12 my $match = $self->match_phrase_format_id($contains, $table, $text);
182 12 ▶ if ($self->start_end_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->start_end_offset) 4 8 12
183 4 ▶ if ($text =~ $self->pattern_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ $self->pattern_end) { } 4 0 4
184 4 ▶ if (not defined $match or $-[0] < $match->start_offset) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $match or $-[0] < $match->start_offset) 4 0 4
Condition33%1/3
not defined $match or $-[0] < $match->start_offset
l!l&&r!l&&!r
400
MC/DC0%0/2
not defined $match or $-[0] < $match->start_offset
not defined $match $-[0] < $match->start_offset
Truth table33%1/3
not(A) or B
A= not defined $matchB= $-[0] < $match->start_offset
ABresult
1X1
011
000
185 4 push @$units, substr($text, 0, $-[0]);
186 4 return substr($text, $+[0]);
187 }
188 }
189 else {
190 0 $self->end_offset(length $text);
191 0 push @$units, $text;
192 0 return '';
193 }
194 }
195 8 ▶ if (not defined $match) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $match) 4 4 8
196 4 push @$units, $text;
197 4 return '';
198 }
199 # XXX: this code is never called (as far as we know...)
200 # if ($match->end_start_offset) {
201 # push @$units, $match;
202 # $text = substr($text, $match->end_offset);
203 # next;
204 # }
205 4 ▶ push @$units, substr($text, 0, $match->start_offset)
Branch: 1 branch
BranchTrueFalseTotal
if $match->start_offset 0 4 4
206 if $match->start_offset;
207 4 $text = substr($text, $match->start_end_offset);
208 4 $match->text($text);
209 4 $text = $match->parse_phrases;
210 4 $match->unit_match;
211 4 push @$units, $match;
212 }
213 }
214  
215 # empty for hooking
216 6 ▶sub unit_match {
Subroutine unit_match: called CC=1 SCAR=0.0
217 }
218  
219 8 ▶sub match_phrase {
Subroutine match_phrase: called CC=3 SCAR=11.0
220 8 ▶ return unless $self->text =~ $self->pattern_start;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->text =~ $self->pattern_start 4 4 8
221 4 $self->start_offset($-[0]);
222 4 $self->start_end_offset($+[0]);
223 4 $self->matched(substr($self->text, $-[0], $+[0] - $-[0]));
224 4 ▶ my $pattern_end = $self->pattern_end
Branch: 1 branch
BranchTrueFalseTotal
my $pattern_end = $self->pattern_end or return 1 4 0 4
225 or return 1;
226 4 return substr($self->text, $+[0]) =~ $pattern_end;
227 }
228  
229 2 ▶sub set_match {
Subroutine set_match: called CC=5 SCAR=16.6
230 2 my ($text, $start, $end) = @_;
231 2 ▶ $text = $1 unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 2 2
232 2 ▶ $text = '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 2 2
233 2 ▶ $start = $-[0] unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 2 0 2
234 2 ▶ $end = $+[0] unless defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 2 0 2
235 2 $self->text($text);
236 2 $self->start_offset($start);
237 2 $self->end_offset($end);
238 2 return 1;
239 }
240  
241 12 ▶sub to_html {
Subroutine to_html: called CC=3 SCAR=11.0
242 12 my $units = $self->units;
243 12 ▶ for (my $i = 0; $i < @$units; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$units) 16 12 28
244 16 ▶ $units->[$i] = $self->escape_html($units->[$i])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $units->[$i] 10 6 16
245 unless ref $units->[$i];
246 }
247 12 $self->html;
248 }
249  
250 12 ▶sub html {
Subroutine html: called CC=2 SCAR=6.9
251 my $inner = $self->text_filter(join '',
252 map {
253 16 ▶ ref($_) ? $_->to_html : $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 6 10 16
254 12 } @{$self->units}
255 );
256 12 $self->html_start . $inner . $self->html_end;
257 }
258  
259 12 ▶sub text_filter { shift }
Subroutine text_filter: called CC=1 SCAR=0.0
260  
261 10 ▶sub escape_html { $self->html_escape(shift) }
Subroutine escape_html: called CC=1 SCAR=0.0
262  
263 ################################################################################
264 package Spoon::Formatter::Container;
265 3 ▶use base 'Spoon::Formatter::Unit';
Subroutine BEGIN: called CC=1 SCAR=0.0
266 6 ▶sub contains_blocks {
Subroutine contains_blocks: called CC=1 SCAR=0.0
267 6 $self->hub->formatter->all_blocks;
268 }
269  
270 ################################################################################
271 package Spoon::Formatter::Block;
272 3 ▶use base 'Spoon::Formatter::Unit';
Subroutine BEGIN: called CC=1 SCAR=0.0
273 0 ▶sub contains_phrases {
Subroutine contains_phrases: not called CC=1 SCAR=6.9
274 0 $self->hub->formatter->all_phrases;
275 }
276  
277 ################################################################################
278 package Spoon::Formatter::Phrase;
279 3 ▶use base 'Spoon::Formatter::Unit';
Subroutine BEGIN: called CC=1 SCAR=0.0
280 0 ▶sub contains_phrases {
Subroutine contains_phrases: not called CC=1 SCAR=6.9
281 0 my $id = $self->formatter_id;
282 0 [ grep {$_ ne $id} @{$self->hub->formatter->all_phrases} ];
283 }
284  
285 ################################################################################
286 package Spoon::Formatter::Wafl;
287 3 ▶use Spoon::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
288 3 const contains_phrases => [];
289  
290 6 ▶sub bless_wafl_class {
Subroutine bless_wafl_class: called CC=4 SCAR=16.8
291 6 my $package = caller;
292 6 my $class = $self->hub->formatter->wafl_table->{$self->method};
293 6 ▶ if (ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 6 6
294 0 my $class_id;
295 0 ($class_id, $class) = @$class;
296 0 $self->hub->load_class($class_id);
297 }
298 6 ▶ bless $self, $class
Branch: 1 branch
BranchTrueFalseTotal
if defined $class and $class->isa($package) 0 6 6
Condition33%1/3
defined $class and $class->isa($package)
!ll&&!rl&&r
600
MC/DC0%0/2
defined $class and $class->isa($package)
defined $class $class->isa($package)
Truth table33%1/3
A and B
A= defined $classB= $class->isa($package)
ABresult
0X0
100
111
299 if defined $class and $class->isa($package);
300 6 return 1;
301 }
302  
303 ################################################################################
304 package Spoon::Formatter::WaflBlock;
305 3 ▶use base 'Spoon::Formatter::Wafl';
Subroutine BEGIN: called CC=1 SCAR=0.0
306 3 ▶use base 'Spoon::Formatter::Block';
Subroutine BEGIN: called CC=1 SCAR=0.0
307 3 const formatter_id => 'wafl_block';
308 3 const html_end => "</div>\n";
309 3 field 'method';
310 3 field 'arguments';
311  
312 2 ▶sub html_start {
Subroutine html_start: called CC=1 SCAR=0.0
313 2 '<div class="' . $self->method . '">';
314 }
315  
316 6 ▶sub match {
Subroutine match: called CC=2 SCAR=6.9
317 return unless
318 6 ▶ $self->text =~ /(?:^\.([\w\-]+)\ *\n)((?:.*\n)*?)(?:^\.\1\ *\n|\z)/m;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->text =~ /(?:^\.([\w\-]+)\ *\n)((?:.*\n)*?)(?:^\.\1\ *\n|\z)/m 4 2 6
319 2 $self->set_match($2);
320 2 my $method = lc $1;
321 2 $method =~ s/-/_/g;
322 2 $self->method($method);
323 2 $self->matched($2);
324 2 $self->bless_wafl_class;
325 }
326  
327 0 ▶sub block_text {
Subroutine block_text: not called CC=1 SCAR=6.9
328 0 $self->units->[0];
329 }
330  
331 ################################################################################
332 package Spoon::Formatter::WaflPhrase;
333 3 ▶use base 'Spoon::Formatter::Wafl';
Subroutine BEGIN: called CC=1 SCAR=0.0
334 3 ▶use base 'Spoon::Formatter::Unit';
Subroutine BEGIN: called CC=1 SCAR=0.0
335 3 const formatter_id => 'wafl_phrase';
336 3 const pattern_start =>
337 qr/(^|(?<=[\s\-]))\{[\w-]+(\s*:)?\s*.*?\}(?=[^A-Za-z0-9]|\z)/;
338 3 field 'method';
339 3 field 'arguments';
340  
341 4 ▶sub html_start {
Subroutine html_start: called CC=1 SCAR=0.0
342 4 '<span class="' . $self->method . '">' . $self->arguments . '</span>';
343 }
344  
345 8 ▶sub match_phrase {
Subroutine match_phrase: called CC=3 SCAR=11.0
346 8 ▶ return unless super;
Branch: 1 branch
BranchTrueFalseTotal
unless super() 4 4 8
347 4 ▶ return unless $self->matched =~ /^\{([\w\-]+)(?:\s*\:)?\s*(.*)\}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->matched =~ /^\{([\w\-]+)(?:\s*\:)?\s*(.*)\}$/ 0 4 4
348 4 $self->arguments($2);
349 4 my $method = lc $1;
350 4 $method =~ s/-/_/g;
351 4 $self->method($method);
352 4 $self->bless_wafl_class;
353 }
354  
355 0 ▶sub wafl_error {
Subroutine wafl_error: not called CC=1 SCAR=6.9
356 0 join '',
357 '<span class="wafl_error">{',
358 $self->method,
359 ': ',
360 $self->arguments,
361 '}</span>';
362 }
363  
364 3 __END__
« Spoon/Base.pm ↑ Summary Spoon/Template.pm »