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

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