blib/lib/XML/Grammar/FictionBase/TagsTree2XML.pm

filter: Statementstmt 81.8% 144 / 176 Branchbran 56.5% 26 / 46 Conditioncond 50.0% 6 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 97.3% 37 / 38 Podpod 100% 1 / 1 totaltotal 76.4% 214 / 280 CC 30 SCAR 19.0
CC
30
Cov
75%
_convert_while_handling_errors
42.0
_write_elem_childs
10.1
_write_Element_elem
6.2
CRAP 6.7
« blib/lib/XML/Grammar/Screenplay/API/Concat.pm ↑ Summary blib/lib/XML/Grammar/Screenplay/FromProto/Parser/QnD.pm »
1 package XML::Grammar::FictionBase::TagsTree2XML;
2 19 $XML::Grammar::FictionBase::TagsTree2XML::VERSION = '0.32.0';
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use MooX 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 21 ▶use XML::Writer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Path::Tiny qw/ path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 19 require XML::Grammar::Fiction::FromProto::Parser::QnD;
13 19 require XML::Grammar::Fiction::FromProto::Parser;
14  
15 sub _get_xml_xml_ns
16 116 ▶{
Subroutine _get_xml_xml_ns: called CC=1 SCAR=0.0
17 116 return "http://www.w3.org/XML/1998/namespace";
18 }
19  
20 sub _get_xlink_xml_ns
21 57 ▶{
Subroutine _get_xlink_xml_ns: called CC=1 SCAR=0.0
22 57 return "http://www.w3.org/1999/xlink";
23 }
24  
25  
26 19 has '_parser_class' => (
27 is => "ro",
28 isa => "Str",
29 init_arg => "parser_class",
30 default => "XML::Grammar::Fiction::FromProto::Parser::QnD",
31 );
32  
33 has "_parser" => (
34 'isa' => "XML::Grammar::Fiction::FromProto::Parser",
35 'is' => "rw",
36 lazy => 1,
37 14 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 14 my $self = shift;
39 14 return $self->_parser_class->new();
40 },
41 19 );
42  
43 sub _get_initial_writer
44 50 ▶{
Subroutine _get_initial_writer: called CC=1 SCAR=0.0
45 50 my $self = shift;
46  
47 50 my $writer = XML::Writer->new(
48 OUTPUT => $self->_buffer(),
49 ENCODING => "utf-8",
50 NAMESPACES => 1,
51 PREFIX_MAP => {
52 $self->_get_default_xml_ns() => "",
53 $self->_get_xml_xml_ns() => 'xml',
54 $self->_get_xlink_xml_ns() => 'xlink',
55 },
56 );
57  
58 50 $writer->xmlDecl("utf-8");
59  
60 50 return $writer;
61 }
62  
63 has "_writer" => (
64 'isa' => "Maybe[XML::Writer]",
65 'is' => "rw",
66 lazy => 1,
67 4 ▶ default => sub { return shift->_get_initial_writer(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 19 );
69  
70 19 has "_wrote_last" => (
71 'isa' => "Str",
72 'is' => "rw",
73 default => "nothing",
74 );
75  
76 sub _get_initial_buffer
77 50 ▶{
Subroutine _get_initial_buffer: called CC=1 SCAR=0.0
78 50 my $buffer = '';
79 50 return \$buffer;
80 }
81  
82 has '_buffer' => (
83 is => "rw",
84 lazy => 1,
85 4 ▶ default => sub { return shift->_get_initial_buffer; },
Subroutine __ANON__: called CC=1 SCAR=0.0
86 19 );
87  
88 sub _reset_buffer
89 46 ▶{
Subroutine _reset_buffer: called CC=1 SCAR=0.0
90 46 my $self = shift;
91  
92 46 $self->_buffer( $self->_get_initial_buffer() );
93 46 $self->_writer( $self->_get_initial_writer() );
94  
95 46 return;
96 }
97  
98 sub _flush_buffer
99 46 ▶{
Subroutine _flush_buffer: called CC=1 SCAR=0.0
100 46 my $self = shift;
101  
102 46 my $ret = $self->_buffer();
103 46 $self->_reset_buffer();
104  
105 46 return $ret;
106 }
107  
108 my %passthrough_elem = (
109 28 ▶ b => sub { return +{ tag => shift->_bold_tag_name(), wrap_para => 1, }; },
Subroutine __ANON__: called CC=1 SCAR=0.0
110 i =>
111 20 ▶ sub { return +{ tag => shift->_italics_tag_name(), wrap_para => 1, }; },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 19 );
113  
114 sub _calc_passthrough_cb
115 248 ▶{
Subroutine _calc_passthrough_cb: called CC=2 SCAR=6.9
116 248 my ( $self, $name ) = @_;
117  
118 248 ▶ if ( exists( $passthrough_elem{$name} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $passthrough_elem{$name}) { } 48 200 248
119 {
120 48 return $passthrough_elem{$name};
121 }
122 else
123 {
124 200 return;
125 }
126 }
127  
128 sub _calc_passthrough_name
129 298 ▶{
Subroutine _calc_passthrough_name: called CC=2 SCAR=6.9
130 298 my ( $self, $name, $elem ) = @_;
131  
132 298 my $cb = $self->_calc_passthrough_cb($name);
133  
134 298 ▶ if ( ref($cb) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 48 250 298
135 {
136 48 return $cb->( $self, $name, $elem, );
137 }
138 else
139 {
140 250 return $cb;
141 }
142 }
143  
144 sub _write_elem
145 2110 ▶{
Subroutine _write_elem: called CC=2 SCAR=6.9
146 2110 my ( $self, $args ) = @_;
147  
148 2110 my $elem = $args->{elem};
149  
150 2110 ▶ if ( ref($elem) eq "" )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elem eq '') { } 621 1489 2110
151 {
152 621 $self->_writer->characters($elem);
153 621 $self->_wrote_last("characters");
154 }
155 else
156 {
157 1489 return $self->_write_elem_obj($args);
158 }
159 }
160  
161 sub _write_Element_Paragraph
162 362 ▶{
Subroutine _write_Element_Paragraph: called CC=2 SCAR=7.0
163 362 my ( $self, $elem ) = @_;
164  
165 362 ▶ my $out_cb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
166 362 return $self->_write_elem_childs( $elem, );
167 362 };
168 return (
169 362 ▶ ( $self->_writer->within_element( $self->_paragraph_tag ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->_writer->within_element($self->_paragraph_tag) ? : 0 362 362
170 ? $out_cb->()
171 : $self->_output_tag(
172 {
173 start => [ $self->_paragraph_tag, ],
174 in => $out_cb,
175 }
176 )
177 );
178 }
179  
180 sub _write_Element_Element
181 302 ▶{
Subroutine _write_Element_Element: called CC=1 SCAR=0.0
182 302 my ( $self, $elem ) = @_;
183  
184 302 return $self->_write_Element_elem($elem);
185 }
186  
187 sub _write_Element_Comment
188 22 ▶{
Subroutine _write_Element_Comment: called CC=2 SCAR=6.9
189 22 my ( $self, $elem ) = @_;
190  
191 22 my $text = $elem->text();
192  
193 # To avoid trailing space due to a problem in XML::Writer
194 22 $text =~ s{\A[\r\n]+}{}ms;
195  
196 22 ▶ if ( $text =~ m{\n\z} )
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\n\z/) 6 16 22
197 {
198 6 $text .= ' ';
199 }
200  
201 22 $self->_writer->comment($text);
202 22 $self->_wrote_last("comment");
203 }
204  
205 sub _calc_write_elem_obj_classes
206 1489 ▶{
Subroutine _calc_write_elem_obj_classes: called CC=1 SCAR=0.0
207 1489 return [qw(Text Paragraph Element Comment)];
208 }
209  
210 sub _output_tag_with_childs
211 475 ▶{
Subroutine _output_tag_with_childs: called CC=1 SCAR=0.2
212 475 my ( $self, $args ) = @_;
213  
214 return $self->_output_tag(
215 {
216 %$args,
217 475 ▶ 'in' => sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
218 475 ▶ if ( not $args->{elem} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"elem"}) 0 475 475
219 {
220 0 die "args elem";
221 }
222 475 return $self->_write_elem_childs( $args->{elem} );
223 },
224 }
225 475 );
226 }
227  
228 sub _write_elem_childs
229 1452 ▶{
Subroutine _write_elem_childs: called CC=8 SCAR=23.2
230 1452 my ( $self, $elem ) = @_;
231  
232 1452 ▶ if ( not ref $elem )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $elem) 0 1452 1452
233 {
234 0 return;
235  
236 0 die "elem=[$elem]";
237 }
238 1452 my $kids;
239 1452 ▶ if ( ref($elem) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elem eq 'ARRAY') { } 0 1452 1452
240 {
241 0 $kids = $elem;
242 }
243 else
244 {
245 1452 $kids = $elem->_get_childs();
246 }
247 1452 my $prev_child;
248 1452 foreach my $child ( @{$kids} )
249 {
250 2054 ▶ if ( defined $prev_child )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_child) 619 1435 2054
251 {
252 619 ▶ if ( $prev_child->_short_isa("InnerDesc")
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_child->_short_isa("InnerDesc") and (ref $child and not $child->_short_isa("Text"))) 0 619 619
Condition50%3/6
$prev_child->_short_isa("InnerDesc") and (ref $child and not $child->_short_isa("Text"))
!ll&&!rl&&r
61540
ref $child and not $child->_short_isa("Text")
!ll&&!rl&&r
040
MC/DC0%0/3
$prev_child->_short_isa("InnerDesc") and (ref $child and not $child->_short_isa("Text"))
$prev_child->_short_isa("InnerDesc") ref $child not $child->_short_isa("Text")
Truth table50%2/4
A and B and not(C)
A= $prev_child->_short_isa("InnerDesc")B= ref $childC= not $child->_short_isa("Text")
ABCresult
0XX0
10X0
1100
1111
253 and ( ref($child) and !$child->_short_isa("Text") ) )
254 {
255 0 $self->_write_elem( { elem => ' ', }, );
256 }
257 }
258 2054 $self->_write_elem( { elem => $child, }, );
259 2054 $prev_child = $child;
260 }
261  
262 1452 return;
263 }
264  
265 sub _write_elem_obj
266 1489 ▶{
Subroutine _write_elem_obj: called CC=3 SCAR=11.0
267 1489 my ( $self, $args ) = @_;
268  
269 1489 my $elem = $args->{elem};
270  
271 1489 foreach my $class ( @{ $self->_calc_write_elem_obj_classes() } )
272 {
273 3070 ▶ if ( $elem->_short_isa($class) )
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->_short_isa($class)) 1489 1581 3070
274 {
275 1489 my $meth = "_write_Element_$class";
276 1489 $self->$meth($elem);
277 1489 return;
278 }
279 }
280  
281 0 Carp::confess( "Class of element not detected - " . ref($elem) . "!" );
282 }
283  
284 sub _write_Element_elem
285 302 ▶{
Subroutine _write_Element_elem: called CC=6 SCAR=18.2
286 302 my ( $self, $elem ) = @_;
287  
288 302 my $name = $elem->name();
289  
290 302 ▶ if ( $elem->_short_isa("InnerDesc") )
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->_short_isa('InnerDesc')) { } 4 298 302
elsif (defined(my $out_name = $self->_calc_passthrough_name($name, $elem))) { } 98 200 298
291 {
292 4 ▶ if ( "endTag" eq $self->_wrote_last() )
Branch: 1 branch
BranchTrueFalseTotal
if ("endTag" eq $self->_wrote_last) 0 4 4
293 {
294 0 $self->_write_elem( { elem => ' ', }, );
295 }
296 $self->_output_tag_with_childs(
297 {
298 4 start => ["inlinedesc"],
299 elem => $elem,
300 }
301 );
302 4 return;
303 }
304 elsif (
305 defined( my $out_name = $self->_calc_passthrough_name( $name, $elem ) )
306 )
307 {
308 98 ▶ my $out_cb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
309 return $self->_output_tag_with_childs(
310 {
311 98 start => [ $out_name->{tag} ],
312 elem => $elem,
313 }
314 );
315 98 };
316  
317 # warn "\$out_name=[$out_name]";
318 return (
319 (
320 $out_name->{wrap_para}
321 && (
322 98 ▶ !$self->_writer->within_element( $self->_paragraph_tag ) )
Branch: 1 branch
BranchTrueFalseTotal
$out_name->{'wrap_para'} && !$self->_writer->within_element($self->_paragraph_tag) ? : 0 98 98
Condition66%2/3
$out_name->{"wrap_para"} and not $self->_writer->within_element($self->_paragraph_tag)
!ll&&!rl&&r
50480
MC/DC0%0/2
$out_name->{"wrap_para"} and not $self->_writer->within_element($self->_paragraph_tag)
$out_name->{"wrap_para"} not $self->_writer->within_element($self->_paragraph_tag)
Truth table66%2/3
A and not(B)
A= $out_name->{"wrap_para"}B= not $self->_writer->within_element($self->_paragraph_tag)
ABresult
0X0
100
111
323 )
324 ? $self->_output_tag(
325 {
326 start => [ $self->_paragraph_tag, ],
327 in => $out_cb,
328 }
329 )
330 : $out_cb->()
331 );
332 }
333 else
334 {
335 200 my $method = "_handle_elem_of_name_$name";
336  
337 200 $self->$method($elem);
338  
339 200 return;
340 }
341 }
342  
343 sub _handle_elem_of_name_s
344 70 ▶{
Subroutine _handle_elem_of_name_s: called CC=1 SCAR=0.0
345 70 my ( $self, $elem ) = @_;
346  
347 70 $self->_write_scene( { scene => $elem } );
348 }
349  
350 sub _handle_elem_of_name_br
351 5 ▶{
Subroutine _handle_elem_of_name_br: called CC=1 SCAR=0.0
352 5 my ( $self, $elem ) = @_;
353  
354 5 $self->_writer->emptyTag("br");
355 5 $self->_wrote_last("endTag");
356  
357 5 return;
358 }
359  
360 sub _output_tag
361 893 ▶{
Subroutine _output_tag: called CC=1 SCAR=0.0
362 893 my ( $self, $args ) = @_;
363  
364 893 my @start = @{ $args->{start} };
365 893 $self->_writer->startTag( [ $self->_get_default_xml_ns(), $start[0] ],
366 @start[ 1 .. $#start ] );
367  
368 893 $args->{in}->( $self, $args );
369  
370 893 $self->_writer->endTag();
371 893 $self->_wrote_last("endTag");
372 }
373  
374 sub _convert_while_handling_errors
375 0 ▶{
Subroutine _convert_while_handling_errors: not called CC=6 SCAR=37.4
376 0 my ( $self, $args ) = @_;
377  
378 0 eval {
379 0 my $output_xml = $self->convert( $args->{convert_args}, );
380  
381 0 open my $out, ">:encoding(UTF-8)", $args->{output_filename};
382 0 print {$out} $output_xml;
383 0 close($out);
384 };
385  
386 # Error handling.
387  
388 0 my $e;
389 0 ▶ if (
Branch: 4 branches
BranchTrueFalseTotal
if ($e = 'Exception::Class'->caught('XML::Grammar::Fiction::Err::Parse::TagsMismatch')) { } 0 0 0
elsif ($e = 'Exception::Class'->caught('XML::Grammar::Fiction::Err::Parse::LineError')) { } 0 0 0
elsif ($e = 'Exception::Class'->caught('XML::Grammar::Fiction::Err::Parse::TagNotClosedAtEOF')) { } 0 0 0
elsif ($e = 'Exception::Class'->caught) { } 0 0 0
390 $e = Exception::Class->caught(
391 "XML::Grammar::Fiction::Err::Parse::TagsMismatch")
392 )
393 {
394 0 warn $e->error(), "\n";
395 0 warn "Open: ", $e->opening_tag->name(),
396 " at line ", $e->opening_tag->line(), "\n";
397 0 warn "Close: ",
398 $e->closing_tag->name(), " at line ",
399 $e->closing_tag->line(), "\n";
400  
401 0 exit(-1);
402 }
403 elsif (
404 $e = Exception::Class->caught(
405 "XML::Grammar::Fiction::Err::Parse::LineError")
406 )
407 {
408 0 warn $e->error(), "\n";
409 0 warn "At line ", $e->line(), "\n";
410 0 exit(-1);
411 }
412 elsif (
413 $e = Exception::Class->caught(
414 "XML::Grammar::Fiction::Err::Parse::TagNotClosedAtEOF")
415 )
416 {
417 0 warn $e->error(), "\n";
418 0 warn "Open: ", $e->opening_tag->name(),
419 " at line ", $e->opening_tag->line(), "\n";
420  
421 0 exit(-1);
422 }
423 elsif ( $e = Exception::Class->caught() )
424 {
425 0 ▶ if ( ref($e) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e) { } 0 0 0
426 {
427 0 $e->rethrow();
428 }
429 else
430 {
431 0 die $e;
432 }
433 }
434  
435 0 return;
436 }
437  
438 sub _calc_tree
439 56 ▶{
Subroutine _calc_tree: called CC=2 SCAR=7.1
440 56 my ( $self, $args ) = @_;
441  
442 my $filename = $args->{source}->{file}
443 56 ▶ or confess "Wrong filename given.";
Branch: 1 branch
BranchTrueFalseTotal
my $filename = $args->{"source"}{"file"} or confess("Wrong filename given.") 56 0 56
444  
445 56 return $self->_parser->process_text( path($filename)->slurp_utf8() );
446 }
447  
448 sub _write_scene
449 100 ▶{
Subroutine _write_scene: called CC=3 SCAR=12.0
450 100 my ( $self, $args ) = @_;
451  
452 100 my $scene = $args->{scene};
453  
454 100 my $tag = $scene->name;
455  
456 100 ▶ if ( ( $tag eq "s" ) || ( $tag eq "scene" ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 's' or $tag eq 'scene') { } 100 0 100
Condition33%1/3
$tag eq "s" or $tag eq "scene"
l!l&&r!l&&!r
10000
MC/DC0%0/2
$tag eq "s" or $tag eq "scene"
$tag eq "s" $tag eq "scene"
Truth table33%1/3
A or B
A= $tag eq "s"B= $tag eq "scene"
ABresult
1X1
011
000
457 {
458 100 $self->_write_scene_main($scene);
459 }
460 else
461 {
462 0 confess "Improper scene tag - should be '<s>' or '<scene>'!";
463 }
464  
465 100 return;
466 }
467  
468  
469 sub convert
470 56 ▶{
Subroutine convert: called CC=2 SCAR=7.1
Pod documented
471 56 my ( $self, $args ) = @_;
472  
473 56 my $tree = $self->_calc_tree($args);
474 46 ▶ if ( !defined($tree) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree) 0 46 46
475 {
476 0 Carp::confess("Parsing failed.");
477 }
478  
479 46 $self->_convert_write_content($tree);
480  
481 46 return ${ $self->_flush_buffer() };
482 }
483  
484  
485 19 1;
486  
487 __END__
« blib/lib/XML/Grammar/Screenplay/API/Concat.pm ↑ Summary blib/lib/XML/Grammar/Screenplay/FromProto/Parser/QnD.pm »