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

filter: Statementstmt 80.2% 130 / 162 Branchbran 55.0% 22 / 40 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 97.3% 37 / 38 Podpod 100% 1 / 1 totaltotal 76.8% 193 / 251 CC 26 SCAR 18.2
CC
26
Cov
75%
_convert_while_handling_errors
42.0
_write_elem_childs
7.1
_write_Element_elem
5.0
CRAP 6.2
↑ Summary Build »
1 package XML::Grammar::FictionBase::TagsTree2XML;
2 20 $XML::Grammar::FictionBase::TagsTree2XML::VERSION = '0.22.4';
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use MooX 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use XML::Writer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 20 ▶use Path::Tiny qw/ path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub _get_xml_xml_ns
14 {
15 114 ▶ return "http://www.w3.org/XML/1998/namespace";
Subroutine _get_xml_xml_ns: called CC=1 SCAR=0.0
16 }
17  
18 sub _get_xlink_xml_ns
19 {
20 55 ▶ return "http://www.w3.org/1999/xlink";
Subroutine _get_xlink_xml_ns: called CC=1 SCAR=0.0
21 }
22  
23  
24 20 has '_parser_class' => (
25 is => "ro",
26 isa => "Str",
27 init_arg => "parser_class",
28 default => "XML::Grammar::Fiction::FromProto::Parser::QnD",
29 );
30  
31 has "_parser" => (
32 'isa' => "XML::Grammar::Fiction::FromProto::Parser",
33 'is' => "rw",
34 lazy => 1,
35 default => sub {
36 13 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
37 13 return $self->_parser_class->new();
38 },
39 20 );
40  
41 sub _get_initial_writer
42 {
43 48 ▶ my $self = shift;
Subroutine _get_initial_writer: called CC=1 SCAR=0.0
44  
45 48 my $writer = XML::Writer->new(
46 OUTPUT => $self->_buffer(),
47 ENCODING => "utf-8",
48 NAMESPACES => 1,
49 PREFIX_MAP => {
50 $self->_get_default_xml_ns() => "",
51 $self->_get_xml_xml_ns() => 'xml',
52 $self->_get_xlink_xml_ns() => 'xlink',
53 },
54 );
55  
56 48 $writer->xmlDecl("utf-8");
57  
58 48 return $writer;
59 }
60  
61 has "_writer" => (
62 'isa' => "Maybe[XML::Writer]",
63 'is' => "rw",
64 lazy => 1,
65 4 ▶ default => sub { return shift->_get_initial_writer(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 20 );
67  
68 sub _get_initial_buffer
69 {
70 48 ▶ my $buffer = '';
Subroutine _get_initial_buffer: called CC=1 SCAR=0.0
71 48 return \$buffer;
72 }
73  
74 has '_buffer' => (
75 is => "rw",
76 lazy => 1,
77 4 ▶ default => sub { return shift->_get_initial_buffer; },
Subroutine __ANON__: called CC=1 SCAR=0.0
78 20 );
79  
80 sub _reset_buffer
81 {
82 44 ▶ my $self = shift;
Subroutine _reset_buffer: called CC=1 SCAR=0.0
83  
84 44 $self->_buffer( $self->_get_initial_buffer() );
85 44 $self->_writer( $self->_get_initial_writer() );
86  
87 44 return;
88 }
89  
90 sub _flush_buffer
91 {
92 44 ▶ my $self = shift;
Subroutine _flush_buffer: called CC=1 SCAR=0.0
93  
94 44 my $ret = $self->_buffer();
95 44 $self->_reset_buffer();
96  
97 44 return $ret;
98 }
99  
100 my %passthrough_elem = (
101 24 ▶ b => sub { return +{ tag => shift->_bold_tag_name(), wrap_para => 1, }; },
Subroutine __ANON__: called CC=1 SCAR=0.0
102 i =>
103 20 ▶ sub { return +{ tag => shift->_italics_tag_name(), wrap_para => 1, }; },
Subroutine __ANON__: called CC=1 SCAR=0.0
104 20 );
105  
106 sub _calc_passthrough_cb
107 {
108 232 ▶ my ( $self, $name ) = @_;
Subroutine _calc_passthrough_cb: called CC=2 SCAR=6.9
109  
110 232 ▶ if ( exists( $passthrough_elem{$name} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $passthrough_elem{$name}) { } 44 188 232
111 {
112 44 return $passthrough_elem{$name};
113 }
114 else
115 {
116 188 return;
117 }
118 }
119  
120 sub _calc_passthrough_name
121 {
122 282 ▶ my ( $self, $name, $elem ) = @_;
Subroutine _calc_passthrough_name: called CC=2 SCAR=6.9
123  
124 282 my $cb = $self->_calc_passthrough_cb($name);
125  
126 282 ▶ if ( ref($cb) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cb eq 'CODE') { } 44 238 282
127 {
128 44 return $cb->( $self, $name, $elem, );
129 }
130 else
131 {
132 238 return $cb;
133 }
134 }
135  
136 sub _write_elem
137 {
138 1953 ▶ my ( $self, $args ) = @_;
Subroutine _write_elem: called CC=2 SCAR=6.9
139  
140 1953 my $elem = $args->{elem};
141  
142 1953 ▶ if ( ref($elem) eq "" )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elem eq '') { } 573 1380 1953
143 {
144 573 $self->_writer->characters($elem);
145 }
146 else
147 {
148 1380 return $self->_write_elem_obj($args);
149 }
150 }
151  
152 sub _write_Element_Paragraph
153 {
154 334 ▶ my ( $self, $elem ) = @_;
Subroutine _write_Element_Paragraph: called CC=2 SCAR=7.0
155  
156 my $out_cb = sub {
157 334 ▶ return $self->_write_elem_childs( $elem, );
Subroutine __ANON__: called CC=1 SCAR=0.0
158 334 };
159 return (
160 334 ▶ ( $self->_writer->within_element( $self->_paragraph_tag ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->_writer->within_element($self->_paragraph_tag) ? : 0 334 334
161 ? $out_cb->()
162 : $self->_output_tag(
163 {
164 start => [ $self->_paragraph_tag, ],
165 in => $out_cb,
166 }
167 )
168 );
169 }
170  
171 sub _write_Element_Element
172 {
173 286 ▶ my ( $self, $elem ) = @_;
Subroutine _write_Element_Element: called CC=1 SCAR=0.0
174  
175 286 return $self->_write_Element_elem($elem);
176 }
177  
178 sub _write_Element_Comment
179 {
180 22 ▶ my ( $self, $elem ) = @_;
Subroutine _write_Element_Comment: called CC=2 SCAR=6.9
181  
182 22 my $text = $elem->text();
183  
184 # To avoid trailing space due to a problem in XML::Writer
185 22 $text =~ s{\A[\r\n]+}{}ms;
186  
187 22 ▶ if ( $text =~ m{\n\z} )
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\n\z/) 6 16 22
188 {
189 6 $text .= ' ';
190 }
191  
192 22 $self->_writer->comment($text);
193 }
194  
195 sub _calc_write_elem_obj_classes
196 {
197 1380 ▶ return [qw(Text Paragraph Element Comment)];
Subroutine _calc_write_elem_obj_classes: called CC=1 SCAR=0.0
198 }
199  
200 sub _output_tag_with_childs
201 {
202 440 ▶ my ( $self, $args ) = @_;
Subroutine _output_tag_with_childs: called CC=1 SCAR=0.2
203  
204 return $self->_output_tag(
205 {
206 %$args,
207 'in' => sub {
208 440 ▶ if ( not $args->{elem} )
Subroutine __ANON__: called CC=2 SCAR=8.1
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"elem"}) 0 440 440
209 {
210 0 die "args elem";
211 }
212 440 return $self->_write_elem_childs( $args->{elem} );
213 },
214 }
215 440 );
216 }
217  
218 sub _write_elem_childs
219 {
220 1343 ▶ my ( $self, $elem ) = @_;
Subroutine _write_elem_childs: called CC=5 SCAR=19.6
221  
222 1343 ▶ if ( not ref $elem )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $elem) 0 1343 1343
223 {
224 0 return;
225  
226 0 die "elem=[$elem]";
227 }
228 1343 ▶ if ( ref($elem) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $elem eq "ARRAY") 0 1343 1343
229 {
230 # body...
231 0 foreach my $child (@$elem)
232 {
233 0 $self->_write_elem( { elem => $child, }, );
234 }
235 0 return;
236 }
237 1343 foreach my $child ( @{ $elem->_get_childs() } )
238 {
239 1899 $self->_write_elem( { elem => $child, }, );
240 }
241  
242 1343 return;
243 }
244  
245 sub _write_elem_obj
246 {
247 1380 ▶ my ( $self, $args ) = @_;
Subroutine _write_elem_obj: called CC=3 SCAR=11.0
248  
249 1380 my $elem = $args->{elem};
250  
251 1380 foreach my $class ( @{ $self->_calc_write_elem_obj_classes() } )
252 {
253 2901 ▶ if ( $elem->_short_isa($class) )
Branch: 1 branch
BranchTrueFalseTotal
if ($elem->_short_isa($class)) 1380 1521 2901
254 {
255 1380 my $meth = "_write_Element_$class";
256 1380 $self->$meth($elem);
257 1380 return;
258 }
259 }
260  
261 0 Carp::confess( "Class of element not detected - " . ref($elem) . "!" );
262 }
263  
264 sub _write_Element_elem
265 {
266 286 ▶ my ( $self, $elem ) = @_;
Subroutine _write_Element_elem: called CC=5 SCAR=16.1
267  
268 286 my $name = $elem->name();
269  
270 286 ▶ if ( $elem->_short_isa("InnerDesc") )
Branch: 2 branches
BranchTrueFalseTotal
if ($elem->_short_isa('InnerDesc')) { } 4 282 286
elsif (defined(my $out_name = $self->_calc_passthrough_name($name, $elem))) { } 94 188 282
271 {
272 4 $self->_output_tag_with_childs(
273 {
274 start => ["inlinedesc"],
275 elem => $elem,
276 }
277 );
278 4 return;
279 }
280 elsif (
281 defined( my $out_name = $self->_calc_passthrough_name( $name, $elem ) )
282 )
283 {
284 my $out_cb = sub {
285 return $self->_output_tag_with_childs(
286 {
287 94 ▶ start => [ $out_name->{tag} ],
Subroutine __ANON__: called CC=1 SCAR=0.0
288 elem => $elem,
289 }
290 );
291 94 };
292  
293 # warn "\$out_name=[$out_name]";
294 return (
295 (
296 $out_name->{wrap_para}
297 && (
298 94 ▶ !$self->_writer->within_element( $self->_paragraph_tag ) )
Branch: 1 branch
BranchTrueFalseTotal
$out_name->{'wrap_para'} && !$self->_writer->within_element($self->_paragraph_tag) ? : 0 94 94
Condition66%2/3
$out_name->{"wrap_para"} and not $self->_writer->within_element($self->_paragraph_tag)
!ll&&!rl&&r
50440
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
299 )
300 ? $self->_output_tag(
301 {
302 start => [ $self->_paragraph_tag, ],
303 in => $out_cb,
304 }
305 )
306 : $out_cb->()
307 );
308 }
309 else
310 {
311 188 my $method = "_handle_elem_of_name_$name";
312  
313 188 $self->$method($elem);
314  
315 188 return;
316 }
317 }
318  
319 sub _handle_elem_of_name_s
320 {
321 68 ▶ my ( $self, $elem ) = @_;
Subroutine _handle_elem_of_name_s: called CC=1 SCAR=0.0
322  
323 68 $self->_write_scene( { scene => $elem } );
324 }
325  
326 sub _handle_elem_of_name_br
327 {
328 5 ▶ my ( $self, $elem ) = @_;
Subroutine _handle_elem_of_name_br: called CC=1 SCAR=0.0
329  
330 5 $self->_writer->emptyTag("br");
331  
332 5 return;
333 }
334  
335 sub _output_tag
336 {
337 828 ▶ my ( $self, $args ) = @_;
Subroutine _output_tag: called CC=1 SCAR=0.0
338  
339 828 my @start = @{ $args->{start} };
340 828 $self->_writer->startTag( [ $self->_get_default_xml_ns(), $start[0] ],
341 @start[ 1 .. $#start ] );
342  
343 828 $args->{in}->( $self, $args );
344  
345 828 $self->_writer->endTag();
346 }
347  
348 sub _convert_while_handling_errors
349 {
350 0 ▶ my ( $self, $args ) = @_;
Subroutine _convert_while_handling_errors: not called CC=6 SCAR=37.4
351  
352 0 eval {
353 0 my $output_xml = $self->convert( $args->{convert_args}, );
354  
355 0 open my $out, ">:encoding(UTF-8)", $args->{output_filename};
356 0 print {$out} $output_xml;
357 0 close($out);
358 };
359  
360 # Error handling.
361  
362 0 my $e;
363 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
364 $e = Exception::Class->caught(
365 "XML::Grammar::Fiction::Err::Parse::TagsMismatch")
366 )
367 {
368 0 warn $e->error(), "\n";
369 0 warn "Open: ", $e->opening_tag->name(),
370 " at line ", $e->opening_tag->line(), "\n";
371 0 warn "Close: ",
372 $e->closing_tag->name(), " at line ",
373 $e->closing_tag->line(), "\n";
374  
375 0 exit(-1);
376 }
377 elsif (
378 $e = Exception::Class->caught(
379 "XML::Grammar::Fiction::Err::Parse::LineError")
380 )
381 {
382 0 warn $e->error(), "\n";
383 0 warn "At line ", $e->line(), "\n";
384 0 exit(-1);
385 }
386 elsif (
387 $e = Exception::Class->caught(
388 "XML::Grammar::Fiction::Err::Parse::TagNotClosedAtEOF")
389 )
390 {
391 0 warn $e->error(), "\n";
392 0 warn "Open: ", $e->opening_tag->name(),
393 " at line ", $e->opening_tag->line(), "\n";
394  
395 0 exit(-1);
396 }
397 elsif ( $e = Exception::Class->caught() )
398 {
399 0 ▶ if ( ref($e) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e) { } 0 0 0
400 {
401 0 $e->rethrow();
402 }
403 else
404 {
405 0 die $e;
406 }
407 }
408  
409 0 return;
410 }
411  
412 sub _calc_tree
413 {
414 53 ▶ my ( $self, $args ) = @_;
Subroutine _calc_tree: called CC=2 SCAR=7.1
415  
416 my $filename = $args->{source}->{file}
417 53 ▶ or confess "Wrong filename given.";
Branch: 1 branch
BranchTrueFalseTotal
my $filename = $args->{"source"}{"file"} or confess("Wrong filename given.") 0 53 53
418  
419 53 return $self->_parser->process_text( path($filename)->slurp_utf8() );
420 }
421  
422 sub _write_scene
423 {
424 96 ▶ my ( $self, $args ) = @_;
Subroutine _write_scene: called CC=3 SCAR=12.0
425  
426 96 my $scene = $args->{scene};
427  
428 96 my $tag = $scene->name;
429  
430 96 ▶ if ( ( $tag eq "s" ) || ( $tag eq "scene" ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq 's' or $tag eq 'scene') { } 96 0 96
Condition33%1/3
$tag eq "s" or $tag eq "scene"
l!l&&r!l&&!r
9600
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
431 {
432 96 $self->_write_scene_main($scene);
433 }
434 else
435 {
436 0 confess "Improper scene tag - should be '<s>' or '<scene>'!";
437 }
438  
439 96 return;
440 }
441  
442  
443 sub convert
444 {
445 53 ▶ my ( $self, $args ) = @_;
Subroutine convert: called CC=2 SCAR=7.1
Pod documented
446  
447 53 my $tree = $self->_calc_tree($args);
448 44 ▶ if ( !defined($tree) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tree) 0 44 44
449 {
450 0 Carp::confess("Parsing failed.");
451 }
452  
453 44 $self->_convert_write_content($tree);
454  
455 44 return ${ $self->_flush_buffer() };
456 }
457  
458  
459 20 1;
460  
461 __END__
462  
463 =pod
464  
465 =encoding UTF-8
466  
467 =head1 NAME
468  
469 XML::Grammar::FictionBase::TagsTree2XML - base class for the tags-tree
470 to XML converters.
471  
472 =head2 $self->convert({ source => { file => $path_to_file } })
473  
474 Converts the file $path_to_file to XML and returns it. Throws an exception
475 on failure.
476  
477 =head2 meta()
478  
479 Internal - (to settle pod-coverage.).
480  
481 =head1 VERSION
482  
483 version 0.22.4
484  
485 =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
486  
487 =head1 SUPPORT
488  
489 =head2 Websites
490  
491 The following websites have more information about this module, and may be of help to you. As always,
492 in addition to those websites please use your favorite search engine to discover more resources.
493  
494 =over 4
495  
496 =item *
497  
498 MetaCPAN
499  
500 A modern, open-source CPAN search engine, useful to view POD in HTML format.
501  
502 L<https://metacpan.org/release/XML-Grammar-Fiction>
503  
504 =item *
505  
506 RT: CPAN's Bug Tracker
507  
508 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
509  
510 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Grammar-Fiction>
511  
512 =item *
513  
514 CPANTS
515  
516 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
517  
518 L<http://cpants.cpanauthors.org/dist/XML-Grammar-Fiction>
519  
520 =item *
521  
522 CPAN Testers
523  
524 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
525  
526 L<http://www.cpantesters.org/distro/X/XML-Grammar-Fiction>
527  
528 =item *
529  
530 CPAN Testers Matrix
531  
532 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
533  
534 L<http://matrix.cpantesters.org/?dist=XML-Grammar-Fiction>
535  
536 =item *
537  
538 CPAN Testers Dependencies
539  
540 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
541  
542 L<http://deps.cpantesters.org/?module=XML::Grammar::Fiction>
543  
544 =back
545  
546 =head2 Bugs / Feature Requests
547  
548 Please report any bugs or feature requests by email to C<bug-xml-grammar-fiction at rt.cpan.org>, or through
549 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-Grammar-Fiction>. You will be automatically notified of any
550 progress on the request by the system.
551  
552 =head2 Source Code
553  
554 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
555 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
556 from your repository :)
557  
558 L<https://github.com/shlomif/fiction-xml>
559  
560 git clone git://github.com/shlomif/fiction-xml.git
561  
562 =head1 AUTHOR
563  
564 Shlomi Fish <shlomif@cpan.org>
565  
566 =head1 BUGS
567  
568 Please report any bugs or feature requests on the bugtracker website
569 L<https://github.com/shlomif/fiction-xml/issues>
570  
571 When submitting a bug or request, please include a test-file or a
572 patch to an existing test-file that illustrates the bug or desired
573 feature.
574  
575 =head1 COPYRIGHT AND LICENSE
576  
577 This software is Copyright (c) 2007 by Shlomi Fish.
578  
579 This is free software, licensed under:
580  
581 The MIT (X11) License
582  
583 =cut
↑ Summary Build »