File Coverage

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


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