File Coverage

blib/lib/HTML/Tree/Create/Callback/ChildrenPerLevel.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 4 100.0
condition 2 2 100.0
subroutine 8 8 100.0
pod 1 1 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1             package HTML::Tree::Create::Callback::ChildrenPerLevel;
2              
3             our $DATE = '2016-03-31'; # DATE
4             our $VERSION = '0.002'; # VERSION
5              
6 1     1   485 use 5.010001;
  1         2  
7 1     1   3 use strict;
  1         0  
  1         14  
8 1     1   2 use warnings;
  1         1  
  1         16  
9 1     1   408 use HTML::Entities;
  1         4402  
  1         61  
10              
11 1     1   403 use HTML::Tree::Create::Callback qw();
  1         1  
  1         16  
12              
13 1     1   4 use Exporter qw(import);
  1         0  
  1         200  
14             our @EXPORT_OK = qw(create_html_tree_using_callback);
15              
16             sub create_html_tree_using_callback {
17 1     1 1 13 my ($callback, $num_children_per_level) = @_;
18              
19 1         2 my $num_children_per_level_so_far = [];
20              
21             HTML::Tree::Create::Callback::create_html_tree_using_callback(
22             sub {
23 6     6   6 my ($level, $seniority) = @_;
24              
25 6         9 my ($element, $attrs, $text_before, $text_after) =
26             $callback->($level, $seniority);
27 6         37 my $num_children;
28 6 100       11 if ($level >= @$num_children_per_level) {
    100          
29 2         3 $num_children = 0;
30             } elsif ($level == 0) {
31 1         1 $num_children = $num_children_per_level->[0];
32             } else {
33              
34             # at this point we must already have this number of children
35 3         9 my $target = sprintf("%.0f",
36             ($seniority+1) *
37             ($num_children_per_level->[$level] /
38             $num_children_per_level->[$level-1]));
39              
40             # we have this number of children so far
41 3   100     7 $num_children_per_level_so_far->[$level] //= 0;
42 3         3 my $has = $num_children_per_level_so_far->[$level];
43              
44 3         3 $num_children = $target - $has;
45 3         2 $num_children_per_level_so_far->[$level] += $num_children;
46             }
47 6         15 return ($element, $attrs, $text_before, $text_after, $num_children);
48             },
49 1         7 );
50             }
51              
52             1;
53             # ABSTRACT: Create HTML document by using a callback (and number of elements per level)
54              
55             __END__