blib/lib/XML/Grammar/Fortune/ToText.pm

filter: Statementstmt 15.2% 41 / 269 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 16.0% 8 / 50 Podpod 100% 1 / 1 totaltotal 12.5% 50 / 400 CC 41 SCAR 32.6
CC
41
Cov
12%
_process_irc_node
113.6
_render_para
56.0
_out_info_field_node
42.0
CRAP 26.1
↑ Summary Build »
1 package XML::Grammar::Fortune::ToText;
2 1 $XML::Grammar::Fortune::ToText::VERSION = '0.0701';
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Text::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use List::Util (qw(max));
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use MooX qw/late/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 has '_formatter' => (
15 is => 'rw',
16 default => sub {
17 0 ▶ return Text::Format->new(
Subroutine __ANON__: not called CC=1 SCAR=6.9
18 {
19 columns => 78,
20 firstIndent => 0,
21 leftMargin => 0,
22 }
23 );
24 },
25 1 );
26 1 has '_is_first_line' => ( isa => "Bool", is => 'rw' );
27 1 has '_input' => ( is => 'rw', init_arg => 'input', required => 1, );
28 1 has '_output' => ( is => 'rw', init_arg => 'output', required => 1, );
29 1 has '_this_line' => ( isa => 'Str', is => 'rw', default => '', );
30 has '_buf' => (
31 isa => 'ScalarRef[Str]',
32 is => 'rw',
33 0 ▶ default => sub { my $s = ''; return \$s; }
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 1 );
35  
36  
37 sub _out
38 {
39 0 ▶ my $self = shift;
Subroutine _out: not called CC=1 SCAR=6.9
40  
41 0 ${ $self->_buf() } .= join( '', @_ );
42  
43 0 return;
44 }
45  
46 sub _start_new_line
47 {
48 0 ▶ my ($self) = @_;
Subroutine _start_new_line: not called CC=1 SCAR=6.9
49  
50 0 return $self->_out("\n");
51 }
52  
53 sub _render_single_fortune_cookie
54 {
55 0 ▶ my ( $self, $fortune_node ) = @_;
Subroutine _render_single_fortune_cookie: not called CC=1 SCAR=6.9
56  
57 0 my ($node) = $fortune_node->findnodes("raw|irc|screenplay|quote");
58  
59 0 my $method = sprintf( "_process_%s_node", $node->localname() );
60  
61 0 $self->$method($node);
62  
63 0 $self->_render_info_if_exists($fortune_node);
64  
65 0 return;
66 }
67  
68 sub _output_next_fortune_delim
69 {
70 0 ▶ my $self = shift;
Subroutine _output_next_fortune_delim: not called CC=1 SCAR=6.9
71  
72 0 return $self->_out("%\n");
73 }
74  
75 ▶sub _do_nothing { }
Subroutine _do_nothing: not called CC=1 SCAR=0.0
76  
77 sub _iterate_on_child_elems
78 {
79 0 ▶ my ( $self, $top_elem, $xpath, $args ) = @_;
Subroutine _iterate_on_child_elems: not called CC=5 SCAR=34.0
80  
81 0 my $process = $args->{'process'};
82 0 my $if_remainaing_meth = $args->{'if_more'};
83 0 ▶ my $continue_cb = ( $args->{'cont'} || \&_do_nothing );
Condition0%0/2
$args->{'cont'} || \&XML::Grammar::Fortune::ToText::_do_nothing
l!l
00
MC/DC0%0/1
$args->{'cont'} || \&XML::Grammar::Fortune::ToText::_do_nothing
$args->{'cont'}
Truth table0%0/2
A
A= $args->{'cont'}
Aresult
11
00
84  
85 my $process_cb = (
86 ( ref($process) eq "CODE" )
87 ? $process
88 0 ▶ : sub { return $self->$process(shift); }
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
ref $process eq 'CODE' ? : 0 0 0
90  
91 0 my $list = $top_elem->findnodes($xpath);
92  
93 0 ▶ while ( my $elem = $list->shift() )
Branch: 1 branch
BranchTrueFalseTotal
if (my $elem = $list->shift) 0 0 0
94 {
95 0 $process_cb->($elem);
96 }
97 continue
98 {
99 0 $continue_cb->();
100  
101 0 ▶ if ( $list->size() )
Branch: 1 branch
BranchTrueFalseTotal
if ($list->size) 0 0 0
102 {
103 0 $self->$if_remainaing_meth();
104 }
105 }
106  
107 0 return;
108 }
109  
110  
111 sub run
112 {
113 0 ▶ my $self = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
114  
115 0 my $xml = XML::LibXML->new->parse_file( $self->_input() );
116  
117 0 $self->_iterate_on_child_elems(
118 $xml,
119 "//fortune",
120 {
121 process => '_render_single_fortune_cookie',
122 if_more => '_output_next_fortune_delim',
123 }
124 );
125  
126 0 my $buf = ${ $self->_buf() };
127  
128 0 $buf =~ s/[ \t]+$//gms;
129  
130 0 print { $self->_output() } $buf;
131  
132 0 return;
133 }
134  
135 sub _render_info_if_exists
136 {
137 0 ▶ my ( $self, $fortune_node ) = @_;
Subroutine _render_info_if_exists: not called CC=2 SCAR=17.9
138  
139 0 ▶ if ( my ($info_node) = $fortune_node->findnodes("descendant::info") )
Branch: 1 branch
BranchTrueFalseTotal
if (my($info_node) = $fortune_node->findnodes("descendant::info")) 0 0 0
140 {
141 0 $self->_render_info_node($info_node);
142 }
143  
144 0 return;
145 }
146  
147 sub _process_raw_node
148 {
149 0 ▶ my ( $self, $raw_node ) = @_;
Subroutine _process_raw_node: not called CC=3 SCAR=24.8
150  
151 0 my ($text_node) = $raw_node->findnodes("body/text");
152  
153 0 my @text_childs = $text_node->childNodes();
154  
155 0 ▶ if ( @text_childs != 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@text_childs != 1) 0 0 0
156 {
157 0 Carp::confess('@cdata is not 1');
158 }
159  
160 0 my $cdata = $text_childs[0];
161  
162 0 ▶ if ( $cdata->nodeType() != XML_CDATA_SECTION_NODE() )
Branch: 1 branch
BranchTrueFalseTotal
if ($cdata->nodeType != 4) 0 0 0
163 {
164 0 Carp::confess("Not a cdata");
165 }
166  
167 0 my $value = $cdata->nodeValue();
168  
169 0 $value =~ s{\n+\z}{}g;
170 0 $self->_out("$value\n");
171  
172 0 return;
173 }
174  
175 sub _process_irc_node
176 {
177 0 ▶ my ( $self, $irc_node ) = @_;
Subroutine _process_irc_node: not called CC=11 SCAR=47.3
178  
179 0 my ($body_node) = $irc_node->findnodes("body");
180  
181 0 my @lines_list = $body_node->findnodes("saying|me_is|joins|leaves");
182  
183 1 ▶ use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
184  
185 0 my $longest_nick_len = 0;
186 0 my @messages;
187 0 foreach my $line (@lines_list)
188 {
189 0 ▶ if ( $line->localname() eq "saying" )
Branch: 4 branches
BranchTrueFalseTotal
if ($line->localname eq 'saying') { } 0 0 0
elsif ($line->localname eq 'me_is') { } 0 0 0
elsif ($line->localname eq 'joins') { } 0 0 0
elsif ($line->localname eq 'leaves') { } 0 0 0
190 {
191 0 my $nick = $line->getAttribute("who");
192 0 push @messages,
193 {
194 type => "say",
195 nick => $nick,
196 msg => $line->textContent(),
197 };
198  
199 0 $longest_nick_len = max( $longest_nick_len, length($nick) );
200 }
201 elsif ( $line->localname() eq "me_is" )
202 {
203 0 my $nick = $line->getAttribute("who");
204 0 push @messages,
205 {
206 type => "me_is",
207 nick => $nick,
208 msg => $line->textContent(),
209 };
210  
211 0 $longest_nick_len = max( $longest_nick_len, length("*") );
212 }
213 elsif ( $line->localname() eq "joins" )
214 {
215 0 my $nick = $line->getAttribute("who");
216 0 push @messages,
217 {
218 type => "joins",
219 nick => $nick,
220 msg => $line->textContent(),
221 };
222  
223 0 $longest_nick_len = max( $longest_nick_len, length("<--") );
224 }
225 elsif ( $line->localname() eq "leaves" )
226 {
227 0 my $nick = $line->getAttribute("who");
228 0 push @messages,
229 {
230 type => "leaves",
231 nick => $nick,
232 msg => $line->textContent(),
233 };
234  
235 0 $longest_nick_len = max( $longest_nick_len, length("-->") );
236 }
237 else
238 {
239 0 Carp::confess(
240 'Unimplemented localname "' . $line->localname() . '"' );
241 }
242 }
243  
244  
245 0 my $formatter = Text::Format->new(
246 {
247 columns => 72 - 1 - 2 - $longest_nick_len,
248 firstIndent => 0,
249 leftMargin => 0,
250 }
251 );
252  
253 0 my $line_starts_at = $longest_nick_len + 1 # For the <
254 + 1 # For the >
255 + 1 # For the space at the beginning
256 + 2 # For the space after the nick.
257 ;
258  
259 0 my $nick_len_with_delim = $longest_nick_len + 2;
260  
261 0 foreach my $m (@messages)
262 {
263 0 my %cmds = ( "me_is" => "*", "joins" => "-->", "leaves" => "<--", );
264  
265 0 ▶ if ( $m->{'type'} eq "say" )
Branch: 4 branches
BranchTrueFalseTotal
if ($m->{'type'} eq 'say') { } 0 0 0
elsif ($m->{'type'} eq 'raw') { } 0 0 0
elsif ($m->{'type'} eq 'change_nick') { } 0 0 0
elsif (exists $cmds{$m->{'type'}}) { } 0 0 0
266 {
267 0 my @lines = ( $formatter->format( [ $m->{'msg'} ] ) );
268 $self->_out(
269 " "
270 . sprintf( "%${nick_len_with_delim}s",
271 0 "<" . $m->{'nick'} . ">" )
272 . " "
273 . $lines[0]
274 );
275 $self->_out(
276 join( "",
277 0 map { ( " " x $line_starts_at ) . $_ }
278 @lines[ 1 .. $#lines ] )
279 );
280 }
281 elsif ( $m->{'type'} eq "raw" )
282 {
283 0 $self->_out( $m->{'msg'} . "\n" );
284 }
285 elsif ( $m->{'type'} eq "change_nick" )
286 {
287 $self->_out( ( " " x ($line_starts_at) )
288 . $m->{'old'}
289 . " is now known as "
290 0 . $m->{'new'}
291 . "\n" );
292 }
293 elsif ( exists( $cmds{ $m->{'type'} } ) )
294 {
295 my @lines =
296 0 $formatter->format( [ $m->{'nick'} . " " . $m->{'msg'} ] );
297  
298 $self->_out(
299 " "
300 . sprintf(
301 0 "%${nick_len_with_delim}s", $cmds{ $m->{'type'} }
302 )
303 . " "
304 . $lines[0]
305 );
306  
307 $self->_out(
308 join( "",
309 0 map { ( " " x $line_starts_at ) . $_ }
310 @lines[ 1 .. $#lines ] )
311 );
312 }
313 }
314  
315 0 return;
316 }
317  
318 sub _render_screenplay_paras
319 {
320 0 ▶ my ( $self, $portion ) = @_;
Subroutine _render_screenplay_paras: not called CC=1 SCAR=6.9
321 0 return $self->_render_portion_paras( $portion, { para_is => "para" } );
322 }
323  
324 sub _is_portion_desc
325 {
326 0 ▶ my ( $self, $portion ) = @_;
Subroutine _is_portion_desc: not called CC=1 SCAR=6.9
327  
328 0 return ( $portion->localname() eq "description" );
329 }
330  
331 sub _get_screenplay_portion_opening
332 {
333 0 ▶ my ( $self, $portion ) = @_;
Subroutine _get_screenplay_portion_opening: not called CC=2 SCAR=17.9
334  
335 0 ▶ return $self->_is_portion_desc($portion)
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_portion_desc($portion) ? : 0 0 0
336 ? "["
337  
338 # A saying.
339 : ( $portion->getAttribute("character") . ": " );
340 }
341  
342 sub _handle_screenplay_portion
343 {
344 0 ▶ my ( $self, $portion ) = @_;
Subroutine _handle_screenplay_portion: not called CC=2 SCAR=17.9
345  
346 0 $self->_this_line( $self->_get_screenplay_portion_opening($portion) );
347  
348 0 $self->_render_screenplay_paras($portion);
349  
350 0 ▶ if ( $self->_is_portion_desc($portion) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_portion_desc($portion)) 0 0 0
351 {
352 0 $self->_out("]");
353 }
354  
355 0 $self->_start_new_line;
356  
357 0 return;
358 }
359  
360 sub _process_screenplay_node
361 {
362 0 ▶ my ( $self, $play_node ) = @_;
Subroutine _process_screenplay_node: not called CC=1 SCAR=6.9
363  
364 0 my ($body_node) = $play_node->findnodes("body");
365  
366 0 $self->_iterate_on_child_elems(
367 $body_node,
368 "description|saying",
369 {
370 process => '_handle_screenplay_portion',
371 if_more => '_start_new_line',
372 }
373 );
374  
375 0 return;
376 }
377  
378 sub _out_formatted_line
379 {
380 0 ▶ my $self = shift;
Subroutine _out_formatted_line: not called CC=2 SCAR=17.9
381 0 my $text = $self->_this_line();
382  
383 0 $text =~ s{\A\n+}{}ms;
384 0 $text =~ s{\n+\z}{}ms;
385 0 $text =~ s{\s+}{ }gms;
386  
387 0 ▶ if ( $self->_is_first_line() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_first_line) { } 0 0 0
388 {
389 0 $self->_is_first_line(0);
390 }
391 else
392 {
393 0 $self->_start_new_line;
394 }
395  
396 0 my $output_string = $self->_formatter->format($text);
397  
398 # Text::Format inserts a new line - remove it.
399 0 chomp($output_string);
400  
401 0 $self->_out($output_string);
402  
403 0 $self->_this_line("");
404  
405 0 return;
406 }
407  
408 sub _append_to_this_line
409 {
410 0 ▶ my ( $self, $more_text ) = @_;
Subroutine _append_to_this_line: not called CC=1 SCAR=6.9
411  
412 0 $self->_this_line( $self->_this_line() . $more_text );
413 }
414  
415 sub _append_different_formatting_node
416 {
417 0 ▶ my ( $self, $prefix, $suffix, $node ) = @_;
Subroutine _append_different_formatting_node: not called CC=1 SCAR=6.9
418  
419 0 return $self->_append_to_this_line(
420 $prefix . $node->textContent() . $suffix );
421 }
422  
423 {
424 1 my @_highlights =
425 ( [ '/', [qw(em i italics)] ], [ '*', [qw(b bold strong)] ] );
426  
427 my %_formats_map = (
428 (
429 map {
430 1 my ( $f, $tags ) = @$_;
431 2 map { $_ => [ ($f) x 2 ] } @$tags
432 } @_highlights
433 ),
434 'inlinedesc' => [ '[', ']' ],
435 );
436  
437 sub _get_node_formatting_delims
438 {
439 0 ▶ my ( $self, $node ) = @_;
Subroutine _get_node_formatting_delims: not called CC=2 SCAR=17.9
440  
441 0 my $name = $node->localname();
442  
443 return exists( $_formats_map{$name} )
444 0 ▶ ? $_formats_map{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $_formats_map{$name} ? : 0 0 0
445 : [ (q{}) x 2 ];
446 }
447 }
448  
449 sub _handle_format_node
450 1 {
451 0 ▶ my ( $self, $node ) = @_;
Subroutine _handle_format_node: not called CC=1 SCAR=6.9
452  
453 $self->_append_different_formatting_node(
454 0 @{ $self->_get_node_formatting_delims($node) }, $node, );
455  
456 0 return;
457 }
458  
459 sub _get_formatted_node_text
460 {
461 0 ▶ my $self = shift;
Subroutine _get_formatted_node_text: not called CC=1 SCAR=6.9
462 0 my $node = shift;
463  
464 0 my $text = $node->textContent();
465  
466 # Intent: format the text.
467 # Trim leading and trailing nelines.
468 # $text =~ s{\A\n+}{}ms;
469 # $text =~ s{\n+\z}{}ms;
470  
471 # Convert a sequence of spaces to a single space.
472 0 $text =~ s{\s+}{ }gms;
473  
474 0 return $text;
475 }
476  
477 sub _render_para
478 {
479 0 ▶ my ( $self, $para ) = @_;
Subroutine _render_para: not called CC=7 SCAR=40.3
480  
481 0 my $first_text = 1;
482  
483 0 foreach my $node ( $para->childNodes() )
484 {
485 0 ▶ if ( $node->nodeType() == XML_ELEMENT_NODE() )
Branch: 2 branches
BranchTrueFalseTotal
if ($node->nodeType == 1) { } 0 0 0
elsif ($node->nodeType == 3) { } 0 0 0
486 {
487 0 my $name = $node->localname();
488  
489 0 ▶ if ( $name eq "br" )
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'br') { } 0 0 0
elsif ($name eq 'a') { } 0 0 0
490 {
491 0 $self->_out_formatted_line();
492 }
493 elsif ( $name eq "a" )
494 {
495 0 $self->_append_different_formatting_node( "[",
496 ( "](" . $node->getAttribute("href") . ")" ), $node );
497 }
498 else
499 {
500 0 $self->_handle_format_node($node);
501 }
502 }
503 elsif ( $node->nodeType() == XML_TEXT_NODE() )
504 {
505 0 my $text = $self->_get_formatted_node_text($node);
506  
507 0 ▶ if ($first_text)
Branch: 1 branch
BranchTrueFalseTotal
if ($first_text) 0 0 0
508 {
509 0 $text =~ s/\A\s+//;
510 }
511  
512 0 $self->_append_to_this_line($text);
513 }
514 }
515 continue
516 {
517 0 $first_text = 0;
518 }
519 }
520  
521 sub _render_quote_list
522 {
523 0 ▶ my ( $self, $ul ) = @_;
Subroutine _render_quote_list: not called CC=1 SCAR=6.9
524  
525 0 my $is_bullets = ( $ul->localname() eq "ul" );
526  
527 0 my $idx = 1;
528  
529 $self->_iterate_on_child_elems(
530 $ul, "li",
531 {
532 process => sub {
533 0 ▶ my $li = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
534  
535 0 ▶ $self->_append_to_this_line(
Branch: 1 branch
BranchTrueFalseTotal
$is_bullets ? : 0 0 0
536 ( $is_bullets ? "*" : "$idx." ) . " " );
537  
538 0 $self->_render_para($li);
539  
540 0 return;
541 },
542 cont => sub {
543 0 ▶ $idx++;
Subroutine __ANON__: not called CC=1 SCAR=6.9
544  
545 0 $self->_out_formatted_line();
546  
547 0 return;
548 },
549 0 if_more => '_start_new_line',
550 }
551 );
552  
553 0 return;
554 }
555  
556 sub _render_quote_portion_paras
557 {
558 0 ▶ my ( $self, $node ) = @_;
Subroutine _render_quote_portion_paras: not called CC=1 SCAR=6.9
559  
560 0 $self->_render_portion_paras( $node, { para_is => "blockquote|p|ol|ul" } );
561  
562 0 return;
563 }
564  
565 sub _render_quote_blockquote
566 {
567 0 ▶ my ( $self, $node ) = @_;
Subroutine _render_quote_blockquote: not called CC=1 SCAR=6.9
568  
569 0 $self->_out("<<<\n\n");
570  
571 0 $self->_render_quote_portion_paras($node);
572  
573 0 $self->_out("\n\n>>>");
574 }
575  
576 sub _start_new_para
577 {
578 0 ▶ my ($self) = @_;
Subroutine _start_new_para: not called CC=1 SCAR=6.9
579  
580 0 return $self->_out("\n\n");
581 }
582  
583 sub _render_generalized_para
584 {
585 0 ▶ my ( $self, $para ) = @_;
Subroutine _render_generalized_para: not called CC=4 SCAR=30.0
586  
587 return (
588 0 ▶ ( ( $para->localname() eq "ul" ) || ( $para->localname() eq "ol" ) )
Branch: 2 branches
BranchTrueFalseTotal
$para->localname eq 'ul' || $para->localname eq 'ol' ? : 0 0 0
$para->localname eq 'blockquote' ? : 0 0 0
Condition0%0/3
$para->localname eq "ul" or $para->localname eq "ol"
l!l&&r!l&&!r
000
MC/DC0%0/2
$para->localname eq "ul" or $para->localname eq "ol"
$para->localname eq "ul" $para->localname eq "ol"
Truth table0%0/3
A or B
A= $para->localname eq "ul"B= $para->localname eq "ol"
ABresult
1X1
011
000
589 ? $self->_render_quote_list($para)
590 : ( $para->localname() eq "blockquote" )
591 ? $self->_render_quote_blockquote($para)
592 : $self->_render_para($para)
593 );
594 }
595  
596 sub _move_to_next_line
597 {
598 0 ▶ my $self = shift;
Subroutine _move_to_next_line: not called CC=2 SCAR=17.9
599  
600 0 ▶ if ( $self->_this_line() =~ m{\S} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_this_line =~ /\S/) 0 0 0
601 {
602 0 $self->_out_formatted_line();
603 0 $self->_this_line("");
604 }
605  
606 0 return;
607 }
608  
609 sub _handle_portion_paragraph
610 {
611 0 ▶ my ( $self, $para ) = @_;
Subroutine _handle_portion_paragraph: not called CC=1 SCAR=6.9
612  
613 0 $self->_is_first_line(1);
614  
615 0 $self->_render_generalized_para($para);
616  
617 0 $self->_move_to_next_line;
618  
619 0 return;
620 }
621  
622 sub _render_portion_paras
623 {
624 0 ▶ my ( $self, $portion, $args ) = @_;
Subroutine _render_portion_paras: not called CC=1 SCAR=6.9
625  
626 0 my $para_name = $args->{para_is};
627  
628 0 $self->_iterate_on_child_elems(
629 $portion,
630 $para_name,
631 {
632 process => '_handle_portion_paragraph',
633 if_more => '_start_new_para',
634 }
635 );
636  
637 0 return;
638 }
639  
640 sub _process_quote_node
641 {
642 0 ▶ my ( $self, $quote_node ) = @_;
Subroutine _process_quote_node: not called CC=1 SCAR=6.9
643  
644 0 my ($body_node) = $quote_node->findnodes("body");
645  
646 0 $self->_render_quote_portion_paras($body_node);
647  
648 0 $self->_start_new_line;
649  
650 0 return;
651 }
652  
653 1 my @info_fields_order = (qw(work author channel tagline));
654  
655 my %info_fields_order_map =
656 1 ( map { $info_fields_order[$_] => $_ + 1 } ( 0 .. $#info_fields_order ) );
657  
658 sub _info_field_value
659 {
660 0 ▶ my $self = shift;
Subroutine _info_field_value: not called CC=2 SCAR=17.9
661 0 my $field = shift;
662  
663 0 ▶ return $info_fields_order_map{ $field->localname() } || (-1);
Condition0%0/2
$info_fields_order_map{$field->localname} || -1
l!l
00
MC/DC0%0/1
$info_fields_order_map{$field->localname} || -1
$info_fields_order_map{$field->localname}
Truth table0%0/2
A
A= $info_fields_order_map{$field->localname}
Aresult
11
00
664 }
665  
666 sub _calc_info_field_processed_content
667 {
668 0 ▶ my $self = shift;
Subroutine _calc_info_field_processed_content: not called CC=1 SCAR=6.9
669 0 my $field_node = shift;
670  
671 0 my $content = $field_node->textContent();
672  
673 # Squash whitespace including newlines into a single space.
674 0 $content =~ s{\s+}{ }g;
675  
676 # Remove leading and trailing space - it is not desirable here
677 # because we want it formatted consistently.
678 0 $content =~ s{\A\s+}{};
679 0 $content =~ s{\s+\z}{};
680  
681 0 return $content;
682 }
683  
684 sub _output_info_value
685 {
686 0 ▶ my ( $self, $string ) = @_;
Subroutine _output_info_value: not called CC=1 SCAR=6.9
687  
688 0 return $self->_out( ( ' ' x 4 ) . '-- ' . $string . "\n" );
689 }
690  
691 sub _out_info_field_node
692 {
693 0 ▶ my ( $self, $info_node, $field_node ) = @_;
Subroutine _out_info_field_node: not called CC=6 SCAR=37.4
694  
695 0 my $name = $field_node->localname();
696 0 my $value = $self->_calc_info_field_processed_content($field_node);
697  
698 0 ▶ if ( $name eq "author" )
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'author') { } 0 0 0
elsif ($name eq 'work' or $name eq 'tagline') { } 0 0 0
elsif ($name eq 'channel') { } 0 0 0
699 {
700 0 $self->_output_info_value($value);
701 }
702 ▶ elsif ( ( $name eq "work" ) || ( $name eq "tagline" ) )
Condition0%0/3
$name eq "work" or $name eq "tagline"
l!l&&r!l&&!r
000
MC/DC0%0/2
$name eq "work" or $name eq "tagline"
$name eq "work" $name eq "tagline"
Truth table0%0/3
A or B
A= $name eq "work"B= $name eq "tagline"
ABresult
1X1
011
000
703 {
704 0 my $url = "";
705  
706 0 ▶ if ( $field_node->hasAttribute("href") )
Branch: 1 branch
BranchTrueFalseTotal
if ($field_node->hasAttribute("href")) 0 0 0
707 {
708 0 $url = " ( " . $field_node->getAttribute("href") . " )";
709 }
710  
711 0 $self->_output_info_value( $value . $url );
712 }
713 elsif ( $name eq "channel" )
714 {
715 0 my $channel = $field_node->textContent();
716 0 my $network = $info_node->findnodes("network")->shift()->textContent();
717  
718 0 $self->_output_info_value("$channel, $network");
719 }
720  
721 0 return;
722 }
723  
724 sub _get_info_node_fields
725 {
726 0 ▶ my ( $self, $info_node ) = @_;
Subroutine _get_info_node_fields: not called CC=1 SCAR=6.9
727  
728 return reverse
729 0 sort { $self->_info_field_value($a) <=> $self->_info_field_value($b) }
730 $info_node->findnodes("*");
731  
732 }
733  
734 sub _render_info_node
735 {
736 0 ▶ my ( $self, $info_node ) = @_;
Subroutine _render_info_node: not called CC=3 SCAR=24.8
737  
738 0 ▶ if ( my @f = $self->_get_info_node_fields($info_node) )
Branch: 1 branch
BranchTrueFalseTotal
if (my(@f) = $self->_get_info_node_fields($info_node)) 0 0 0
739 {
740 0 $self->_start_new_line;
741  
742 0 foreach my $field_node (@f)
743 {
744 0 $self->_out_info_field_node( $info_node, $field_node );
745 }
746 }
747  
748 0 return;
749 }
750  
751 1 1;
752  
753 __END__
754  
755 =pod
756  
757 =encoding UTF-8
758  
759 =head1 NAME
760  
761 XML::Grammar::Fortune::ToText - convert the FortunesXML grammar to plaintext.
762  
763 =head1 VERSION
764  
765 version 0.0701
766  
767 =head1 SYNOPSIS
768  
769 use XML::Grammar::Fortune::ToText;
770  
771 open my $out_fh, '>', 'my-fortunes.txt'
772 or die "Cannot open 'my-fortunes.txt' for writing - $!";
773 my $converter = XML::Grammar::Fortune::ToText->new(
774 {
775 'input' => "my-fortunes.fortune-xml.xml",
776 'output' => $out_fh,
777 }
778 );
779  
780 $converter->run();
781  
782 close($out_fh);
783  
784 =head1 VERSION
785  
786 version 0.0701
787  
788 =head1 FUNCTIONS
789  
790 =head2 my $processor = XML::Grammar::Fortune::ToText->new({input => "path/to/file.xml", output => \*STDOUT,});
791  
792 Creates a new processor with input and output files.
793  
794 =head2 $self->run()
795  
796 Runs the processor.
797  
798 =for nothing {
799 elsif (m{^[^\-]* ---\t(\S+) is now known as (\S+)})
800 {
801 my ($old_nick, $new_nick) = ($1, $2);
802 push @messages,
803 {'type' => "change_nick", 'old' => $old_nick, 'new' => $new_nick};
804 $longest_nick_len =
805 max($longest_nick_len, length($old_nick), length($new_nick));
806 }
807 else
808 {
809 push @messages, {'type' => "raw", 'msg' => $_};
810 }
811 }
812  
813 =head1 AUTHOR
814  
815 Shlomi Fish, L<http://www.shlomifish.org/> .
816  
817 =head1 BUGS
818  
819 Please report any bugs or feature requests to C<bug-xml-grammar-fortune at rt.cpan.org>, or through
820 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-Grammar-Fortune>. I will be notified, and then you'll
821 automatically be notified of progress on your bug as I make changes.
822  
823 =head1 SUPPORT
824  
825 You can find documentation for this module with the perldoc command.
826  
827 perldoc XML::Grammar::Fortune
828  
829 You can also look for information at:
830  
831 =over 4
832  
833 =item * RT: CPAN's request tracker
834  
835 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-Grammar-Fortune>
836  
837 =item * AnnoCPAN: Annotated CPAN documentation
838  
839 L<http://annocpan.org/dist/XML-Grammar-Fortune>
840  
841 =item * CPAN Ratings
842  
843 L<http://cpanratings.perl.org/d/XML-Grammar-Fortune>
844  
845 =item * Search CPAN
846  
847 L<http://search.cpan.org/dist/XML-Grammar-Fortune>
848  
849 =back
850  
851 =head1 ACKNOWLEDGEMENTS
852  
853 =head1 COPYRIGHT & LICENSE
854  
855 Copyright 2008 by Shlomi Fish
856  
857 This program is distributed under the MIT (X11) License:
858 L<http://www.opensource.org/licenses/mit-license.php>
859  
860 Permission is hereby granted, free of charge, to any person
861 obtaining a copy of this software and associated documentation
862 files (the "Software"), to deal in the Software without
863 restriction, including without limitation the rights to use,
864 copy, modify, merge, publish, distribute, sublicense, and/or sell
865 copies of the Software, and to permit persons to whom the
866 Software is furnished to do so, subject to the following
867 conditions:
868  
869 The above copyright notice and this permission notice shall be
870 included in all copies or substantial portions of the Software.
871  
872 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
873 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
874 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
875 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
876 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
877 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
878 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
879 OTHER DEALINGS IN THE SOFTWARE.
880  
881 =head1 AUTHOR
882  
883 Shlomi Fish <shlomif@cpan.org>
884  
885 =head1 COPYRIGHT AND LICENSE
886  
887 This software is Copyright (c) 2018 by Shlomi Fish.
888  
889 This is free software, licensed under:
890  
891 The MIT (X11) License
892  
893 =head1 BUGS
894  
895 Please report any bugs or feature requests on the bugtracker website
896 L<https://github.com/shlomif/xml-grammar-fortune/issues>
897  
898 When submitting a bug or request, please include a test-file or a
899 patch to an existing test-file that illustrates the bug or desired
900 feature.
901  
902 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
903  
904 =head1 SUPPORT
905  
906 =head2 Perldoc
907  
908 You can find documentation for this module with the perldoc command.
909  
910 perldoc XML::Grammar::Fortune::ToText
911  
912 =head2 Websites
913  
914 The following websites have more information about this module, and may be of help to you. As always,
915 in addition to those websites please use your favorite search engine to discover more resources.
916  
917 =over 4
918  
919 =item *
920  
921 MetaCPAN
922  
923 A modern, open-source CPAN search engine, useful to view POD in HTML format.
924  
925 L<https://metacpan.org/release/XML-Grammar-Fortune>
926  
927 =item *
928  
929 Search CPAN
930  
931 The default CPAN search engine, useful to view POD in HTML format.
932  
933 L<http://search.cpan.org/dist/XML-Grammar-Fortune>
934  
935 =item *
936  
937 RT: CPAN's Bug Tracker
938  
939 The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
940  
941 L<https://rt.cpan.org/Public/Dist/Display.html?Name=XML-Grammar-Fortune>
942  
943 =item *
944  
945 AnnoCPAN
946  
947 The AnnoCPAN is a website that allows community annotations of Perl module documentation.
948  
949 L<http://annocpan.org/dist/XML-Grammar-Fortune>
950  
951 =item *
952  
953 CPAN Ratings
954  
955 The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
956  
957 L<http://cpanratings.perl.org/d/XML-Grammar-Fortune>
958  
959 =item *
960  
961 CPANTS
962  
963 The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
964  
965 L<http://cpants.cpanauthors.org/dist/XML-Grammar-Fortune>
966  
967 =item *
968  
969 CPAN Testers
970  
971 The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
972  
973 L<http://www.cpantesters.org/distro/X/XML-Grammar-Fortune>
974  
975 =item *
976  
977 CPAN Testers Matrix
978  
979 The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
980  
981 L<http://matrix.cpantesters.org/?dist=XML-Grammar-Fortune>
982  
983 =item *
984  
985 CPAN Testers Dependencies
986  
987 The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
988  
989 L<http://deps.cpantesters.org/?module=XML::Grammar::Fortune>
990  
991 =back
992  
993 =head2 Bugs / Feature Requests
994  
995 Please report any bugs or feature requests by email to C<bug-xml-grammar-fortune at rt.cpan.org>, or through
996 the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=XML-Grammar-Fortune>. You will be automatically notified of any
997 progress on the request by the system.
998  
999 =head2 Source Code
1000  
1001 The code is open to the world, and available for you to hack on. Please feel free to browse it and play
1002 with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
1003 from your repository :)
1004  
1005 L<https://github.com/shlomif/xml-grammar-fortune>
1006  
1007 git clone git://github.com/shlomif/xml-grammar-fortune.git
1008  
1009 =cut
↑ Summary Build »